Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/extensions/extension_context_menu_model.h" | 5 #include "chrome/browser/extensions/extension_context_menu_model.h" |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/app/chrome_command_ids.h" | 9 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/extensions/active_script_controller.h" | 10 #include "chrome/browser/extensions/active_script_controller.h" |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 27 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.h" |
| 28 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 29 #include "content/public/common/context_menu_params.h" | 29 #include "content/public/common/context_menu_params.h" |
| 30 #include "extensions/browser/extension_prefs.h" | 30 #include "extensions/browser/extension_prefs.h" |
| 31 #include "extensions/browser/extension_registry.h" | 31 #include "extensions/browser/extension_registry.h" |
| 32 #include "extensions/browser/extension_system.h" | 32 #include "extensions/browser/extension_system.h" |
| 33 #include "extensions/browser/management_policy.h" | 33 #include "extensions/browser/management_policy.h" |
| 34 #include "extensions/browser/uninstall_reason.h" | 34 #include "extensions/browser/uninstall_reason.h" |
| 35 #include "extensions/common/extension.h" | 35 #include "extensions/common/extension.h" |
| 36 #include "extensions/common/feature_switch.h" | |
| 36 #include "grit/chromium_strings.h" | 37 #include "grit/chromium_strings.h" |
| 37 #include "grit/generated_resources.h" | 38 #include "grit/generated_resources.h" |
| 38 #include "ui/base/l10n/l10n_util.h" | 39 #include "ui/base/l10n/l10n_util.h" |
| 39 | 40 |
| 40 using content::OpenURLParams; | 41 using content::OpenURLParams; |
| 41 using content::Referrer; | 42 using content::Referrer; |
| 42 using content::WebContents; | 43 using content::WebContents; |
| 43 using extensions::Extension; | 44 using extensions::Extension; |
| 45 using extensions::ExtensionActionAPI; | |
| 46 using extensions::ExtensionPrefs; | |
| 44 using extensions::MenuItem; | 47 using extensions::MenuItem; |
| 45 using extensions::MenuManager; | 48 using extensions::MenuManager; |
| 46 | 49 |
| 47 namespace { | 50 namespace { |
| 48 | 51 |
| 49 // Returns true if the given |item| is of the given |type|. | 52 // Returns true if the given |item| is of the given |type|. |
| 50 bool MenuItemMatchesAction(ExtensionContextMenuModel::ActionType type, | 53 bool MenuItemMatchesAction(ExtensionContextMenuModel::ActionType type, |
| 51 const MenuItem* item) { | 54 const MenuItem* item) { |
| 52 if (type == ExtensionContextMenuModel::NO_ACTION) | 55 if (type == ExtensionContextMenuModel::NO_ACTION) |
| 53 return false; | 56 return false; |
| 54 | 57 |
| 55 const MenuItem::ContextList& contexts = item->contexts(); | 58 const MenuItem::ContextList& contexts = item->contexts(); |
| 56 | 59 |
| 57 if (contexts.Contains(MenuItem::ALL)) | 60 if (contexts.Contains(MenuItem::ALL)) |
| 58 return true; | 61 return true; |
| 59 if (contexts.Contains(MenuItem::PAGE_ACTION) && | 62 if (contexts.Contains(MenuItem::PAGE_ACTION) && |
| 60 (type == ExtensionContextMenuModel::PAGE_ACTION)) | 63 (type == ExtensionContextMenuModel::PAGE_ACTION)) |
| 61 return true; | 64 return true; |
| 62 if (contexts.Contains(MenuItem::BROWSER_ACTION) && | 65 if (contexts.Contains(MenuItem::BROWSER_ACTION) && |
| 63 (type == ExtensionContextMenuModel::BROWSER_ACTION)) | 66 (type == ExtensionContextMenuModel::BROWSER_ACTION)) |
| 64 return true; | 67 return true; |
| 65 | 68 |
| 66 return false; | 69 return false; |
| 67 } | 70 } |
| 68 | 71 |
| 72 // Returns the id for the visibility command for the given |extension|, or -1 | |
| 73 // if none should be shown. | |
| 74 int GetVisibilityStringId(Profile* profile, const Extension* extension) { | |
|
Finnur
2014/08/19 10:18:04
nit: const Profile&
Devlin
2014/08/19 16:54:39
Sadly... can't.
ExtensionActionAPI takes a non-co
| |
| 75 int string_id = -1; | |
| 76 if (!extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()) { | |
| 77 // Without the toolbar redesign, we only show the visibility toggle for | |
| 78 // browser actions, and only give the option to hide. | |
| 79 if (extensions::ExtensionActionManager::Get(profile)->GetBrowserAction( | |
| 80 *extension)) { | |
| 81 string_id = IDS_EXTENSIONS_HIDE_BUTTON; | |
| 82 } | |
| 83 } else { | |
| 84 // With the redesign, we display "show" or "hide" based on the icon's | |
| 85 // visibility. | |
| 86 bool visible = ExtensionActionAPI::GetBrowserActionVisibility( | |
| 87 ExtensionPrefs::Get(profile), extension->id()); | |
| 88 string_id = | |
| 89 visible ? IDS_EXTENSIONS_HIDE_BUTTON : IDS_EXTENSIONS_SHOW_BUTTON; | |
| 90 } | |
| 91 return string_id; | |
| 92 } | |
| 93 | |
| 69 } // namespace | 94 } // namespace |
| 70 | 95 |
| 71 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, | 96 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
| 72 Browser* browser, | 97 Browser* browser, |
| 73 PopupDelegate* delegate) | 98 PopupDelegate* delegate) |
| 74 : SimpleMenuModel(this), | 99 : SimpleMenuModel(this), |
| 75 extension_id_(extension->id()), | 100 extension_id_(extension->id()), |
| 76 browser_(browser), | 101 browser_(browser), |
| 77 profile_(browser->profile()), | 102 profile_(browser->profile()), |
| 78 delegate_(delegate), | 103 delegate_(delegate), |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 if (web_contents) { | 195 if (web_contents) { |
| 171 extensions::ActiveScriptController::GetForWebContents(web_contents) | 196 extensions::ActiveScriptController::GetForWebContents(web_contents) |
| 172 ->AlwaysRunOnVisibleOrigin(extension); | 197 ->AlwaysRunOnVisibleOrigin(extension); |
| 173 } | 198 } |
| 174 break; | 199 break; |
| 175 } | 200 } |
| 176 case CONFIGURE: | 201 case CONFIGURE: |
| 177 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); | 202 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); |
| 178 extensions::ExtensionTabUtil::OpenOptionsPage(extension, browser_); | 203 extensions::ExtensionTabUtil::OpenOptionsPage(extension, browser_); |
| 179 break; | 204 break; |
| 180 case HIDE: { | 205 case TOGGLE_VISIBILITY: { |
| 181 extensions::ExtensionActionAPI::SetBrowserActionVisibility( | 206 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); |
| 182 extensions::ExtensionPrefs::Get(profile_), extension->id(), false); | 207 bool visible = ExtensionActionAPI::GetBrowserActionVisibility( |
| 208 prefs, extension->id()); | |
| 209 ExtensionActionAPI::SetBrowserActionVisibility( | |
| 210 prefs, extension->id(), !visible); | |
| 183 break; | 211 break; |
| 184 } | 212 } |
| 185 case UNINSTALL: { | 213 case UNINSTALL: { |
| 186 AddRef(); // Balanced in Accepted() and Canceled() | 214 AddRef(); // Balanced in Accepted() and Canceled() |
| 187 extension_uninstall_dialog_.reset( | 215 extension_uninstall_dialog_.reset( |
| 188 extensions::ExtensionUninstallDialog::Create( | 216 extensions::ExtensionUninstallDialog::Create( |
| 189 profile_, browser_->window()->GetNativeWindow(), this)); | 217 profile_, browser_->window()->GetNativeWindow(), this)); |
| 190 extension_uninstall_dialog_->ConfirmUninstall(extension); | 218 extension_uninstall_dialog_->ConfirmUninstall(extension); |
| 191 break; | 219 break; |
| 192 } | 220 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 253 // attached to the script injection request icon, but that's okay. | 281 // attached to the script injection request icon, but that's okay. |
| 254 WebContents* web_contents = GetActiveWebContents(); | 282 WebContents* web_contents = GetActiveWebContents(); |
| 255 if (web_contents && | 283 if (web_contents && |
| 256 extensions::ActiveScriptController::GetForWebContents(web_contents) | 284 extensions::ActiveScriptController::GetForWebContents(web_contents) |
| 257 ->HasActiveScriptAction(extension)) { | 285 ->HasActiveScriptAction(extension)) { |
| 258 AddItemWithStringId(ALWAYS_RUN, IDS_EXTENSIONS_ALWAYS_RUN); | 286 AddItemWithStringId(ALWAYS_RUN, IDS_EXTENSIONS_ALWAYS_RUN); |
| 259 } | 287 } |
| 260 | 288 |
| 261 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); | 289 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); |
| 262 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); | 290 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); |
| 263 if (extension_action_manager->GetBrowserAction(*extension)) | 291 |
| 264 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); | 292 // Add a toggle visibility (show/hide) if the extension icon is shown on the |
| 293 // toolbar. | |
| 294 int visibility_string_id = GetVisibilityStringId(profile_, extension); | |
| 295 if (visibility_string_id != -1) | |
| 296 AddItemWithStringId(TOGGLE_VISIBILITY, visibility_string_id); | |
| 297 | |
| 265 AddSeparator(ui::NORMAL_SEPARATOR); | 298 AddSeparator(ui::NORMAL_SEPARATOR); |
| 266 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); | 299 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); |
| 267 } | 300 } |
| 268 | 301 |
| 269 const Extension* ExtensionContextMenuModel::GetExtension() const { | 302 const Extension* ExtensionContextMenuModel::GetExtension() const { |
| 270 return extensions::ExtensionRegistry::Get(profile_) | 303 return extensions::ExtensionRegistry::Get(profile_) |
| 271 ->enabled_extensions() | 304 ->enabled_extensions() |
| 272 .GetByID(extension_id_); | 305 .GetByID(extension_id_); |
| 273 } | 306 } |
| 274 | 307 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 285 extension_items_count_ = 0; | 318 extension_items_count_ = 0; |
| 286 extension_items_->AppendExtensionItems(MenuItem::ExtensionKey(extension_id_), | 319 extension_items_->AppendExtensionItems(MenuItem::ExtensionKey(extension_id_), |
| 287 base::string16(), | 320 base::string16(), |
| 288 &extension_items_count_, | 321 &extension_items_count_, |
| 289 true); // is_action_menu | 322 true); // is_action_menu |
| 290 } | 323 } |
| 291 | 324 |
| 292 content::WebContents* ExtensionContextMenuModel::GetActiveWebContents() const { | 325 content::WebContents* ExtensionContextMenuModel::GetActiveWebContents() const { |
| 293 return browser_->tab_strip_model()->GetActiveWebContents(); | 326 return browser_->tab_strip_model()->GetActiveWebContents(); |
| 294 } | 327 } |
| OLD | NEW |