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

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: Refactoring URL itteration function as per comments, addressing other formatting and style errors 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
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; 61 int num_bookmark_urls_before_prompting = 15;
62 62
63 namespace { 63 namespace {
64 64
65 // Iterator that iterates through a set of BookmarkNodes returning the URLs 65 // Iterate through a set of nodes, adding all the URLs to a vector, and for the
66 // for nodes that are urls, or the URLs for the children of non-url urls. 66 // nodes that are not URLs, add their child URLs.
Peter Kasting 2017/04/14 08:17:15 Nit: How about "Returns a vector of all URLs in |n
Paezagon 2017/04/14 18:47:24 Done.
67 // This does not recurse through all descendants, only immediate children. 67 std::vector<const GURL*> GetOpenURLs(
Peter Kasting 2017/04/14 08:17:15 Why does this vector contain GURL* instead of GURL
Paezagon 2017/04/14 18:47:24 Done.
68 // The following illustrates 68 const std::vector<const BookmarkNode*>& nodes) {
69 // typical usage: 69 std::vector<const GURL*> urls;
70 // OpenURLIterator iterator(nodes); 70 for (std::vector<const BookmarkNode*>::const_iterator nodes_it =
71 // while (iterator.has_next()) { 71 nodes.begin();
72 // const GURL* url = iterator.NextURL(); 72 nodes_it != nodes.end(); ++nodes_it) {
Peter Kasting 2017/04/14 08:17:15 Nit: Use range-based for, which will simplify both
Paezagon 2017/04/14 18:47:24 Done.
73 // // do something with |urll|. 73 if ((*nodes_it)->is_url()) {
74 // } 74 // if node is URL add it
Peter Kasting 2017/04/14 08:17:15 Nit: Comments should be complete sentences, with c
Paezagon 2017/04/14 18:47:24 Done.
75 class OpenURLIterator { 75 urls.push_back(&(*nodes_it)->url());
76 public: 76 } else {
77 explicit OpenURLIterator(const std::vector<const BookmarkNode*>& nodes) 77 // if node is not URL, it's a folder, add it's children that are URLs
78 : child_index_(0), 78 for (int child_index = 0; child_index < (*nodes_it)->child_count();
79 next_(NULL), 79 ++child_index) {
Peter Kasting 2017/04/14 08:17:15 Nit: Again, use range-based for
Paezagon 2017/04/14 18:47:24 Can't use range for here because BookmarkNode's ch
Peter Kasting 2017/04/14 19:04:40 That might indeed make sense to do. I'm fine with
80 parent_(nodes.begin()), 80 const BookmarkNode* child = (*nodes_it)->GetChild(child_index);
81 end_(nodes.end()) { 81 if (child->is_url()) {
82 FindNext(); 82 urls.push_back(&child->url());
83 }
84
85 bool has_next() { return next_ != NULL;}
86
87 const GURL* NextURL() {
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 } 83 }
116 } 84 }
117 } 85 }
118 next_ = NULL;
119 } 86 }
87 return urls;
88 }
120 89
121 int child_index_; 90 std::vector<const GURL*> GetOpenURLsIncognito(
122 const GURL* next_; 91 const std::vector<const BookmarkNode*>& nodes,
123 std::vector<const BookmarkNode*>::const_iterator parent_; 92 content::BrowserContext* browser_context) {
124 const std::vector<const BookmarkNode*>::const_iterator end_; 93 std::vector<const GURL*> urls = GetOpenURLs(nodes);
125 94 urls.erase(std::remove_if(urls.begin(), urls.end(),
Peter Kasting 2017/04/14 08:17:15 Nit: Prefer base::EraseIf() to erase(remove_if())
Paezagon 2017/04/14 18:47:24 Done.
126 DISALLOW_COPY_AND_ASSIGN(OpenURLIterator); 95 [&browser_context](const GURL* url) {
127 }; 96 return !IsURLAllowedInIncognito(*url,
97 browser_context);
98 }),
99 urls.end());
100 return urls;
101 }
128 102
129 #if !defined(OS_ANDROID) 103 #if !defined(OS_ANDROID)
130 bool ShouldOpenAll(gfx::NativeWindow parent, 104 bool ShouldOpenAll(gfx::NativeWindow parent,
131 const std::vector<const BookmarkNode*>& nodes) { 105 const std::vector<const BookmarkNode*>& nodes) {
132 int child_count = 0; 106 int child_count = GetOpenURLs(nodes).size();
Peter Kasting 2017/04/14 08:17:15 Nit: Declare as size_t, change |num_bookmark_urls_
Paezagon 2017/04/14 18:47:24 Done.
133 OpenURLIterator iterator(nodes);
134 while (iterator.has_next()) {
135 iterator.NextURL();
136 child_count++;
137 }
138
139 if (child_count < num_bookmark_urls_before_prompting) 107 if (child_count < num_bookmark_urls_before_prompting)
140 return true; 108 return true;
141 109
142 return ShowQuestionMessageBox( 110 return ShowQuestionMessageBox(
143 parent, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), 111 parent, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
144 l10n_util::GetStringFUTF16(IDS_BOOKMARK_BAR_SHOULD_OPEN_ALL, 112 l10n_util::GetStringFUTF16(IDS_BOOKMARK_BAR_SHOULD_OPEN_ALL,
145 base::IntToString16(child_count))) == 113 base::IntToString16(child_count))) ==
146 MESSAGE_BOX_RESULT_YES; 114 MESSAGE_BOX_RESULT_YES;
147 } 115 }
148 #endif 116 #endif
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 content::BrowserContext* browser_context) { 150 content::BrowserContext* browser_context) {
183 if (!ShouldOpenAll(parent, nodes)) 151 if (!ShouldOpenAll(parent, nodes))
184 return; 152 return;
185 153
186 // Opens all |nodes| of type URL and any children of |nodes| that are of type 154 // 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 155 // 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 156 // url is opened |opened_first_url| is set to true and |navigator| is set to
189 // the PageNavigator of the last active tab. This is done to handle a window 157 // the PageNavigator of the last active tab. This is done to handle a window
190 // disposition of new window, in which case we want subsequent tabs to open in 158 // disposition of new window, in which case we want subsequent tabs to open in
191 // that window. 159 // that window.
192 bool opened_first_url = false;
193 WindowOpenDisposition disposition = initial_disposition;
194 OpenURLIterator iterator(nodes);
195 while (iterator.has_next()) {
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 160
204 content::WebContents* opened_tab = navigator->OpenURL( 161 std::vector<const GURL*> urls =
205 content::OpenURLParams(*url, content::Referrer(), disposition, 162 initial_disposition == WindowOpenDisposition::OFF_THE_RECORD
163 ? GetOpenURLsIncognito(nodes, browser_context)
164 : GetOpenURLs(nodes);
Peter Kasting 2017/04/14 08:17:15 Nit: I suggest collapsing GetOpenURLs[InIncognito]
Paezagon 2017/04/14 18:47:24 Done.
165
166 std::vector<const GURL*>::const_iterator url_it = urls.begin();
167
168 content::WebContents* opened_tab = navigator->OpenURL(
169 content::OpenURLParams(**url_it, content::Referrer(), initial_disposition,
170 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false));
Peter Kasting 2017/04/14 08:17:15 Nit: The old loop-with-bool was ugly, but duplicat
Paezagon 2017/04/14 18:47:24 Done.
171
172 url_it++;
Peter Kasting 2017/04/14 08:17:15 Nit: Preincrement iterators
Paezagon 2017/04/14 18:47:24 Gone.
173
174 // We opened the first URL which may have opened a new window or clobbered
175 // the current page, reset the navigator just to be sure. |opened_tab| may
176 // be NULL in tests.
177 if (opened_tab)
178 navigator = opened_tab;
179
180 WindowOpenDisposition disposition = WindowOpenDisposition::NEW_BACKGROUND_TAB;
181 for (; url_it != urls.end(); ++url_it) {
182 navigator->OpenURL(
183 content::OpenURLParams(**url_it, content::Referrer(), disposition,
206 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); 184 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false));
207
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
212 // the current page, reset the navigator just to be sure. |opened_tab| may
213 // be NULL in tests.
214 if (opened_tab)
215 navigator = opened_tab;
216 }
217 } 185 }
218 } 186 }
219 187
220 void OpenAll(gfx::NativeWindow parent, 188 void OpenAll(gfx::NativeWindow parent,
221 content::PageNavigator* navigator, 189 content::PageNavigator* navigator,
222 const BookmarkNode* node, 190 const BookmarkNode* node,
223 WindowOpenDisposition initial_disposition, 191 WindowOpenDisposition initial_disposition,
224 content::BrowserContext* browser_context) { 192 content::BrowserContext* browser_context) {
225 std::vector<const BookmarkNode*> nodes; 193 std::vector<const BookmarkNode*> nodes;
226 nodes.push_back(node); 194 nodes.push_back(node);
227 OpenAll(parent, navigator, nodes, initial_disposition, browser_context); 195 OpenAll(parent, navigator, nodes, initial_disposition, browser_context);
228 } 196 }
229 197
198 int OpenCount(gfx::NativeWindow parent,
199 const std::vector<const bookmarks::BookmarkNode*>& nodes,
200 bool incognito,
201 content::BrowserContext* browser_context) {
202 return (incognito ? GetOpenURLsIncognito(nodes, browser_context)
203 : GetOpenURLs(nodes))
204 .size();
205 }
206
207 int OpenCount(gfx::NativeWindow parent,
208 const BookmarkNode* node,
209 bool incognito,
210 content::BrowserContext* browser_context) {
211 std::vector<const BookmarkNode*> nodes;
212 nodes.push_back(node);
213 return OpenCount(parent, nodes, incognito, browser_context);
214 }
215
230 bool ConfirmDeleteBookmarkNode(const BookmarkNode* node, 216 bool ConfirmDeleteBookmarkNode(const BookmarkNode* node,
231 gfx::NativeWindow window) { 217 gfx::NativeWindow window) {
232 DCHECK(node && node->is_folder() && !node->empty()); 218 DCHECK(node && node->is_folder() && !node->empty());
233 return ShowQuestionMessageBox( 219 return ShowQuestionMessageBox(
234 window, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), 220 window, l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
235 l10n_util::GetPluralStringFUTF16( 221 l10n_util::GetPluralStringFUTF16(
236 IDS_BOOKMARK_EDITOR_CONFIRM_DELETE, 222 IDS_BOOKMARK_EDITOR_CONFIRM_DELETE,
237 ChildURLCountTotal(node))) == 223 ChildURLCountTotal(node))) ==
238 MESSAGE_BOX_RESULT_YES; 224 MESSAGE_BOX_RESULT_YES;
239 } 225 }
240 226
241 void ShowBookmarkAllTabsDialog(Browser* browser) { 227 void ShowBookmarkAllTabsDialog(Browser* browser) {
242 Profile* profile = browser->profile(); 228 Profile* profile = browser->profile();
243 BookmarkModel* model = BookmarkModelFactory::GetForBrowserContext(profile); 229 BookmarkModel* model = BookmarkModelFactory::GetForBrowserContext(profile);
244 DCHECK(model && model->loaded()); 230 DCHECK(model && model->loaded());
245 231
246 const BookmarkNode* parent = GetParentForNewNodes(model); 232 const BookmarkNode* parent = GetParentForNewNodes(model);
247 BookmarkEditor::EditDetails details = 233 BookmarkEditor::EditDetails details =
248 BookmarkEditor::EditDetails::AddFolder(parent, parent->child_count()); 234 BookmarkEditor::EditDetails::AddFolder(parent, parent->child_count());
249 GetURLsForOpenTabs(browser, &(details.urls)); 235 GetURLsForOpenTabs(browser, &(details.urls));
250 DCHECK(!details.urls.empty()); 236 DCHECK(!details.urls.empty());
251 237
252 BookmarkEditor::Show(browser->window()->GetNativeWindow(), profile, details, 238 BookmarkEditor::Show(browser->window()->GetNativeWindow(), profile, details,
253 BookmarkEditor::SHOW_TREE); 239 BookmarkEditor::SHOW_TREE);
254 } 240 }
255 241
256 bool HasBookmarkURLs(const std::vector<const BookmarkNode*>& selection) { 242 bool HasBookmarkURLs(const std::vector<const BookmarkNode*>& selection) {
257 OpenURLIterator iterator(selection); 243 return GetOpenURLs(selection).size() > 0;
Peter Kasting 2017/04/14 08:17:15 Nit: Prefer !empty() to size() > 0 (2 places)
Paezagon 2017/04/14 18:47:24 Done.
258 return iterator.has_next();
259 } 244 }
260 245
261 bool HasBookmarkURLsAllowedInIncognitoMode( 246 bool HasBookmarkURLsAllowedInIncognitoMode(
262 const std::vector<const BookmarkNode*>& selection, 247 const std::vector<const BookmarkNode*>& selection,
263 content::BrowserContext* browser_context) { 248 content::BrowserContext* browser_context) {
264 OpenURLIterator iterator(selection); 249 return GetOpenURLsIncognito(selection, browser_context).size() > 0;
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 } 250 }
272 #endif // !defined(OS_ANDROID) 251 #endif // !defined(OS_ANDROID)
273 252
274 } // namespace chrome 253 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698