Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)
[chromium-blink-merge.git] / chrome / browser / ui / toolbar / wrench_menu_model.cc
blobb3ffec09a6b2efe95c7d5202b825030fa0ac97df
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 #include "chrome/browser/ui/toolbar/wrench_menu_model.h"
7 #include <algorithm>
8 #include <cmath>
10 #include "base/command_line.h"
11 #include "base/metrics/histogram.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/app/chrome_command_ids.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/defaults.h"
19 #include "chrome/browser/extensions/extension_util.h"
20 #include "chrome/browser/prefs/incognito_mode_prefs.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/browser/search/search.h"
24 #include "chrome/browser/signin/signin_manager_factory.h"
25 #include "chrome/browser/signin/signin_ui_util.h"
26 #include "chrome/browser/task_manager/task_manager.h"
27 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/browser_commands.h"
30 #include "chrome/browser/ui/browser_finder.h"
31 #include "chrome/browser/ui/browser_window.h"
32 #include "chrome/browser/ui/global_error/global_error.h"
33 #include "chrome/browser/ui/global_error/global_error_service.h"
34 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
35 #include "chrome/browser/ui/tabs/tab_strip_model.h"
36 #include "chrome/browser/ui/toolbar/bookmark_sub_menu_model.h"
37 #include "chrome/browser/ui/toolbar/encoding_menu_controller.h"
38 #include "chrome/browser/ui/toolbar/recent_tabs_sub_menu_model.h"
39 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
40 #include "chrome/browser/upgrade_detector.h"
41 #include "chrome/common/chrome_paths.h"
42 #include "chrome/common/chrome_switches.h"
43 #include "chrome/common/pref_names.h"
44 #include "chrome/common/profiling.h"
45 #include "chrome/grit/chromium_strings.h"
46 #include "chrome/grit/generated_resources.h"
47 #include "components/dom_distiller/core/dom_distiller_switches.h"
48 #include "components/signin/core/browser/signin_manager.h"
49 #include "components/signin/core/common/profile_management_switches.h"
50 #include "components/ui/zoom/zoom_controller.h"
51 #include "components/ui/zoom/zoom_event_manager.h"
52 #include "content/public/browser/host_zoom_map.h"
53 #include "content/public/browser/navigation_entry.h"
54 #include "content/public/browser/notification_service.h"
55 #include "content/public/browser/notification_source.h"
56 #include "content/public/browser/notification_types.h"
57 #include "content/public/browser/user_metrics.h"
58 #include "content/public/browser/web_contents.h"
59 #include "extensions/common/feature_switch.h"
60 #include "grit/theme_resources.h"
61 #include "ui/base/l10n/l10n_util.h"
62 #include "ui/base/layout.h"
63 #include "ui/base/models/button_menu_item_model.h"
64 #include "ui/base/resource/resource_bundle.h"
65 #include "ui/gfx/image/image.h"
66 #include "ui/gfx/image/image_skia.h"
68 #if defined(OS_CHROMEOS)
69 #include "chromeos/chromeos_switches.h"
70 #endif
72 #if defined(OS_WIN)
73 #include "base/win/metro.h"
74 #include "base/win/windows_version.h"
75 #include "chrome/browser/enumerate_modules_model_win.h"
76 #include "chrome/browser/ui/metro_pin_tab_helper_win.h"
77 #include "content/public/browser/gpu_data_manager.h"
78 #endif
80 #if defined(USE_ASH)
81 #include "ash/shell.h"
82 #endif
84 using base::UserMetricsAction;
85 using content::WebContents;
87 namespace {
89 #if defined(OS_MACOSX)
90 // An empty command used because of a bug in AppKit menus.
91 // See comment in CreateActionToolbarOverflowMenu().
92 const int kEmptyMenuItemCommand = 0;
93 #endif
95 // Conditionally return the update app menu item title based on upgrade detector
96 // state.
97 base::string16 GetUpgradeDialogMenuItemName() {
98 if (UpgradeDetector::GetInstance()->is_outdated_install() ||
99 UpgradeDetector::GetInstance()->is_outdated_install_no_au()) {
100 return l10n_util::GetStringUTF16(IDS_UPGRADE_BUBBLE_MENU_ITEM);
101 } else {
102 return l10n_util::GetStringUTF16(IDS_UPDATE_NOW);
106 #if defined(OS_WIN)
107 bool GetRestartMenuItemIfRequired(const chrome::HostDesktopType& desktop_type,
108 int* command_id,
109 int* string_id) {
110 if (base::win::GetVersion() == base::win::VERSION_WIN8 ||
111 base::win::GetVersion() == base::win::VERSION_WIN8_1) {
112 if (desktop_type != chrome::HOST_DESKTOP_TYPE_ASH) {
113 *command_id = IDC_WIN8_METRO_RESTART;
114 *string_id = IDS_WIN8_METRO_RESTART;
115 } else {
116 *command_id = IDC_WIN_DESKTOP_RESTART;
117 *string_id = IDS_WIN_DESKTOP_RESTART;
119 return true;
122 // Windows 7 ASH mode is only supported in DEBUG for now.
123 #if !defined(NDEBUG)
124 // Windows 8 can support ASH mode using WARP, but Windows 7 requires a working
125 // GPU compositor.
126 if (base::win::GetVersion() == base::win::VERSION_WIN7 &&
127 content::GpuDataManager::GetInstance()->CanUseGpuBrowserCompositor()) {
128 if (desktop_type != chrome::HOST_DESKTOP_TYPE_ASH) {
129 *command_id = IDC_WIN_CHROMEOS_RESTART;
130 *string_id = IDS_WIN_CHROMEOS_RESTART;
131 } else {
132 *command_id = IDC_WIN_DESKTOP_RESTART;
133 *string_id = IDS_WIN_DESKTOP_RESTART;
135 return true;
137 #endif
138 return false;
140 #endif
142 } // namespace
144 ////////////////////////////////////////////////////////////////////////////////
145 // EncodingMenuModel
147 EncodingMenuModel::EncodingMenuModel(Browser* browser)
148 : ui::SimpleMenuModel(this),
149 browser_(browser) {
150 Build();
153 EncodingMenuModel::~EncodingMenuModel() {
156 void EncodingMenuModel::Build() {
157 EncodingMenuController::EncodingMenuItemList encoding_menu_items;
158 EncodingMenuController encoding_menu_controller;
159 encoding_menu_controller.GetEncodingMenuItems(browser_->profile(),
160 &encoding_menu_items);
162 int group_id = 0;
163 EncodingMenuController::EncodingMenuItemList::iterator it =
164 encoding_menu_items.begin();
165 for (; it != encoding_menu_items.end(); ++it) {
166 int id = it->first;
167 base::string16& label = it->second;
168 if (id == 0) {
169 AddSeparator(ui::NORMAL_SEPARATOR);
170 } else {
171 if (id == IDC_ENCODING_AUTO_DETECT) {
172 AddCheckItem(id, label);
173 } else {
174 // Use the id of the first radio command as the id of the group.
175 if (group_id <= 0)
176 group_id = id;
177 AddRadioItem(id, label, group_id);
183 bool EncodingMenuModel::IsCommandIdChecked(int command_id) const {
184 WebContents* current_tab =
185 browser_->tab_strip_model()->GetActiveWebContents();
186 if (!current_tab)
187 return false;
188 EncodingMenuController controller;
189 return controller.IsItemChecked(browser_->profile(),
190 current_tab->GetEncoding(), command_id);
193 bool EncodingMenuModel::IsCommandIdEnabled(int command_id) const {
194 bool enabled = chrome::IsCommandEnabled(browser_, command_id);
195 // Special handling for the contents of the Encoding submenu. On Mac OS,
196 // instead of enabling/disabling the top-level menu item, the submenu's
197 // contents get disabled, per Apple's HIG.
198 #if defined(OS_MACOSX)
199 enabled &= chrome::IsCommandEnabled(browser_, IDC_ENCODING_MENU);
200 #endif
201 return enabled;
204 bool EncodingMenuModel::GetAcceleratorForCommandId(
205 int command_id,
206 ui::Accelerator* accelerator) {
207 return false;
210 void EncodingMenuModel::ExecuteCommand(int command_id, int event_flags) {
211 chrome::ExecuteCommand(browser_, command_id);
214 ////////////////////////////////////////////////////////////////////////////////
215 // ZoomMenuModel
217 ZoomMenuModel::ZoomMenuModel(ui::SimpleMenuModel::Delegate* delegate)
218 : SimpleMenuModel(delegate) {
219 Build();
222 ZoomMenuModel::~ZoomMenuModel() {
225 void ZoomMenuModel::Build() {
226 AddItemWithStringId(IDC_ZOOM_PLUS, IDS_ZOOM_PLUS);
227 AddItemWithStringId(IDC_ZOOM_NORMAL, IDS_ZOOM_NORMAL);
228 AddItemWithStringId(IDC_ZOOM_MINUS, IDS_ZOOM_MINUS);
231 ////////////////////////////////////////////////////////////////////////////////
232 // HelpMenuModel
234 #if defined(GOOGLE_CHROME_BUILD)
236 class WrenchMenuModel::HelpMenuModel : public ui::SimpleMenuModel {
237 public:
238 HelpMenuModel(ui::SimpleMenuModel::Delegate* delegate,
239 Browser* browser)
240 : SimpleMenuModel(delegate) {
241 Build(browser);
244 private:
245 void Build(Browser* browser) {
246 #if defined(OS_CHROMEOS) && defined(OFFICIAL_BUILD)
247 int help_string_id = IDS_GET_HELP;
248 #else
249 int help_string_id = IDS_HELP_PAGE;
250 #endif
251 AddItem(IDC_ABOUT, l10n_util::GetStringUTF16(IDS_ABOUT));
252 AddItemWithStringId(IDC_HELP_PAGE_VIA_MENU, help_string_id);
253 if (browser_defaults::kShowHelpMenuItemIcon) {
254 ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance();
255 SetIcon(GetIndexOfCommandId(IDC_HELP_PAGE_VIA_MENU),
256 rb.GetNativeImageNamed(IDR_HELP_MENU));
258 AddItemWithStringId(IDC_FEEDBACK, IDS_FEEDBACK);
261 DISALLOW_COPY_AND_ASSIGN(HelpMenuModel);
264 #endif // defined(GOOGLE_CHROME_BUILD)
266 ////////////////////////////////////////////////////////////////////////////////
267 // ToolsMenuModel
269 ToolsMenuModel::ToolsMenuModel(ui::SimpleMenuModel::Delegate* delegate,
270 Browser* browser)
271 : SimpleMenuModel(delegate) {
272 Build(browser);
275 ToolsMenuModel::~ToolsMenuModel() {}
277 // More tools submenu is constructed as follows:
278 // - Page specific actions overflow (save page, adding to taskbar).
279 // - Browser / OS level tools (extensions, task manager).
280 // - Developer tools.
281 // - Option to enable profiling.
282 void ToolsMenuModel::Build(Browser* browser) {
283 bool show_create_shortcuts = true;
284 #if defined(OS_CHROMEOS) || defined(OS_MACOSX)
285 show_create_shortcuts = false;
286 #elif defined(USE_ASH)
287 if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
288 show_create_shortcuts = false;
289 #endif
290 AddItemWithStringId(IDC_SAVE_PAGE, IDS_SAVE_PAGE);
291 if (extensions::util::IsNewBookmarkAppsEnabled()) {
292 #if defined(OS_MACOSX)
293 int string_id = IDS_ADD_TO_APPLICATIONS;
294 #elif defined(OS_WIN)
295 int string_id = IDS_ADD_TO_TASKBAR;
296 #else
297 int string_id = IDS_ADD_TO_DESKTOP;
298 #endif
299 #if defined(USE_ASH)
300 if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
301 string_id = IDS_ADD_TO_SHELF;
302 #endif
303 AddItemWithStringId(IDC_CREATE_HOSTED_APP, string_id);
304 } else if (show_create_shortcuts) {
305 AddItemWithStringId(IDC_CREATE_SHORTCUTS, IDS_CREATE_SHORTCUTS);
308 AddSeparator(ui::NORMAL_SEPARATOR);
309 AddItemWithStringId(IDC_CLEAR_BROWSING_DATA, IDS_CLEAR_BROWSING_DATA);
310 AddItemWithStringId(IDC_MANAGE_EXTENSIONS, IDS_SHOW_EXTENSIONS);
311 if (chrome::CanOpenTaskManager())
312 AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER);
313 #if defined(OS_CHROMEOS)
314 AddItemWithStringId(IDC_TAKE_SCREENSHOT, IDS_TAKE_SCREENSHOT);
315 #endif
316 encoding_menu_model_.reset(new EncodingMenuModel(browser));
317 AddSubMenuWithStringId(IDC_ENCODING_MENU, IDS_ENCODING_MENU,
318 encoding_menu_model_.get());
320 AddSeparator(ui::NORMAL_SEPARATOR);
321 AddItemWithStringId(IDC_DEV_TOOLS, IDS_DEV_TOOLS);
323 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC)
324 AddSeparator(ui::NORMAL_SEPARATOR);
325 AddCheckItemWithStringId(IDC_PROFILING_ENABLED, IDS_PROFILING_ENABLED);
326 #endif
329 ////////////////////////////////////////////////////////////////////////////////
330 // WrenchMenuModel
332 WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider,
333 Browser* browser)
334 : ui::SimpleMenuModel(this),
335 uma_action_recorded_(false),
336 provider_(provider),
337 browser_(browser),
338 tab_strip_model_(browser_->tab_strip_model()) {
339 Build();
340 UpdateZoomControls();
342 browser_zoom_subscription_ =
343 ui_zoom::ZoomEventManager::GetForBrowserContext(browser->profile())
344 ->AddZoomLevelChangedCallback(base::Bind(
345 &WrenchMenuModel::OnZoomLevelChanged, base::Unretained(this)));
347 tab_strip_model_->AddObserver(this);
349 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
350 content::NotificationService::AllSources());
353 WrenchMenuModel::~WrenchMenuModel() {
354 if (tab_strip_model_)
355 tab_strip_model_->RemoveObserver(this);
358 bool WrenchMenuModel::DoesCommandIdDismissMenu(int command_id) const {
359 return command_id != IDC_ZOOM_MINUS && command_id != IDC_ZOOM_PLUS;
362 bool WrenchMenuModel::IsItemForCommandIdDynamic(int command_id) const {
363 return command_id == IDC_ZOOM_PERCENT_DISPLAY ||
364 #if defined(OS_MACOSX)
365 command_id == IDC_FULLSCREEN ||
366 #elif defined(OS_WIN)
367 command_id == IDC_PIN_TO_START_SCREEN ||
368 #endif
369 command_id == IDC_UPGRADE_DIALOG;
372 base::string16 WrenchMenuModel::GetLabelForCommandId(int command_id) const {
373 switch (command_id) {
374 case IDC_ZOOM_PERCENT_DISPLAY:
375 return zoom_label_;
376 #if defined(OS_MACOSX)
377 case IDC_FULLSCREEN: {
378 int string_id = IDS_ENTER_FULLSCREEN_MAC; // Default to Enter.
379 // Note: On startup, |window()| may be NULL.
380 if (browser_->window() && browser_->window()->IsFullscreen())
381 string_id = IDS_EXIT_FULLSCREEN_MAC;
382 return l10n_util::GetStringUTF16(string_id);
384 #elif defined(OS_WIN)
385 case IDC_PIN_TO_START_SCREEN: {
386 int string_id = IDS_PIN_TO_START_SCREEN;
387 WebContents* web_contents =
388 browser_->tab_strip_model()->GetActiveWebContents();
389 MetroPinTabHelper* tab_helper =
390 web_contents ? MetroPinTabHelper::FromWebContents(web_contents)
391 : NULL;
392 if (tab_helper && tab_helper->IsPinned())
393 string_id = IDS_UNPIN_FROM_START_SCREEN;
394 return l10n_util::GetStringUTF16(string_id);
396 #endif
397 case IDC_UPGRADE_DIALOG:
398 return GetUpgradeDialogMenuItemName();
399 default:
400 NOTREACHED();
401 return base::string16();
405 bool WrenchMenuModel::GetIconForCommandId(int command_id,
406 gfx::Image* icon) const {
407 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
408 switch (command_id) {
409 case IDC_UPGRADE_DIALOG: {
410 if (UpgradeDetector::GetInstance()->notify_upgrade()) {
411 *icon = rb.GetNativeImageNamed(
412 UpgradeDetector::GetInstance()->GetIconResourceID());
413 return true;
415 return false;
417 default:
418 break;
420 return false;
423 void WrenchMenuModel::ExecuteCommand(int command_id, int event_flags) {
424 GlobalError* error = GlobalErrorServiceFactory::GetForProfile(
425 browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id);
426 if (error) {
427 error->ExecuteMenuItem(browser_);
428 return;
431 LogMenuMetrics(command_id);
432 chrome::ExecuteCommand(browser_, command_id);
435 void WrenchMenuModel::LogMenuMetrics(int command_id) {
436 base::TimeDelta delta = timer_.Elapsed();
438 switch (command_id) {
439 case IDC_NEW_TAB:
440 if (!uma_action_recorded_)
441 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.NewTab", delta);
442 LogMenuAction(MENU_ACTION_NEW_TAB);
443 break;
444 case IDC_NEW_WINDOW:
445 if (!uma_action_recorded_)
446 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.NewWindow", delta);
447 LogMenuAction(MENU_ACTION_NEW_WINDOW);
448 break;
449 case IDC_NEW_INCOGNITO_WINDOW:
450 if (!uma_action_recorded_) {
451 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.NewIncognitoWindow",
452 delta);
454 LogMenuAction(MENU_ACTION_NEW_INCOGNITO_WINDOW);
455 break;
457 // Bookmarks sub menu.
458 case IDC_SHOW_BOOKMARK_BAR:
459 if (!uma_action_recorded_) {
460 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ShowBookmarkBar",
461 delta);
463 LogMenuAction(MENU_ACTION_SHOW_BOOKMARK_BAR);
464 break;
465 case IDC_SHOW_BOOKMARK_MANAGER:
466 if (!uma_action_recorded_) {
467 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ShowBookmarkMgr",
468 delta);
470 LogMenuAction(MENU_ACTION_SHOW_BOOKMARK_MANAGER);
471 break;
472 case IDC_IMPORT_SETTINGS:
473 if (!uma_action_recorded_) {
474 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ImportSettings",
475 delta);
477 LogMenuAction(MENU_ACTION_IMPORT_SETTINGS);
478 break;
479 case IDC_BOOKMARK_PAGE:
480 if (!uma_action_recorded_) {
481 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.BookmarkPage",
482 delta);
484 LogMenuAction(MENU_ACTION_BOOKMARK_PAGE);
485 break;
486 case IDC_BOOKMARK_ALL_TABS:
487 if (!uma_action_recorded_) {
488 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.BookmarkAllTabs",
489 delta);
491 LogMenuAction(MENU_ACTION_BOOKMARK_ALL_TABS);
492 break;
493 case IDC_PIN_TO_START_SCREEN:
494 if (!uma_action_recorded_) {
495 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.PinToStartScreen",
496 delta);
498 LogMenuAction(MENU_ACTION_PIN_TO_START_SCREEN);
499 break;
501 // Recent tabs menu.
502 case IDC_RESTORE_TAB:
503 if (!uma_action_recorded_)
504 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.RestoreTab", delta);
505 LogMenuAction(MENU_ACTION_RESTORE_TAB);
506 break;
508 // Windows.
509 case IDC_WIN_DESKTOP_RESTART:
510 if (!uma_action_recorded_) {
511 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.WinDesktopRestart",
512 delta);
514 LogMenuAction(MENU_ACTION_WIN_DESKTOP_RESTART);
515 break;
516 case IDC_WIN8_METRO_RESTART:
517 if (!uma_action_recorded_) {
518 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.Win8MetroRestart",
519 delta);
521 LogMenuAction(MENU_ACTION_WIN8_METRO_RESTART);
522 break;
524 case IDC_WIN_CHROMEOS_RESTART:
525 if (!uma_action_recorded_) {
526 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ChromeOSRestart",
527 delta);
529 LogMenuAction(MENU_ACTION_WIN_CHROMEOS_RESTART);
530 break;
531 case IDC_DISTILL_PAGE:
532 if (!uma_action_recorded_) {
533 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.DistillPage",
534 delta);
536 LogMenuAction(MENU_ACTION_DISTILL_PAGE);
537 break;
538 case IDC_SAVE_PAGE:
539 if (!uma_action_recorded_)
540 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.SavePage", delta);
541 LogMenuAction(MENU_ACTION_SAVE_PAGE);
542 break;
543 case IDC_FIND:
544 if (!uma_action_recorded_)
545 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.Find", delta);
546 LogMenuAction(MENU_ACTION_FIND);
547 break;
548 case IDC_PRINT:
549 if (!uma_action_recorded_)
550 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.Print", delta);
551 LogMenuAction(MENU_ACTION_PRINT);
552 break;
554 // Edit menu.
555 case IDC_CUT:
556 if (!uma_action_recorded_)
557 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.Cut", delta);
558 LogMenuAction(MENU_ACTION_CUT);
559 break;
560 case IDC_COPY:
561 if (!uma_action_recorded_)
562 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.Copy", delta);
563 LogMenuAction(MENU_ACTION_COPY);
564 break;
565 case IDC_PASTE:
566 if (!uma_action_recorded_)
567 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.Paste", delta);
568 LogMenuAction(MENU_ACTION_PASTE);
569 break;
571 // Tools menu.
572 case IDC_CREATE_HOSTED_APP:
573 if (!uma_action_recorded_) {
574 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.CreateHostedApp",
575 delta);
577 LogMenuAction(MENU_ACTION_CREATE_HOSTED_APP);
578 break;
579 case IDC_CREATE_SHORTCUTS:
580 if (!uma_action_recorded_)
581 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.CreateShortcuts",
582 delta);
583 LogMenuAction(MENU_ACTION_CREATE_SHORTCUTS);
584 break;
585 case IDC_MANAGE_EXTENSIONS:
586 if (!uma_action_recorded_) {
587 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ManageExtensions",
588 delta);
590 LogMenuAction(MENU_ACTION_MANAGE_EXTENSIONS);
591 break;
592 case IDC_TASK_MANAGER:
593 if (!uma_action_recorded_) {
594 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.TaskManager",
595 delta);
597 LogMenuAction(MENU_ACTION_TASK_MANAGER);
598 break;
599 case IDC_CLEAR_BROWSING_DATA:
600 if (!uma_action_recorded_) {
601 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ClearBrowsingData",
602 delta);
604 LogMenuAction(MENU_ACTION_CLEAR_BROWSING_DATA);
605 break;
606 case IDC_VIEW_SOURCE:
607 if (!uma_action_recorded_)
608 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ViewSource", delta);
609 LogMenuAction(MENU_ACTION_VIEW_SOURCE);
610 break;
611 case IDC_DEV_TOOLS:
612 if (!uma_action_recorded_)
613 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.DevTools", delta);
614 LogMenuAction(MENU_ACTION_DEV_TOOLS);
615 break;
616 case IDC_DEV_TOOLS_CONSOLE:
617 if (!uma_action_recorded_) {
618 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.DevToolsConsole",
619 delta);
621 LogMenuAction(MENU_ACTION_DEV_TOOLS_CONSOLE);
622 break;
623 case IDC_DEV_TOOLS_DEVICES:
624 if (!uma_action_recorded_) {
625 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.DevToolsDevices",
626 delta);
628 LogMenuAction(MENU_ACTION_DEV_TOOLS_DEVICES);
629 break;
630 case IDC_PROFILING_ENABLED:
631 if (!uma_action_recorded_) {
632 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ProfilingEnabled",
633 delta);
635 LogMenuAction(MENU_ACTION_PROFILING_ENABLED);
636 break;
638 // Zoom menu
639 case IDC_ZOOM_MINUS:
640 if (!uma_action_recorded_) {
641 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ZoomMinus", delta);
642 LogMenuAction(MENU_ACTION_ZOOM_MINUS);
644 break;
645 case IDC_ZOOM_PLUS:
646 if (!uma_action_recorded_) {
647 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ZoomPlus", delta);
648 LogMenuAction(MENU_ACTION_ZOOM_PLUS);
650 break;
651 case IDC_FULLSCREEN:
652 content::RecordAction(UserMetricsAction("EnterFullScreenWithWrenchMenu"));
654 if (!uma_action_recorded_) {
655 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.EnterFullScreen",
656 delta);
658 LogMenuAction(MENU_ACTION_FULLSCREEN);
659 break;
661 case IDC_SHOW_HISTORY:
662 if (!uma_action_recorded_) {
663 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ShowHistory",
664 delta);
666 LogMenuAction(MENU_ACTION_SHOW_HISTORY);
667 break;
668 case IDC_SHOW_DOWNLOADS:
669 if (!uma_action_recorded_) {
670 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.ShowDownloads",
671 delta);
673 LogMenuAction(MENU_ACTION_SHOW_DOWNLOADS);
674 break;
675 case IDC_OPTIONS:
676 if (!uma_action_recorded_)
677 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.Settings", delta);
678 LogMenuAction(MENU_ACTION_OPTIONS);
679 break;
680 case IDC_ABOUT:
681 if (!uma_action_recorded_)
682 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.About", delta);
683 LogMenuAction(MENU_ACTION_ABOUT);
684 break;
686 // Help menu.
687 case IDC_HELP_PAGE_VIA_MENU:
688 content::RecordAction(UserMetricsAction("ShowHelpTabViaWrenchMenu"));
690 if (!uma_action_recorded_)
691 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.HelpPage", delta);
692 LogMenuAction(MENU_ACTION_HELP_PAGE_VIA_MENU);
693 break;
694 #if defined(GOOGLE_CHROME_BUILD)
695 case IDC_FEEDBACK:
696 if (!uma_action_recorded_)
697 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.Feedback", delta);
698 LogMenuAction(MENU_ACTION_FEEDBACK);
699 break;
700 #endif
702 case IDC_TOGGLE_REQUEST_TABLET_SITE:
703 if (!uma_action_recorded_) {
704 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.RequestTabletSite",
705 delta);
707 LogMenuAction(MENU_ACTION_TOGGLE_REQUEST_TABLET_SITE);
708 break;
709 case IDC_EXIT:
710 if (!uma_action_recorded_)
711 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction.Exit", delta);
712 LogMenuAction(MENU_ACTION_EXIT);
713 break;
716 if (!uma_action_recorded_) {
717 UMA_HISTOGRAM_MEDIUM_TIMES("WrenchMenu.TimeToAction", delta);
718 uma_action_recorded_ = true;
722 void WrenchMenuModel::LogMenuAction(int action_id) {
723 UMA_HISTOGRAM_ENUMERATION("WrenchMenu.MenuAction", action_id,
724 LIMIT_MENU_ACTION);
727 bool WrenchMenuModel::IsCommandIdChecked(int command_id) const {
728 if (command_id == IDC_SHOW_BOOKMARK_BAR) {
729 return browser_->profile()->GetPrefs()->GetBoolean(
730 bookmarks::prefs::kShowBookmarkBar);
731 } else if (command_id == IDC_PROFILING_ENABLED) {
732 return Profiling::BeingProfiled();
733 } else if (command_id == IDC_TOGGLE_REQUEST_TABLET_SITE) {
734 return chrome::IsRequestingTabletSite(browser_);
737 return false;
740 bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const {
741 GlobalError* error = GlobalErrorServiceFactory::GetForProfile(
742 browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id);
743 if (error)
744 return true;
746 return chrome::IsCommandEnabled(browser_, command_id);
749 bool WrenchMenuModel::IsCommandIdVisible(int command_id) const {
750 switch (command_id) {
751 #if defined(OS_MACOSX)
752 case kEmptyMenuItemCommand:
753 return false; // Always hidden (see CreateActionToolbarOverflowMenu).
754 #endif
755 #if defined(OS_WIN)
756 case IDC_VIEW_INCOMPATIBILITIES: {
757 EnumerateModulesModel* loaded_modules =
758 EnumerateModulesModel::GetInstance();
759 if (loaded_modules->confirmed_bad_modules_detected() <= 0)
760 return false;
761 // We'll leave the wrench adornment on until the user clicks the link.
762 if (loaded_modules->modules_to_notify_about() <= 0)
763 loaded_modules->AcknowledgeConflictNotification();
764 return true;
766 case IDC_PIN_TO_START_SCREEN:
767 return base::win::IsMetroProcess();
768 #else
769 case IDC_VIEW_INCOMPATIBILITIES:
770 case IDC_PIN_TO_START_SCREEN:
771 return false;
772 #endif
773 case IDC_UPGRADE_DIALOG:
774 return browser_defaults::kShowUpgradeMenuItem &&
775 UpgradeDetector::GetInstance()->notify_upgrade();
776 #if !defined(OS_LINUX) || defined(USE_AURA)
777 case IDC_BOOKMARK_PAGE:
778 return !chrome::ShouldRemoveBookmarkThisPageUI(browser_->profile());
779 case IDC_BOOKMARK_ALL_TABS:
780 return !chrome::ShouldRemoveBookmarkOpenPagesUI(browser_->profile());
781 #endif
782 default:
783 return true;
787 bool WrenchMenuModel::GetAcceleratorForCommandId(
788 int command_id,
789 ui::Accelerator* accelerator) {
790 return provider_->GetAcceleratorForCommandId(command_id, accelerator);
793 void WrenchMenuModel::ActiveTabChanged(WebContents* old_contents,
794 WebContents* new_contents,
795 int index,
796 int reason) {
797 // The user has switched between tabs and the new tab may have a different
798 // zoom setting.
799 UpdateZoomControls();
802 void WrenchMenuModel::TabReplacedAt(TabStripModel* tab_strip_model,
803 WebContents* old_contents,
804 WebContents* new_contents,
805 int index) {
806 UpdateZoomControls();
809 void WrenchMenuModel::TabStripModelDeleted() {
810 // During views shutdown, the tabstrip model/browser is deleted first, while
811 // it is the opposite in gtk land.
812 tab_strip_model_->RemoveObserver(this);
813 tab_strip_model_ = NULL;
816 void WrenchMenuModel::Observe(int type,
817 const content::NotificationSource& source,
818 const content::NotificationDetails& details) {
819 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED);
820 UpdateZoomControls();
823 // For testing.
824 WrenchMenuModel::WrenchMenuModel()
825 : ui::SimpleMenuModel(this),
826 uma_action_recorded_(false),
827 provider_(NULL),
828 browser_(NULL),
829 tab_strip_model_(NULL) {}
831 bool WrenchMenuModel::ShouldShowNewIncognitoWindowMenuItem() {
832 if (browser_->profile()->IsGuestSession())
833 return false;
835 return IncognitoModePrefs::GetAvailability(browser_->profile()->GetPrefs()) !=
836 IncognitoModePrefs::DISABLED;
839 // Note: When adding new menu items please place under an appropriate section.
840 // Menu is organised as follows:
841 // - Extension toolbar overflow.
842 // - Global browser errors and warnings.
843 // - Tabs and windows.
844 // - Places previously been e.g. History, bookmarks, recent tabs.
845 // - Page actions e.g. zoom, edit, find, print.
846 // - Learn about the browser and global customisation e.g. settings, help.
847 // - Browser relaunch, quit.
848 void WrenchMenuModel::Build() {
849 if (extensions::FeatureSwitch::extension_action_redesign()->IsEnabled())
850 CreateActionToolbarOverflowMenu();
852 AddItem(IDC_VIEW_INCOMPATIBILITIES,
853 l10n_util::GetStringUTF16(IDS_VIEW_INCOMPATIBILITIES));
854 SetIcon(GetIndexOfCommandId(IDC_VIEW_INCOMPATIBILITIES),
855 ui::ResourceBundle::GetSharedInstance().
856 GetNativeImageNamed(IDR_INPUT_ALERT_MENU));
857 if (IsCommandIdVisible(IDC_UPGRADE_DIALOG))
858 AddItem(IDC_UPGRADE_DIALOG, GetUpgradeDialogMenuItemName());
859 if (AddGlobalErrorMenuItems() ||
860 IsCommandIdVisible(IDC_VIEW_INCOMPATIBILITIES) ||
861 IsCommandIdVisible(IDC_UPGRADE_DIALOG))
862 AddSeparator(ui::NORMAL_SEPARATOR);
864 AddItemWithStringId(IDC_NEW_TAB, IDS_NEW_TAB);
865 AddItemWithStringId(IDC_NEW_WINDOW, IDS_NEW_WINDOW);
866 if (ShouldShowNewIncognitoWindowMenuItem())
867 AddItemWithStringId(IDC_NEW_INCOGNITO_WINDOW, IDS_NEW_INCOGNITO_WINDOW);
868 AddSeparator(ui::NORMAL_SEPARATOR);
870 if (!browser_->profile()->IsOffTheRecord()) {
871 recent_tabs_sub_menu_model_.reset(new RecentTabsSubMenuModel(provider_,
872 browser_,
873 NULL));
874 AddSubMenuWithStringId(IDC_RECENT_TABS_MENU, IDS_HISTORY_RECENT_TABS_MENU,
875 recent_tabs_sub_menu_model_.get());
877 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS);
878 if (!browser_->profile()->IsGuestSession()) {
879 bookmark_sub_menu_model_.reset(new BookmarkSubMenuModel(this, browser_));
880 AddSubMenuWithStringId(IDC_BOOKMARKS_MENU, IDS_BOOKMARKS_MENU,
881 bookmark_sub_menu_model_.get());
884 CreateZoomMenu();
885 AddItemWithStringId(IDC_PRINT, IDS_PRINT);
886 AddItemWithStringId(IDC_FIND, IDS_FIND);
887 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
888 switches::kEnableDomDistiller))
889 AddItemWithStringId(IDC_DISTILL_PAGE, IDS_DISTILL_PAGE);
890 tools_menu_model_.reset(new ToolsMenuModel(this, browser_));
891 AddSubMenuWithStringId(
892 IDC_MORE_TOOLS_MENU, IDS_MORE_TOOLS_MENU, tools_menu_model_.get());
893 // Append the full menu including separators. The final separator only gets
894 // appended when this is a touch menu - otherwise it would get added twice.
895 CreateCutCopyPasteMenu();
897 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS);
899 // The help submenu is only displayed on official Chrome builds. As the
900 // 'About' item has been moved to this submenu, it's reinstated here for
901 // Chromium builds.
902 #if defined(GOOGLE_CHROME_BUILD)
903 help_menu_model_.reset(new HelpMenuModel(this, browser_));
904 AddSubMenuWithStringId(IDC_HELP_MENU, IDS_HELP_MENU,
905 help_menu_model_.get());
906 #else
907 AddItem(IDC_ABOUT, l10n_util::GetStringUTF16(IDS_ABOUT));
908 #endif
909 #if defined(OS_CHROMEOS)
910 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
911 chromeos::switches::kEnableRequestTabletSite))
912 AddCheckItemWithStringId(IDC_TOGGLE_REQUEST_TABLET_SITE,
913 IDS_TOGGLE_REQUEST_TABLET_SITE);
914 #endif
916 #if defined(OS_WIN)
917 int command_id = IDC_WIN_DESKTOP_RESTART;
918 int string_id = IDS_WIN_DESKTOP_RESTART;
919 if (GetRestartMenuItemIfRequired(browser_->host_desktop_type(),
920 &command_id,
921 &string_id)) {
922 AddSeparator(ui::NORMAL_SEPARATOR);
923 AddItemWithStringId(command_id, string_id);
925 #endif
926 bool show_exit_menu = browser_defaults::kShowExitMenuItem;
927 #if defined(OS_WIN)
928 if (browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
929 show_exit_menu = false;
930 #endif
931 if (show_exit_menu) {
932 AddSeparator(ui::NORMAL_SEPARATOR);
933 AddItemWithStringId(IDC_EXIT, IDS_EXIT);
935 uma_action_recorded_ = false;
938 bool WrenchMenuModel::AddGlobalErrorMenuItems() {
939 // TODO(sail): Currently we only build the wrench menu once per browser
940 // window. This means that if a new error is added after the menu is built
941 // it won't show in the existing wrench menu. To fix this we need to some
942 // how update the menu if new errors are added.
943 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
944 const GlobalErrorService::GlobalErrorList& errors =
945 GlobalErrorServiceFactory::GetForProfile(browser_->profile())->errors();
946 bool menu_items_added = false;
947 for (GlobalErrorService::GlobalErrorList::const_iterator
948 it = errors.begin(); it != errors.end(); ++it) {
949 GlobalError* error = *it;
950 DCHECK(error);
951 if (error->HasMenuItem()) {
952 AddItem(error->MenuItemCommandID(), error->MenuItemLabel());
953 int icon_id = error->MenuItemIconResourceID();
954 if (icon_id) {
955 const gfx::Image& image = rb.GetNativeImageNamed(icon_id);
956 SetIcon(GetIndexOfCommandId(error->MenuItemCommandID()),
957 image);
959 menu_items_added = true;
962 return menu_items_added;
965 void WrenchMenuModel::CreateActionToolbarOverflowMenu() {
966 // We only add the extensions overflow container if there are any icons that
967 // aren't shown in the main container.
968 if (!ToolbarActionsModel::Get(browser_->profile())->all_icons_visible()) {
969 #if defined(OS_MACOSX)
970 // There's a bug in AppKit menus, where if a menu item with a custom view
971 // (like the extensions overflow menu) is the first menu item, it is not
972 // highlightable or keyboard-selectable.
973 // Adding any menu item before it (even one which is never visible) prevents
974 // it, so add a bogus item here that will always be hidden.
975 AddItem(kEmptyMenuItemCommand, base::string16());
976 #endif
977 AddItem(IDC_EXTENSIONS_OVERFLOW_MENU, base::string16());
978 AddSeparator(ui::UPPER_SEPARATOR);
982 void WrenchMenuModel::CreateCutCopyPasteMenu() {
983 AddSeparator(ui::LOWER_SEPARATOR);
985 // WARNING: Mac does not use the ButtonMenuItemModel, but instead defines the
986 // layout for this menu item in WrenchMenu.xib. It does, however, use the
987 // command_id value from AddButtonItem() to identify this special item.
988 edit_menu_item_model_.reset(new ui::ButtonMenuItemModel(IDS_EDIT, this));
989 edit_menu_item_model_->AddGroupItemWithStringId(IDC_CUT, IDS_CUT);
990 edit_menu_item_model_->AddGroupItemWithStringId(IDC_COPY, IDS_COPY);
991 edit_menu_item_model_->AddGroupItemWithStringId(IDC_PASTE, IDS_PASTE);
992 AddButtonItem(IDC_EDIT_MENU, edit_menu_item_model_.get());
994 AddSeparator(ui::UPPER_SEPARATOR);
997 void WrenchMenuModel::CreateZoomMenu() {
998 // This menu needs to be enclosed by separators.
999 AddSeparator(ui::LOWER_SEPARATOR);
1001 // WARNING: Mac does not use the ButtonMenuItemModel, but instead defines the
1002 // layout for this menu item in WrenchMenu.xib. It does, however, use the
1003 // command_id value from AddButtonItem() to identify this special item.
1004 zoom_menu_item_model_.reset(
1005 new ui::ButtonMenuItemModel(IDS_ZOOM_MENU, this));
1006 zoom_menu_item_model_->AddGroupItemWithStringId(IDC_ZOOM_MINUS,
1007 IDS_ZOOM_MINUS2);
1008 zoom_menu_item_model_->AddGroupItemWithStringId(IDC_ZOOM_PLUS,
1009 IDS_ZOOM_PLUS2);
1010 zoom_menu_item_model_->AddItemWithImage(IDC_FULLSCREEN,
1011 IDR_FULLSCREEN_MENU_BUTTON);
1012 AddButtonItem(IDC_ZOOM_MENU, zoom_menu_item_model_.get());
1014 AddSeparator(ui::UPPER_SEPARATOR);
1017 void WrenchMenuModel::UpdateZoomControls() {
1018 int zoom_percent = 100;
1019 if (browser_->tab_strip_model() &&
1020 browser_->tab_strip_model()->GetActiveWebContents()) {
1021 zoom_percent = ui_zoom::ZoomController::FromWebContents(
1022 browser_->tab_strip_model()->GetActiveWebContents())
1023 ->GetZoomPercent();
1025 zoom_label_ = l10n_util::GetStringFUTF16(
1026 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent));
1029 void WrenchMenuModel::OnZoomLevelChanged(
1030 const content::HostZoomMap::ZoomLevelChange& change) {
1031 UpdateZoomControls();