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