| 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" | |
| 10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 11 #include "chrome/browser/extensions/context_menu_matcher.h" | |
| 12 #include "chrome/browser/extensions/extension_action.h" | 10 #include "chrome/browser/extensions/extension_action.h" |
| 13 #include "chrome/browser/extensions/extension_action_manager.h" | 11 #include "chrome/browser/extensions/extension_action_manager.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/extensions/extension_tab_util.h" | 13 #include "chrome/browser/extensions/extension_tab_util.h" |
| 16 #include "chrome/browser/extensions/menu_manager.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 19 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
| 20 #include "chrome/browser/ui/chrome_pages.h" | 17 #include "chrome/browser/ui/chrome_pages.h" |
| 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 22 #include "chrome/common/extensions/extension_constants.h" | 19 #include "chrome/common/extensions/extension_constants.h" |
| 23 #include "chrome/common/extensions/manifest_url_handler.h" | 20 #include "chrome/common/extensions/manifest_url_handler.h" |
| 24 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 25 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
| 26 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 27 #include "content/public/common/context_menu_params.h" | |
| 28 #include "extensions/browser/extension_prefs.h" | 24 #include "extensions/browser/extension_prefs.h" |
| 29 #include "extensions/browser/extension_registry.h" | |
| 30 #include "extensions/browser/extension_system.h" | 25 #include "extensions/browser/extension_system.h" |
| 31 #include "extensions/browser/management_policy.h" | 26 #include "extensions/browser/management_policy.h" |
| 32 #include "extensions/browser/uninstall_reason.h" | 27 #include "extensions/browser/uninstall_reason.h" |
| 33 #include "extensions/common/extension.h" | 28 #include "extensions/common/extension.h" |
| 34 #include "grit/chromium_strings.h" | 29 #include "grit/chromium_strings.h" |
| 35 #include "grit/generated_resources.h" | 30 #include "grit/generated_resources.h" |
| 36 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 37 | 32 |
| 38 using content::OpenURLParams; | 33 using content::OpenURLParams; |
| 39 using content::Referrer; | 34 using content::Referrer; |
| 40 using content::WebContents; | 35 using content::WebContents; |
| 41 using extensions::Extension; | 36 using extensions::Extension; |
| 42 using extensions::MenuItem; | |
| 43 using extensions::MenuManager; | |
| 44 | |
| 45 namespace { | |
| 46 | |
| 47 // Returns true if the given |item| is of the given |type|. | |
| 48 bool MenuItemMatchesAction(ExtensionContextMenuModel::ActionType type, | |
| 49 const MenuItem* item) { | |
| 50 if (type == ExtensionContextMenuModel::NO_ACTION) | |
| 51 return false; | |
| 52 | |
| 53 const MenuItem::ContextList& contexts = item->contexts(); | |
| 54 | |
| 55 if (contexts.Contains(MenuItem::ALL)) | |
| 56 return true; | |
| 57 if (contexts.Contains(MenuItem::PAGE_ACTION) && | |
| 58 (type == ExtensionContextMenuModel::PAGE_ACTION)) | |
| 59 return true; | |
| 60 if (contexts.Contains(MenuItem::BROWSER_ACTION) && | |
| 61 (type == ExtensionContextMenuModel::BROWSER_ACTION)) | |
| 62 return true; | |
| 63 | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 } // namespace | |
| 68 | 37 |
| 69 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, | 38 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
| 70 Browser* browser, | 39 Browser* browser, |
| 71 PopupDelegate* delegate) | 40 PopupDelegate* delegate) |
| 72 : SimpleMenuModel(this), | 41 : SimpleMenuModel(this), |
| 73 extension_id_(extension->id()), | 42 extension_id_(extension->id()), |
| 74 browser_(browser), | 43 browser_(browser), |
| 75 profile_(browser->profile()), | 44 profile_(browser->profile()), |
| 76 delegate_(delegate), | 45 delegate_(delegate) { |
| 77 action_type_(NO_ACTION), | |
| 78 extension_items_count_(0) { | |
| 79 InitMenu(extension); | 46 InitMenu(extension); |
| 80 | 47 |
| 81 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && | 48 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && |
| 82 delegate_) { | 49 delegate_) { |
| 83 AddSeparator(ui::NORMAL_SEPARATOR); | 50 AddSeparator(ui::NORMAL_SEPARATOR); |
| 84 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); | 51 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); |
| 85 } | 52 } |
| 86 } | 53 } |
| 87 | 54 |
| 88 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, | 55 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
| 89 Browser* browser) | 56 Browser* browser) |
| 90 : SimpleMenuModel(this), | 57 : SimpleMenuModel(this), |
| 91 extension_id_(extension->id()), | 58 extension_id_(extension->id()), |
| 92 browser_(browser), | 59 browser_(browser), |
| 93 profile_(browser->profile()), | 60 profile_(browser->profile()), |
| 94 delegate_(NULL), | 61 delegate_(NULL) { |
| 95 action_type_(NO_ACTION), | |
| 96 extension_items_count_(0) { | |
| 97 InitMenu(extension); | 62 InitMenu(extension); |
| 98 } | 63 } |
| 99 | 64 |
| 100 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { | 65 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { |
| 101 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | |
| 102 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) | |
| 103 return extension_items_->IsCommandIdChecked(command_id); | |
| 104 return false; | 66 return false; |
| 105 } | 67 } |
| 106 | 68 |
| 107 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { | 69 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { |
| 108 const Extension* extension = GetExtension(); | 70 const Extension* extension = this->GetExtension(); |
| 109 if (!extension) | 71 if (!extension) |
| 110 return false; | 72 return false; |
| 111 | 73 |
| 112 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | 74 if (command_id == CONFIGURE) { |
| 113 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) { | |
| 114 return extension_items_->IsCommandIdEnabled(command_id); | |
| 115 } else if (command_id == CONFIGURE) { | |
| 116 return | 75 return |
| 117 extensions::ManifestURL::GetOptionsPage(extension).spec().length() > 0; | 76 extensions::ManifestURL::GetOptionsPage(extension).spec().length() > 0; |
| 118 } else if (command_id == NAME) { | 77 } else if (command_id == NAME) { |
| 119 // The NAME links to the Homepage URL. If the extension doesn't have a | 78 // The NAME links to the Homepage URL. If the extension doesn't have a |
| 120 // homepage, we just disable this menu item. | 79 // homepage, we just disable this menu item. |
| 121 return extensions::ManifestURL::GetHomepageURL(extension).is_valid(); | 80 return extensions::ManifestURL::GetHomepageURL(extension).is_valid(); |
| 122 } else if (command_id == INSPECT_POPUP) { | 81 } else if (command_id == INSPECT_POPUP) { |
| 123 WebContents* web_contents = | 82 WebContents* web_contents = |
| 124 browser_->tab_strip_model()->GetActiveWebContents(); | 83 browser_->tab_strip_model()->GetActiveWebContents(); |
| 125 if (!web_contents) | 84 if (!web_contents) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 139 int command_id, ui::Accelerator* accelerator) { | 98 int command_id, ui::Accelerator* accelerator) { |
| 140 return false; | 99 return false; |
| 141 } | 100 } |
| 142 | 101 |
| 143 void ExtensionContextMenuModel::ExecuteCommand(int command_id, | 102 void ExtensionContextMenuModel::ExecuteCommand(int command_id, |
| 144 int event_flags) { | 103 int event_flags) { |
| 145 const Extension* extension = GetExtension(); | 104 const Extension* extension = GetExtension(); |
| 146 if (!extension) | 105 if (!extension) |
| 147 return; | 106 return; |
| 148 | 107 |
| 149 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | |
| 150 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) { | |
| 151 WebContents* web_contents = | |
| 152 browser_->tab_strip_model()->GetActiveWebContents(); | |
| 153 DCHECK(extension_items_); | |
| 154 extension_items_->ExecuteCommand( | |
| 155 command_id, web_contents, content::ContextMenuParams()); | |
| 156 return; | |
| 157 } | |
| 158 | |
| 159 switch (command_id) { | 108 switch (command_id) { |
| 160 case NAME: { | 109 case NAME: { |
| 161 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), | 110 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), |
| 162 Referrer(), NEW_FOREGROUND_TAB, | 111 Referrer(), NEW_FOREGROUND_TAB, |
| 163 content::PAGE_TRANSITION_LINK, false); | 112 content::PAGE_TRANSITION_LINK, false); |
| 164 browser_->OpenURL(params); | 113 browser_->OpenURL(params); |
| 165 break; | 114 break; |
| 166 } | 115 } |
| 167 case CONFIGURE: | 116 case CONFIGURE: |
| 168 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); | 117 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 } | 161 } |
| 213 | 162 |
| 214 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} | 163 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} |
| 215 | 164 |
| 216 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { | 165 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { |
| 217 DCHECK(extension); | 166 DCHECK(extension); |
| 218 | 167 |
| 219 extensions::ExtensionActionManager* extension_action_manager = | 168 extensions::ExtensionActionManager* extension_action_manager = |
| 220 extensions::ExtensionActionManager::Get(profile_); | 169 extensions::ExtensionActionManager::Get(profile_); |
| 221 extension_action_ = extension_action_manager->GetBrowserAction(*extension); | 170 extension_action_ = extension_action_manager->GetBrowserAction(*extension); |
| 222 if (!extension_action_) { | 171 if (!extension_action_) |
| 223 extension_action_ = extension_action_manager->GetPageAction(*extension); | 172 extension_action_ = extension_action_manager->GetPageAction(*extension); |
| 224 if (extension_action_) | |
| 225 action_type_ = PAGE_ACTION; | |
| 226 } else { | |
| 227 action_type_ = BROWSER_ACTION; | |
| 228 } | |
| 229 | |
| 230 extension_items_.reset(new extensions::ContextMenuMatcher( | |
| 231 profile_, | |
| 232 this, | |
| 233 this, | |
| 234 base::Bind(MenuItemMatchesAction, action_type_))); | |
| 235 | 173 |
| 236 std::string extension_name = extension->name(); | 174 std::string extension_name = extension->name(); |
| 237 // Ampersands need to be escaped to avoid being treated like | 175 // Ampersands need to be escaped to avoid being treated like |
| 238 // mnemonics in the menu. | 176 // mnemonics in the menu. |
| 239 base::ReplaceChars(extension_name, "&", "&&", &extension_name); | 177 base::ReplaceChars(extension_name, "&", "&&", &extension_name); |
| 240 AddItem(NAME, base::UTF8ToUTF16(extension_name)); | 178 AddItem(NAME, base::UTF8ToUTF16(extension_name)); |
| 241 AppendExtensionItems(); | |
| 242 AddSeparator(ui::NORMAL_SEPARATOR); | 179 AddSeparator(ui::NORMAL_SEPARATOR); |
| 243 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); | 180 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); |
| 244 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); | 181 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); |
| 245 if (extension_action_manager->GetBrowserAction(*extension)) | 182 if (extension_action_manager->GetBrowserAction(*extension)) |
| 246 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); | 183 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); |
| 247 AddSeparator(ui::NORMAL_SEPARATOR); | 184 AddSeparator(ui::NORMAL_SEPARATOR); |
| 248 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); | 185 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); |
| 249 } | 186 } |
| 250 | 187 |
| 251 const Extension* ExtensionContextMenuModel::GetExtension() const { | 188 const Extension* ExtensionContextMenuModel::GetExtension() const { |
| 252 return extensions::ExtensionRegistry::Get(profile_) | 189 ExtensionService* extension_service = |
| 253 ->enabled_extensions() | 190 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| 254 .GetByID(extension_id_); | 191 return extension_service->GetExtensionById(extension_id_, false); |
| 255 } | 192 } |
| 256 | |
| 257 void ExtensionContextMenuModel::AppendExtensionItems() { | |
| 258 extension_items_->Clear(); | |
| 259 | |
| 260 MenuManager* menu_manager = MenuManager::Get(profile_); | |
| 261 if (!menu_manager || | |
| 262 !menu_manager->MenuItems(MenuItem::ExtensionKey(extension_id_))) | |
| 263 return; | |
| 264 | |
| 265 AddSeparator(ui::NORMAL_SEPARATOR); | |
| 266 | |
| 267 extension_items_count_ = 0; | |
| 268 extension_items_->AppendExtensionItems(MenuItem::ExtensionKey(extension_id_), | |
| 269 base::string16(), | |
| 270 &extension_items_count_, | |
| 271 true); // is_action_menu | |
| 272 } | |
| OLD | NEW |