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"
10 #include "base/command_line.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/app/chrome_command_ids.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/defaults.h"
18 #include "chrome/browser/extensions/extension_toolbar_model.h"
19 #include "chrome/browser/extensions/extension_util.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/search/search.h"
23 #include "chrome/browser/signin/signin_manager_factory.h"
24 #include "chrome/browser/signin/signin_ui_util.h"
25 #include "chrome/browser/task_manager/task_manager.h"
26 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
27 #include "chrome/browser/ui/browser.h"
28 #include "chrome/browser/ui/browser_commands.h"
29 #include "chrome/browser/ui/browser_finder.h"
30 #include "chrome/browser/ui/browser_window.h"
31 #include "chrome/browser/ui/global_error/global_error.h"
32 #include "chrome/browser/ui/global_error/global_error_service.h"
33 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
34 #include "chrome/browser/ui/tabs/tab_strip_model.h"
35 #include "chrome/browser/ui/toolbar/bookmark_sub_menu_model.h"
36 #include "chrome/browser/ui/toolbar/encoding_menu_controller.h"
37 #include "chrome/browser/ui/toolbar/recent_tabs_sub_menu_model.h"
38 #include "chrome/browser/ui/zoom/zoom_controller.h"
39 #include "chrome/browser/ui/zoom/zoom_event_manager.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/signin/core/browser/signin_manager.h"
48 #include "components/signin/core/common/profile_management_switches.h"
49 #include "content/public/browser/host_zoom_map.h"
50 #include "content/public/browser/navigation_entry.h"
51 #include "content/public/browser/notification_service.h"
52 #include "content/public/browser/notification_source.h"
53 #include "content/public/browser/notification_types.h"
54 #include "content/public/browser/user_metrics.h"
55 #include "content/public/browser/web_contents.h"
56 #include "extensions/common/feature_switch.h"
57 #include "grit/theme_resources.h"
58 #include "ui/base/l10n/l10n_util.h"
59 #include "ui/base/layout.h"
60 #include "ui/base/models/button_menu_item_model.h"
61 #include "ui/base/resource/resource_bundle.h"
62 #include "ui/gfx/image/image.h"
63 #include "ui/gfx/image/image_skia.h"
65 #if defined(OS_CHROMEOS)
66 #include "chromeos/chromeos_switches.h"
70 #include "base/win/metro.h"
71 #include "base/win/windows_version.h"
72 #include "chrome/browser/enumerate_modules_model_win.h"
73 #include "chrome/browser/ui/metro_pin_tab_helper_win.h"
74 #include "content/public/browser/gpu_data_manager.h"
78 #include "ash/shell.h"
81 using base::UserMetricsAction;
82 using content::WebContents;
85 // Conditionally return the update app menu item title based on upgrade detector
87 base::string16 GetUpgradeDialogMenuItemName() {
88 if (UpgradeDetector::GetInstance()->is_outdated_install() ||
89 UpgradeDetector::GetInstance()->is_outdated_install_no_au()) {
90 return l10n_util::GetStringUTF16(IDS_UPGRADE_BUBBLE_MENU_ITEM);
92 return l10n_util::GetStringUTF16(IDS_UPDATE_NOW);
98 ////////////////////////////////////////////////////////////////////////////////
101 EncodingMenuModel::EncodingMenuModel(Browser* browser)
102 : ui::SimpleMenuModel(this),
107 EncodingMenuModel::~EncodingMenuModel() {
110 void EncodingMenuModel::Build() {
111 EncodingMenuController::EncodingMenuItemList encoding_menu_items;
112 EncodingMenuController encoding_menu_controller;
113 encoding_menu_controller.GetEncodingMenuItems(browser_->profile(),
114 &encoding_menu_items);
117 EncodingMenuController::EncodingMenuItemList::iterator it =
118 encoding_menu_items.begin();
119 for (; it != encoding_menu_items.end(); ++it) {
121 base::string16& label = it->second;
123 AddSeparator(ui::NORMAL_SEPARATOR);
125 if (id == IDC_ENCODING_AUTO_DETECT) {
126 AddCheckItem(id, label);
128 // Use the id of the first radio command as the id of the group.
131 AddRadioItem(id, label, group_id);
137 bool EncodingMenuModel::IsCommandIdChecked(int command_id) const {
138 WebContents* current_tab =
139 browser_->tab_strip_model()->GetActiveWebContents();
142 EncodingMenuController controller;
143 return controller.IsItemChecked(browser_->profile(),
144 current_tab->GetEncoding(), command_id);
147 bool EncodingMenuModel::IsCommandIdEnabled(int command_id) const {
148 bool enabled = chrome::IsCommandEnabled(browser_, command_id);
149 // Special handling for the contents of the Encoding submenu. On Mac OS,
150 // instead of enabling/disabling the top-level menu item, the submenu's
151 // contents get disabled, per Apple's HIG.
152 #if defined(OS_MACOSX)
153 enabled &= chrome::IsCommandEnabled(browser_, IDC_ENCODING_MENU);
158 bool EncodingMenuModel::GetAcceleratorForCommandId(
160 ui::Accelerator* accelerator) {
164 void EncodingMenuModel::ExecuteCommand(int command_id, int event_flags) {
165 chrome::ExecuteCommand(browser_, command_id);
168 ////////////////////////////////////////////////////////////////////////////////
171 ZoomMenuModel::ZoomMenuModel(ui::SimpleMenuModel::Delegate* delegate)
172 : SimpleMenuModel(delegate) {
176 ZoomMenuModel::~ZoomMenuModel() {
179 void ZoomMenuModel::Build() {
180 AddItemWithStringId(IDC_ZOOM_PLUS, IDS_ZOOM_PLUS);
181 AddItemWithStringId(IDC_ZOOM_NORMAL, IDS_ZOOM_NORMAL);
182 AddItemWithStringId(IDC_ZOOM_MINUS, IDS_ZOOM_MINUS);
185 ////////////////////////////////////////////////////////////////////////////////
188 #if defined(GOOGLE_CHROME_BUILD)
190 class WrenchMenuModel::HelpMenuModel : public ui::SimpleMenuModel {
192 HelpMenuModel(ui::SimpleMenuModel::Delegate* delegate,
194 : SimpleMenuModel(delegate) {
197 virtual ~HelpMenuModel() {
201 void Build(Browser* browser) {
202 #if defined(OS_CHROMEOS) && defined(OFFICIAL_BUILD)
203 int help_string_id = IDS_GET_HELP;
205 int help_string_id = IDS_HELP_PAGE;
207 AddItemWithStringId(IDC_HELP_PAGE_VIA_MENU, help_string_id);
208 if (browser_defaults::kShowHelpMenuItemIcon) {
209 ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance();
210 SetIcon(GetIndexOfCommandId(IDC_HELP_PAGE_VIA_MENU),
211 rb.GetNativeImageNamed(IDR_HELP_MENU));
214 AddItemWithStringId(IDC_FEEDBACK, IDS_FEEDBACK);
217 DISALLOW_COPY_AND_ASSIGN(HelpMenuModel);
220 #endif // defined(GOOGLE_CHROME_BUILD)
222 ////////////////////////////////////////////////////////////////////////////////
225 ToolsMenuModel::ToolsMenuModel(ui::SimpleMenuModel::Delegate* delegate,
227 : SimpleMenuModel(delegate) {
231 ToolsMenuModel::~ToolsMenuModel() {}
233 void ToolsMenuModel::Build(Browser* browser) {
234 bool show_create_shortcuts = true;
235 #if defined(OS_CHROMEOS) || defined(OS_MACOSX)
236 show_create_shortcuts = false;
237 #elif defined(USE_ASH)
238 if (browser->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
239 show_create_shortcuts = false;
242 if (extensions::util::IsStreamlinedHostedAppsEnabled()) {
243 AddItemWithStringId(IDC_CREATE_HOSTED_APP, IDS_CREATE_HOSTED_APP);
244 AddSeparator(ui::NORMAL_SEPARATOR);
245 } else if (show_create_shortcuts) {
246 AddItemWithStringId(IDC_CREATE_SHORTCUTS, IDS_CREATE_SHORTCUTS);
247 AddSeparator(ui::NORMAL_SEPARATOR);
250 // If settings-in-a-window is enabled the Extensions item is at the top level.
251 if (!::switches::SettingsWindowEnabled())
252 AddItemWithStringId(IDC_MANAGE_EXTENSIONS, IDS_SHOW_EXTENSIONS);
254 if (chrome::CanOpenTaskManager())
255 AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER);
257 AddItemWithStringId(IDC_CLEAR_BROWSING_DATA, IDS_CLEAR_BROWSING_DATA);
259 AddSeparator(ui::NORMAL_SEPARATOR);
261 encoding_menu_model_.reset(new EncodingMenuModel(browser));
262 AddSubMenuWithStringId(IDC_ENCODING_MENU, IDS_ENCODING_MENU,
263 encoding_menu_model_.get());
264 AddItemWithStringId(IDC_VIEW_SOURCE, IDS_VIEW_SOURCE);
265 AddItemWithStringId(IDC_DEV_TOOLS, IDS_DEV_TOOLS);
266 AddItemWithStringId(IDC_DEV_TOOLS_CONSOLE, IDS_DEV_TOOLS_CONSOLE);
267 AddItemWithStringId(IDC_DEV_TOOLS_DEVICES, IDS_DEV_TOOLS_DEVICES);
269 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC)
270 AddSeparator(ui::NORMAL_SEPARATOR);
271 AddCheckItemWithStringId(IDC_PROFILING_ENABLED, IDS_PROFILING_ENABLED);
275 ////////////////////////////////////////////////////////////////////////////////
278 WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider,
280 : ui::SimpleMenuModel(this),
283 tab_strip_model_(browser_->tab_strip_model()) {
285 UpdateZoomControls();
287 content_zoom_subscription_ =
288 content::HostZoomMap::GetDefaultForBrowserContext(browser->profile())
289 ->AddZoomLevelChangedCallback(base::Bind(
290 &WrenchMenuModel::OnZoomLevelChanged, base::Unretained(this)));
292 browser_zoom_subscription_ = ZoomEventManager::GetForBrowserContext(
293 browser->profile())->AddZoomLevelChangedCallback(
294 base::Bind(&WrenchMenuModel::OnZoomLevelChanged,
295 base::Unretained(this)));
297 tab_strip_model_->AddObserver(this);
299 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
300 content::NotificationService::AllSources());
303 WrenchMenuModel::~WrenchMenuModel() {
304 if (tab_strip_model_)
305 tab_strip_model_->RemoveObserver(this);
308 bool WrenchMenuModel::DoesCommandIdDismissMenu(int command_id) const {
309 return command_id != IDC_ZOOM_MINUS && command_id != IDC_ZOOM_PLUS;
312 bool WrenchMenuModel::IsItemForCommandIdDynamic(int command_id) const {
313 return command_id == IDC_ZOOM_PERCENT_DISPLAY ||
314 #if defined(OS_MACOSX)
315 command_id == IDC_FULLSCREEN ||
316 #elif defined(OS_WIN)
317 command_id == IDC_PIN_TO_START_SCREEN ||
319 command_id == IDC_UPGRADE_DIALOG ||
320 (!switches::IsNewAvatarMenu() && command_id == IDC_SHOW_SIGNIN);
323 base::string16 WrenchMenuModel::GetLabelForCommandId(int command_id) const {
324 switch (command_id) {
325 case IDC_ZOOM_PERCENT_DISPLAY:
327 #if defined(OS_MACOSX)
328 case IDC_FULLSCREEN: {
329 int string_id = IDS_ENTER_FULLSCREEN_MAC; // Default to Enter.
330 // Note: On startup, |window()| may be NULL.
331 if (browser_->window() && browser_->window()->IsFullscreen())
332 string_id = IDS_EXIT_FULLSCREEN_MAC;
333 return l10n_util::GetStringUTF16(string_id);
335 #elif defined(OS_WIN)
336 case IDC_PIN_TO_START_SCREEN: {
337 int string_id = IDS_PIN_TO_START_SCREEN;
338 WebContents* web_contents =
339 browser_->tab_strip_model()->GetActiveWebContents();
340 MetroPinTabHelper* tab_helper =
341 web_contents ? MetroPinTabHelper::FromWebContents(web_contents)
343 if (tab_helper && tab_helper->IsPinned())
344 string_id = IDS_UNPIN_FROM_START_SCREEN;
345 return l10n_util::GetStringUTF16(string_id);
348 case IDC_UPGRADE_DIALOG:
349 return GetUpgradeDialogMenuItemName();
350 case IDC_SHOW_SIGNIN:
351 DCHECK(!switches::IsNewAvatarMenu());
352 return signin_ui_util::GetSigninMenuLabel(
353 browser_->profile()->GetOriginalProfile());
356 return base::string16();
360 bool WrenchMenuModel::GetIconForCommandId(int command_id,
361 gfx::Image* icon) const {
362 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
363 switch (command_id) {
364 case IDC_UPGRADE_DIALOG: {
365 if (UpgradeDetector::GetInstance()->notify_upgrade()) {
366 *icon = rb.GetNativeImageNamed(
367 UpgradeDetector::GetInstance()->GetIconResourceID());
372 case IDC_SHOW_SIGNIN: {
373 DCHECK(!switches::IsNewAvatarMenu());
374 GlobalError* error = signin_ui_util::GetSignedInServiceError(
375 browser_->profile()->GetOriginalProfile());
377 int icon_id = error->MenuItemIconResourceID();
379 *icon = rb.GetNativeImageNamed(icon_id);
391 void WrenchMenuModel::ExecuteCommand(int command_id, int event_flags) {
392 GlobalError* error = GlobalErrorServiceFactory::GetForProfile(
393 browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id);
395 error->ExecuteMenuItem(browser_);
399 if (!switches::IsNewAvatarMenu() && command_id == IDC_SHOW_SIGNIN) {
400 // If a custom error message is being shown, handle it.
401 GlobalError* error = signin_ui_util::GetSignedInServiceError(
402 browser_->profile()->GetOriginalProfile());
404 error->ExecuteMenuItem(browser_);
409 if (command_id == IDC_HELP_PAGE_VIA_MENU)
410 content::RecordAction(UserMetricsAction("ShowHelpTabViaWrenchMenu"));
412 if (command_id == IDC_FULLSCREEN) {
413 // We issue the UMA command here and not in BrowserCommandController or even
414 // FullscreenController since we want to be able to distinguish this event
415 // and a menu which is under development.
416 content::RecordAction(UserMetricsAction("EnterFullScreenWithWrenchMenu"));
419 chrome::ExecuteCommand(browser_, command_id);
422 bool WrenchMenuModel::IsCommandIdChecked(int command_id) const {
423 if (command_id == IDC_SHOW_BOOKMARK_BAR) {
424 return browser_->profile()->GetPrefs()->GetBoolean(
425 bookmarks::prefs::kShowBookmarkBar);
426 } else if (command_id == IDC_PROFILING_ENABLED) {
427 return Profiling::BeingProfiled();
428 } else if (command_id == IDC_TOGGLE_REQUEST_TABLET_SITE) {
429 return chrome::IsRequestingTabletSite(browser_);
435 bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const {
436 GlobalError* error = GlobalErrorServiceFactory::GetForProfile(
437 browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id);
441 return chrome::IsCommandEnabled(browser_, command_id);
444 bool WrenchMenuModel::IsCommandIdVisible(int command_id) const {
445 switch (command_id) {
447 case IDC_VIEW_INCOMPATIBILITIES: {
448 EnumerateModulesModel* loaded_modules =
449 EnumerateModulesModel::GetInstance();
450 if (loaded_modules->confirmed_bad_modules_detected() <= 0)
452 // We'll leave the wrench adornment on until the user clicks the link.
453 if (loaded_modules->modules_to_notify_about() <= 0)
454 loaded_modules->AcknowledgeConflictNotification();
457 case IDC_PIN_TO_START_SCREEN:
458 return base::win::IsMetroProcess();
460 case IDC_VIEW_INCOMPATIBILITIES:
461 case IDC_PIN_TO_START_SCREEN:
464 case IDC_UPGRADE_DIALOG:
465 return UpgradeDetector::GetInstance()->notify_upgrade();
466 #if !defined(OS_LINUX) || defined(USE_AURA)
467 case IDC_BOOKMARK_PAGE:
468 return !chrome::ShouldRemoveBookmarkThisPageUI(browser_->profile());
469 case IDC_BOOKMARK_ALL_TABS:
470 return !chrome::ShouldRemoveBookmarkOpenPagesUI(browser_->profile());
477 bool WrenchMenuModel::GetAcceleratorForCommandId(
479 ui::Accelerator* accelerator) {
480 return provider_->GetAcceleratorForCommandId(command_id, accelerator);
483 void WrenchMenuModel::ActiveTabChanged(WebContents* old_contents,
484 WebContents* new_contents,
487 // The user has switched between tabs and the new tab may have a different
489 UpdateZoomControls();
492 void WrenchMenuModel::TabReplacedAt(TabStripModel* tab_strip_model,
493 WebContents* old_contents,
494 WebContents* new_contents,
496 UpdateZoomControls();
499 void WrenchMenuModel::TabStripModelDeleted() {
500 // During views shutdown, the tabstrip model/browser is deleted first, while
501 // it is the opposite in gtk land.
502 tab_strip_model_->RemoveObserver(this);
503 tab_strip_model_ = NULL;
506 void WrenchMenuModel::Observe(int type,
507 const content::NotificationSource& source,
508 const content::NotificationDetails& details) {
509 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED);
510 UpdateZoomControls();
514 WrenchMenuModel::WrenchMenuModel()
515 : ui::SimpleMenuModel(this),
518 tab_strip_model_(NULL) {
521 bool WrenchMenuModel::ShouldShowNewIncognitoWindowMenuItem() {
522 if (browser_->profile()->IsSupervised())
525 return !browser_->profile()->IsGuestSession();
528 void WrenchMenuModel::Build() {
530 AddItem(IDC_VIEW_INCOMPATIBILITIES,
531 l10n_util::GetStringUTF16(IDS_VIEW_INCOMPATIBILITIES));
532 EnumerateModulesModel* model =
533 EnumerateModulesModel::GetInstance();
534 if (model->modules_to_notify_about() > 0 ||
535 model->confirmed_bad_modules_detected() > 0)
536 AddSeparator(ui::NORMAL_SEPARATOR);
539 if (extensions::FeatureSwitch::extension_action_redesign()->IsEnabled())
540 CreateExtensionToolbarOverflowMenu();
542 AddItemWithStringId(IDC_NEW_TAB, IDS_NEW_TAB);
543 AddItemWithStringId(IDC_NEW_WINDOW, IDS_NEW_WINDOW);
545 if (ShouldShowNewIncognitoWindowMenuItem())
546 AddItemWithStringId(IDC_NEW_INCOGNITO_WINDOW, IDS_NEW_INCOGNITO_WINDOW);
548 bookmark_sub_menu_model_.reset(new BookmarkSubMenuModel(this, browser_));
549 AddSubMenuWithStringId(IDC_BOOKMARKS_MENU, IDS_BOOKMARKS_MENU,
550 bookmark_sub_menu_model_.get());
552 if (!browser_->profile()->IsOffTheRecord()) {
553 recent_tabs_sub_menu_model_.reset(new RecentTabsSubMenuModel(provider_,
556 AddSubMenuWithStringId(IDC_RECENT_TABS_MENU, IDS_RECENT_TABS_MENU,
557 recent_tabs_sub_menu_model_.get());
561 // Windows 8 can support ASH mode using WARP, but Windows 7 requires a working
563 if ((base::win::GetVersion() >= base::win::VERSION_WIN7 &&
564 content::GpuDataManager::GetInstance()->CanUseGpuBrowserCompositor()) ||
565 (base::win::GetVersion() >= base::win::VERSION_WIN8)) {
566 if (browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) {
567 // ASH/Metro mode, add the 'Relaunch Chrome in desktop mode'.
568 AddSeparator(ui::NORMAL_SEPARATOR);
569 AddItemWithStringId(IDC_WIN_DESKTOP_RESTART, IDS_WIN_DESKTOP_RESTART);
571 // In Windows 8 desktop, add the 'Relaunch Chrome in Windows 8 mode'.
572 // In Windows 7 desktop, add the 'Relaunch Chrome in Windows ASH mode'
573 AddSeparator(ui::NORMAL_SEPARATOR);
574 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
575 AddItemWithStringId(IDC_WIN8_METRO_RESTART, IDS_WIN8_METRO_RESTART);
577 AddItemWithStringId(IDC_WIN_CHROMEOS_RESTART, IDS_WIN_CHROMEOS_RESTART);
583 // Append the full menu including separators. The final separator only gets
584 // appended when this is a touch menu - otherwise it would get added twice.
585 CreateCutCopyPasteMenu();
587 if (CommandLine::ForCurrentProcess()->HasSwitch(
588 switches::kEnableDomDistiller)) {
589 AddItemWithStringId(IDC_DISTILL_PAGE, IDS_DISTILL_PAGE);
592 AddItemWithStringId(IDC_SAVE_PAGE, IDS_SAVE_PAGE);
593 AddItemWithStringId(IDC_FIND, IDS_FIND);
594 AddItemWithStringId(IDC_PRINT, IDS_PRINT);
596 tools_menu_model_.reset(new ToolsMenuModel(this, browser_));
599 AddItemWithStringId(IDC_SHOW_HISTORY, IDS_SHOW_HISTORY);
600 AddItemWithStringId(IDC_SHOW_DOWNLOADS, IDS_SHOW_DOWNLOADS);
601 // If settings-in-a-window is enabled the Extensions item is at the top level.
602 if (::switches::SettingsWindowEnabled())
603 AddItemWithStringId(IDC_MANAGE_EXTENSIONS, IDS_SHOW_EXTENSIONS);
604 AddSeparator(ui::NORMAL_SEPARATOR);
606 #if !defined(OS_CHROMEOS)
607 if (!switches::IsNewAvatarMenu()) {
608 // No "Sign in to Chromium..." menu item on ChromeOS.
609 SigninManager* signin = SigninManagerFactory::GetForProfile(
610 browser_->profile()->GetOriginalProfile());
611 if (signin && signin->IsSigninAllowed()) {
612 const base::string16 short_product_name =
613 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME);
614 AddItem(IDC_SHOW_SYNC_SETUP, l10n_util::GetStringFUTF16(
615 IDS_SYNC_MENU_PRE_SYNCED_LABEL, short_product_name));
616 AddSeparator(ui::NORMAL_SEPARATOR);
621 AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS);
623 // On ChromeOS we don't want the about menu option.
624 #if !defined(OS_CHROMEOS)
625 AddItem(IDC_ABOUT, l10n_util::GetStringUTF16(IDS_ABOUT));
628 #if defined(GOOGLE_CHROME_BUILD)
629 help_menu_model_.reset(new HelpMenuModel(this, browser_));
630 AddSubMenuWithStringId(IDC_HELP_MENU, IDS_HELP_MENU,
631 help_menu_model_.get());
634 #if defined(OS_CHROMEOS)
635 if (CommandLine::ForCurrentProcess()->HasSwitch(
636 chromeos::switches::kEnableRequestTabletSite))
637 AddCheckItemWithStringId(IDC_TOGGLE_REQUEST_TABLET_SITE,
638 IDS_TOGGLE_REQUEST_TABLET_SITE);
641 if (browser_defaults::kShowUpgradeMenuItem)
642 AddItem(IDC_UPGRADE_DIALOG, GetUpgradeDialogMenuItemName());
645 SetIcon(GetIndexOfCommandId(IDC_VIEW_INCOMPATIBILITIES),
646 ui::ResourceBundle::GetSharedInstance().
647 GetNativeImageNamed(IDR_INPUT_ALERT_MENU));
650 AddGlobalErrorMenuItems();
652 AddSeparator(ui::NORMAL_SEPARATOR);
653 AddSubMenuWithStringId(
654 IDC_ZOOM_MENU, IDS_MORE_TOOLS_MENU, tools_menu_model_.get());
656 bool show_exit_menu = browser_defaults::kShowExitMenuItem;
658 if (browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
659 show_exit_menu = false;
662 if (show_exit_menu) {
663 AddSeparator(ui::NORMAL_SEPARATOR);
664 AddItemWithStringId(IDC_EXIT, IDS_EXIT);
667 RemoveTrailingSeparators();
670 void WrenchMenuModel::AddGlobalErrorMenuItems() {
671 // TODO(sail): Currently we only build the wrench menu once per browser
672 // window. This means that if a new error is added after the menu is built
673 // it won't show in the existing wrench menu. To fix this we need to some
674 // how update the menu if new errors are added.
675 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
676 // GetSignedInServiceErrors() can modify the global error list, so call it
677 // before iterating through that list below.
678 std::vector<GlobalError*> signin_errors;
679 signin_errors = signin_ui_util::GetSignedInServiceErrors(
680 browser_->profile()->GetOriginalProfile());
681 const GlobalErrorService::GlobalErrorList& errors =
682 GlobalErrorServiceFactory::GetForProfile(browser_->profile())->errors();
683 for (GlobalErrorService::GlobalErrorList::const_iterator
684 it = errors.begin(); it != errors.end(); ++it) {
685 GlobalError* error = *it;
687 if (error->HasMenuItem()) {
688 #if !defined(OS_CHROMEOS)
689 // Don't add a signin error if it's already being displayed elsewhere.
690 if (std::find(signin_errors.begin(), signin_errors.end(), error) !=
691 signin_errors.end()) {
692 MenuModel* model = this;
694 if (MenuModel::GetModelAndIndexForCommandId(
695 IDC_SHOW_SIGNIN, &model, &index)) {
701 AddItem(error->MenuItemCommandID(), error->MenuItemLabel());
702 int icon_id = error->MenuItemIconResourceID();
704 const gfx::Image& image = rb.GetNativeImageNamed(icon_id);
705 SetIcon(GetIndexOfCommandId(error->MenuItemCommandID()),
712 void WrenchMenuModel::CreateExtensionToolbarOverflowMenu() {
713 #if defined(TOOLKIT_VIEWS)
714 AddItem(IDC_EXTENSIONS_OVERFLOW_MENU, base::string16());
715 // We only add the separator if there are > 0 items to show in the overflow.
716 extensions::ExtensionToolbarModel* toolbar_model =
717 extensions::ExtensionToolbarModel::Get(browser_->profile());
718 // A count of -1 means all actions are visible.
719 if (toolbar_model->GetVisibleIconCount() != -1)
720 AddSeparator(ui::UPPER_SEPARATOR);
721 #endif // defined(TOOLKIT_VIEWS)
724 void WrenchMenuModel::CreateCutCopyPasteMenu() {
725 AddSeparator(ui::LOWER_SEPARATOR);
727 #if defined(OS_POSIX) && !defined(TOOLKIT_VIEWS)
728 // WARNING: Mac does not use the ButtonMenuItemModel, but instead defines the
729 // layout for this menu item in Toolbar.xib. It does, however, use the
730 // command_id value from AddButtonItem() to identify this special item.
731 edit_menu_item_model_.reset(new ui::ButtonMenuItemModel(IDS_EDIT, this));
732 edit_menu_item_model_->AddGroupItemWithStringId(IDC_CUT, IDS_CUT);
733 edit_menu_item_model_->AddGroupItemWithStringId(IDC_COPY, IDS_COPY);
734 edit_menu_item_model_->AddGroupItemWithStringId(IDC_PASTE, IDS_PASTE);
735 AddButtonItem(IDC_EDIT_MENU, edit_menu_item_model_.get());
737 // WARNING: views/wrench_menu assumes these items are added in this order. If
738 // you change the order you'll need to update wrench_menu as well.
739 AddItemWithStringId(IDC_CUT, IDS_CUT);
740 AddItemWithStringId(IDC_COPY, IDS_COPY);
741 AddItemWithStringId(IDC_PASTE, IDS_PASTE);
744 AddSeparator(ui::UPPER_SEPARATOR);
747 void WrenchMenuModel::CreateZoomMenu() {
748 // This menu needs to be enclosed by separators.
749 AddSeparator(ui::LOWER_SEPARATOR);
751 #if defined(OS_POSIX) && !defined(TOOLKIT_VIEWS)
752 // WARNING: Mac does not use the ButtonMenuItemModel, but instead defines the
753 // layout for this menu item in Toolbar.xib. It does, however, use the
754 // command_id value from AddButtonItem() to identify this special item.
755 zoom_menu_item_model_.reset(
756 new ui::ButtonMenuItemModel(IDS_ZOOM_MENU, this));
757 zoom_menu_item_model_->AddGroupItemWithStringId(
758 IDC_ZOOM_MINUS, IDS_ZOOM_MINUS2);
759 zoom_menu_item_model_->AddButtonLabel(IDC_ZOOM_PERCENT_DISPLAY,
761 zoom_menu_item_model_->AddGroupItemWithStringId(
762 IDC_ZOOM_PLUS, IDS_ZOOM_PLUS2);
763 zoom_menu_item_model_->AddSpace();
764 zoom_menu_item_model_->AddItemWithImage(
765 IDC_FULLSCREEN, IDR_FULLSCREEN_MENU_BUTTON);
766 AddButtonItem(IDC_ZOOM_MENU, zoom_menu_item_model_.get());
768 // WARNING: views/wrench_menu assumes these items are added in this order. If
769 // you change the order you'll need to update wrench_menu as well.
770 AddItemWithStringId(IDC_ZOOM_MINUS, IDS_ZOOM_MINUS);
771 AddItemWithStringId(IDC_ZOOM_PLUS, IDS_ZOOM_PLUS);
772 AddItemWithStringId(IDC_FULLSCREEN, IDS_FULLSCREEN);
775 AddSeparator(ui::UPPER_SEPARATOR);
778 void WrenchMenuModel::UpdateZoomControls() {
779 int zoom_percent = 100;
780 if (browser_->tab_strip_model()->GetActiveWebContents()) {
781 zoom_percent = ZoomController::FromWebContents(
782 browser_->tab_strip_model()->GetActiveWebContents())
785 zoom_label_ = l10n_util::GetStringFUTF16(
786 IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent));
789 void WrenchMenuModel::OnZoomLevelChanged(
790 const content::HostZoomMap::ZoomLevelChange& change) {
791 UpdateZoomControls();