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