Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1744)

Side by Side Diff: chrome/browser/ui/ash/launcher/launcher_context_menu.cc

Issue 1857213004: Refactor of LauncherContextMenu. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nits update Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/ash/launcher/launcher_context_menu.h" 5 #include "chrome/browser/ui/ash/launcher/launcher_context_menu.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "ash/desktop_background/user_wallpaper_delegate.h" 9 #include "ash/desktop_background/user_wallpaper_delegate.h"
10 #include "ash/metrics/user_metrics_recorder.h" 10 #include "ash/metrics/user_metrics_recorder.h"
11 #include "ash/session/session_state_delegate.h" 11 #include "ash/session/session_state_delegate.h"
12 #include "ash/shelf/shelf.h" 12 #include "ash/shelf/shelf.h"
13 #include "ash/shelf/shelf_item_delegate.h"
14 #include "ash/shelf/shelf_widget.h"
15 #include "ash/shell.h" 13 #include "ash/shell.h"
16 #include "base/bind.h"
17 #include "build/build_config.h" 14 #include "build/build_config.h"
18 #include "chrome/browser/extensions/context_menu_matcher.h"
19 #include "chrome/browser/extensions/extension_util.h"
20 #include "chrome/browser/fullscreen.h" 15 #include "chrome/browser/fullscreen.h"
21 #include "chrome/browser/prefs/incognito_mode_prefs.h"
22 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/ash/chrome_shell_delegate.h"
24 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" 17 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
25 #include "chrome/common/extensions/extension_constants.h" 18 #include "chrome/browser/ui/ash/launcher/desktop_shell_launcher_context_menu.h"
19 #include "chrome/browser/ui/ash/launcher/extension_launcher_context_menu.h"
26 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
27 #include "chrome/grit/generated_resources.h" 21 #include "chrome/grit/generated_resources.h"
28 #include "components/prefs/pref_service.h" 22 #include "components/prefs/pref_service.h"
29 #include "content/public/common/context_menu_params.h" 23 #include "content/public/common/context_menu_params.h"
30 #include "grit/ash_strings.h" 24 #include "grit/ash_strings.h"
31 #include "ui/base/l10n/l10n_util.h" 25 #include "ui/base/l10n/l10n_util.h"
32 26
33 namespace { 27 namespace {
34 28
35 bool MenuItemHasLauncherContext(const extensions::MenuItem* item) {
36 return item->contexts().Contains(extensions::MenuItem::LAUNCHER);
37 }
38
39 // Returns true if the user can modify the |shelf|'s auto-hide behavior. 29 // Returns true if the user can modify the |shelf|'s auto-hide behavior.
40 bool CanUserModifyShelfAutoHideBehavior(const Profile* profile) { 30 bool CanUserModifyShelfAutoHideBehavior(const Profile* profile) {
41 const std::string& pref = prefs::kShelfAutoHideBehaviorLocal; 31 const std::string& pref = prefs::kShelfAutoHideBehaviorLocal;
42 return profile->GetPrefs()->FindPreference(pref)->IsUserModifiable(); 32 return profile->GetPrefs()->FindPreference(pref)->IsUserModifiable();
43 } 33 }
44 34
45 } // namespace 35 } // namespace
46 36
37 // static
38 LauncherContextMenu* LauncherContextMenu::Create(
39 ChromeLauncherController* controller,
40 const ash::ShelfItem* item,
41 ash::Shelf* shelf) {
42 DCHECK(controller);
43 DCHECK(shelf);
44 // Create DesktopShellLauncherContextMenu if no item is selected.
45 if (!item || item->id == 0)
46 return new DesktopShellLauncherContextMenu(controller, item, shelf);
47
48 // Create ExtensionLauncherContextMenu for the item.
49 return new ExtensionLauncherContextMenu(controller, item, shelf);
50 }
51
47 LauncherContextMenu::LauncherContextMenu(ChromeLauncherController* controller, 52 LauncherContextMenu::LauncherContextMenu(ChromeLauncherController* controller,
48 const ash::ShelfItem* item, 53 const ash::ShelfItem* item,
49 ash::Shelf* shelf) 54 ash::Shelf* shelf)
50 : ui::SimpleMenuModel(nullptr), 55 : ui::SimpleMenuModel(nullptr),
51 controller_(controller), 56 controller_(controller),
52 item_(item ? *item : ash::ShelfItem()), 57 item_(item ? *item : ash::ShelfItem()),
53 shelf_alignment_menu_(shelf), 58 shelf_alignment_menu_(shelf),
54 shelf_(shelf) { 59 shelf_(shelf) {
55 DCHECK(shelf_);
56 Init();
57 }
58
59 void LauncherContextMenu::Init() {
60 set_delegate(this); 60 set_delegate(this);
61
62 if (item_.id != 0) {
63 extension_items_.reset(new extensions::ContextMenuMatcher(
64 controller_->profile(), this, this,
65 base::Bind(MenuItemHasLauncherContext)));
66 if (item_.type == ash::TYPE_APP_SHORTCUT ||
67 item_.type == ash::TYPE_WINDOWED_APP) {
68 // V1 apps can be started from the menu - but V2 apps should not.
69 if (!controller_->IsPlatformApp(item_.id)) {
70 AddItem(MENU_OPEN_NEW, base::string16());
71 AddSeparator(ui::NORMAL_SEPARATOR);
72 }
73 const std::string app_id = controller_->GetAppIDForShelfID(item_.id);
74 int menu_pin_string_id;
75 if (!controller_->CanPin(app_id))
76 menu_pin_string_id = IDS_LAUNCHER_CONTEXT_MENU_PIN_ENFORCED_BY_POLICY;
77 else if (controller_->IsPinned(item_.id))
78 menu_pin_string_id = IDS_LAUNCHER_CONTEXT_MENU_UNPIN;
79 else
80 menu_pin_string_id = IDS_LAUNCHER_CONTEXT_MENU_PIN;
81 AddItem(MENU_PIN, l10n_util::GetStringUTF16(menu_pin_string_id));
82 if (controller_->IsOpen(item_.id)) {
83 AddItem(MENU_CLOSE,
84 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_CLOSE));
85 }
86 if (!controller_->IsPlatformApp(item_.id) &&
87 item_.type != ash::TYPE_WINDOWED_APP) {
88 AddSeparator(ui::NORMAL_SEPARATOR);
89 if (extensions::util::IsNewBookmarkAppsEnabled()) {
90 // With bookmark apps enabled, hosted apps launch in a window by
91 // default. This menu item is re-interpreted as a single, toggle-able
92 // option to launch the hosted app as a tab.
93 AddCheckItemWithStringId(LAUNCH_TYPE_WINDOW,
94 IDS_APP_CONTEXT_MENU_OPEN_WINDOW);
95 } else {
96 AddCheckItemWithStringId(
97 LAUNCH_TYPE_REGULAR_TAB,
98 IDS_APP_CONTEXT_MENU_OPEN_REGULAR);
99 AddCheckItemWithStringId(
100 LAUNCH_TYPE_PINNED_TAB,
101 IDS_APP_CONTEXT_MENU_OPEN_PINNED);
102 AddCheckItemWithStringId(
103 LAUNCH_TYPE_WINDOW,
104 IDS_APP_CONTEXT_MENU_OPEN_WINDOW);
105 // Even though the launch type is Full Screen it is more accurately
106 // described as Maximized in Ash.
107 AddCheckItemWithStringId(
108 LAUNCH_TYPE_FULLSCREEN,
109 IDS_APP_CONTEXT_MENU_OPEN_MAXIMIZED);
110 }
111 }
112 } else if (item_.type == ash::TYPE_BROWSER_SHORTCUT) {
113 AddItem(MENU_NEW_WINDOW,
114 l10n_util::GetStringUTF16(IDS_APP_LIST_NEW_WINDOW));
115 if (!controller_->IsLoggedInAsGuest()) {
116 AddItem(MENU_NEW_INCOGNITO_WINDOW,
117 l10n_util::GetStringUTF16(IDS_APP_LIST_NEW_INCOGNITO_WINDOW));
118 }
119 } else if (item_.type == ash::TYPE_DIALOG) {
120 AddItem(MENU_CLOSE,
121 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_CLOSE));
122 } else {
123 if (item_.type == ash::TYPE_PLATFORM_APP) {
124 AddItem(
125 MENU_PIN,
126 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_PIN));
127 }
128 bool show_close_button = controller_->IsOpen(item_.id);
129 #if defined(OS_CHROMEOS)
130 if (extension_misc::IsImeMenuExtensionId(
131 controller_->GetAppIDForShelfID(item_.id))) {
132 show_close_button = false;
133 }
134 #endif
135 if (show_close_button) {
136 AddItem(MENU_CLOSE,
137 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_CLOSE));
138 }
139 }
140 AddSeparator(ui::NORMAL_SEPARATOR);
141 if (item_.type == ash::TYPE_APP_SHORTCUT ||
142 item_.type == ash::TYPE_WINDOWED_APP ||
143 item_.type == ash::TYPE_PLATFORM_APP) {
144 const extensions::MenuItem::ExtensionKey app_key(
145 controller_->GetAppIDForShelfID(item_.id));
146 if (!app_key.empty()) {
147 int index = 0;
148 extension_items_->AppendExtensionItems(app_key,
149 base::string16(),
150 &index,
151 false); // is_action_menu
152 AddSeparator(ui::NORMAL_SEPARATOR);
153 }
154 }
155 }
156 // In fullscreen, the launcher is either hidden or autohidden depending on the
157 // type of fullscreen. Do not show the auto-hide menu item while in fullscreen
158 // because it is confusing when the preference appears not to apply.
159 if (!IsFullScreenMode() &&
160 CanUserModifyShelfAutoHideBehavior(controller_->profile())) {
161 AddCheckItemWithStringId(MENU_AUTO_HIDE,
162 IDS_ASH_SHELF_CONTEXT_MENU_AUTO_HIDE);
163 }
164 if (ash::ShelfWidget::ShelfAlignmentAllowed() &&
165 !ash::Shell::GetInstance()->session_state_delegate()->IsScreenLocked()) {
166 AddSubMenuWithStringId(MENU_ALIGNMENT_MENU,
167 IDS_ASH_SHELF_CONTEXT_MENU_POSITION,
168 &shelf_alignment_menu_);
169 }
170 #if defined(OS_CHROMEOS)
171 if (!controller_->IsLoggedInAsGuest()) {
172 AddItem(MENU_CHANGE_WALLPAPER,
173 l10n_util::GetStringUTF16(IDS_AURA_SET_DESKTOP_WALLPAPER));
174 }
175 #endif
176 } 61 }
177 62
178 LauncherContextMenu::~LauncherContextMenu() { 63 LauncherContextMenu::~LauncherContextMenu() {
179 } 64 }
180 65
181 bool LauncherContextMenu::IsItemForCommandIdDynamic(int command_id) const { 66 bool LauncherContextMenu::IsItemForCommandIdDynamic(int command_id) const {
182 return command_id == MENU_OPEN_NEW; 67 return false;
183 } 68 }
184 69
185 base::string16 LauncherContextMenu::GetLabelForCommandId(int command_id) const { 70 base::string16 LauncherContextMenu::GetLabelForCommandId(int command_id) const {
186 if (command_id == MENU_OPEN_NEW) {
187 if (item_.type == ash::TYPE_PLATFORM_APP) {
188 return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTEXT_MENU_NEW_WINDOW);
189 }
190 switch (controller_->GetLaunchType(item_.id)) {
191 case extensions::LAUNCH_TYPE_PINNED:
192 case extensions::LAUNCH_TYPE_REGULAR:
193 return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTEXT_MENU_NEW_TAB);
194 case extensions::LAUNCH_TYPE_FULLSCREEN:
195 case extensions::LAUNCH_TYPE_WINDOW:
196 return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTEXT_MENU_NEW_WINDOW);
197 default:
198 NOTREACHED();
199 return base::string16();
200 }
201 }
202 NOTREACHED(); 71 NOTREACHED();
203 return base::string16(); 72 return base::string16();
204 } 73 }
205 74
206 bool LauncherContextMenu::IsCommandIdChecked(int command_id) const { 75 bool LauncherContextMenu::IsCommandIdChecked(int command_id) const {
207 switch (command_id) { 76 if (command_id == MENU_AUTO_HIDE) {
208 case LAUNCH_TYPE_PINNED_TAB: 77 return shelf_->GetAutoHideBehavior() ==
209 return controller_->GetLaunchType(item_.id) == 78 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS;
210 extensions::LAUNCH_TYPE_PINNED;
211 case LAUNCH_TYPE_REGULAR_TAB:
212 return controller_->GetLaunchType(item_.id) ==
213 extensions::LAUNCH_TYPE_REGULAR;
214 case LAUNCH_TYPE_WINDOW:
215 return controller_->GetLaunchType(item_.id) ==
216 extensions::LAUNCH_TYPE_WINDOW;
217 case LAUNCH_TYPE_FULLSCREEN:
218 return controller_->GetLaunchType(item_.id) ==
219 extensions::LAUNCH_TYPE_FULLSCREEN;
220 case MENU_AUTO_HIDE:
221 return shelf_->GetAutoHideBehavior() ==
222 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS;
223 default:
224 if (command_id < MENU_ITEM_COUNT)
225 return false;
226 return (extension_items_ &&
227 extension_items_->IsCommandIdChecked(command_id));
228 } 79 }
80 DCHECK(command_id < MENU_ITEM_COUNT);
81 return false;
229 } 82 }
230 83
231 bool LauncherContextMenu::IsCommandIdEnabled(int command_id) const { 84 bool LauncherContextMenu::IsCommandIdEnabled(int command_id) const {
232 switch (command_id) { 85 switch (command_id) {
233 case MENU_PIN: 86 case MENU_PIN:
234 return controller_->IsPinnable(item_.id); 87 return controller_->IsPinnable(item_.id);
235 case MENU_CHANGE_WALLPAPER: 88 case MENU_CHANGE_WALLPAPER:
236 return ash::Shell::GetInstance()->user_wallpaper_delegate()-> 89 return ash::Shell::GetInstance()
237 CanOpenSetWallpaperPage(); 90 ->user_wallpaper_delegate()
238 case MENU_NEW_WINDOW: 91 ->CanOpenSetWallpaperPage();
239 // "Normal" windows are not allowed when incognito is enforced.
240 return IncognitoModePrefs::GetAvailability(
241 controller_->profile()->GetPrefs()) != IncognitoModePrefs::FORCED;
242 case MENU_AUTO_HIDE: 92 case MENU_AUTO_HIDE:
243 return CanUserModifyShelfAutoHideBehavior(controller_->profile()); 93 return CanUserModifyShelfAutoHideBehavior(controller_->profile());
244 case MENU_NEW_INCOGNITO_WINDOW:
245 // Incognito windows are not allowed when incognito is disabled.
246 return IncognitoModePrefs::GetAvailability(
247 controller_->profile()->GetPrefs()) != IncognitoModePrefs::DISABLED;
248 default: 94 default:
249 if (command_id < MENU_ITEM_COUNT) 95 DCHECK(command_id < MENU_ITEM_COUNT);
250 return true; 96 return true;
251 return (extension_items_ &&
252 extension_items_->IsCommandIdEnabled(command_id));
253 } 97 }
254 } 98 }
255 99
256 bool LauncherContextMenu::GetAcceleratorForCommandId( 100 bool LauncherContextMenu::GetAcceleratorForCommandId(
257 int command_id, 101 int command_id,
258 ui::Accelerator* accelerator) { 102 ui::Accelerator* accelerator) {
259 return false; 103 return false;
260 } 104 }
261 105
262 void LauncherContextMenu::ExecuteCommand(int command_id, int event_flags) { 106 void LauncherContextMenu::ExecuteCommand(int command_id, int event_flags) {
(...skipping 12 matching lines...) Expand all
275 } else { 119 } else {
276 // TODO(simonhong): Use ShelfItemDelegate::Close(). 120 // TODO(simonhong): Use ShelfItemDelegate::Close().
277 controller_->Close(item_.id); 121 controller_->Close(item_.id);
278 } 122 }
279 ash::Shell::GetInstance()->metrics()->RecordUserMetricsAction( 123 ash::Shell::GetInstance()->metrics()->RecordUserMetricsAction(
280 ash::UMA_CLOSE_THROUGH_CONTEXT_MENU); 124 ash::UMA_CLOSE_THROUGH_CONTEXT_MENU);
281 break; 125 break;
282 case MENU_PIN: 126 case MENU_PIN:
283 controller_->TogglePinned(item_.id); 127 controller_->TogglePinned(item_.id);
284 break; 128 break;
285 case LAUNCH_TYPE_PINNED_TAB:
286 controller_->SetLaunchType(item_.id, extensions::LAUNCH_TYPE_PINNED);
287 break;
288 case LAUNCH_TYPE_REGULAR_TAB:
289 controller_->SetLaunchType(item_.id, extensions::LAUNCH_TYPE_REGULAR);
290 break;
291 case LAUNCH_TYPE_WINDOW: {
292 extensions::LaunchType launch_type = extensions::LAUNCH_TYPE_WINDOW;
293 // With bookmark apps enabled, hosted apps can only toggle between
294 // LAUNCH_WINDOW and LAUNCH_REGULAR.
295 if (extensions::util::IsNewBookmarkAppsEnabled()) {
296 launch_type = controller_->GetLaunchType(item_.id) ==
297 extensions::LAUNCH_TYPE_WINDOW
298 ? extensions::LAUNCH_TYPE_REGULAR
299 : extensions::LAUNCH_TYPE_WINDOW;
300 }
301 controller_->SetLaunchType(item_.id, launch_type);
302 break;
303 }
304 case LAUNCH_TYPE_FULLSCREEN:
305 controller_->SetLaunchType(item_.id, extensions::LAUNCH_TYPE_FULLSCREEN);
306 break;
307 case MENU_AUTO_HIDE: 129 case MENU_AUTO_HIDE:
308 shelf_->SetAutoHideBehavior(shelf_->GetAutoHideBehavior() == 130 shelf_->SetAutoHideBehavior(shelf_->GetAutoHideBehavior() ==
309 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS 131 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
310 ? ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER 132 ? ash::SHELF_AUTO_HIDE_BEHAVIOR_NEVER
311 : ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS); 133 : ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
312 break; 134 break;
313 case MENU_NEW_WINDOW:
314 controller_->CreateNewWindow();
315 break;
316 case MENU_NEW_INCOGNITO_WINDOW:
317 controller_->CreateNewIncognitoWindow();
318 break;
319 case MENU_ALIGNMENT_MENU: 135 case MENU_ALIGNMENT_MENU:
320 break; 136 break;
321 case MENU_CHANGE_WALLPAPER: 137 case MENU_CHANGE_WALLPAPER:
322 ash::Shell::GetInstance()->user_wallpaper_delegate()-> 138 ash::Shell::GetInstance()->user_wallpaper_delegate()->
323 OpenSetWallpaperPage(); 139 OpenSetWallpaperPage();
324 break; 140 break;
325 default: 141 default:
326 if (extension_items_) { 142 NOTREACHED();
327 extension_items_->ExecuteCommand(command_id, nullptr, nullptr,
328 content::ContextMenuParams());
329 }
330 } 143 }
331 } 144 }
145
146 void LauncherContextMenu::AddPinMenu() {
147 // Expect an item with a none zero id to add pin/unpin menu item.
148 DCHECK(item_.id);
149 const std::string app_id = controller_->GetAppIDForShelfID(item_.id);
150 int menu_pin_string_id;
151 if (!controller_->CanPin(app_id))
152 menu_pin_string_id = IDS_LAUNCHER_CONTEXT_MENU_PIN_ENFORCED_BY_POLICY;
153 else if (controller_->IsPinned(item_.id))
154 menu_pin_string_id = IDS_LAUNCHER_CONTEXT_MENU_UNPIN;
155 else
156 menu_pin_string_id = IDS_LAUNCHER_CONTEXT_MENU_PIN;
157 AddItemWithStringId(MENU_PIN, menu_pin_string_id);
158 }
159
160 void LauncherContextMenu::AddShelfOptionsMenu() {
161 // In fullscreen, the launcher is either hidden or autohidden depending
162 // on thethe type of fullscreen. Do not show the auto-hide menu item while in
163 // while in fullscreen because it is confusing when the preference appears
164 // not to apply.
165 if (!IsFullScreenMode() &&
166 CanUserModifyShelfAutoHideBehavior(controller_->profile())) {
167 AddCheckItemWithStringId(MENU_AUTO_HIDE,
168 IDS_ASH_SHELF_CONTEXT_MENU_AUTO_HIDE);
169 }
170 if (ash::ShelfWidget::ShelfAlignmentAllowed() &&
171 !ash::Shell::GetInstance()->session_state_delegate()->IsScreenLocked()) {
172 AddSubMenuWithStringId(MENU_ALIGNMENT_MENU,
173 IDS_ASH_SHELF_CONTEXT_MENU_POSITION,
174 &shelf_alignment_menu_);
175 }
176 #if defined(OS_CHROMEOS)
177 if (!controller_->IsLoggedInAsGuest())
178 AddItemWithStringId(MENU_CHANGE_WALLPAPER, IDS_AURA_SET_DESKTOP_WALLPAPER);
179 #endif
180 }
181
182 bool LauncherContextMenu::ExecuteCommonCommand(int command_id,
183 int event_flags) {
184 switch (command_id) {
185 case MENU_OPEN_NEW:
186 case MENU_CLOSE:
187 case MENU_PIN:
188 case MENU_AUTO_HIDE:
189 case MENU_ALIGNMENT_MENU:
190 case MENU_CHANGE_WALLPAPER:
191 ExecuteCommand(command_id, event_flags);
192 return true;
193 default:
194 return false;
195 }
196 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698