| OLD | NEW |
| 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/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/app/chrome_command_ids.h" | 9 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 11 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" | |
| 12 #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/bookmarks/bookmark_drag_drop.h" | 12 #include "chrome/browser/ui/bookmarks/bookmark_drag_drop.h" |
| 15 #include "chrome/browser/ui/bookmarks/bookmark_utils.h" | 13 #include "chrome/browser/ui/bookmarks/bookmark_utils.h" |
| 16 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" | 15 #include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h" |
| 18 #include "chrome/browser/ui/views/event_utils.h" | 16 #include "chrome/browser/ui/views/event_utils.h" |
| 19 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 20 #include "components/bookmarks/browser/bookmark_model.h" | 18 #include "components/bookmarks/browser/bookmark_model.h" |
| 21 #include "content/public/browser/page_navigator.h" | 19 #include "content/public/browser/page_navigator.h" |
| 22 #include "content/public/browser/user_metrics.h" | 20 #include "content/public/browser/user_metrics.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 GetBookmarkModel()->AddObserver(this); | 67 GetBookmarkModel()->AddObserver(this); |
| 70 real_delegate_ = real_delegate; | 68 real_delegate_ = real_delegate; |
| 71 location_ = location; | 69 location_ = location; |
| 72 if (parent) { | 70 if (parent) { |
| 73 parent_menu_item_ = parent; | 71 parent_menu_item_ = parent; |
| 74 | 72 |
| 75 // Add a separator if there are existing items in the menu, and if the | 73 // Add a separator if there are existing items in the menu, and if the |
| 76 // current node has children. If |node| is the bookmark bar then the | 74 // current node has children. If |node| is the bookmark bar then the |
| 77 // managed node is shown as its first child, if it's not empty. | 75 // managed node is shown as its first child, if it's not empty. |
| 78 BookmarkModel* model = GetBookmarkModel(); | 76 BookmarkModel* model = GetBookmarkModel(); |
| 79 ChromeBookmarkClient* client = GetChromeBookmarkClient(); | |
| 80 bool show_forced_folders = show_options == SHOW_PERMANENT_FOLDERS && | 77 bool show_forced_folders = show_options == SHOW_PERMANENT_FOLDERS && |
| 81 node == model->bookmark_bar_node(); | 78 node == model->bookmark_bar_node(); |
| 82 bool show_managed = show_forced_folders && !client->managed_node()->empty(); | 79 bool show_managed = show_forced_folders && !model->managed_node()->empty(); |
| 83 bool show_supervised = | 80 bool show_supervised = |
| 84 show_forced_folders && !client->supervised_node()->empty(); | 81 show_forced_folders && !model->supervised_node()->empty(); |
| 85 bool has_children = (start_child_index < node->child_count()) || | 82 bool has_children = (start_child_index < node->child_count()) || |
| 86 show_managed || show_supervised; | 83 show_managed || show_supervised; |
| 87 int initial_count = parent->GetSubmenu() ? | 84 int initial_count = parent->GetSubmenu() ? |
| 88 parent->GetSubmenu()->GetMenuItemCount() : 0; | 85 parent->GetSubmenu()->GetMenuItemCount() : 0; |
| 89 if (has_children && initial_count > 0) | 86 if (has_children && initial_count > 0) |
| 90 parent->AppendSeparator(); | 87 parent->AppendSeparator(); |
| 91 if (show_managed) | 88 if (show_managed) |
| 92 BuildMenuForManagedNode(parent); | 89 BuildMenuForManagedNode(parent); |
| 93 if (show_supervised) | 90 if (show_supervised) |
| 94 BuildMenuForSupervisedNode(parent); | 91 BuildMenuForSupervisedNode(parent); |
| 95 BuildMenu(node, start_child_index, parent); | 92 BuildMenu(node, start_child_index, parent); |
| 96 if (show_options == SHOW_PERMANENT_FOLDERS) | 93 if (show_options == SHOW_PERMANENT_FOLDERS) |
| 97 BuildMenusForPermanentNodes(parent); | 94 BuildMenusForPermanentNodes(parent); |
| 98 } else { | 95 } else { |
| 99 menu_ = CreateMenu(node, start_child_index, show_options); | 96 menu_ = CreateMenu(node, start_child_index, show_options); |
| 100 } | 97 } |
| 101 } | 98 } |
| 102 | 99 |
| 103 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) { | 100 void BookmarkMenuDelegate::SetPageNavigator(PageNavigator* navigator) { |
| 104 page_navigator_ = navigator; | 101 page_navigator_ = navigator; |
| 105 if (context_menu_.get()) | 102 if (context_menu_.get()) |
| 106 context_menu_->SetPageNavigator(navigator); | 103 context_menu_->SetPageNavigator(navigator); |
| 107 } | 104 } |
| 108 | 105 |
| 109 BookmarkModel* BookmarkMenuDelegate::GetBookmarkModel() { | 106 BookmarkModel* BookmarkMenuDelegate::GetBookmarkModel() { |
| 110 return BookmarkModelFactory::GetForProfile(profile_); | 107 return BookmarkModelFactory::GetForProfile(profile_); |
| 111 } | 108 } |
| 112 | 109 |
| 113 ChromeBookmarkClient* BookmarkMenuDelegate::GetChromeBookmarkClient() { | |
| 114 return ChromeBookmarkClientFactory::GetForProfile(profile_); | |
| 115 } | |
| 116 | |
| 117 void BookmarkMenuDelegate::SetActiveMenu(const BookmarkNode* node, | 110 void BookmarkMenuDelegate::SetActiveMenu(const BookmarkNode* node, |
| 118 int start_index) { | 111 int start_index) { |
| 119 DCHECK(!parent_menu_item_); | 112 DCHECK(!parent_menu_item_); |
| 120 if (!node_to_menu_map_[node]) | 113 if (!node_to_menu_map_[node]) |
| 121 CreateMenu(node, start_index, HIDE_PERMANENT_FOLDERS); | 114 CreateMenu(node, start_index, HIDE_PERMANENT_FOLDERS); |
| 122 menu_ = node_to_menu_map_[node]; | 115 menu_ = node_to_menu_map_[node]; |
| 123 } | 116 } |
| 124 | 117 |
| 125 base::string16 BookmarkMenuDelegate::GetTooltipText( | 118 base::string16 BookmarkMenuDelegate::GetTooltipText( |
| 126 int id, | 119 int id, |
| (...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 467 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| 475 gfx::ImageSkia* folder_icon = rb->GetImageSkiaNamed(icon_resource_id); | 468 gfx::ImageSkia* folder_icon = rb->GetImageSkiaNamed(icon_resource_id); |
| 476 AddMenuToMaps(menu->AppendSubMenuWithIcon(next_menu_id_++, node->GetTitle(), | 469 AddMenuToMaps(menu->AppendSubMenuWithIcon(next_menu_id_++, node->GetTitle(), |
| 477 *folder_icon), | 470 *folder_icon), |
| 478 node); | 471 node); |
| 479 } | 472 } |
| 480 | 473 |
| 481 void BookmarkMenuDelegate::BuildMenuForManagedNode(MenuItemView* menu) { | 474 void BookmarkMenuDelegate::BuildMenuForManagedNode(MenuItemView* menu) { |
| 482 // Don't add a separator for this menu. | 475 // Don't add a separator for this menu. |
| 483 bool added_separator = true; | 476 bool added_separator = true; |
| 484 const BookmarkNode* node = GetChromeBookmarkClient()->managed_node(); | 477 const BookmarkNode* node = GetBookmarkModel()->managed_node(); |
| 485 BuildMenuForPermanentNode(node, IDR_BOOKMARK_BAR_FOLDER_MANAGED, menu, | 478 BuildMenuForPermanentNode(node, IDR_BOOKMARK_BAR_FOLDER_MANAGED, menu, |
| 486 &added_separator); | 479 &added_separator); |
| 487 } | 480 } |
| 488 | 481 |
| 489 void BookmarkMenuDelegate::BuildMenuForSupervisedNode(MenuItemView* menu) { | 482 void BookmarkMenuDelegate::BuildMenuForSupervisedNode(MenuItemView* menu) { |
| 490 // Don't add a separator for this menu. | 483 // Don't add a separator for this menu. |
| 491 bool added_separator = true; | 484 bool added_separator = true; |
| 492 const BookmarkNode* node = GetChromeBookmarkClient()->supervised_node(); | 485 const BookmarkNode* node = GetBookmarkModel()->supervised_node(); |
| 493 BuildMenuForPermanentNode(node, IDR_BOOKMARK_BAR_FOLDER_SUPERVISED, menu, | 486 BuildMenuForPermanentNode(node, IDR_BOOKMARK_BAR_FOLDER_SUPERVISED, menu, |
| 494 &added_separator); | 487 &added_separator); |
| 495 } | 488 } |
| 496 | 489 |
| 497 void BookmarkMenuDelegate::BuildMenu(const BookmarkNode* parent, | 490 void BookmarkMenuDelegate::BuildMenu(const BookmarkNode* parent, |
| 498 int start_child_index, | 491 int start_child_index, |
| 499 MenuItemView* menu) { | 492 MenuItemView* menu) { |
| 500 DCHECK(parent->empty() || start_child_index < parent->child_count()); | 493 DCHECK(parent->empty() || start_child_index < parent->child_count()); |
| 501 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 494 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| 502 for (int i = start_child_index; i < parent->child_count(); ++i) { | 495 for (int i = start_child_index; i < parent->child_count(); ++i) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 518 } | 511 } |
| 519 AddMenuToMaps(child_menu_item, node); | 512 AddMenuToMaps(child_menu_item, node); |
| 520 } | 513 } |
| 521 } | 514 } |
| 522 | 515 |
| 523 void BookmarkMenuDelegate::AddMenuToMaps(MenuItemView* menu, | 516 void BookmarkMenuDelegate::AddMenuToMaps(MenuItemView* menu, |
| 524 const BookmarkNode* node) { | 517 const BookmarkNode* node) { |
| 525 menu_id_to_node_map_[menu->GetCommand()] = node; | 518 menu_id_to_node_map_[menu->GetCommand()] = node; |
| 526 node_to_menu_map_[node] = menu; | 519 node_to_menu_map_[node] = menu; |
| 527 } | 520 } |
| OLD | NEW |