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

Side by Side Diff: chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.cc

Issue 10827047: Removing instances of profile_->GetBookmarkModel() as part of converting BookmarkModel to a PKS. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/views/bookmarks/bookmark_menu_delegate.h" 5 #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h"
6 6
7 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/bookmarks/bookmark_model.h" 8 #include "chrome/browser/bookmarks/bookmark_model.h"
9 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
9 #include "chrome/browser/bookmarks/bookmark_node_data.h" 10 #include "chrome/browser/bookmarks/bookmark_node_data.h"
10 #include "chrome/browser/bookmarks/bookmark_utils.h" 11 #include "chrome/browser/bookmarks/bookmark_utils.h"
11 #include "chrome/browser/event_disposition.h" 12 #include "chrome/browser/event_disposition.h"
12 #include "chrome/browser/prefs/pref_service.h" 13 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" 16 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
16 #include "chrome/browser/ui/views/event_utils.h" 17 #include "chrome/browser/ui/views/event_utils.h"
17 #include "chrome/common/pref_names.h" 18 #include "chrome/common/pref_names.h"
18 #include "content/public/browser/page_navigator.h" 19 #include "content/public/browser/page_navigator.h"
(...skipping 28 matching lines...) Expand all
47 menu_(NULL), 48 menu_(NULL),
48 for_drop_(false), 49 for_drop_(false),
49 parent_menu_item_(NULL), 50 parent_menu_item_(NULL),
50 next_menu_id_(first_menu_id), 51 next_menu_id_(first_menu_id),
51 real_delegate_(NULL), 52 real_delegate_(NULL),
52 is_mutating_model_(false), 53 is_mutating_model_(false),
53 location_(bookmark_utils::LAUNCH_NONE){ 54 location_(bookmark_utils::LAUNCH_NONE){
54 } 55 }
55 56
56 BookmarkMenuDelegate::~BookmarkMenuDelegate() { 57 BookmarkMenuDelegate::~BookmarkMenuDelegate() {
57 profile_->GetBookmarkModel()->RemoveObserver(this); 58 BookmarkModelFactory::GetForProfile(profile_)->RemoveObserver(this);
58 } 59 }
59 60
60 void BookmarkMenuDelegate::Init( 61 void BookmarkMenuDelegate::Init(
61 views::MenuDelegate* real_delegate, 62 views::MenuDelegate* real_delegate,
62 MenuItemView* parent, 63 MenuItemView* parent,
63 const BookmarkNode* node, 64 const BookmarkNode* node,
64 int start_child_index, 65 int start_child_index,
65 ShowOptions show_options, 66 ShowOptions show_options,
66 bookmark_utils::BookmarkLaunchLocation location) { 67 bookmark_utils::BookmarkLaunchLocation location) {
67 profile_->GetBookmarkModel()->AddObserver(this); 68 BookmarkModelFactory::GetForProfile(profile_)->AddObserver(this);
68 real_delegate_ = real_delegate; 69 real_delegate_ = real_delegate;
69 if (parent) { 70 if (parent) {
70 parent_menu_item_ = parent; 71 parent_menu_item_ = parent;
71 int initial_count = parent->GetSubmenu() ? 72 int initial_count = parent->GetSubmenu() ?
72 parent->GetSubmenu()->GetMenuItemCount() : 0; 73 parent->GetSubmenu()->GetMenuItemCount() : 0;
73 if ((start_child_index < node->child_count()) && 74 if ((start_child_index < node->child_count()) &&
74 (initial_count > 0)) { 75 (initial_count > 0)) {
75 parent->AppendSeparator(); 76 parent->AppendSeparator();
76 } 77 }
77 BuildMenu(node, start_child_index, parent, &next_menu_id_); 78 BuildMenu(node, start_child_index, parent, &next_menu_id_);
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 const views::DropTargetEvent& event, 185 const views::DropTargetEvent& event,
185 views::MenuDelegate::DropPosition* position) { 186 views::MenuDelegate::DropPosition* position) {
186 // Should only get here if we have drop data. 187 // Should only get here if we have drop data.
187 DCHECK(drop_data_.is_valid()); 188 DCHECK(drop_data_.is_valid());
188 189
189 const BookmarkNode* node = menu_id_to_node_map_[item->GetCommand()]; 190 const BookmarkNode* node = menu_id_to_node_map_[item->GetCommand()];
190 const BookmarkNode* drop_parent = node->parent(); 191 const BookmarkNode* drop_parent = node->parent();
191 int index_to_drop_at = drop_parent->GetIndexOf(node); 192 int index_to_drop_at = drop_parent->GetIndexOf(node);
192 switch (*position) { 193 switch (*position) {
193 case views::MenuDelegate::DROP_AFTER: 194 case views::MenuDelegate::DROP_AFTER:
194 if (node == profile_->GetBookmarkModel()->other_node() || 195 if (node == BookmarkModelFactory::GetForProfile(
195 node == profile_->GetBookmarkModel()->mobile_node()) { 196 profile_)->other_node() ||
197 node == BookmarkModelFactory::GetForProfile(
198 profile_)->mobile_node()) {
196 // Dropping after these nodes makes no sense. 199 // Dropping after these nodes makes no sense.
197 *position = views::MenuDelegate::DROP_NONE; 200 *position = views::MenuDelegate::DROP_NONE;
198 } 201 }
199 index_to_drop_at++; 202 index_to_drop_at++;
200 break; 203 break;
201 204
202 case views::MenuDelegate::DROP_BEFORE: 205 case views::MenuDelegate::DROP_BEFORE:
203 if (node == profile_->GetBookmarkModel()->mobile_node()) { 206 if (node == BookmarkModelFactory::GetForProfile(
207 profile_)->mobile_node()) {
204 // Dropping before this node makes no sense. 208 // Dropping before this node makes no sense.
205 *position = views::MenuDelegate::DROP_NONE; 209 *position = views::MenuDelegate::DROP_NONE;
206 } 210 }
207 index_to_drop_at++; 211 index_to_drop_at++;
208 break; 212 break;
209 213
210 case views::MenuDelegate::DROP_ON: 214 case views::MenuDelegate::DROP_ON:
211 drop_parent = node; 215 drop_parent = node;
212 index_to_drop_at = node->child_count(); 216 index_to_drop_at = node->child_count();
213 break; 217 break;
214 218
215 default: 219 default:
216 break; 220 break;
217 } 221 }
218 DCHECK(drop_parent); 222 DCHECK(drop_parent);
219 return bookmark_utils::BookmarkDropOperation( 223 return bookmark_utils::BookmarkDropOperation(
220 profile_, event, drop_data_, drop_parent, index_to_drop_at); 224 profile_, event, drop_data_, drop_parent, index_to_drop_at);
221 } 225 }
222 226
223 int BookmarkMenuDelegate::OnPerformDrop( 227 int BookmarkMenuDelegate::OnPerformDrop(
224 MenuItemView* menu, 228 MenuItemView* menu,
225 views::MenuDelegate::DropPosition position, 229 views::MenuDelegate::DropPosition position,
226 const views::DropTargetEvent& event) { 230 const views::DropTargetEvent& event) {
227 const BookmarkNode* drop_node = menu_id_to_node_map_[menu->GetCommand()]; 231 const BookmarkNode* drop_node = menu_id_to_node_map_[menu->GetCommand()];
228 DCHECK(drop_node); 232 DCHECK(drop_node);
229 BookmarkModel* model = profile_->GetBookmarkModel(); 233 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
230 DCHECK(model); 234 DCHECK(model);
231 const BookmarkNode* drop_parent = drop_node->parent(); 235 const BookmarkNode* drop_parent = drop_node->parent();
232 DCHECK(drop_parent); 236 DCHECK(drop_parent);
233 int index_to_drop_at = drop_parent->GetIndexOf(drop_node); 237 int index_to_drop_at = drop_parent->GetIndexOf(drop_node);
234 switch (position) { 238 switch (position) {
235 case views::MenuDelegate::DROP_AFTER: 239 case views::MenuDelegate::DROP_AFTER:
236 index_to_drop_at++; 240 index_to_drop_at++;
237 break; 241 break;
238 242
239 case views::MenuDelegate::DROP_ON: 243 case views::MenuDelegate::DROP_ON:
(...skipping 21 matching lines...) Expand all
261 } 265 }
262 266
263 bool BookmarkMenuDelegate::ShowContextMenu(MenuItemView* source, 267 bool BookmarkMenuDelegate::ShowContextMenu(MenuItemView* source,
264 int id, 268 int id,
265 const gfx::Point& p, 269 const gfx::Point& p,
266 bool is_mouse_gesture) { 270 bool is_mouse_gesture) {
267 DCHECK(menu_id_to_node_map_.find(id) != menu_id_to_node_map_.end()); 271 DCHECK(menu_id_to_node_map_.find(id) != menu_id_to_node_map_.end());
268 std::vector<const BookmarkNode*> nodes; 272 std::vector<const BookmarkNode*> nodes;
269 nodes.push_back(menu_id_to_node_map_[id]); 273 nodes.push_back(menu_id_to_node_map_[id]);
270 bool close_on_delete = !parent_menu_item_ && 274 bool close_on_delete = !parent_menu_item_ &&
271 (nodes[0]->parent() == profile()->GetBookmarkModel()->other_node() && 275 (nodes[0]->parent() == BookmarkModelFactory::GetForProfile(
276 profile())->other_node() &&
272 nodes[0]->parent()->child_count() == 1); 277 nodes[0]->parent()->child_count() == 1);
273 context_menu_.reset( 278 context_menu_.reset(
274 new BookmarkContextMenu( 279 new BookmarkContextMenu(
275 parent_, 280 parent_,
276 browser_, 281 browser_,
277 profile_, 282 profile_,
278 page_navigator_, 283 page_navigator_,
279 nodes[0]->parent(), 284 nodes[0]->parent(),
280 nodes, 285 nodes,
281 close_on_delete)); 286 close_on_delete));
282 context_menu_->set_observer(this); 287 context_menu_->set_observer(this);
283 context_menu_->RunMenuAt(p); 288 context_menu_->RunMenuAt(p);
284 context_menu_.reset(NULL); 289 context_menu_.reset(NULL);
285 return true; 290 return true;
286 } 291 }
287 292
288 bool BookmarkMenuDelegate::CanDrag(MenuItemView* menu) { 293 bool BookmarkMenuDelegate::CanDrag(MenuItemView* menu) {
289 const BookmarkNode* node = menu_id_to_node_map_[menu->GetCommand()]; 294 const BookmarkNode* node = menu_id_to_node_map_[menu->GetCommand()];
290 // Don't let users drag the other folder. 295 // Don't let users drag the other folder.
291 return node->parent() != profile_->GetBookmarkModel()->root_node(); 296 return node->parent() != BookmarkModelFactory::GetForProfile(
297 profile_)->root_node();
292 } 298 }
293 299
294 void BookmarkMenuDelegate::WriteDragData(MenuItemView* sender, 300 void BookmarkMenuDelegate::WriteDragData(MenuItemView* sender,
295 ui::OSExchangeData* data) { 301 ui::OSExchangeData* data) {
296 DCHECK(sender && data); 302 DCHECK(sender && data);
297 303
298 content::RecordAction(UserMetricsAction("BookmarkBar_DragFromFolder")); 304 content::RecordAction(UserMetricsAction("BookmarkBar_DragFromFolder"));
299 305
300 BookmarkNodeData drag_data(menu_id_to_node_map_[sender->GetCommand()]); 306 BookmarkNodeData drag_data(menu_id_to_node_map_[sender->GetCommand()]);
301 drag_data.Write(profile_, data); 307 drag_data.Write(profile_, data);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 } 348 }
343 } 349 }
344 350
345 void BookmarkMenuDelegate::WillRemoveBookmarks( 351 void BookmarkMenuDelegate::WillRemoveBookmarks(
346 const std::vector<const BookmarkNode*>& bookmarks) { 352 const std::vector<const BookmarkNode*>& bookmarks) {
347 DCHECK(!is_mutating_model_); 353 DCHECK(!is_mutating_model_);
348 is_mutating_model_ = true; 354 is_mutating_model_ = true;
349 355
350 // Remove the observer so that when the remove happens we don't prematurely 356 // Remove the observer so that when the remove happens we don't prematurely
351 // cancel the menu. The observer ias added back in DidRemoveBookmarks. 357 // cancel the menu. The observer ias added back in DidRemoveBookmarks.
352 profile_->GetBookmarkModel()->RemoveObserver(this); 358 BookmarkModelFactory::GetForProfile(profile_)->RemoveObserver(this);
353 359
354 // Remove the menu items. 360 // Remove the menu items.
355 std::set<MenuItemView*> changed_parent_menus; 361 std::set<MenuItemView*> changed_parent_menus;
356 for (std::vector<const BookmarkNode*>::const_iterator i(bookmarks.begin()); 362 for (std::vector<const BookmarkNode*>::const_iterator i(bookmarks.begin());
357 i != bookmarks.end(); ++i) { 363 i != bookmarks.end(); ++i) {
358 NodeToMenuIDMap::iterator node_to_menu = node_to_menu_id_map_.find(*i); 364 NodeToMenuIDMap::iterator node_to_menu = node_to_menu_id_map_.find(*i);
359 if (node_to_menu != node_to_menu_id_map_.end()) { 365 if (node_to_menu != node_to_menu_id_map_.end()) {
360 MenuItemView* menu = GetMenuByID(node_to_menu->second); 366 MenuItemView* menu = GetMenuByID(node_to_menu->second);
361 DCHECK(menu); // If there an entry in node_to_menu_id_map_, there should 367 DCHECK(menu); // If there an entry in node_to_menu_id_map_, there should
362 // be a menu. 368 // be a menu.
(...skipping 28 matching lines...) Expand all
391 } 397 }
392 if (ancestor_removed) 398 if (ancestor_removed)
393 node_to_menu_id_map_.erase(i++); 399 node_to_menu_id_map_.erase(i++);
394 else 400 else
395 ++i; 401 ++i;
396 } 402 }
397 } 403 }
398 404
399 void BookmarkMenuDelegate::DidRemoveBookmarks() { 405 void BookmarkMenuDelegate::DidRemoveBookmarks() {
400 // Balances remove in WillRemoveBookmarksImpl. 406 // Balances remove in WillRemoveBookmarksImpl.
401 profile_->GetBookmarkModel()->AddObserver(this); 407 BookmarkModelFactory::GetForProfile(profile_)->AddObserver(this);
402 DCHECK(is_mutating_model_); 408 DCHECK(is_mutating_model_);
403 is_mutating_model_ = false; 409 is_mutating_model_ = false;
404 } 410 }
405 411
406 MenuItemView* BookmarkMenuDelegate::CreateMenu(const BookmarkNode* parent, 412 MenuItemView* BookmarkMenuDelegate::CreateMenu(const BookmarkNode* parent,
407 int start_child_index, 413 int start_child_index,
408 ShowOptions show_options) { 414 ShowOptions show_options) {
409 MenuItemView* menu = new MenuItemView(real_delegate_); 415 MenuItemView* menu = new MenuItemView(real_delegate_);
410 menu->SetCommand(next_menu_id_++); 416 menu->SetCommand(next_menu_id_++);
411 menu_id_to_node_map_[menu->GetCommand()] = parent; 417 menu_id_to_node_map_[menu->GetCommand()] = parent;
412 menu->set_has_icons(true); 418 menu->set_has_icons(true);
413 BuildMenu(parent, start_child_index, menu, &next_menu_id_); 419 BuildMenu(parent, start_child_index, menu, &next_menu_id_);
414 if (show_options == SHOW_PERMANENT_FOLDERS) 420 if (show_options == SHOW_PERMANENT_FOLDERS)
415 BuildMenusForPermanentNodes(menu, &next_menu_id_); 421 BuildMenusForPermanentNodes(menu, &next_menu_id_);
416 node_to_menu_map_[parent] = menu; 422 node_to_menu_map_[parent] = menu;
417 return menu; 423 return menu;
418 } 424 }
419 425
420 void BookmarkMenuDelegate::BuildMenusForPermanentNodes( 426 void BookmarkMenuDelegate::BuildMenusForPermanentNodes(
421 views::MenuItemView* menu, 427 views::MenuItemView* menu,
422 int* next_menu_id) { 428 int* next_menu_id) {
423 BookmarkModel* model = profile_->GetBookmarkModel(); 429 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
424 bool added_separator = false; 430 bool added_separator = false;
425 BuildMenuForPermanentNode(model->other_node(), menu, next_menu_id, 431 BuildMenuForPermanentNode(model->other_node(), menu, next_menu_id,
426 &added_separator); 432 &added_separator);
427 BuildMenuForPermanentNode(model->mobile_node(), menu, next_menu_id, 433 BuildMenuForPermanentNode(model->mobile_node(), menu, next_menu_id,
428 &added_separator); 434 &added_separator);
429 } 435 }
430 436
431 void BookmarkMenuDelegate::BuildMenuForPermanentNode( 437 void BookmarkMenuDelegate::BuildMenuForPermanentNode(
432 const BookmarkNode* node, 438 const BookmarkNode* node,
433 MenuItemView* menu, 439 MenuItemView* menu,
(...skipping 21 matching lines...) Expand all
455 MenuItemView* menu, 461 MenuItemView* menu,
456 int* next_menu_id) { 462 int* next_menu_id) {
457 DCHECK(parent->empty() || start_child_index < parent->child_count()); 463 DCHECK(parent->empty() || start_child_index < parent->child_count());
458 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 464 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
459 for (int i = start_child_index; i < parent->child_count(); ++i) { 465 for (int i = start_child_index; i < parent->child_count(); ++i) {
460 const BookmarkNode* node = parent->GetChild(i); 466 const BookmarkNode* node = parent->GetChild(i);
461 int id = *next_menu_id; 467 int id = *next_menu_id;
462 468
463 (*next_menu_id)++; 469 (*next_menu_id)++;
464 if (node->is_url()) { 470 if (node->is_url()) {
465 const gfx::Image& image = profile_->GetBookmarkModel()->GetFavicon(node); 471 const gfx::Image& image = BookmarkModelFactory::GetForProfile(
472 profile_)->GetFavicon(node);
466 const gfx::ImageSkia* icon = image.IsEmpty() ? 473 const gfx::ImageSkia* icon = image.IsEmpty() ?
467 rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON) : image.ToImageSkia(); 474 rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON) : image.ToImageSkia();
468 menu->AppendMenuItemWithIcon(id, node->GetTitle(), *icon); 475 menu->AppendMenuItemWithIcon(id, node->GetTitle(), *icon);
469 node_to_menu_id_map_[node] = id; 476 node_to_menu_id_map_[node] = id;
470 } else if (node->is_folder()) { 477 } else if (node->is_folder()) {
471 gfx::ImageSkia* folder_icon = 478 gfx::ImageSkia* folder_icon =
472 rb.GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER); 479 rb.GetImageSkiaNamed(IDR_BOOKMARK_BAR_FOLDER);
473 MenuItemView* submenu = menu->AppendSubMenuWithIcon( 480 MenuItemView* submenu = menu->AppendSubMenuWithIcon(
474 id, node->GetTitle(), *folder_icon); 481 id, node->GetTitle(), *folder_icon);
475 node_to_menu_id_map_[node] = id; 482 node_to_menu_id_map_[node] = id;
476 BuildMenu(node, 0, submenu, next_menu_id); 483 BuildMenu(node, 0, submenu, next_menu_id);
477 } else { 484 } else {
478 NOTREACHED(); 485 NOTREACHED();
479 } 486 }
480 menu_id_to_node_map_[id] = node; 487 menu_id_to_node_map_[id] = node;
481 } 488 }
482 } 489 }
483 490
484 MenuItemView* BookmarkMenuDelegate::GetMenuByID(int id) { 491 MenuItemView* BookmarkMenuDelegate::GetMenuByID(int id) {
485 for (NodeToMenuMap::const_iterator i(node_to_menu_map_.begin()); 492 for (NodeToMenuMap::const_iterator i(node_to_menu_map_.begin());
486 i != node_to_menu_map_.end(); ++i) { 493 i != node_to_menu_map_.end(); ++i) {
487 MenuItemView* menu = i->second->GetMenuItemByID(id); 494 MenuItemView* menu = i->second->GetMenuItemByID(id);
488 if (menu) 495 if (menu)
489 return menu; 496 return menu;
490 } 497 }
491 498
492 return parent_menu_item_ ? parent_menu_item_->GetMenuItemByID(id) : NULL; 499 return parent_menu_item_ ? parent_menu_item_->GetMenuItemByID(id) : NULL;
493 } 500 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/bookmarks/bookmark_menu_controller_views.cc ('k') | chrome/browser/ui/views/wrench_menu.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698