Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: chrome/browser/ui/bookmarks/bookmark_utils_desktop.cc

Issue 2809003002: Making bookmark folder context menu display the number of bookmarks that will be opened by Open All (Closed)
Patch Set: removing boolean flag from open count Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/bookmarks/bookmark_utils_desktop.h" 5 #include "chrome/browser/ui/bookmarks/bookmark_utils_desktop.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 #include "chrome/grit/theme_resources.h" 51 #include "chrome/grit/theme_resources.h"
52 #include "ui/base/material_design/material_design_controller.h" 52 #include "ui/base/material_design/material_design_controller.h"
53 #include "ui/base/resource/resource_bundle.h" 53 #include "ui/base/resource/resource_bundle.h"
54 #endif 54 #endif
55 55
56 using bookmarks::BookmarkModel; 56 using bookmarks::BookmarkModel;
57 using bookmarks::BookmarkNode; 57 using bookmarks::BookmarkNode;
58 58
59 namespace chrome { 59 namespace chrome {
60 60
61 int num_bookmark_urls_before_prompting = 15;
62
63 namespace { 61 namespace {
64 62
65 // Iterator that iterates through a set of BookmarkNodes returning the URLs 63 // Returns a vector of all URLs in |nodes| and their immediate children. Only
66 // for nodes that are urls, or the URLs for the children of non-url urls. 64 // recurses one level deep, not infinitely. TODO(pkasting): It's not clear why
67 // This does not recurse through all descendants, only immediate children. 65 // this shouldn't just recurse infinitely.
68 // The following illustrates 66 std::vector<GURL> GetURLsToOpen(
69 // typical usage: 67 const std::vector<const BookmarkNode*>& nodes,
70 // OpenURLIterator iterator(nodes); 68 content::BrowserContext* browser_context = nullptr,
71 // while (iterator.has_next()) { 69 bool incognito_urls_only = false) {
72 // const GURL* url = iterator.NextURL(); 70 std::vector<GURL> urls;
73 // // do something with |urll|. 71 for (const BookmarkNode* node : nodes) {
74 // } 72 if (node->is_url() and
Peter Kasting 2017/04/14 19:04:40 Hey now, this is C++, not Pascal :)
Paezagon 2017/04/14 19:52:35 Eheh. I tend to get confused when switching langue
75 class OpenURLIterator { 73 (!incognito_urls_only or
76 public: 74 IsURLAllowedInIncognito(node->url(), browser_context))) {
Peter Kasting 2017/04/14 19:04:40 Nit: You could define a lambda locally: const a
Paezagon 2017/04/14 19:52:35 Done with couple alterations.
77 explicit OpenURLIterator(const std::vector<const BookmarkNode*>& nodes) 75 urls.push_back(node->url());
78 : child_index_(0), 76 } else {
79 next_(NULL), 77 // If the node is not a URL, it is a folder. We want to add those of its
80 parent_(nodes.begin()), 78 // children which are URLs.
81 end_(nodes.end()) { 79 for (int child_index = 0; child_index < node->child_count();
82 FindNext(); 80 ++child_index) {
83 } 81 const BookmarkNode* child = node->GetChild(child_index);
84 82 if (child->is_url() and
85 bool has_next() { return next_ != NULL;} 83 (!incognito_urls_only or
86 84 IsURLAllowedInIncognito(child->url(), browser_context))) {
87 const GURL* NextURL() { 85 urls.push_back(child->url());
88 if (!has_next()) {
89 NOTREACHED();
90 return NULL;
91 }
92
93 const GURL* next = next_;
94 FindNext();
95 return next;
96 }
97
98 private:
99 // Seach next node which has URL.
100 void FindNext() {
101 for (; parent_ < end_; ++parent_, child_index_ = 0) {
102 if ((*parent_)->is_url()) {
103 next_ = &(*parent_)->url();
104 ++parent_;
105 child_index_ = 0;
106 return;
107 } else {
108 for (; child_index_ < (*parent_)->child_count(); ++child_index_) {
109 const BookmarkNode* child = (*parent_)->GetChild(child_index_);
110 if (child->is_url()) {
111 next_ = &child->url();
112 ++child_index_;
113 return;
114 }
115 } 86 }
116 } 87 }
117 } 88 }
118 next_ = NULL;
119 } 89 }
120 90 return urls;
121 int child_index_; 91 }
122 const GURL* next_;
123 std::vector<const BookmarkNode*>::const_iterator parent_;
124 const std::vector<const BookmarkNode*>::const_iterator end_;
125
126 DISALLOW_COPY_AND_ASSIGN(OpenURLIterator);
127 };
128 92
129 #if !defined(OS_ANDROID) 93 #if !defined(OS_ANDROID)
130 bool ShouldOpenAll(gfx::NativeWindow parent, 94 bool ShouldOpenAll(gfx::NativeWindow parent,
131 const std::vector<const BookmarkNode*>& nodes) { 95 const std::vector<const BookmarkNode*>& nodes) {
132 int child_count = 0; 96 const size_t kNumBookmarkUrlsBeforePrompting = 15;
Peter Kasting 2017/04/14 19:04:40 Nit: Prefer constexpr to const for compile-time co
Paezagon 2017/04/14 19:52:35 Done.
133 OpenURLIterator iterator(nodes);
134 while (iterator.has_next()) {
135 iterator.NextURL();
136 child_count++;
137 }
138 97
139 if (child_count < num_bookmark_urls_before_prompting) 98 size_t child_count = GetURLsToOpen(nodes).size();
99 if (child_count < kNumBookmarkUrlsBeforePrompting)
140 return true; 100 return true;
141 101
142 return ShowQuestionMessageBox( 102 return ShowQuestionMessageBox(
143 parent, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), 103 parent, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
144 l10n_util::GetStringFUTF16(IDS_BOOKMARK_BAR_SHOULD_OPEN_ALL, 104 l10n_util::GetStringFUTF16(IDS_BOOKMARK_BAR_SHOULD_OPEN_ALL,
145 base::IntToString16(child_count))) == 105 base::SizeTToString16(child_count))) ==
146 MESSAGE_BOX_RESULT_YES; 106 MESSAGE_BOX_RESULT_YES;
147 } 107 }
148 #endif 108 #endif
149 109
150 // Returns the total number of descendants nodes. 110 // Returns the total number of descendants nodes.
151 int ChildURLCountTotal(const BookmarkNode* node) { 111 int ChildURLCountTotal(const BookmarkNode* node) {
152 int result = 0; 112 int result = 0;
153 for (int i = 0; i < node->child_count(); ++i) { 113 for (int i = 0; i < node->child_count(); ++i) {
154 const BookmarkNode* child = node->GetChild(i); 114 const BookmarkNode* child = node->GetChild(i);
155 result++; 115 result++;
(...skipping 22 matching lines...) Expand all
178 void OpenAll(gfx::NativeWindow parent, 138 void OpenAll(gfx::NativeWindow parent,
179 content::PageNavigator* navigator, 139 content::PageNavigator* navigator,
180 const std::vector<const BookmarkNode*>& nodes, 140 const std::vector<const BookmarkNode*>& nodes,
181 WindowOpenDisposition initial_disposition, 141 WindowOpenDisposition initial_disposition,
182 content::BrowserContext* browser_context) { 142 content::BrowserContext* browser_context) {
183 if (!ShouldOpenAll(parent, nodes)) 143 if (!ShouldOpenAll(parent, nodes))
184 return; 144 return;
185 145
186 // Opens all |nodes| of type URL and any children of |nodes| that are of type 146 // Opens all |nodes| of type URL and any children of |nodes| that are of type
187 // URL. |navigator| is the PageNavigator used to open URLs. After the first 147 // URL. |navigator| is the PageNavigator used to open URLs. After the first
188 // url is opened |opened_first_url| is set to true and |navigator| is set to 148 // url is opened |navigator| is set to the PageNavigator of the last active
189 // the PageNavigator of the last active tab. This is done to handle a window 149 // tab. This is done to handle a window disposition of new window, in which
190 // disposition of new window, in which case we want subsequent tabs to open in 150 // case we want subsequent tabs to open in that window.
191 // that window. 151
192 bool opened_first_url = false; 152 std::vector<GURL> urls = GetURLsToOpen(
153 nodes, browser_context,
154 initial_disposition == WindowOpenDisposition::OFF_THE_RECORD);
155
193 WindowOpenDisposition disposition = initial_disposition; 156 WindowOpenDisposition disposition = initial_disposition;
194 OpenURLIterator iterator(nodes); 157 for (std::vector<GURL>::const_iterator url_it = urls.begin();
195 while (iterator.has_next()) { 158 url_it != urls.end(); ++url_it) {
196 const GURL* url = iterator.NextURL();
197 // When |initial_disposition| is OFF_THE_RECORD, a node which can't be
198 // opened in incognito window, it is detected using |browser_context|, is
199 // not opened.
200 if (initial_disposition == WindowOpenDisposition::OFF_THE_RECORD &&
201 !IsURLAllowedInIncognito(*url, browser_context))
202 continue;
203
204 content::WebContents* opened_tab = navigator->OpenURL( 159 content::WebContents* opened_tab = navigator->OpenURL(
205 content::OpenURLParams(*url, content::Referrer(), disposition, 160 content::OpenURLParams(*url_it, content::Referrer(), disposition,
206 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); 161 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false));
207 162 if (url_it == urls.begin()) {
208 if (!opened_first_url) {
209 opened_first_url = true;
210 disposition = WindowOpenDisposition::NEW_BACKGROUND_TAB;
211 // We opened the first URL which may have opened a new window or clobbered 163 // We opened the first URL which may have opened a new window or clobbered
212 // the current page, reset the navigator just to be sure. |opened_tab| may 164 // the current page, reset the navigator just to be sure. |opened_tab| may
213 // be NULL in tests. 165 // be nullptr in tests.
214 if (opened_tab) 166 if (opened_tab)
215 navigator = opened_tab; 167 navigator = opened_tab;
168 disposition = WindowOpenDisposition::NEW_BACKGROUND_TAB;
216 } 169 }
217 } 170 }
218 } 171 }
219 172
220 void OpenAll(gfx::NativeWindow parent, 173 void OpenAll(gfx::NativeWindow parent,
221 content::PageNavigator* navigator, 174 content::PageNavigator* navigator,
222 const BookmarkNode* node, 175 const BookmarkNode* node,
223 WindowOpenDisposition initial_disposition, 176 WindowOpenDisposition initial_disposition,
224 content::BrowserContext* browser_context) { 177 content::BrowserContext* browser_context) {
225 std::vector<const BookmarkNode*> nodes; 178 std::vector<const BookmarkNode*> nodes;
226 nodes.push_back(node); 179 nodes.push_back(node);
227 OpenAll(parent, navigator, nodes, initial_disposition, browser_context); 180 OpenAll(parent, navigator, std::vector<const bookmarks::BookmarkNode*>{node},
181 initial_disposition, browser_context);
182 }
183
184 int OpenCount(gfx::NativeWindow parent,
185 const std::vector<const bookmarks::BookmarkNode*>& nodes,
186 content::BrowserContext* incognito_context) {
187 return GetURLsToOpen(nodes, incognito_context, incognito_context != nullptr)
188 .size();
189 }
190
191 int OpenCount(gfx::NativeWindow parent,
192 const BookmarkNode* node,
193 content::BrowserContext* incognito_context) {
194 std::vector<const BookmarkNode*> nodes;
195 nodes.push_back(node);
196 return OpenCount(parent, std::vector<const bookmarks::BookmarkNode*>{node},
197 incognito_context);
228 } 198 }
229 199
230 bool ConfirmDeleteBookmarkNode(const BookmarkNode* node, 200 bool ConfirmDeleteBookmarkNode(const BookmarkNode* node,
231 gfx::NativeWindow window) { 201 gfx::NativeWindow window) {
232 DCHECK(node && node->is_folder() && !node->empty()); 202 DCHECK(node && node->is_folder() && !node->empty());
233 return ShowQuestionMessageBox( 203 return ShowQuestionMessageBox(
234 window, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), 204 window, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
235 l10n_util::GetPluralStringFUTF16( 205 l10n_util::GetPluralStringFUTF16(
236 IDS_BOOKMARK_EDITOR_CONFIRM_DELETE, 206 IDS_BOOKMARK_EDITOR_CONFIRM_DELETE,
237 ChildURLCountTotal(node))) == 207 ChildURLCountTotal(node))) ==
238 MESSAGE_BOX_RESULT_YES; 208 MESSAGE_BOX_RESULT_YES;
239 } 209 }
240 210
241 void ShowBookmarkAllTabsDialog(Browser* browser) { 211 void ShowBookmarkAllTabsDialog(Browser* browser) {
242 Profile* profile = browser->profile(); 212 Profile* profile = browser->profile();
243 BookmarkModel* model = BookmarkModelFactory::GetForBrowserContext(profile); 213 BookmarkModel* model = BookmarkModelFactory::GetForBrowserContext(profile);
244 DCHECK(model && model->loaded()); 214 DCHECK(model && model->loaded());
245 215
246 const BookmarkNode* parent = GetParentForNewNodes(model); 216 const BookmarkNode* parent = GetParentForNewNodes(model);
247 BookmarkEditor::EditDetails details = 217 BookmarkEditor::EditDetails details =
248 BookmarkEditor::EditDetails::AddFolder(parent, parent->child_count()); 218 BookmarkEditor::EditDetails::AddFolder(parent, parent->child_count());
249 GetURLsForOpenTabs(browser, &(details.urls)); 219 GetURLsForOpenTabs(browser, &(details.urls));
250 DCHECK(!details.urls.empty()); 220 DCHECK(!details.urls.empty());
251 221
252 BookmarkEditor::Show(browser->window()->GetNativeWindow(), profile, details, 222 BookmarkEditor::Show(browser->window()->GetNativeWindow(), profile, details,
253 BookmarkEditor::SHOW_TREE); 223 BookmarkEditor::SHOW_TREE);
254 } 224 }
255 225
256 bool HasBookmarkURLs(const std::vector<const BookmarkNode*>& selection) { 226 bool HasBookmarkURLs(const std::vector<const BookmarkNode*>& selection) {
257 OpenURLIterator iterator(selection); 227 return !GetURLsToOpen(selection).empty();
258 return iterator.has_next();
259 } 228 }
260 229
261 bool HasBookmarkURLsAllowedInIncognitoMode( 230 bool HasBookmarkURLsAllowedInIncognitoMode(
262 const std::vector<const BookmarkNode*>& selection, 231 const std::vector<const BookmarkNode*>& selection,
263 content::BrowserContext* browser_context) { 232 content::BrowserContext* browser_context) {
264 OpenURLIterator iterator(selection); 233 return !GetURLsToOpen(selection, browser_context, true).empty();
265 while (iterator.has_next()) {
266 const GURL* url = iterator.NextURL();
267 if (IsURLAllowedInIncognito(*url, browser_context))
268 return true;
269 }
270 return false;
271 } 234 }
272 #endif // !defined(OS_ANDROID) 235 #endif // !defined(OS_ANDROID)
273 236
274 } // namespace chrome 237 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698