NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / ash / launcher / launcher_context_menu.cc
blob64b0b1707b69c6f0ef07960adf29739d9307a2da
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/ash/launcher/launcher_context_menu.h"
7 #include <string>
9 #include "ash/desktop_background/user_wallpaper_delegate.h"
10 #include "ash/metrics/user_metrics_recorder.h"
11 #include "ash/root_window_controller.h"
12 #include "ash/shelf/shelf_item_delegate.h"
13 #include "ash/shelf/shelf_widget.h"
14 #include "ash/shell.h"
15 #include "base/bind.h"
16 #include "base/command_line.h"
17 #include "base/prefs/pref_service.h"
18 #include "chrome/browser/extensions/context_menu_matcher.h"
19 #include "chrome/browser/fullscreen.h"
20 #include "chrome/browser/prefs/incognito_mode_prefs.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/ash/chrome_shell_delegate.h"
23 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/extensions/extension_constants.h"
26 #include "content/public/common/context_menu_params.h"
27 #include "grit/ash_strings.h"
28 #include "grit/generated_resources.h"
29 #include "ui/base/l10n/l10n_util.h"
31 namespace {
33 bool MenuItemHasLauncherContext(const extensions::MenuItem* item) {
34 return item->contexts().Contains(extensions::MenuItem::LAUNCHER);
37 } // namespace
39 LauncherContextMenu::LauncherContextMenu(ChromeLauncherController* controller,
40 const ash::ShelfItem* item,
41 aura::Window* root)
42 : ui::SimpleMenuModel(NULL),
43 controller_(controller),
44 item_(*item),
45 shelf_alignment_menu_(root),
46 root_window_(root),
47 item_delegate_(NULL) {
48 DCHECK(item);
49 DCHECK(root_window_);
50 Init();
53 LauncherContextMenu::LauncherContextMenu(ChromeLauncherController* controller,
54 ash::ShelfItemDelegate* item_delegate,
55 ash::ShelfItem* item,
56 aura::Window* root)
57 : ui::SimpleMenuModel(NULL),
58 controller_(controller),
59 item_(*item),
60 shelf_alignment_menu_(root),
61 root_window_(root),
62 item_delegate_(item_delegate) {
63 DCHECK(item);
64 DCHECK(root_window_);
65 Init();
68 LauncherContextMenu::LauncherContextMenu(ChromeLauncherController* controller,
69 aura::Window* root)
70 : ui::SimpleMenuModel(NULL),
71 controller_(controller),
72 item_(ash::ShelfItem()),
73 shelf_alignment_menu_(root),
74 extension_items_(new extensions::ContextMenuMatcher(
75 controller->profile(), this, this,
76 base::Bind(MenuItemHasLauncherContext))),
77 root_window_(root),
78 item_delegate_(NULL) {
79 DCHECK(root_window_);
80 Init();
83 void LauncherContextMenu::Init() {
84 extension_items_.reset(new extensions::ContextMenuMatcher(
85 controller_->profile(), this, this,
86 base::Bind(MenuItemHasLauncherContext)));
87 set_delegate(this);
89 if (is_valid_item()) {
90 if (item_.type == ash::TYPE_APP_SHORTCUT ||
91 item_.type == ash::TYPE_WINDOWED_APP) {
92 // V1 apps can be started from the menu - but V2 apps should not.
93 if (!controller_->IsPlatformApp(item_.id)) {
94 AddItem(MENU_OPEN_NEW, base::string16());
95 AddSeparator(ui::NORMAL_SEPARATOR);
97 AddItem(
98 MENU_PIN,
99 l10n_util::GetStringUTF16(controller_->IsPinned(item_.id) ?
100 IDS_LAUNCHER_CONTEXT_MENU_UNPIN :
101 IDS_LAUNCHER_CONTEXT_MENU_PIN));
102 if (controller_->IsOpen(item_.id)) {
103 AddItem(MENU_CLOSE,
104 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_CLOSE));
106 if (!controller_->IsPlatformApp(item_.id) &&
107 item_.type != ash::TYPE_WINDOWED_APP) {
108 AddSeparator(ui::NORMAL_SEPARATOR);
109 if (CommandLine::ForCurrentProcess()->HasSwitch(
110 switches::kEnableStreamlinedHostedApps)) {
111 // Streamlined hosted apps launch in a window by default. This menu
112 // item is re-interpreted as a single, toggle-able option to launch
113 // the hosted app as a tab.
114 AddCheckItemWithStringId(
115 LAUNCH_TYPE_REGULAR_TAB,
116 IDS_APP_CONTEXT_MENU_OPEN_TAB);
117 } else {
118 AddCheckItemWithStringId(
119 LAUNCH_TYPE_REGULAR_TAB,
120 IDS_APP_CONTEXT_MENU_OPEN_REGULAR);
121 AddCheckItemWithStringId(
122 LAUNCH_TYPE_PINNED_TAB,
123 IDS_APP_CONTEXT_MENU_OPEN_PINNED);
124 AddCheckItemWithStringId(
125 LAUNCH_TYPE_WINDOW,
126 IDS_APP_CONTEXT_MENU_OPEN_WINDOW);
127 // Even though the launch type is Full Screen it is more accurately
128 // described as Maximized in Ash.
129 AddCheckItemWithStringId(
130 LAUNCH_TYPE_FULLSCREEN,
131 IDS_APP_CONTEXT_MENU_OPEN_MAXIMIZED);
134 } else if (item_.type == ash::TYPE_BROWSER_SHORTCUT) {
135 AddItem(MENU_NEW_WINDOW,
136 l10n_util::GetStringUTF16(IDS_LAUNCHER_NEW_WINDOW));
137 if (!controller_->IsLoggedInAsGuest()) {
138 AddItem(MENU_NEW_INCOGNITO_WINDOW,
139 l10n_util::GetStringUTF16(IDS_LAUNCHER_NEW_INCOGNITO_WINDOW));
141 } else if (item_.type == ash::TYPE_DIALOG) {
142 AddItem(MENU_CLOSE,
143 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_CLOSE));
144 } else {
145 if (item_.type == ash::TYPE_PLATFORM_APP) {
146 AddItem(
147 MENU_PIN,
148 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_PIN));
150 if (controller_->IsOpen(item_.id)) {
151 AddItem(MENU_CLOSE,
152 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_CLOSE));
155 AddSeparator(ui::NORMAL_SEPARATOR);
156 if (item_.type == ash::TYPE_APP_SHORTCUT ||
157 item_.type == ash::TYPE_WINDOWED_APP ||
158 item_.type == ash::TYPE_PLATFORM_APP) {
159 std::string app_id = controller_->GetAppIDForShelfID(item_.id);
160 if (!app_id.empty()) {
161 int index = 0;
162 extension_items_->AppendExtensionItems(
163 app_id, base::string16(), &index);
164 AddSeparator(ui::NORMAL_SEPARATOR);
168 // In fullscreen, the launcher is either hidden or autohidden depending on
169 // the type of fullscreen. Do not show the auto-hide menu item while in
170 // fullscreen because it is confusing when the preference appears not to
171 // apply.
172 if (!IsFullScreenMode() &&
173 controller_->CanUserModifyShelfAutoHideBehavior(root_window_)) {
174 AddCheckItemWithStringId(MENU_AUTO_HIDE,
175 IDS_ASH_SHELF_CONTEXT_MENU_AUTO_HIDE);
177 if (ash::ShelfWidget::ShelfAlignmentAllowed()) {
178 AddSubMenuWithStringId(MENU_ALIGNMENT_MENU,
179 IDS_ASH_SHELF_CONTEXT_MENU_POSITION,
180 &shelf_alignment_menu_);
182 #if defined(OS_CHROMEOS)
183 AddItem(MENU_CHANGE_WALLPAPER,
184 l10n_util::GetStringUTF16(IDS_AURA_SET_DESKTOP_WALLPAPER));
185 #endif
188 LauncherContextMenu::~LauncherContextMenu() {
191 bool LauncherContextMenu::IsItemForCommandIdDynamic(int command_id) const {
192 return command_id == MENU_OPEN_NEW;
195 base::string16 LauncherContextMenu::GetLabelForCommandId(int command_id) const {
196 if (command_id == MENU_OPEN_NEW) {
197 if (item_.type == ash::TYPE_PLATFORM_APP) {
198 return l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_NEW_WINDOW);
200 switch (controller_->GetLaunchType(item_.id)) {
201 case extensions::LAUNCH_TYPE_PINNED:
202 case extensions::LAUNCH_TYPE_REGULAR:
203 return l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_NEW_TAB);
204 case extensions::LAUNCH_TYPE_FULLSCREEN:
205 case extensions::LAUNCH_TYPE_WINDOW:
206 return l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_NEW_WINDOW);
207 default:
208 NOTREACHED();
209 return base::string16();
212 NOTREACHED();
213 return base::string16();
216 bool LauncherContextMenu::IsCommandIdChecked(int command_id) const {
217 switch (command_id) {
218 case LAUNCH_TYPE_PINNED_TAB:
219 return controller_->GetLaunchType(item_.id) ==
220 extensions::LAUNCH_TYPE_PINNED;
221 case LAUNCH_TYPE_REGULAR_TAB:
222 return controller_->GetLaunchType(item_.id) ==
223 extensions::LAUNCH_TYPE_REGULAR;
224 case LAUNCH_TYPE_WINDOW:
225 return controller_->GetLaunchType(item_.id) ==
226 extensions::LAUNCH_TYPE_WINDOW;
227 case LAUNCH_TYPE_FULLSCREEN:
228 return controller_->GetLaunchType(item_.id) ==
229 extensions::LAUNCH_TYPE_FULLSCREEN;
230 case MENU_AUTO_HIDE:
231 return controller_->GetShelfAutoHideBehavior(root_window_) ==
232 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS;
233 default:
234 return extension_items_->IsCommandIdChecked(command_id);
238 bool LauncherContextMenu::IsCommandIdEnabled(int command_id) const {
239 switch (command_id) {
240 case MENU_PIN:
241 return controller_->IsPinnable(item_.id);
242 #if defined(OS_CHROMEOS)
243 case MENU_CHANGE_WALLPAPER:
244 return ash::Shell::GetInstance()->user_wallpaper_delegate()->
245 CanOpenSetWallpaperPage();
246 #endif
247 case MENU_NEW_WINDOW:
248 // "Normal" windows are not allowed when incognito is enforced.
249 return IncognitoModePrefs::GetAvailability(
250 controller_->profile()->GetPrefs()) != IncognitoModePrefs::FORCED;
251 case MENU_AUTO_HIDE:
252 return controller_->CanUserModifyShelfAutoHideBehavior(root_window_);
253 case MENU_NEW_INCOGNITO_WINDOW:
254 // Incognito windows are not allowed when incognito is disabled.
255 return IncognitoModePrefs::GetAvailability(
256 controller_->profile()->GetPrefs()) != IncognitoModePrefs::DISABLED;
257 default:
258 return extension_items_->IsCommandIdEnabled(command_id);
262 bool LauncherContextMenu::GetAcceleratorForCommandId(
263 int command_id,
264 ui::Accelerator* accelerator) {
265 return false;
268 void LauncherContextMenu::ExecuteCommand(int command_id, int event_flags) {
269 switch (static_cast<MenuItem>(command_id)) {
270 case MENU_OPEN_NEW:
271 controller_->Launch(item_.id, ui::EF_NONE);
272 break;
273 case MENU_CLOSE:
274 if (item_.type == ash::TYPE_DIALOG) {
275 DCHECK(item_delegate_);
276 item_delegate_->Close();
277 } else {
278 // TODO(simonhong): Use ShelfItemDelegate::Close().
279 controller_->Close(item_.id);
281 ash::Shell::GetInstance()->metrics()->RecordUserMetricsAction(
282 ash::UMA_CLOSE_THROUGH_CONTEXT_MENU);
283 break;
284 case MENU_PIN:
285 controller_->TogglePinned(item_.id);
286 break;
287 case LAUNCH_TYPE_PINNED_TAB:
288 controller_->SetLaunchType(item_.id, extensions::LAUNCH_TYPE_PINNED);
289 break;
290 case LAUNCH_TYPE_REGULAR_TAB: {
291 extensions::LaunchType launch_type =
292 extensions::LAUNCH_TYPE_REGULAR;
293 // Streamlined hosted apps can only toggle between LAUNCH_WINDOW and
294 // LAUNCH_REGULAR.
295 if (CommandLine::ForCurrentProcess()->HasSwitch(
296 switches::kEnableStreamlinedHostedApps)) {
297 launch_type = controller_->GetLaunchType(item_.id) ==
298 extensions::LAUNCH_TYPE_REGULAR
299 ? extensions::LAUNCH_TYPE_WINDOW
300 : extensions::LAUNCH_TYPE_REGULAR;
302 controller_->SetLaunchType(item_.id, launch_type);
303 break;
305 case LAUNCH_TYPE_WINDOW:
306 controller_->SetLaunchType(item_.id, extensions::LAUNCH_TYPE_WINDOW);
307 break;
308 case LAUNCH_TYPE_FULLSCREEN:
309 controller_->SetLaunchType(item_.id, extensions::LAUNCH_TYPE_FULLSCREEN);
310 break;
311 case MENU_AUTO_HIDE:
312 controller_->ToggleShelfAutoHideBehavior(root_window_);
313 break;
314 case MENU_NEW_WINDOW:
315 controller_->CreateNewWindow();
316 break;
317 case MENU_NEW_INCOGNITO_WINDOW:
318 controller_->CreateNewIncognitoWindow();
319 break;
320 case MENU_ALIGNMENT_MENU:
321 break;
322 #if defined(OS_CHROMEOS)
323 case MENU_CHANGE_WALLPAPER:
324 ash::Shell::GetInstance()->user_wallpaper_delegate()->
325 OpenSetWallpaperPage();
326 break;
327 #endif
328 default:
329 extension_items_->ExecuteCommand(command_id, NULL,
330 content::ContextMenuParams());