| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016 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/ash/launcher/extension_launcher_context_menu.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "ash/desktop_background/user_wallpaper_delegate.h" |
| 10 #include "ash/metrics/user_metrics_recorder.h" |
| 11 #include "ash/shelf/shelf.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/memory/scoped_ptr.h" |
| 17 #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/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/extensions/extension_constants.h" |
| 25 #include "chrome/common/pref_names.h" |
| 26 #include "chrome/grit/generated_resources.h" |
| 27 #include "components/prefs/pref_service.h" |
| 28 #include "content/public/common/context_menu_params.h" |
| 29 #include "grit/ash_strings.h" |
| 30 #include "ui/base/l10n/l10n_util.h" |
| 31 |
| 32 namespace { |
| 33 |
| 34 bool MenuItemHasLauncherContext(const extensions::MenuItem* item) { |
| 35 return item->contexts().Contains(extensions::MenuItem::LAUNCHER); |
| 36 } |
| 37 |
| 38 } // name space |
| 39 |
| 40 ExtensionLauncherContextMenu::ExtensionLauncherContextMenu( |
| 41 ChromeLauncherController* controller, |
| 42 const ash::ShelfItem* item, |
| 43 ash::Shelf* shelf) |
| 44 : LauncherContextMenu(controller, item, shelf) { |
| 45 Init(); |
| 46 } |
| 47 |
| 48 ExtensionLauncherContextMenu::~ExtensionLauncherContextMenu() {} |
| 49 |
| 50 void ExtensionLauncherContextMenu::Init() { |
| 51 extension_items_.reset(new extensions::ContextMenuMatcher( |
| 52 controller()->profile(), this, this, |
| 53 base::Bind(MenuItemHasLauncherContext))); |
| 54 if (item().type == ash::TYPE_APP_SHORTCUT || |
| 55 item().type == ash::TYPE_WINDOWED_APP) { |
| 56 // V1 apps can be started from the menu - but V2 apps should not. |
| 57 if (!controller()->IsPlatformApp(item().id)) { |
| 58 AddItem(MENU_OPEN_NEW, base::string16()); |
| 59 AddSeparator(ui::NORMAL_SEPARATOR); |
| 60 } |
| 61 AddPinMenu(); |
| 62 if (controller()->IsOpen(item().id)) { |
| 63 AddItem(MENU_CLOSE, |
| 64 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_CLOSE)); |
| 65 } |
| 66 if (!controller()->IsPlatformApp(item().id) && |
| 67 item().type != ash::TYPE_WINDOWED_APP) { |
| 68 AddSeparator(ui::NORMAL_SEPARATOR); |
| 69 if (extensions::util::IsNewBookmarkAppsEnabled()) { |
| 70 // With bookmark apps enabled, hosted apps launch in a window by |
| 71 // default. This menu item is re-interpreted as a single, toggle-able |
| 72 // option to launch the hosted app as a tab. |
| 73 AddCheckItemWithStringId(LAUNCH_TYPE_WINDOW, |
| 74 IDS_APP_CONTEXT_MENU_OPEN_WINDOW); |
| 75 } else { |
| 76 AddCheckItemWithStringId(LAUNCH_TYPE_REGULAR_TAB, |
| 77 IDS_APP_CONTEXT_MENU_OPEN_REGULAR); |
| 78 AddCheckItemWithStringId(LAUNCH_TYPE_PINNED_TAB, |
| 79 IDS_APP_CONTEXT_MENU_OPEN_PINNED); |
| 80 AddCheckItemWithStringId(LAUNCH_TYPE_WINDOW, |
| 81 IDS_APP_CONTEXT_MENU_OPEN_WINDOW); |
| 82 // Even though the launch type is Full Screen it is more accurately |
| 83 // described as Maximized in Ash. |
| 84 AddCheckItemWithStringId(LAUNCH_TYPE_FULLSCREEN, |
| 85 IDS_APP_CONTEXT_MENU_OPEN_MAXIMIZED); |
| 86 } |
| 87 } |
| 88 } else if (item().type == ash::TYPE_BROWSER_SHORTCUT) { |
| 89 AddItem(MENU_NEW_WINDOW, |
| 90 l10n_util::GetStringUTF16(IDS_APP_LIST_NEW_WINDOW)); |
| 91 if (!controller()->IsLoggedInAsGuest()) { |
| 92 AddItem(MENU_NEW_INCOGNITO_WINDOW, |
| 93 l10n_util::GetStringUTF16(IDS_APP_LIST_NEW_INCOGNITO_WINDOW)); |
| 94 } |
| 95 } else if (item().type == ash::TYPE_DIALOG) { |
| 96 AddItem(MENU_CLOSE, |
| 97 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_CLOSE)); |
| 98 } else { |
| 99 if (item().type == ash::TYPE_PLATFORM_APP) { |
| 100 AddItem(MENU_PIN, |
| 101 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_PIN)); |
| 102 } |
| 103 bool show_close_button = controller()->IsOpen(item().id); |
| 104 #if defined(OS_CHROMEOS) |
| 105 if (extension_misc::IsImeMenuExtensionId( |
| 106 controller()->GetAppIDForShelfID(item().id))) { |
| 107 show_close_button = false; |
| 108 } |
| 109 #endif |
| 110 if (show_close_button) { |
| 111 AddItem(MENU_CLOSE, |
| 112 l10n_util::GetStringUTF16(IDS_LAUNCHER_CONTEXT_MENU_CLOSE)); |
| 113 } |
| 114 } |
| 115 AddSeparator(ui::NORMAL_SEPARATOR); |
| 116 if (item().type == ash::TYPE_APP_SHORTCUT || |
| 117 item().type == ash::TYPE_WINDOWED_APP || |
| 118 item().type == ash::TYPE_PLATFORM_APP) { |
| 119 const extensions::MenuItem::ExtensionKey app_key( |
| 120 controller()->GetAppIDForShelfID(item().id)); |
| 121 if (!app_key.empty()) { |
| 122 int index = 0; |
| 123 extension_items_->AppendExtensionItems(app_key, base::string16(), &index, |
| 124 false); // is_action_menu |
| 125 AddSeparator(ui::NORMAL_SEPARATOR); |
| 126 } |
| 127 } |
| 128 AddAutohideAlignmentWallpaperMenu(); |
| 129 } |
| 130 |
| 131 bool ExtensionLauncherContextMenu::IsItemForCommandIdDynamic( |
| 132 int command_id) const { |
| 133 return command_id == MENU_OPEN_NEW; |
| 134 } |
| 135 |
| 136 base::string16 ExtensionLauncherContextMenu::GetLabelForCommandId( |
| 137 int command_id) const { |
| 138 if (command_id == MENU_OPEN_NEW) { |
| 139 if (item().type == ash::TYPE_PLATFORM_APP) { |
| 140 return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTEXT_MENU_NEW_WINDOW); |
| 141 } |
| 142 switch (controller()->GetLaunchType(item().id)) { |
| 143 case extensions::LAUNCH_TYPE_PINNED: |
| 144 case extensions::LAUNCH_TYPE_REGULAR: |
| 145 return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTEXT_MENU_NEW_TAB); |
| 146 case extensions::LAUNCH_TYPE_FULLSCREEN: |
| 147 case extensions::LAUNCH_TYPE_WINDOW: |
| 148 return l10n_util::GetStringUTF16(IDS_APP_LIST_CONTEXT_MENU_NEW_WINDOW); |
| 149 default: |
| 150 NOTREACHED(); |
| 151 return base::string16(); |
| 152 } |
| 153 } |
| 154 NOTREACHED(); |
| 155 return base::string16(); |
| 156 } |
| 157 |
| 158 bool ExtensionLauncherContextMenu::IsCommandIdChecked(int command_id) const { |
| 159 switch (command_id) { |
| 160 case LAUNCH_TYPE_PINNED_TAB: |
| 161 return controller()->GetLaunchType(item().id) == |
| 162 extensions::LAUNCH_TYPE_PINNED; |
| 163 case LAUNCH_TYPE_REGULAR_TAB: |
| 164 return controller()->GetLaunchType(item().id) == |
| 165 extensions::LAUNCH_TYPE_REGULAR; |
| 166 case LAUNCH_TYPE_WINDOW: |
| 167 return controller()->GetLaunchType(item().id) == |
| 168 extensions::LAUNCH_TYPE_WINDOW; |
| 169 case LAUNCH_TYPE_FULLSCREEN: |
| 170 return controller()->GetLaunchType(item().id) == |
| 171 extensions::LAUNCH_TYPE_FULLSCREEN; |
| 172 case MENU_AUTO_HIDE: |
| 173 return shelf()->GetAutoHideBehavior() == |
| 174 ash::SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS; |
| 175 default: |
| 176 if (command_id < MENU_ITEM_COUNT) |
| 177 return false; |
| 178 return (extension_items_ && |
| 179 extension_items_->IsCommandIdChecked(command_id)); |
| 180 } |
| 181 } |
| 182 |
| 183 bool ExtensionLauncherContextMenu::IsCommandIdEnabled(int command_id) const { |
| 184 if (IsCommonCommandId(command_id)) |
| 185 return LauncherContextMenu::IsCommandIdEnabled(command_id); |
| 186 switch (command_id) { |
| 187 case MENU_NEW_WINDOW: |
| 188 // "Normal" windows are not allowed when incognito is enforced. |
| 189 return IncognitoModePrefs::GetAvailability( |
| 190 controller()->profile()->GetPrefs()) != |
| 191 IncognitoModePrefs::FORCED; |
| 192 case MENU_NEW_INCOGNITO_WINDOW: |
| 193 // Incognito windows are not allowed when incognito is disabled. |
| 194 return IncognitoModePrefs::GetAvailability( |
| 195 controller()->profile()->GetPrefs()) != |
| 196 IncognitoModePrefs::DISABLED; |
| 197 default: |
| 198 if (command_id < MENU_ITEM_COUNT) |
| 199 return true; |
| 200 return (extension_items_ && |
| 201 extension_items_->IsCommandIdEnabled(command_id)); |
| 202 } |
| 203 } |
| 204 |
| 205 void ExtensionLauncherContextMenu::ExecuteCommand(int command_id, |
| 206 int event_flags) { |
| 207 if (IsCommonCommandId(command_id)) |
| 208 return LauncherContextMenu::ExecuteCommand(command_id, event_flags); |
| 209 switch (static_cast<MenuItem>(command_id)) { |
| 210 case MENU_OPEN_NEW: |
| 211 controller()->Launch(item().id, ui::EF_NONE); |
| 212 break; |
| 213 case LAUNCH_TYPE_PINNED_TAB: |
| 214 controller()->SetLaunchType(item().id, extensions::LAUNCH_TYPE_PINNED); |
| 215 break; |
| 216 case LAUNCH_TYPE_REGULAR_TAB: |
| 217 controller()->SetLaunchType(item().id, extensions::LAUNCH_TYPE_REGULAR); |
| 218 break; |
| 219 case LAUNCH_TYPE_WINDOW: { |
| 220 extensions::LaunchType launch_type = extensions::LAUNCH_TYPE_WINDOW; |
| 221 // With bookmark apps enabled, hosted apps can only toggle between |
| 222 // LAUNCH_WINDOW and LAUNCH_REGULAR. |
| 223 if (extensions::util::IsNewBookmarkAppsEnabled()) { |
| 224 launch_type = controller()->GetLaunchType(item().id) == |
| 225 extensions::LAUNCH_TYPE_WINDOW |
| 226 ? extensions::LAUNCH_TYPE_REGULAR |
| 227 : extensions::LAUNCH_TYPE_WINDOW; |
| 228 } |
| 229 controller()->SetLaunchType(item().id, launch_type); |
| 230 break; |
| 231 } |
| 232 case LAUNCH_TYPE_FULLSCREEN: |
| 233 controller()->SetLaunchType(item().id, |
| 234 extensions::LAUNCH_TYPE_FULLSCREEN); |
| 235 break; |
| 236 case MENU_NEW_WINDOW: |
| 237 controller()->CreateNewWindow(); |
| 238 break; |
| 239 case MENU_NEW_INCOGNITO_WINDOW: |
| 240 controller()->CreateNewIncognitoWindow(); |
| 241 break; |
| 242 default: |
| 243 if (extension_items_) { |
| 244 extension_items_->ExecuteCommand(command_id, nullptr, nullptr, |
| 245 content::ContextMenuParams()); |
| 246 } |
| 247 } |
| 248 } |
| OLD | NEW |