File: bookmark_utils_desktop.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (457 lines) | stat: -rw-r--r-- 17,969 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/bookmarks/bookmark_utils_desktop.h"

#include <iterator>
#include <numeric>

#include "base/containers/contains.h"
#include "base/containers/flat_set.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/bookmarks/url_and_id.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/renderer_host/chrome_navigation_ui_data.h"
#include "chrome/browser/ui/bookmarks/bookmark_editor.h"
#include "chrome/browser/ui/bookmarks/bookmark_stats.h"
#include "chrome/browser/ui/bookmarks/bookmark_stats_tab_helper.h"
#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/simple_message_box.h"
#include "chrome/browser/ui/tabs/tab_group_model.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/browser/bookmark_utils.h"
#include "components/tab_groups/tab_group_id.h"
#include "components/tabs/public/split_tab_visual_data.h"
#include "components/tabs/public/tab_group.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/window_open_disposition.h"
#include "url/gurl.h"

using bookmarks::BookmarkModel;
using bookmarks::BookmarkNode;

namespace chrome {

size_t kNumBookmarkUrlsBeforePrompting = 15;

static BookmarkNavigationWrapper* g_nav_wrapper_test_instance = nullptr;

base::WeakPtr<content::NavigationHandle> BookmarkNavigationWrapper::NavigateTo(
    NavigateParams* params) {
  return g_nav_wrapper_test_instance
             ? g_nav_wrapper_test_instance->NavigateTo(params)
             : Navigate(params);
}

// static
void BookmarkNavigationWrapper::SetInstanceForTesting(
    BookmarkNavigationWrapper* instance) {
  g_nav_wrapper_test_instance = instance;
}

namespace {

// Returns a vector of all URLs in |nodes| and their immediate children.  Only
// recurses one level deep, not infinitely.  TODO(pkasting): It's not clear why
// this shouldn't just recurse infinitely.
std::vector<UrlAndId> GetURLsToOpen(
    const std::vector<raw_ptr<const BookmarkNode, VectorExperimental>>& nodes,
    bool incognito_urls_only = false) {
  std::vector<UrlAndId> url_and_ids;
  const auto AddUrlIfLegal = [&](const GURL url, int64_t id) {
    if (!incognito_urls_only || IsURLAllowedInIncognito(url)) {
      UrlAndId url_and_id;
      url_and_id.url = url;
      url_and_id.id = id;
      url_and_ids.push_back(url_and_id);
    }
  };
  for (const BookmarkNode* node : nodes) {
    if (node->is_url()) {
      AddUrlIfLegal(node->url(), node->id());
    } else {
      // If the node is not a URL, it is a folder. We want to add those of its
      // children which are URLs.
      for (const auto& child : node->children()) {
        if (child->is_url()) {
          AddUrlIfLegal(child->url(), child->id());
        }
      }
    }
  }
  return url_and_ids;
}

// Returns the total number of descendants nodes.
int ChildURLCountTotal(const BookmarkNode* node) {
  const auto count_children = [](int total, const auto& child) {
    if (child->is_folder()) {
      total += ChildURLCountTotal(child.get());
    }
    return total + 1;
  };
  return std::accumulate(node->children().cbegin(), node->children().cend(), 0,
                         count_children);
}

// Returns in |urls|, the url and title pairs for each open tab in browser.
void GetURLsAndFoldersForOpenTabs(
    Browser* browser,
    std::vector<BookmarkEditor::EditDetails::BookmarkData>* folder_data) {
  std::vector<std::pair<GURL, std::u16string>> tab_entries;
  base::flat_map<int, TabGroupData> groups_by_index;
  for (int i = 0; i < browser->tab_strip_model()->count(); ++i) {
    std::pair<GURL, std::u16string> entry;
    auto* contents = browser->tab_strip_model()->GetWebContentsAt(i);
    GetURLAndTitleToBookmark(contents, &(entry.first), &(entry.second));
    tab_entries.push_back(entry);
    auto tab_group_id = browser->tab_strip_model()->GetTabGroupForTab(i);
    std::u16string title;
    if (tab_group_id.has_value()) {
      title = browser->tab_strip_model()
                  ->group_model()
                  ->GetTabGroup(tab_group_id.value())
                  ->visual_data()
                  ->title();
    }
    groups_by_index.emplace(i, std::make_pair(tab_group_id, title));
  }
  GetURLsAndFoldersForTabEntries(folder_data, tab_entries, groups_by_index);
}

// Represents a reference set of web contents opened by OpenAllHelper() so that
// the actual web contents and what browsers they are located in can be
// determined (if necessary).
using OpenedWebContentsSet = base::flat_set<const content::WebContents*>;

// Opens all of the URLs in `bookmark_urls` using `navigator` and
// `initial_disposition` as a starting point. Returns a reference set of the
// WebContents created; see OpenedWebContentsSet.
OpenedWebContentsSet OpenAllHelper(
    Browser* browser,
    std::vector<UrlAndId> bookmark_urls,
    WindowOpenDisposition initial_disposition,
    page_load_metrics::NavigationHandleUserData::InitiatorLocation
        navigation_type,
    std::optional<BookmarkLaunchAction> launch_action) {
  OpenedWebContentsSet::container_type opened_tabs;
  WindowOpenDisposition disposition = initial_disposition;
  // We keep track of (potentially) two browsers in addition to the original
  // browser. This allows us to open the URLs in the correct
  // browser depending on the URL type and `initial_disposition`.
  Browser* regular_browser = nullptr;
  Browser* incognito_browser = nullptr;
  BookmarkNavigationWrapper nav_wrapper;
  Profile* profile = nullptr;
  if (browser) {
    profile = browser->profile();
  }
  bool opening_urls_in_incognito = false;
  if (profile) {
    opening_urls_in_incognito =
        profile->IsIncognitoProfile() ||
        initial_disposition == WindowOpenDisposition::OFF_THE_RECORD;
  } else {
    opening_urls_in_incognito =
        initial_disposition == WindowOpenDisposition::OFF_THE_RECORD;
  }

  for (const auto& bookmark_url : bookmark_urls) {
    const bool url_allowed_in_incognito =
        IsURLAllowedInIncognito(bookmark_url.url);

    // Set the browser from which the URL will be opened. If neither
    // `incognito_browser` nor `regular_browser` is set we use the original
    // browser, but `NavigateTo` can create a new browser
    // depending on the disposition and URL type.
    Browser* browser_to_use = browser;
    if (opening_urls_in_incognito && url_allowed_in_incognito) {
      if (incognito_browser) {
        browser_to_use = incognito_browser;
      }
    } else {
      if (regular_browser) {
        browser_to_use = regular_browser;
      }
    }
    if (browser_to_use) {
      profile = browser_to_use->profile();
    }
    NavigateParams params(profile, bookmark_url.url,
                          ui::PAGE_TRANSITION_AUTO_BOOKMARK);
    params.disposition = disposition;
    params.browser = browser_to_use;
    base::WeakPtr<content::NavigationHandle> handle =
        nav_wrapper.NavigateTo(&params);
    if (handle) {
      page_load_metrics::NavigationHandleUserData::CreateForNavigationHandle(
          *handle, navigation_type);
    }
    content::WebContents* opened_tab =
        handle ? handle->GetWebContents() : nullptr;
    if (!opened_tab) {
      continue;
    }

    if (launch_action.has_value()) {
      BookmarkStatsTabHelper::CreateForWebContents(opened_tab);
      BookmarkStatsTabHelper::FromWebContents(opened_tab)
          ->SetLaunchAction(launch_action.value(), disposition);
    }

    if (bookmark_url.id != -1) {
      ChromeNavigationUIData* ui_data =
          static_cast<ChromeNavigationUIData*>(handle->GetNavigationUIData());
      if (ui_data) {
        ui_data->set_bookmark_id(bookmark_url.id);
      }
    }

    bool opening_in_new_window =
        disposition == WindowOpenDisposition::NEW_WINDOW ||
        disposition == WindowOpenDisposition::OFF_THE_RECORD;
    // If we are opening URLs in a new window we set the disposition to
    // `NEW_BACKGROUND_TAB` so that the rest of the URLs open in a new tab
    // instead of a new window.
    // Exception is when we are opening URLs in new incognito window
    // there is a URL that is not allowed in incognito mode.
    // In this case we don't set the disposition to `NEW_BACKGROUND_TAB`
    // until we have opened the first URL that can be opened in incognito.
    // See crbug.com/1349283.
    if (opening_in_new_window) {
      if (!opening_urls_in_incognito || url_allowed_in_incognito) {
        disposition = WindowOpenDisposition::NEW_BACKGROUND_TAB;
      }
    }

    // After we open a URL there can be a new browser created, depending on
    // the disposition and the URL type.
    Profile* new_tab_profile =
        Profile::FromBrowserContext(opened_tab->GetBrowserContext());
    if (new_tab_profile->IsIncognitoProfile()) {
      if (!incognito_browser) {
        incognito_browser = chrome::FindBrowserWithTab(opened_tab);
      }
    } else {
      if (!regular_browser) {
        regular_browser = chrome::FindBrowserWithTab(opened_tab);
      }
    }

    opened_tabs.push_back(opened_tab);
  }

  // Constructing the return value in this way is significantly more efficient.
  return OpenedWebContentsSet(std::move(opened_tabs));
}

}  // namespace

void OpenAllIfAllowed(
    Browser* browser,
    const std::vector<
        raw_ptr<const bookmarks::BookmarkNode, VectorExperimental>>& nodes,
    WindowOpenDisposition initial_disposition,
    bookmarks::OpenAllBookmarksContext context,
    page_load_metrics::NavigationHandleUserData::InitiatorLocation
        navigation_type,
    std::optional<BookmarkLaunchAction> launch_action) {
  std::vector<UrlAndId> url_and_ids = GetURLsToOpen(
      nodes, initial_disposition == WindowOpenDisposition::OFF_THE_RECORD);
  auto do_open =
      [](Browser* browser, std::vector<UrlAndId> url_and_ids_to_open,
         WindowOpenDisposition initial_disposition,
         std::optional<std::u16string> folder_title, bool add_to_split,
         page_load_metrics::NavigationHandleUserData::InitiatorLocation
             navigation_type,
         std::optional<BookmarkLaunchAction> launch_action,
         chrome::MessageBoxResult result) {
        if (result != chrome::MESSAGE_BOX_RESULT_YES) {
          return;
        }
        const auto opened_web_contents = OpenAllHelper(
            browser, std::move(url_and_ids_to_open), initial_disposition,
            navigation_type, std::move(launch_action));
        if (add_to_split && opened_web_contents.size() == 1) {
          TabStripModel* model = browser->tab_strip_model();
          auto* const single_web_contents = *(opened_web_contents.begin());
          const int opened_web_contents_index =
              model->GetIndexOfWebContents(single_web_contents);
          model->AddToNewSplit({opened_web_contents_index},
                               split_tabs::SplitTabVisualData());
        } else if (folder_title.has_value()) {
          TabStripModel* model = browser->tab_strip_model();

          // Figure out which tabs we actually opened in this browser that
          // aren't already in groups.
          std::vector<int> tab_indices;
          for (int i = 0; i < model->count(); ++i) {
            if (base::Contains(opened_web_contents,
                               model->GetWebContentsAt(i)) &&
                !model->GetTabGroupForTab(i).has_value()) {
              tab_indices.push_back(i);
            }
          }

          if (tab_indices.empty()) {
            return;
          }

          std::optional<tab_groups::TabGroupId> new_group_id =
              model->AddToNewGroup(tab_indices);
          if (!new_group_id.has_value()) {
            return;
          }

          // Use the bookmark folder's title as the group's title.
          TabGroup* group =
              model->group_model()->GetTabGroup(new_group_id.value());
          const tab_groups::TabGroupVisualData* current_visual_data =
              group->visual_data();
          tab_groups::TabGroupVisualData new_visual_data(
              folder_title.value(), current_visual_data->color(),
              current_visual_data->is_collapsed());
          model->ChangeTabGroupVisuals(group->id(), new_visual_data);

          model->OpenTabGroupEditor(new_group_id.value());
        }
      };

  // Skip the prompt if there are few bookmarks.
  size_t child_count = url_and_ids.size();
  if (child_count < kNumBookmarkUrlsBeforePrompting) {
    do_open(
        browser, std::move(url_and_ids), initial_disposition,
        context == bookmarks::OpenAllBookmarksContext::kInGroup
            ? std::optional<std::u16string>(nodes[0]->GetTitledUrlNodeTitle())
            : std::nullopt,
        context == bookmarks::OpenAllBookmarksContext::kInSplit,
        navigation_type, std::move(launch_action),
        chrome::MESSAGE_BOX_RESULT_YES);
    return;
  }

  // The callback passed contains the pointer |browser|. This is safe
  // since if |browser| is closed, the message box will be destroyed
  // before the user can answer "Yes".

  ShowQuestionMessageBox(
      browser->window()->GetNativeWindow(),
      l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
      l10n_util::GetStringFUTF16(IDS_BOOKMARK_BAR_SHOULD_OPEN_ALL,
                                 base::NumberToString16(child_count)),
      base::BindOnce(
          do_open, browser, std::move(url_and_ids), initial_disposition,
          context == bookmarks::OpenAllBookmarksContext::kInGroup
              ? std::optional<std::u16string>(nodes[0]->GetTitledUrlNodeTitle())
              : std::nullopt,
          context == bookmarks::OpenAllBookmarksContext::kInSplit,
          navigation_type, std::nullopt));
}

int OpenCount(gfx::NativeWindow parent,
              const std::vector<raw_ptr<const bookmarks::BookmarkNode,
                                        VectorExperimental>>& nodes,
              content::BrowserContext* incognito_context) {
  return GetURLsToOpen(nodes, incognito_context != nullptr).size();
}

int OpenCount(gfx::NativeWindow parent,
              const BookmarkNode* node,
              content::BrowserContext* incognito_context) {
  std::vector<const BookmarkNode*> nodes;
  nodes.push_back(node);
  return OpenCount(
      parent,
      std::vector<raw_ptr<const bookmarks::BookmarkNode, VectorExperimental>>{
          node},
      incognito_context);
}

bool ConfirmDeleteBookmarkNode(gfx::NativeWindow window,
                               const BookmarkNode* node) {
  DCHECK(node && node->is_folder() && !node->children().empty());
  return ShowQuestionMessageBoxSync(
             window, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
             l10n_util::GetPluralStringFUTF16(
                 IDS_BOOKMARK_EDITOR_CONFIRM_DELETE,
                 ChildURLCountTotal(node))) == MESSAGE_BOX_RESULT_YES;
}

void ShowBookmarkAllTabsDialog(Browser* browser) {
  Profile* profile = browser->profile();
  BookmarkModel* model = BookmarkModelFactory::GetForBrowserContext(profile);
  DCHECK(model && model->loaded());

  const BookmarkNode* parent = GetParentForNewNodes(model);
  BookmarkEditor::EditDetails details =
      BookmarkEditor::EditDetails::AddFolder(parent, parent->children().size());

  GetURLsAndFoldersForOpenTabs(browser, &(details.bookmark_data.children));
  DCHECK(!details.bookmark_data.children.empty());
  BookmarkEditor::Show(browser->window()->GetNativeWindow(), profile, details,
                       BookmarkEditor::SHOW_TREE,
                       base::BindOnce(
                           [](const Profile* profile) {
                             // We record the profile that invoked this option.
                             RecordBookmarksAdded(profile);
                           },
                           base::Unretained(profile)));
}

bool HasBookmarkURLs(
    const std::vector<raw_ptr<const BookmarkNode, VectorExperimental>>&
        selection) {
  return !GetURLsToOpen(selection).empty();
}

bool HasBookmarkURLsAllowedInIncognitoMode(
    const std::vector<raw_ptr<const BookmarkNode, VectorExperimental>>&
        selection) {
  return !GetURLsToOpen(selection, true).empty();
}

void GetURLsAndFoldersForTabEntries(
    std::vector<BookmarkEditor::EditDetails::BookmarkData>* folder_data,
    std::vector<std::pair<GURL, std::u16string>> tab_entries,
    base::flat_map<int, TabGroupData> groups_by_index) {
  std::optional<tab_groups::TabGroupId> current_group_id;
  for (size_t i = 0; i < tab_entries.size(); ++i) {
    std::pair<GURL, std::u16string> entry = tab_entries.at(i);
    if (entry.first.is_empty()) {
      continue;
    }
    BookmarkEditor::EditDetails::BookmarkData child;
    child.url = entry.first;
    child.title = entry.second;
    if (groups_by_index.at(i).first.has_value()) {
      if (current_group_id != groups_by_index.at(i).first.value()) {
        BookmarkEditor::EditDetails::BookmarkData tab_group;
        tab_group.title = groups_by_index.at(i).second;
        folder_data->push_back(tab_group);
        current_group_id = groups_by_index.at(i).first;
      }
      folder_data->back().children.push_back(child);
    } else {
      folder_data->push_back(child);
    }
  }
}

}  // namespace chrome