NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / system_menu_model_builder.cc
blob85296b40cae4d7d72e22800c51b70833c192abca
1 // Copyright (c) 2013 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/views/frame/system_menu_model_builder.h"
7 #include "base/command_line.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/host_desktop.h"
12 #include "chrome/browser/ui/toolbar/wrench_menu_model.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "grit/generated_resources.h"
15 #include "ui/base/accelerators/accelerator.h"
16 #include "ui/base/models/simple_menu_model.h"
18 #if defined(OS_CHROMEOS)
19 #include "ash/session_state_delegate.h"
20 #include "ash/shell.h"
21 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
22 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
23 #include "chrome/browser/ui/browser_window.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #endif
27 SystemMenuModelBuilder::SystemMenuModelBuilder(
28 ui::AcceleratorProvider* provider,
29 Browser* browser)
30 : menu_delegate_(provider, browser) {
33 SystemMenuModelBuilder::~SystemMenuModelBuilder() {
36 void SystemMenuModelBuilder::Init() {
37 ui::SimpleMenuModel* model = new ui::SimpleMenuModel(&menu_delegate_);
38 menu_model_.reset(model);
39 BuildMenu(model);
40 #if defined(OS_WIN)
41 // On Windows with HOST_DESKTOP_TYPE_NATIVE we put the menu items in the
42 // system menu (not at the end). Doing this necessitates adding a trailing
43 // separator.
44 if (browser()->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_NATIVE)
45 model->AddSeparator(ui::NORMAL_SEPARATOR);
46 #endif
49 void SystemMenuModelBuilder::BuildMenu(ui::SimpleMenuModel* model) {
50 // We add the menu items in reverse order so that insertion_index never needs
51 // to change.
52 if (browser()->is_type_tabbed())
53 BuildSystemMenuForBrowserWindow(model);
54 else
55 BuildSystemMenuForAppOrPopupWindow(model);
56 AddFrameToggleItems(model);
59 void SystemMenuModelBuilder::BuildSystemMenuForBrowserWindow(
60 ui::SimpleMenuModel* model) {
61 model->AddItemWithStringId(IDC_NEW_TAB, IDS_NEW_TAB);
62 model->AddItemWithStringId(IDC_RESTORE_TAB, IDS_RESTORE_TAB);
63 if (chrome::CanOpenTaskManager()) {
64 model->AddSeparator(ui::NORMAL_SEPARATOR);
65 model->AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER);
67 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
68 model->AddSeparator(ui::NORMAL_SEPARATOR);
69 model->AddCheckItemWithStringId(IDC_USE_SYSTEM_TITLE_BAR,
70 IDS_SHOW_WINDOW_DECORATIONS_MENU);
71 #endif
72 AppendTeleportMenu(model);
73 // If it's a regular browser window with tabs, we don't add any more items,
74 // since it already has menus (Page, Chrome).
77 void SystemMenuModelBuilder::BuildSystemMenuForAppOrPopupWindow(
78 ui::SimpleMenuModel* model) {
79 model->AddItemWithStringId(IDC_BACK, IDS_CONTENT_CONTEXT_BACK);
80 model->AddItemWithStringId(IDC_FORWARD, IDS_CONTENT_CONTEXT_FORWARD);
81 model->AddItemWithStringId(IDC_RELOAD, IDS_APP_MENU_RELOAD);
82 model->AddSeparator(ui::NORMAL_SEPARATOR);
83 if (browser()->is_app())
84 model->AddItemWithStringId(IDC_NEW_TAB, IDS_APP_MENU_NEW_WEB_PAGE);
85 else
86 model->AddItemWithStringId(IDC_SHOW_AS_TAB, IDS_SHOW_AS_TAB);
87 model->AddSeparator(ui::NORMAL_SEPARATOR);
88 model->AddItemWithStringId(IDC_CUT, IDS_CUT);
89 model->AddItemWithStringId(IDC_COPY, IDS_COPY);
90 model->AddItemWithStringId(IDC_PASTE, IDS_PASTE);
91 model->AddSeparator(ui::NORMAL_SEPARATOR);
92 model->AddItemWithStringId(IDC_FIND, IDS_FIND);
93 model->AddItemWithStringId(IDC_PRINT, IDS_PRINT);
94 zoom_menu_contents_.reset(new ZoomMenuModel(&menu_delegate_));
95 model->AddSubMenuWithStringId(IDC_ZOOM_MENU, IDS_ZOOM_MENU,
96 zoom_menu_contents_.get());
97 encoding_menu_contents_.reset(new EncodingMenuModel(browser()));
98 model->AddSubMenuWithStringId(IDC_ENCODING_MENU,
99 IDS_ENCODING_MENU,
100 encoding_menu_contents_.get());
101 if (browser()->is_app() && chrome::CanOpenTaskManager()) {
102 model->AddSeparator(ui::NORMAL_SEPARATOR);
103 model->AddItemWithStringId(IDC_TASK_MANAGER, IDS_TASK_MANAGER);
105 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
106 model->AddSeparator(ui::NORMAL_SEPARATOR);
107 model->AddItemWithStringId(IDC_CLOSE_WINDOW, IDS_CLOSE);
108 #endif
110 AppendTeleportMenu(model);
113 void SystemMenuModelBuilder::AddFrameToggleItems(ui::SimpleMenuModel* model) {
114 if (CommandLine::ForCurrentProcess()->HasSwitch(
115 switches::kDebugEnableFrameToggle)) {
116 model->AddSeparator(ui::NORMAL_SEPARATOR);
117 model->AddItem(IDC_DEBUG_FRAME_TOGGLE,
118 base::ASCIIToUTF16("Toggle Frame Type"));
122 void SystemMenuModelBuilder::AppendTeleportMenu(ui::SimpleMenuModel* model) {
123 #if defined(OS_CHROMEOS)
124 DCHECK(browser()->window());
125 // If there is no manager, we are not in the proper multi user mode.
126 if (chrome::MultiUserWindowManager::GetMultiProfileMode() !=
127 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED)
128 return;
130 // To show the menu we need at least two logged in users.
131 ash::SessionStateDelegate* delegate =
132 ash::Shell::GetInstance()->session_state_delegate();
133 int logged_in_users = delegate->NumberOfLoggedInUsers();
134 if (logged_in_users <= 1)
135 return;
137 // If this does not belong to a profile or there is no window, or the window
138 // is not owned by anyone, we don't show the menu addition.
139 chrome::MultiUserWindowManager* manager =
140 chrome::MultiUserWindowManager::GetInstance();
141 const std::string user_id =
142 multi_user_util::GetUserIDFromProfile(browser()->profile());
143 aura::Window* window = browser()->window()->GetNativeWindow();
144 if (user_id.empty() || !window || manager->GetWindowOwner(window).empty())
145 return;
147 model->AddSeparator(ui::NORMAL_SEPARATOR);
148 DCHECK(logged_in_users <= 3);
149 for (int user_index = 1; user_index < logged_in_users; ++user_index) {
150 model->AddItem(
151 user_index == 1 ? IDC_VISIT_DESKTOP_OF_LRU_USER_2 :
152 IDC_VISIT_DESKTOP_OF_LRU_USER_3,
153 l10n_util::GetStringFUTF16(
154 IDS_VISIT_DESKTOP_OF_LRU_USER,
155 delegate->GetUserDisplayName(user_index),
156 base::ASCIIToUTF16(delegate->GetUserEmail(user_index))));
158 #endif