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 IDs to use for the items in the recent tabs submenu.
132 static const int kMinRecentTabsCommandId
= 1001;
133 static const int kMaxRecentTabsCommandId
= 1200;
135 WrenchMenuModel(ui::AcceleratorProvider
* provider
, Browser
* browser
);
136 ~WrenchMenuModel() override
;
138 // Overridden for ButtonMenuItemModel::Delegate:
139 bool DoesCommandIdDismissMenu(int command_id
) const override
;
141 // Overridden for both ButtonMenuItemModel::Delegate and SimpleMenuModel:
142 bool IsItemForCommandIdDynamic(int command_id
) const override
;
143 base::string16
GetLabelForCommandId(int command_id
) const override
;
144 bool GetIconForCommandId(int command_id
, gfx::Image
* icon
) const override
;
145 void ExecuteCommand(int command_id
, int event_flags
) override
;
146 bool IsCommandIdChecked(int command_id
) const override
;
147 bool IsCommandIdEnabled(int command_id
) const override
;
148 bool IsCommandIdVisible(int command_id
) const override
;
149 bool GetAcceleratorForCommandId(int command_id
,
150 ui::Accelerator
* accelerator
) override
;
152 // Overridden from TabStripModelObserver:
153 void ActiveTabChanged(content::WebContents
* old_contents
,
154 content::WebContents
* new_contents
,
156 int reason
) override
;
157 void TabReplacedAt(TabStripModel
* tab_strip_model
,
158 content::WebContents
* old_contents
,
159 content::WebContents
* new_contents
,
161 void TabStripModelDeleted() override
;
163 // Overridden from content::NotificationObserver:
164 void Observe(int type
,
165 const content::NotificationSource
& source
,
166 const content::NotificationDetails
& details
) override
;
169 Browser
* browser() const { return browser_
; }
171 BookmarkSubMenuModel
* bookmark_sub_menu_model() const {
172 return bookmark_sub_menu_model_
.get();
175 // Calculates |zoom_label_| in response to a zoom change.
176 void UpdateZoomControls();
180 // Testing constructor used for mocking.
181 friend class ::MockWrenchMenuModel
;
187 // Adds actionable global error menu items to the menu.
188 // Examples: Extension permissions and sign in errors.
189 // Returns a boolean indicating whether any menu items were added.
190 bool AddGlobalErrorMenuItems();
192 // Appends everything needed for the clipboard menu: a menu break, the
193 // clipboard menu content and the finalizing menu break.
194 void CreateCutCopyPasteMenu();
196 // Add a menu item for the browser action icons.
197 void CreateActionToolbarOverflowMenu();
199 // Appends everything needed for the zoom menu: a menu break, then the zoom
200 // menu content and then another menu break.
201 void CreateZoomMenu();
203 void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange
& change
);
205 bool ShouldShowNewIncognitoWindowMenuItem();
207 // Called when a command is selected.
208 // Logs UMA metrics about which command was chosen and how long the user
209 // took to select the command.
210 void LogMenuMetrics(int command_id
);
212 // Helper function to record the menu action in a UMA histogram.
213 void LogMenuAction(int action_id
);
215 // Time menu has been open. Used by LogMenuMetrics() to record the time
216 // to action when the user selects a menu item.
217 base::ElapsedTimer timer_
;
219 // Whether a UMA menu action has been recorded since the menu is open.
220 // Only the first time to action is recorded since some commands
221 // (zoom controls) don't dimiss the menu.
222 bool uma_action_recorded_
;
224 // Models for the special menu items with buttons.
225 scoped_ptr
<ui::ButtonMenuItemModel
> edit_menu_item_model_
;
226 scoped_ptr
<ui::ButtonMenuItemModel
> zoom_menu_item_model_
;
228 // Label of the zoom label in the zoom menu item.
229 base::string16 zoom_label_
;
231 #if defined(GOOGLE_CHROME_BUILD)
233 scoped_ptr
<HelpMenuModel
> help_menu_model_
;
237 scoped_ptr
<ToolsMenuModel
> tools_menu_model_
;
240 scoped_ptr
<BookmarkSubMenuModel
> bookmark_sub_menu_model_
;
242 // Recent Tabs submenu.
243 scoped_ptr
<RecentTabsSubMenuModel
> recent_tabs_sub_menu_model_
;
245 ui::AcceleratorProvider
* provider_
; // weak
247 Browser
* browser_
; // weak
248 TabStripModel
* tab_strip_model_
; // weak
250 scoped_ptr
<content::HostZoomMap::Subscription
> browser_zoom_subscription_
;
251 content::NotificationRegistrar registrar_
;
253 DISALLOW_COPY_AND_ASSIGN(WrenchMenuModel
);
256 #endif // CHROME_BROWSER_UI_TOOLBAR_WRENCH_MENU_MODEL_H_