Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / toolbar / wrench_menu_model.cc
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 #include "chrome/browser/ui/toolbar/wrench_menu_model.h"
6
7 #include <algorithm>
8 #include <cmath>
9
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"
64
65 #if defined(OS_CHROMEOS)
66 #include "chromeos/chromeos_switches.h"
67 #endif
68
69 #if defined(OS_WIN)
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"
75 #endif
76
77 #if defined(USE_ASH)
78 #include "ash/shell.h"
79 #endif
80
81 using base::UserMetricsAction;
82 using content::WebContents;
83
84 namespace {
85 // Conditionally return the update app menu item title based on upgrade detector
86 // state.
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);
91   } else {
92     return l10n_util::GetStringUTF16(IDS_UPDATE_NOW);
93   }
94 }
95
96 }  // namespace
97
98 ////////////////////////////////////////////////////////////////////////////////
99 // EncodingMenuModel
100
101 EncodingMenuModel::EncodingMenuModel(Browser* browser)
102     : ui::SimpleMenuModel(this),
103       browser_(browser) {
104   Build();
105 }
106
107 EncodingMenuModel::~EncodingMenuModel() {
108 }
109
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);
115
116   int group_id = 0;
117   EncodingMenuController::EncodingMenuItemList::iterator it =
118       encoding_menu_items.begin();
119   for (; it != encoding_menu_items.end(); ++it) {
120     int id = it->first;
121     base::string16& label = it->second;
122     if (id == 0) {
123       AddSeparator(ui::NORMAL_SEPARATOR);
124     } else {
125       if (id == IDC_ENCODING_AUTO_DETECT) {
126         AddCheckItem(id, label);
127       } else {
128         // Use the id of the first radio command as the id of the group.
129         if (group_id <= 0)
130           group_id = id;
131         AddRadioItem(id, label, group_id);
132       }
133     }
134   }
135 }
136
137 bool EncodingMenuModel::IsCommandIdChecked(int command_id) const {
138   WebContents* current_tab =
139       browser_->tab_strip_model()->GetActiveWebContents();
140   if (!current_tab)
141     return false;
142   EncodingMenuController controller;
143   return controller.IsItemChecked(browser_->profile(),
144                                   current_tab->GetEncoding(), command_id);
145 }
146
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);
154 #endif
155   return enabled;
156 }
157
158 bool EncodingMenuModel::GetAcceleratorForCommandId(
159     int command_id,
160     ui::Accelerator* accelerator) {
161   return false;
162 }
163
164 void EncodingMenuModel::ExecuteCommand(int command_id, int event_flags) {
165   chrome::ExecuteCommand(browser_, command_id);
166 }
167
168 ////////////////////////////////////////////////////////////////////////////////
169 // ZoomMenuModel
170
171 ZoomMenuModel::ZoomMenuModel(ui::SimpleMenuModel::Delegate* delegate)
172     : SimpleMenuModel(delegate) {
173   Build();
174 }
175
176 ZoomMenuModel::~ZoomMenuModel() {
177 }
178
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);
183 }
184
185 ////////////////////////////////////////////////////////////////////////////////
186 // HelpMenuModel
187
188 #if defined(GOOGLE_CHROME_BUILD)
189
190 class WrenchMenuModel::HelpMenuModel : public ui::SimpleMenuModel {
191  public:
192   HelpMenuModel(ui::SimpleMenuModel::Delegate* delegate,
193                 Browser* browser)
194       : SimpleMenuModel(delegate) {
195     Build(browser);
196   }
197   virtual ~HelpMenuModel() {
198   }
199
200  private:
201   void Build(Browser* browser) {
202 #if defined(OS_CHROMEOS) && defined(OFFICIAL_BUILD)
203     int help_string_id = IDS_GET_HELP;
204 #else
205     int help_string_id = IDS_HELP_PAGE;
206 #endif
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));
212     }
213
214     AddItemWithStringId(IDC_FEEDBACK, IDS_FEEDBACK);
215   }
216
217   DISALLOW_COPY_AND_ASSIGN(HelpMenuModel);
218 };
219
220 #endif  // defined(GOOGLE_CHROME_BUILD)
221
222 ////////////////////////////////////////////////////////////////////////////////
223 // ToolsMenuModel
224
225 ToolsMenuModel::ToolsMenuModel(ui::SimpleMenuModel::Delegate* delegate,
226                                Browser* browser)
227     : SimpleMenuModel(delegate) {
228   Build(browser);
229 }
230
231 ToolsMenuModel::~ToolsMenuModel() {}
232
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;
240 #endif
241
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);
248   }
249
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);
253
254   if (chrome::CanOpenTaskManager())
255     AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER);
256
257   AddItemWithStringId(IDC_CLEAR_BROWSING_DATA, IDS_CLEAR_BROWSING_DATA);
258
259   AddSeparator(ui::NORMAL_SEPARATOR);
260
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);
268
269 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC)
270   AddSeparator(ui::NORMAL_SEPARATOR);
271   AddCheckItemWithStringId(IDC_PROFILING_ENABLED, IDS_PROFILING_ENABLED);
272 #endif
273 }
274
275 ////////////////////////////////////////////////////////////////////////////////
276 // WrenchMenuModel
277
278 WrenchMenuModel::WrenchMenuModel(ui::AcceleratorProvider* provider,
279                                  Browser* browser)
280     : ui::SimpleMenuModel(this),
281       provider_(provider),
282       browser_(browser),
283       tab_strip_model_(browser_->tab_strip_model()) {
284   Build();
285   UpdateZoomControls();
286
287   content_zoom_subscription_ =
288       content::HostZoomMap::GetDefaultForBrowserContext(browser->profile())
289           ->AddZoomLevelChangedCallback(base::Bind(
290               &WrenchMenuModel::OnZoomLevelChanged, base::Unretained(this)));
291
292   browser_zoom_subscription_ = ZoomEventManager::GetForBrowserContext(
293       browser->profile())->AddZoomLevelChangedCallback(
294           base::Bind(&WrenchMenuModel::OnZoomLevelChanged,
295                      base::Unretained(this)));
296
297   tab_strip_model_->AddObserver(this);
298
299   registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
300                  content::NotificationService::AllSources());
301 }
302
303 WrenchMenuModel::~WrenchMenuModel() {
304   if (tab_strip_model_)
305     tab_strip_model_->RemoveObserver(this);
306 }
307
308 bool WrenchMenuModel::DoesCommandIdDismissMenu(int command_id) const {
309   return command_id != IDC_ZOOM_MINUS && command_id != IDC_ZOOM_PLUS;
310 }
311
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 ||
318 #endif
319          command_id == IDC_UPGRADE_DIALOG ||
320          (!switches::IsNewAvatarMenu() && command_id == IDC_SHOW_SIGNIN);
321 }
322
323 base::string16 WrenchMenuModel::GetLabelForCommandId(int command_id) const {
324   switch (command_id) {
325     case IDC_ZOOM_PERCENT_DISPLAY:
326       return zoom_label_;
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);
334     }
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)
342                        : NULL;
343       if (tab_helper && tab_helper->IsPinned())
344         string_id = IDS_UNPIN_FROM_START_SCREEN;
345       return l10n_util::GetStringUTF16(string_id);
346     }
347 #endif
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());
354     default:
355       NOTREACHED();
356       return base::string16();
357   }
358 }
359
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());
368         return true;
369       }
370       return false;
371     }
372     case IDC_SHOW_SIGNIN: {
373       DCHECK(!switches::IsNewAvatarMenu());
374       GlobalError* error = signin_ui_util::GetSignedInServiceError(
375           browser_->profile()->GetOriginalProfile());
376       if (error) {
377         int icon_id = error->MenuItemIconResourceID();
378         if (icon_id) {
379           *icon = rb.GetNativeImageNamed(icon_id);
380           return true;
381         }
382       }
383       return false;
384     }
385     default:
386       break;
387   }
388   return false;
389 }
390
391 void WrenchMenuModel::ExecuteCommand(int command_id, int event_flags) {
392   GlobalError* error = GlobalErrorServiceFactory::GetForProfile(
393       browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id);
394   if (error) {
395     error->ExecuteMenuItem(browser_);
396     return;
397   }
398
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());
403     if (error) {
404       error->ExecuteMenuItem(browser_);
405       return;
406     }
407   }
408
409   if (command_id == IDC_HELP_PAGE_VIA_MENU)
410     content::RecordAction(UserMetricsAction("ShowHelpTabViaWrenchMenu"));
411
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"));
417   }
418
419   chrome::ExecuteCommand(browser_, command_id);
420 }
421
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_);
430   }
431
432   return false;
433 }
434
435 bool WrenchMenuModel::IsCommandIdEnabled(int command_id) const {
436   GlobalError* error = GlobalErrorServiceFactory::GetForProfile(
437       browser_->profile())->GetGlobalErrorByMenuItemCommandID(command_id);
438   if (error)
439     return true;
440
441   return chrome::IsCommandEnabled(browser_, command_id);
442 }
443
444 bool WrenchMenuModel::IsCommandIdVisible(int command_id) const {
445   switch (command_id) {
446 #if defined(OS_WIN)
447     case IDC_VIEW_INCOMPATIBILITIES: {
448       EnumerateModulesModel* loaded_modules =
449           EnumerateModulesModel::GetInstance();
450       if (loaded_modules->confirmed_bad_modules_detected() <= 0)
451         return false;
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();
455       return true;
456     }
457     case IDC_PIN_TO_START_SCREEN:
458       return base::win::IsMetroProcess();
459 #else
460     case IDC_VIEW_INCOMPATIBILITIES:
461     case IDC_PIN_TO_START_SCREEN:
462       return false;
463 #endif
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());
471 #endif
472     default:
473       return true;
474   }
475 }
476
477 bool WrenchMenuModel::GetAcceleratorForCommandId(
478       int command_id,
479       ui::Accelerator* accelerator) {
480   return provider_->GetAcceleratorForCommandId(command_id, accelerator);
481 }
482
483 void WrenchMenuModel::ActiveTabChanged(WebContents* old_contents,
484                                        WebContents* new_contents,
485                                        int index,
486                                        int reason) {
487   // The user has switched between tabs and the new tab may have a different
488   // zoom setting.
489   UpdateZoomControls();
490 }
491
492 void WrenchMenuModel::TabReplacedAt(TabStripModel* tab_strip_model,
493                                     WebContents* old_contents,
494                                     WebContents* new_contents,
495                                     int index) {
496   UpdateZoomControls();
497 }
498
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;
504 }
505
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();
511 }
512
513 // For testing.
514 WrenchMenuModel::WrenchMenuModel()
515     : ui::SimpleMenuModel(this),
516       provider_(NULL),
517       browser_(NULL),
518       tab_strip_model_(NULL) {
519 }
520
521 bool WrenchMenuModel::ShouldShowNewIncognitoWindowMenuItem() {
522   if (browser_->profile()->IsSupervised())
523     return false;
524
525   return !browser_->profile()->IsGuestSession();
526 }
527
528 void WrenchMenuModel::Build() {
529 #if defined(OS_WIN)
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);
537 #endif
538
539   if (extensions::FeatureSwitch::extension_action_redesign()->IsEnabled())
540     CreateExtensionToolbarOverflowMenu();
541
542   AddItemWithStringId(IDC_NEW_TAB, IDS_NEW_TAB);
543   AddItemWithStringId(IDC_NEW_WINDOW, IDS_NEW_WINDOW);
544
545   if (ShouldShowNewIncognitoWindowMenuItem())
546     AddItemWithStringId(IDC_NEW_INCOGNITO_WINDOW, IDS_NEW_INCOGNITO_WINDOW);
547
548   bookmark_sub_menu_model_.reset(new BookmarkSubMenuModel(this, browser_));
549   AddSubMenuWithStringId(IDC_BOOKMARKS_MENU, IDS_BOOKMARKS_MENU,
550                          bookmark_sub_menu_model_.get());
551
552   if (!browser_->profile()->IsOffTheRecord()) {
553     recent_tabs_sub_menu_model_.reset(new RecentTabsSubMenuModel(provider_,
554                                                                  browser_,
555                                                                  NULL));
556     AddSubMenuWithStringId(IDC_RECENT_TABS_MENU, IDS_RECENT_TABS_MENU,
557                            recent_tabs_sub_menu_model_.get());
558   }
559
560 #if defined(OS_WIN)
561   // Windows 8 can support ASH mode using WARP, but Windows 7 requires a working
562   // GPU compositor.
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);
570     } else {
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);
576       } else {
577         AddItemWithStringId(IDC_WIN_CHROMEOS_RESTART, IDS_WIN_CHROMEOS_RESTART);
578       }
579     }
580   }
581 #endif
582
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();
586
587   if (CommandLine::ForCurrentProcess()->HasSwitch(
588           switches::kEnableDomDistiller)) {
589     AddItemWithStringId(IDC_DISTILL_PAGE, IDS_DISTILL_PAGE);
590   }
591
592   AddItemWithStringId(IDC_SAVE_PAGE, IDS_SAVE_PAGE);
593   AddItemWithStringId(IDC_FIND, IDS_FIND);
594   AddItemWithStringId(IDC_PRINT, IDS_PRINT);
595
596   tools_menu_model_.reset(new ToolsMenuModel(this, browser_));
597   CreateZoomMenu();
598
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);
605
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);
617     }
618   }
619 #endif
620
621   AddItemWithStringId(IDC_OPTIONS, IDS_SETTINGS);
622
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));
626 #endif
627
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());
632 #endif
633
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);
639 #endif
640
641   if (browser_defaults::kShowUpgradeMenuItem)
642     AddItem(IDC_UPGRADE_DIALOG, GetUpgradeDialogMenuItemName());
643
644 #if defined(OS_WIN)
645   SetIcon(GetIndexOfCommandId(IDC_VIEW_INCOMPATIBILITIES),
646           ui::ResourceBundle::GetSharedInstance().
647               GetNativeImageNamed(IDR_INPUT_ALERT_MENU));
648 #endif
649
650   AddGlobalErrorMenuItems();
651
652   AddSeparator(ui::NORMAL_SEPARATOR);
653   AddSubMenuWithStringId(
654       IDC_ZOOM_MENU, IDS_MORE_TOOLS_MENU, tools_menu_model_.get());
655
656   bool show_exit_menu = browser_defaults::kShowExitMenuItem;
657 #if defined(OS_WIN)
658   if (browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
659     show_exit_menu = false;
660 #endif
661
662   if (show_exit_menu) {
663     AddSeparator(ui::NORMAL_SEPARATOR);
664     AddItemWithStringId(IDC_EXIT, IDS_EXIT);
665   }
666
667   RemoveTrailingSeparators();
668 }
669
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;
686     DCHECK(error);
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;
693         int index = 0;
694         if (MenuModel::GetModelAndIndexForCommandId(
695                 IDC_SHOW_SIGNIN, &model, &index)) {
696           continue;
697         }
698       }
699 #endif
700
701       AddItem(error->MenuItemCommandID(), error->MenuItemLabel());
702       int icon_id = error->MenuItemIconResourceID();
703       if (icon_id) {
704         const gfx::Image& image = rb.GetNativeImageNamed(icon_id);
705         SetIcon(GetIndexOfCommandId(error->MenuItemCommandID()),
706                 image);
707       }
708     }
709   }
710 }
711
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)
722 }
723
724 void WrenchMenuModel::CreateCutCopyPasteMenu() {
725   AddSeparator(ui::LOWER_SEPARATOR);
726
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());
736 #else
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);
742 #endif
743
744   AddSeparator(ui::UPPER_SEPARATOR);
745 }
746
747 void WrenchMenuModel::CreateZoomMenu() {
748   // This menu needs to be enclosed by separators.
749   AddSeparator(ui::LOWER_SEPARATOR);
750
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,
760                                         IDS_ZOOM_PLUS2);
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());
767 #else
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);
773 #endif
774
775   AddSeparator(ui::UPPER_SEPARATOR);
776 }
777
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())
783                        ->GetZoomPercent();
784   }
785   zoom_label_ = l10n_util::GetStringFUTF16(
786       IDS_ZOOM_PERCENT, base::IntToString16(zoom_percent));
787 }
788
789 void WrenchMenuModel::OnZoomLevelChanged(
790     const content::HostZoomMap::ZoomLevelChange& change) {
791   UpdateZoomControls();
792 }