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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_ 6 #define CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_ 7 8 #include "base/files/file_path.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" 11 #include "content/public/browser/host_zoom_map.h" 12 #include "content/public/browser/notification_observer.h" 13 #include "content/public/browser/notification_registrar.h" 14 #include "ui/base/accelerators/accelerator.h" 15 #include "ui/base/models/button_menu_item_model.h" 16 #include "ui/base/models/simple_menu_model.h" 17 18 class BookmarkSubMenuModel; 19 class Browser; 20 class RecentTabsSubMenuModel; 21 class TabStripModel; 22 23 namespace { 24 class MockWrenchMenuModel; 25 } // namespace 26 27 // A menu model that builds the contents of an encoding menu. 28 class EncodingMenuModel : public ui::SimpleMenuModel, 29 public ui::SimpleMenuModel::Delegate { 30 public: 31 explicit EncodingMenuModel(Browser* browser); 32 virtual ~EncodingMenuModel(); 33 34 // Overridden from ui::SimpleMenuModel::Delegate: 35 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; 36 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; 37 virtual bool GetAcceleratorForCommandId( 38 int command_id, 39 ui::Accelerator* accelerator) OVERRIDE; 40 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; 41 42 private: 43 void Build(); 44 45 Browser* browser_; // weak 46 47 DISALLOW_COPY_AND_ASSIGN(EncodingMenuModel); 48 }; 49 50 // A menu model that builds the contents of the zoom menu. 51 class ZoomMenuModel : public ui::SimpleMenuModel { 52 public: 53 explicit ZoomMenuModel(ui::SimpleMenuModel::Delegate* delegate); 54 virtual ~ZoomMenuModel(); 55 56 private: 57 void Build(); 58 59 DISALLOW_COPY_AND_ASSIGN(ZoomMenuModel); 60 }; 61 62 class ToolsMenuModel : public ui::SimpleMenuModel { 63 public: 64 ToolsMenuModel(ui::SimpleMenuModel::Delegate* delegate, Browser* browser); 65 virtual ~ToolsMenuModel(); 66 67 private: 68 void Build(Browser* browser); 69 70 scoped_ptr<EncodingMenuModel> encoding_menu_model_; 71 72 DISALLOW_COPY_AND_ASSIGN(ToolsMenuModel); 73 }; 74 75 // A menu model that builds the contents of the wrench menu. 76 class WrenchMenuModel : public ui::SimpleMenuModel, 77 public ui::SimpleMenuModel::Delegate, 78 public ui::ButtonMenuItemModel::Delegate, 79 public TabStripModelObserver, 80 public content::NotificationObserver { 81 public: 82 // Range of command ID's to use for the items representing bookmarks in the 83 // bookmark menu, must not overlap with that for recent tabs submenu. 84 static const int kMinBookmarkCommandId = 1; 85 static const int kMaxBookmarkCommandId = 1000; 86 87 // Range of command ID's to use for the items in the recent tabs submenu, must 88 // not overlap with that for bookmarks. 89 static const int kMinRecentTabsCommandId = 1001; 90 static const int kMaxRecentTabsCommandId = 1200; 91 92 WrenchMenuModel(ui::AcceleratorProvider* provider, Browser* browser); 93 virtual ~WrenchMenuModel(); 94 95 // Overridden for ButtonMenuItemModel::Delegate: 96 virtual bool DoesCommandIdDismissMenu(int command_id) const OVERRIDE; 97 98 // Overridden for both ButtonMenuItemModel::Delegate and SimpleMenuModel: 99 virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE; 100 virtual base::string16 GetLabelForCommandId(int command_id) const OVERRIDE; 101 virtual bool GetIconForCommandId(int command_id, 102 gfx::Image* icon) const OVERRIDE; 103 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; 104 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; 105 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; 106 virtual bool IsCommandIdVisible(int command_id) const OVERRIDE; 107 virtual bool GetAcceleratorForCommandId( 108 int command_id, 109 ui::Accelerator* accelerator) OVERRIDE; 110 111 // Overridden from TabStripModelObserver: 112 virtual void ActiveTabChanged(content::WebContents* old_contents, 113 content::WebContents* new_contents, 114 int index, 115 int reason) OVERRIDE; 116 virtual void TabReplacedAt(TabStripModel* tab_strip_model, 117 content::WebContents* old_contents, 118 content::WebContents* new_contents, 119 int index) OVERRIDE; 120 virtual void TabStripModelDeleted() OVERRIDE; 121 122 // Overridden from content::NotificationObserver: 123 virtual void Observe(int type, 124 const content::NotificationSource& source, 125 const content::NotificationDetails& details) OVERRIDE; 126 127 // Getters. browser()128 Browser* browser() const { return browser_; } 129 bookmark_sub_menu_model()130 BookmarkSubMenuModel* bookmark_sub_menu_model() const { 131 return bookmark_sub_menu_model_.get(); 132 } 133 134 // Calculates |zoom_label_| in response to a zoom change. 135 void UpdateZoomControls(); 136 137 private: 138 class HelpMenuModel; 139 // Testing constructor used for mocking. 140 friend class ::MockWrenchMenuModel; 141 142 WrenchMenuModel(); 143 144 void Build(); 145 146 void AddGlobalErrorMenuItems(); 147 148 // Appends everything needed for the clipboard menu: a menu break, the 149 // clipboard menu content and the finalizing menu break. 150 void CreateCutCopyPasteMenu(); 151 152 // Add a menu item for the extension icons. 153 void CreateExtensionToolbarOverflowMenu(); 154 155 // Appends everything needed for the zoom menu: a menu break, then the zoom 156 // menu content and then another menu break. 157 void CreateZoomMenu(); 158 159 void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change); 160 161 bool ShouldShowNewIncognitoWindowMenuItem(); 162 163 // Models for the special menu items with buttons. 164 scoped_ptr<ui::ButtonMenuItemModel> edit_menu_item_model_; 165 scoped_ptr<ui::ButtonMenuItemModel> zoom_menu_item_model_; 166 167 // Label of the zoom label in the zoom menu item. 168 base::string16 zoom_label_; 169 170 #if defined(GOOGLE_CHROME_BUILD) 171 // Help menu. 172 scoped_ptr<HelpMenuModel> help_menu_model_; 173 #endif 174 175 // Tools menu. 176 scoped_ptr<ToolsMenuModel> tools_menu_model_; 177 178 // Bookmark submenu. 179 scoped_ptr<BookmarkSubMenuModel> bookmark_sub_menu_model_; 180 181 // Recent Tabs submenu. 182 scoped_ptr<RecentTabsSubMenuModel> recent_tabs_sub_menu_model_; 183 184 ui::AcceleratorProvider* provider_; // weak 185 186 Browser* browser_; // weak 187 TabStripModel* tab_strip_model_; // weak 188 189 scoped_ptr<content::HostZoomMap::Subscription> content_zoom_subscription_; 190 scoped_ptr<content::HostZoomMap::Subscription> browser_zoom_subscription_; 191 content::NotificationRegistrar registrar_; 192 193 DISALLOW_COPY_AND_ASSIGN(WrenchMenuModel); 194 }; 195 196 #endif // CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_ 197