| OLD | NEW |
| (Empty) |
| 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 "base/time/time.h" | |
| 11 #include "base/timer/elapsed_timer.h" | |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | |
| 13 #include "content/public/browser/host_zoom_map.h" | |
| 14 #include "content/public/browser/notification_observer.h" | |
| 15 #include "content/public/browser/notification_registrar.h" | |
| 16 #include "ui/base/accelerators/accelerator.h" | |
| 17 #include "ui/base/models/button_menu_item_model.h" | |
| 18 #include "ui/base/models/simple_menu_model.h" | |
| 19 | |
| 20 class BookmarkSubMenuModel; | |
| 21 class Browser; | |
| 22 class RecentTabsSubMenuModel; | |
| 23 | |
| 24 namespace { | |
| 25 class MockWrenchMenuModel; | |
| 26 } // namespace | |
| 27 | |
| 28 enum WrenchMenuAction { | |
| 29 MENU_ACTION_NEW_TAB = 0, | |
| 30 MENU_ACTION_NEW_WINDOW, | |
| 31 MENU_ACTION_NEW_INCOGNITO_WINDOW, | |
| 32 MENU_ACTION_SHOW_BOOKMARK_BAR, | |
| 33 MENU_ACTION_SHOW_BOOKMARK_MANAGER, | |
| 34 MENU_ACTION_IMPORT_SETTINGS, | |
| 35 MENU_ACTION_BOOKMARK_PAGE, | |
| 36 MENU_ACTION_BOOKMARK_ALL_TABS, | |
| 37 MENU_ACTION_PIN_TO_START_SCREEN, | |
| 38 MENU_ACTION_RESTORE_TAB, | |
| 39 MENU_ACTION_WIN_DESKTOP_RESTART, | |
| 40 MENU_ACTION_WIN8_METRO_RESTART, | |
| 41 MENU_ACTION_WIN_CHROMEOS_RESTART, | |
| 42 MENU_ACTION_DISTILL_PAGE, | |
| 43 MENU_ACTION_SAVE_PAGE, | |
| 44 MENU_ACTION_FIND, | |
| 45 MENU_ACTION_PRINT, | |
| 46 MENU_ACTION_CUT, | |
| 47 MENU_ACTION_COPY, | |
| 48 MENU_ACTION_PASTE, | |
| 49 MENU_ACTION_CREATE_HOSTED_APP, | |
| 50 MENU_ACTION_CREATE_SHORTCUTS, | |
| 51 MENU_ACTION_MANAGE_EXTENSIONS, | |
| 52 MENU_ACTION_TASK_MANAGER, | |
| 53 MENU_ACTION_CLEAR_BROWSING_DATA, | |
| 54 MENU_ACTION_VIEW_SOURCE, | |
| 55 MENU_ACTION_DEV_TOOLS, | |
| 56 MENU_ACTION_DEV_TOOLS_CONSOLE, | |
| 57 MENU_ACTION_DEV_TOOLS_DEVICES, | |
| 58 MENU_ACTION_PROFILING_ENABLED, | |
| 59 MENU_ACTION_ZOOM_MINUS, | |
| 60 MENU_ACTION_ZOOM_PLUS, | |
| 61 MENU_ACTION_FULLSCREEN, | |
| 62 MENU_ACTION_SHOW_HISTORY, | |
| 63 MENU_ACTION_SHOW_DOWNLOADS, | |
| 64 MENU_ACTION_SHOW_SYNC_SETUP, | |
| 65 MENU_ACTION_OPTIONS, | |
| 66 MENU_ACTION_ABOUT, | |
| 67 MENU_ACTION_HELP_PAGE_VIA_MENU, | |
| 68 MENU_ACTION_FEEDBACK, | |
| 69 MENU_ACTION_TOGGLE_REQUEST_TABLET_SITE, | |
| 70 MENU_ACTION_EXIT, | |
| 71 MENU_ACTION_RECENT_TAB, | |
| 72 MENU_ACTION_BOOKMARK_OPEN, | |
| 73 LIMIT_MENU_ACTION | |
| 74 }; | |
| 75 | |
| 76 // A menu model that builds the contents of an encoding menu. | |
| 77 class EncodingMenuModel : public ui::SimpleMenuModel, | |
| 78 public ui::SimpleMenuModel::Delegate { | |
| 79 public: | |
| 80 explicit EncodingMenuModel(Browser* browser); | |
| 81 ~EncodingMenuModel() override; | |
| 82 | |
| 83 // Overridden from ui::SimpleMenuModel::Delegate: | |
| 84 bool IsCommandIdChecked(int command_id) const override; | |
| 85 bool IsCommandIdEnabled(int command_id) const override; | |
| 86 bool GetAcceleratorForCommandId(int command_id, | |
| 87 ui::Accelerator* accelerator) override; | |
| 88 void ExecuteCommand(int command_id, int event_flags) override; | |
| 89 | |
| 90 private: | |
| 91 void Build(); | |
| 92 | |
| 93 Browser* browser_; // weak | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(EncodingMenuModel); | |
| 96 }; | |
| 97 | |
| 98 // A menu model that builds the contents of the zoom menu. | |
| 99 class ZoomMenuModel : public ui::SimpleMenuModel { | |
| 100 public: | |
| 101 explicit ZoomMenuModel(ui::SimpleMenuModel::Delegate* delegate); | |
| 102 ~ZoomMenuModel() override; | |
| 103 | |
| 104 private: | |
| 105 void Build(); | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(ZoomMenuModel); | |
| 108 }; | |
| 109 | |
| 110 class ToolsMenuModel : public ui::SimpleMenuModel { | |
| 111 public: | |
| 112 ToolsMenuModel(ui::SimpleMenuModel::Delegate* delegate, Browser* browser); | |
| 113 ~ToolsMenuModel() override; | |
| 114 | |
| 115 private: | |
| 116 void Build(Browser* browser); | |
| 117 | |
| 118 scoped_ptr<EncodingMenuModel> encoding_menu_model_; | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(ToolsMenuModel); | |
| 121 }; | |
| 122 | |
| 123 // A menu model that builds the contents of the wrench menu. | |
| 124 class WrenchMenuModel : public ui::SimpleMenuModel, | |
| 125 public ui::SimpleMenuModel::Delegate, | |
| 126 public ui::ButtonMenuItemModel::Delegate, | |
| 127 public TabStripModelObserver, | |
| 128 public content::NotificationObserver { | |
| 129 public: | |
| 130 // Range of command IDs to use for the items in the recent tabs submenu. | |
| 131 static const int kMinRecentTabsCommandId = 1001; | |
| 132 static const int kMaxRecentTabsCommandId = 1200; | |
| 133 | |
| 134 WrenchMenuModel(ui::AcceleratorProvider* provider, Browser* browser); | |
| 135 ~WrenchMenuModel() override; | |
| 136 | |
| 137 // Overridden for ButtonMenuItemModel::Delegate: | |
| 138 bool DoesCommandIdDismissMenu(int command_id) const override; | |
| 139 | |
| 140 // Overridden for both ButtonMenuItemModel::Delegate and SimpleMenuModel: | |
| 141 bool IsItemForCommandIdDynamic(int command_id) const override; | |
| 142 base::string16 GetLabelForCommandId(int command_id) const override; | |
| 143 bool GetIconForCommandId(int command_id, gfx::Image* icon) const override; | |
| 144 void ExecuteCommand(int command_id, int event_flags) override; | |
| 145 bool IsCommandIdChecked(int command_id) const override; | |
| 146 bool IsCommandIdEnabled(int command_id) const override; | |
| 147 bool IsCommandIdVisible(int command_id) const override; | |
| 148 bool GetAcceleratorForCommandId(int command_id, | |
| 149 ui::Accelerator* accelerator) override; | |
| 150 | |
| 151 // Overridden from TabStripModelObserver: | |
| 152 void ActiveTabChanged(content::WebContents* old_contents, | |
| 153 content::WebContents* new_contents, | |
| 154 int index, | |
| 155 int reason) override; | |
| 156 void TabReplacedAt(TabStripModel* tab_strip_model, | |
| 157 content::WebContents* old_contents, | |
| 158 content::WebContents* new_contents, | |
| 159 int index) override; | |
| 160 | |
| 161 // Overridden from content::NotificationObserver: | |
| 162 void Observe(int type, | |
| 163 const content::NotificationSource& source, | |
| 164 const content::NotificationDetails& details) override; | |
| 165 | |
| 166 // Getters. | |
| 167 Browser* browser() const { return browser_; } | |
| 168 | |
| 169 BookmarkSubMenuModel* bookmark_sub_menu_model() const { | |
| 170 return bookmark_sub_menu_model_.get(); | |
| 171 } | |
| 172 | |
| 173 // Calculates |zoom_label_| in response to a zoom change. | |
| 174 void UpdateZoomControls(); | |
| 175 | |
| 176 private: | |
| 177 class HelpMenuModel; | |
| 178 // Testing constructor used for mocking. | |
| 179 friend class ::MockWrenchMenuModel; | |
| 180 | |
| 181 WrenchMenuModel(); | |
| 182 | |
| 183 void Build(); | |
| 184 | |
| 185 // Adds actionable global error menu items to the menu. | |
| 186 // Examples: Extension permissions and sign in errors. | |
| 187 // Returns a boolean indicating whether any menu items were added. | |
| 188 bool AddGlobalErrorMenuItems(); | |
| 189 | |
| 190 // Appends everything needed for the clipboard menu: a menu break, the | |
| 191 // clipboard menu content and the finalizing menu break. | |
| 192 void CreateCutCopyPasteMenu(); | |
| 193 | |
| 194 // Add a menu item for the browser action icons. | |
| 195 void CreateActionToolbarOverflowMenu(); | |
| 196 | |
| 197 // Appends everything needed for the zoom menu: a menu break, then the zoom | |
| 198 // menu content and then another menu break. | |
| 199 void CreateZoomMenu(); | |
| 200 | |
| 201 void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change); | |
| 202 | |
| 203 bool ShouldShowNewIncognitoWindowMenuItem(); | |
| 204 | |
| 205 // Called when a command is selected. | |
| 206 // Logs UMA metrics about which command was chosen and how long the user | |
| 207 // took to select the command. | |
| 208 void LogMenuMetrics(int command_id); | |
| 209 | |
| 210 // Helper function to record the menu action in a UMA histogram. | |
| 211 void LogMenuAction(int action_id); | |
| 212 | |
| 213 // Time menu has been open. Used by LogMenuMetrics() to record the time | |
| 214 // to action when the user selects a menu item. | |
| 215 base::ElapsedTimer timer_; | |
| 216 | |
| 217 // Whether a UMA menu action has been recorded since the menu is open. | |
| 218 // Only the first time to action is recorded since some commands | |
| 219 // (zoom controls) don't dimiss the menu. | |
| 220 bool uma_action_recorded_; | |
| 221 | |
| 222 // Models for the special menu items with buttons. | |
| 223 scoped_ptr<ui::ButtonMenuItemModel> edit_menu_item_model_; | |
| 224 scoped_ptr<ui::ButtonMenuItemModel> zoom_menu_item_model_; | |
| 225 | |
| 226 // Label of the zoom label in the zoom menu item. | |
| 227 base::string16 zoom_label_; | |
| 228 | |
| 229 #if defined(GOOGLE_CHROME_BUILD) | |
| 230 // Help menu. | |
| 231 scoped_ptr<HelpMenuModel> help_menu_model_; | |
| 232 #endif | |
| 233 | |
| 234 // Tools menu. | |
| 235 scoped_ptr<ToolsMenuModel> tools_menu_model_; | |
| 236 | |
| 237 // Bookmark submenu. | |
| 238 scoped_ptr<BookmarkSubMenuModel> bookmark_sub_menu_model_; | |
| 239 | |
| 240 // Recent Tabs submenu. | |
| 241 scoped_ptr<RecentTabsSubMenuModel> recent_tabs_sub_menu_model_; | |
| 242 | |
| 243 ui::AcceleratorProvider* provider_; // weak | |
| 244 | |
| 245 Browser* browser_; // weak | |
| 246 | |
| 247 scoped_ptr<content::HostZoomMap::Subscription> browser_zoom_subscription_; | |
| 248 content::NotificationRegistrar registrar_; | |
| 249 | |
| 250 DISALLOW_COPY_AND_ASSIGN(WrenchMenuModel); | |
| 251 }; | |
| 252 | |
| 253 #endif // CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_ | |
| OLD | NEW |