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.
5 #ifndef CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_
6 #define CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_
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"
20 class BookmarkSubMenuModel
;
22 class RecentTabsSubMenuModel
;
26 class MockWrenchMenuModel
;
29 enum WrenchMenuAction
{
30 MENU_ACTION_NEW_TAB
= 0,
31 MENU_ACTION_NEW_WINDOW
,
32 MENU_ACTION_NEW_INCOGNITO_WINDOW
,
33 MENU_ACTION_SHOW_BOOKMARK_BAR
,
34 MENU_ACTION_SHOW_BOOKMARK_MANAGER
,
35 MENU_ACTION_IMPORT_SETTINGS
,
36 MENU_ACTION_BOOKMARK_PAGE
,
37 MENU_ACTION_BOOKMARK_ALL_TABS
,
38 MENU_ACTION_PIN_TO_START_SCREEN
,
39 MENU_ACTION_RESTORE_TAB
,
40 MENU_ACTION_WIN_DESKTOP_RESTART
,
41 MENU_ACTION_WIN8_METRO_RESTART
,
42 MENU_ACTION_WIN_CHROMEOS_RESTART
,
43 MENU_ACTION_DISTILL_PAGE
,
44 MENU_ACTION_SAVE_PAGE
,
50 MENU_ACTION_CREATE_HOSTED_APP
,
51 MENU_ACTION_CREATE_SHORTCUTS
,
52 MENU_ACTION_MANAGE_EXTENSIONS
,
53 MENU_ACTION_TASK_MANAGER
,
54 MENU_ACTION_CLEAR_BROWSING_DATA
,
55 MENU_ACTION_VIEW_SOURCE
,
56 MENU_ACTION_DEV_TOOLS
,
57 MENU_ACTION_DEV_TOOLS_CONSOLE
,
58 MENU_ACTION_DEV_TOOLS_DEVICES
,
59 MENU_ACTION_PROFILING_ENABLED
,
60 MENU_ACTION_ZOOM_MINUS
,
61 MENU_ACTION_ZOOM_PLUS
,
62 MENU_ACTION_FULLSCREEN
,
63 MENU_ACTION_SHOW_HISTORY
,
64 MENU_ACTION_SHOW_DOWNLOADS
,
65 MENU_ACTION_SHOW_SYNC_SETUP
,
68 MENU_ACTION_HELP_PAGE_VIA_MENU
,
70 MENU_ACTION_TOGGLE_REQUEST_TABLET_SITE
,
72 MENU_ACTION_RECENT_TAB
,
73 MENU_ACTION_BOOKMARK_OPEN
,
77 // A menu model that builds the contents of an encoding menu.
78 class EncodingMenuModel
: public ui::SimpleMenuModel
,
79 public ui::SimpleMenuModel::Delegate
{
81 explicit EncodingMenuModel(Browser
* browser
);
82 ~EncodingMenuModel() override
;
84 // Overridden from ui::SimpleMenuModel::Delegate:
85 bool IsCommandIdChecked(int command_id
) const override
;
86 bool IsCommandIdEnabled(int command_id
) const override
;
87 bool GetAcceleratorForCommandId(int command_id
,
88 ui::Accelerator
* accelerator
) override
;
89 void ExecuteCommand(int command_id
, int event_flags
) override
;
94 Browser
* browser_
; // weak
96 DISALLOW_COPY_AND_ASSIGN(EncodingMenuModel
);
99 // A menu model that builds the contents of the zoom menu.
100 class ZoomMenuModel
: public ui::SimpleMenuModel
{
102 explicit ZoomMenuModel(ui::SimpleMenuModel::Delegate
* delegate
);
103 ~ZoomMenuModel() override
;
108 DISALLOW_COPY_AND_ASSIGN(ZoomMenuModel
);
111 class ToolsMenuModel
: public ui::SimpleMenuModel
{
113 ToolsMenuModel(ui::SimpleMenuModel::Delegate
* delegate
, Browser
* browser
);
114 ~ToolsMenuModel() override
;
117 void Build(Browser
* browser
);
119 scoped_ptr
<EncodingMenuModel
> encoding_menu_model_
;
121 DISALLOW_COPY_AND_ASSIGN(ToolsMenuModel
);
124 // A menu model that builds the contents of the wrench menu.
125 class WrenchMenuModel
: public ui::SimpleMenuModel
,
126 public ui::SimpleMenuModel::Delegate
,
127 public ui::ButtonMenuItemModel::Delegate
,
128 public TabStripModelObserver
,
129 public content::NotificationObserver
{
131 // Range of command ID's to use for the items representing bookmarks in the
132 // bookmark menu, must not overlap with that for recent tabs submenu.
133 static const int kMinBookmarkCommandId
= 1;
134 static const int kMaxBookmarkCommandId
= 1000;
136 // Range of command ID's to use for the items in the recent tabs submenu, must
137 // not overlap with that for bookmarks.
138 static const int kMinRecentTabsCommandId
= 1001;
139 static const int kMaxRecentTabsCommandId
= 1200;
141 WrenchMenuModel(ui::AcceleratorProvider
* provider
, Browser
* browser
);
142 ~WrenchMenuModel() override
;
144 // Overridden for ButtonMenuItemModel::Delegate:
145 bool DoesCommandIdDismissMenu(int command_id
) const override
;
147 // Overridden for both ButtonMenuItemModel::Delegate and SimpleMenuModel:
148 bool IsItemForCommandIdDynamic(int command_id
) const override
;
149 base::string16
GetLabelForCommandId(int command_id
) const override
;
150 bool GetIconForCommandId(int command_id
, gfx::Image
* icon
) const override
;
151 void ExecuteCommand(int command_id
, int event_flags
) override
;
152 bool IsCommandIdChecked(int command_id
) const override
;
153 bool IsCommandIdEnabled(int command_id
) const override
;
154 bool IsCommandIdVisible(int command_id
) const override
;
155 bool GetAcceleratorForCommandId(int command_id
,
156 ui::Accelerator
* accelerator
) override
;
158 // Overridden from TabStripModelObserver:
159 void ActiveTabChanged(content::WebContents
* old_contents
,
160 content::WebContents
* new_contents
,
162 int reason
) override
;
163 void TabReplacedAt(TabStripModel
* tab_strip_model
,
164 content::WebContents
* old_contents
,
165 content::WebContents
* new_contents
,
167 void TabStripModelDeleted() override
;
169 // Overridden from content::NotificationObserver:
170 void Observe(int type
,
171 const content::NotificationSource
& source
,
172 const content::NotificationDetails
& details
) override
;
175 Browser
* browser() const { return browser_
; }
177 BookmarkSubMenuModel
* bookmark_sub_menu_model() const {
178 return bookmark_sub_menu_model_
.get();
181 // Calculates |zoom_label_| in response to a zoom change.
182 void UpdateZoomControls();
186 // Testing constructor used for mocking.
187 friend class ::MockWrenchMenuModel
;
193 void AddGlobalErrorMenuItems();
195 // Appends everything needed for the clipboard menu: a menu break, the
196 // clipboard menu content and the finalizing menu break.
197 void CreateCutCopyPasteMenu();
199 // Add a menu item for the extension icons.
200 void CreateExtensionToolbarOverflowMenu();
202 // Appends everything needed for the zoom menu: a menu break, then the zoom
203 // menu content and then another menu break.
204 void CreateZoomMenu();
206 void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange
& change
);
208 bool ShouldShowNewIncognitoWindowMenuItem();
210 // Called when a command is selected.
211 // Logs UMA metrics about which command was chosen and how long the user
212 // took to select the command.
213 void LogMenuMetrics(int command_id
);
215 // Helper function to record the menu action in a UMA histogram.
216 void LogMenuAction(int action_id
);
218 // Time menu has been open. Used by LogMenuMetrics() to record the time
219 // to action when the user selects a menu item.
220 base::ElapsedTimer timer_
;
222 // Whether a UMA menu action has been recorded since the menu is open.
223 // Only the first time to action is recorded since some commands
224 // (zoom controls) don't dimiss the menu.
225 bool uma_action_recorded_
;
227 // Models for the special menu items with buttons.
228 scoped_ptr
<ui::ButtonMenuItemModel
> edit_menu_item_model_
;
229 scoped_ptr
<ui::ButtonMenuItemModel
> zoom_menu_item_model_
;
231 // Label of the zoom label in the zoom menu item.
232 base::string16 zoom_label_
;
234 #if defined(GOOGLE_CHROME_BUILD)
236 scoped_ptr
<HelpMenuModel
> help_menu_model_
;
240 scoped_ptr
<ToolsMenuModel
> tools_menu_model_
;
243 scoped_ptr
<BookmarkSubMenuModel
> bookmark_sub_menu_model_
;
245 // Recent Tabs submenu.
246 scoped_ptr
<RecentTabsSubMenuModel
> recent_tabs_sub_menu_model_
;
248 ui::AcceleratorProvider
* provider_
; // weak
250 Browser
* browser_
; // weak
251 TabStripModel
* tab_strip_model_
; // weak
253 scoped_ptr
<content::HostZoomMap::Subscription
> browser_zoom_subscription_
;
254 content::NotificationRegistrar registrar_
;
256 DISALLOW_COPY_AND_ASSIGN(WrenchMenuModel
);
259 #endif // CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_