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/metrics/histogram.h" | |
|
Devlin
2014/07/09 20:07:46
Don't need this.
gpdavis
2014/07/09 22:15:50
Done.
| |
| 7 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/strings/string_util.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/app/chrome_command_ids.h" | |
| 12 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 13 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 14 #include "chrome/browser/extensions/context_menu_matcher.h" | |
| 10 #include "chrome/browser/extensions/extension_action.h" | 15 #include "chrome/browser/extensions/extension_action.h" |
| 11 #include "chrome/browser/extensions/extension_action_manager.h" | 16 #include "chrome/browser/extensions/extension_action_manager.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
|
Devlin
2014/07/09 20:07:47
Don't need this.
gpdavis
2014/07/09 22:15:50
Are you sure? It doesn't compile without it.
Devlin
2014/07/09 22:42:58
Whoops - overly enthusiastic. :) You're right.
gpdavis
2014/07/10 00:22:25
Acknowledged.
| |
| 13 #include "chrome/browser/extensions/extension_tab_util.h" | 18 #include "chrome/browser/extensions/extension_tab_util.h" |
| 19 #include "chrome/browser/extensions/menu_manager.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/chrome_pages.h" | 22 #include "chrome/browser/ui/chrome_pages.h" |
| 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 18 #include "chrome/common/extensions/extension_constants.h" | 24 #include "chrome/common/extensions/extension_constants.h" |
| 19 #include "chrome/common/extensions/manifest_url_handler.h" | 25 #include "chrome/common/extensions/manifest_url_handler.h" |
| 20 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 21 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.h" |
| 22 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 29 #include "content/public/common/context_menu_params.h" | |
| 23 #include "extensions/browser/extension_prefs.h" | 30 #include "extensions/browser/extension_prefs.h" |
| 31 #include "extensions/browser/extension_registry.h" | |
| 24 #include "extensions/browser/extension_system.h" | 32 #include "extensions/browser/extension_system.h" |
| 25 #include "extensions/browser/management_policy.h" | 33 #include "extensions/browser/management_policy.h" |
| 26 #include "extensions/common/extension.h" | 34 #include "extensions/common/extension.h" |
| 27 #include "grit/chromium_strings.h" | 35 #include "grit/chromium_strings.h" |
| 28 #include "grit/generated_resources.h" | 36 #include "grit/generated_resources.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
| 30 | 38 |
| 31 using content::OpenURLParams; | 39 using content::OpenURLParams; |
| 32 using content::Referrer; | 40 using content::Referrer; |
| 33 using content::WebContents; | 41 using content::WebContents; |
| 34 using extensions::Extension; | 42 using extensions::Extension; |
| 43 using extensions::MenuItem; | |
| 44 using extensions::MenuManager; | |
| 45 | |
| 46 namespace { | |
| 47 | |
| 48 // Returns true if the given |item| is for the given |extension| and |type|. | |
| 49 bool MenuItemMatchesExtension( | |
| 50 ExtensionContextMenuModel::ActionType type, | |
| 51 const Extension* extension, | |
| 52 const MenuItem* item) { | |
| 53 if (type == ExtensionContextMenuModel::NO_ACTION || | |
| 54 item->extension_id() != extension->id()) | |
| 55 return false; | |
| 56 | |
| 57 MenuItem::ContextList contexts = item->contexts(); | |
|
Devlin
2014/07/09 20:07:47
Make this a const &.
gpdavis
2014/07/09 22:15:50
Done.
| |
| 58 | |
| 59 if (contexts.Contains(MenuItem::ALL)) | |
| 60 return true; | |
| 61 if (contexts.Contains(MenuItem::PAGE_ACTION) && | |
| 62 (type == ExtensionContextMenuModel::PAGE_ACTION)) | |
| 63 return true; | |
| 64 if (contexts.Contains(MenuItem::BROWSER_ACTION) && | |
| 65 (type == ExtensionContextMenuModel::BROWSER_ACTION)) | |
| 66 return true; | |
| 67 | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 } // namespace | |
| 35 | 72 |
| 36 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, | 73 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
| 37 Browser* browser, | 74 Browser* browser, |
| 38 PopupDelegate* delegate) | 75 PopupDelegate* delegate) |
| 39 : SimpleMenuModel(this), | 76 : SimpleMenuModel(this), |
| 40 extension_id_(extension->id()), | 77 extension_id_(extension->id()), |
| 41 browser_(browser), | 78 browser_(browser), |
| 42 profile_(browser->profile()), | 79 profile_(browser->profile()), |
| 43 delegate_(delegate) { | 80 delegate_(delegate), |
| 81 action_type_(NO_ACTION) { | |
| 44 InitMenu(extension); | 82 InitMenu(extension); |
| 45 | 83 |
| 46 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && | 84 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && |
| 47 delegate_) { | 85 delegate_) { |
| 48 AddSeparator(ui::NORMAL_SEPARATOR); | 86 AddSeparator(ui::NORMAL_SEPARATOR); |
| 49 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); | 87 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); |
| 50 } | 88 } |
| 51 } | 89 } |
| 52 | 90 |
| 53 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, | 91 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
| 54 Browser* browser) | 92 Browser* browser) |
| 55 : SimpleMenuModel(this), | 93 : SimpleMenuModel(this), |
| 56 extension_id_(extension->id()), | 94 extension_id_(extension->id()), |
| 57 browser_(browser), | 95 browser_(browser), |
| 58 profile_(browser->profile()), | 96 profile_(browser->profile()), |
| 59 delegate_(NULL) { | 97 delegate_(NULL), |
| 98 action_type_(NO_ACTION) { | |
| 60 InitMenu(extension); | 99 InitMenu(extension); |
| 61 } | 100 } |
| 62 | 101 |
| 63 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { | 102 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { |
| 103 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | |
| 104 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) | |
| 105 return extension_items_->IsCommandIdChecked(command_id); | |
| 64 return false; | 106 return false; |
| 65 } | 107 } |
| 66 | 108 |
| 67 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { | 109 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { |
| 68 const Extension* extension = this->GetExtension(); | 110 const Extension* extension = this->GetExtension(); |
| 69 if (!extension) | 111 if (!extension) |
| 70 return false; | 112 return false; |
| 71 | 113 |
| 72 if (command_id == CONFIGURE) { | 114 if (command_id == CONFIGURE) { |
| 73 return | 115 return |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 96 int command_id, ui::Accelerator* accelerator) { | 138 int command_id, ui::Accelerator* accelerator) { |
| 97 return false; | 139 return false; |
| 98 } | 140 } |
| 99 | 141 |
| 100 void ExtensionContextMenuModel::ExecuteCommand(int command_id, | 142 void ExtensionContextMenuModel::ExecuteCommand(int command_id, |
| 101 int event_flags) { | 143 int event_flags) { |
| 102 const Extension* extension = GetExtension(); | 144 const Extension* extension = GetExtension(); |
| 103 if (!extension) | 145 if (!extension) |
| 104 return; | 146 return; |
| 105 | 147 |
| 148 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | |
| 149 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) { | |
| 150 WebContents* web_contents = | |
| 151 browser_->tab_strip_model()->GetActiveWebContents(); | |
| 152 DCHECK(extension_items_); | |
| 153 extension_items_->ExecuteCommand(command_id, | |
| 154 web_contents, | |
| 155 content::ContextMenuParams()); | |
| 156 return; | |
| 157 } | |
| 158 | |
| 106 switch (command_id) { | 159 switch (command_id) { |
| 107 case NAME: { | 160 case NAME: { |
| 108 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), | 161 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), |
| 109 Referrer(), NEW_FOREGROUND_TAB, | 162 Referrer(), NEW_FOREGROUND_TAB, |
| 110 content::PAGE_TRANSITION_LINK, false); | 163 content::PAGE_TRANSITION_LINK, false); |
| 111 browser_->OpenURL(params); | 164 browser_->OpenURL(params); |
| 112 break; | 165 break; |
| 113 } | 166 } |
| 114 case CONFIGURE: | 167 case CONFIGURE: |
| 115 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); | 168 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 158 } | 211 } |
| 159 | 212 |
| 160 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} | 213 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} |
| 161 | 214 |
| 162 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { | 215 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { |
| 163 DCHECK(extension); | 216 DCHECK(extension); |
| 164 | 217 |
| 165 extensions::ExtensionActionManager* extension_action_manager = | 218 extensions::ExtensionActionManager* extension_action_manager = |
| 166 extensions::ExtensionActionManager::Get(profile_); | 219 extensions::ExtensionActionManager::Get(profile_); |
| 167 extension_action_ = extension_action_manager->GetBrowserAction(*extension); | 220 extension_action_ = extension_action_manager->GetBrowserAction(*extension); |
| 168 if (!extension_action_) | 221 if (!extension_action_) { |
| 169 extension_action_ = extension_action_manager->GetPageAction(*extension); | 222 extension_action_ = extension_action_manager->GetPageAction(*extension); |
| 223 if (extension_action_) | |
| 224 action_type_ = PAGE_ACTION; | |
| 225 } else { | |
| 226 action_type_ = BROWSER_ACTION; | |
| 227 } | |
| 228 | |
| 229 extension_items_.reset( | |
| 230 new extensions::ContextMenuMatcher(profile_, | |
| 231 this, | |
| 232 this, | |
| 233 base::Bind(MenuItemMatchesExtension, | |
| 234 action_type_, | |
| 235 extension))); | |
| 170 | 236 |
| 171 std::string extension_name = extension->name(); | 237 std::string extension_name = extension->name(); |
| 172 // Ampersands need to be escaped to avoid being treated like | 238 // Ampersands need to be escaped to avoid being treated like |
| 173 // mnemonics in the menu. | 239 // mnemonics in the menu. |
| 174 base::ReplaceChars(extension_name, "&", "&&", &extension_name); | 240 base::ReplaceChars(extension_name, "&", "&&", &extension_name); |
| 175 AddItem(NAME, base::UTF8ToUTF16(extension_name)); | 241 AddItem(NAME, base::UTF8ToUTF16(extension_name)); |
| 242 AppendExtensionItems(); | |
| 176 AddSeparator(ui::NORMAL_SEPARATOR); | 243 AddSeparator(ui::NORMAL_SEPARATOR); |
| 177 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); | 244 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); |
| 178 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); | 245 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); |
| 179 if (extension_action_manager->GetBrowserAction(*extension)) | 246 if (extension_action_manager->GetBrowserAction(*extension)) |
| 180 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); | 247 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); |
| 181 AddSeparator(ui::NORMAL_SEPARATOR); | 248 AddSeparator(ui::NORMAL_SEPARATOR); |
| 182 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); | 249 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); |
| 183 } | 250 } |
| 184 | 251 |
| 185 const Extension* ExtensionContextMenuModel::GetExtension() const { | 252 const Extension* ExtensionContextMenuModel::GetExtension() const { |
| 186 ExtensionService* extension_service = | 253 ExtensionService* extension_service = |
| 187 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 254 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| 188 return extension_service->GetExtensionById(extension_id_, false); | 255 return extension_service->GetExtensionById(extension_id_, false); |
| 189 } | 256 } |
| 257 | |
| 258 void ExtensionContextMenuModel::AppendExtensionItems() { | |
| 259 extension_items_->Clear(); | |
| 260 if (!extensions::ExtensionSystem::Get(profile_)->extension_service()) | |
|
Devlin
2014/07/09 20:07:47
Don't need this? Still?
gpdavis
2014/07/09 22:15:50
Oh shoot. I just realized that the unit test that
| |
| 261 return; // In unit-tests, we may not have an ExtensionService. | |
| 262 | |
| 263 MenuManager* menu_manager = MenuManager::Get(profile_); | |
| 264 if (!menu_manager) | |
| 265 return; | |
| 266 | |
| 267 // Get a list of extension ids that have context menu items, and sort by the | |
| 268 // top level context menu title of the extension. | |
| 269 std::set<MenuItem::ExtensionKey> ids = menu_manager->ExtensionIds(); | |
| 270 std::vector<base::string16> sorted_menu_titles; | |
| 271 std::map<base::string16, std::string> map_ids; | |
| 272 const extensions::ExtensionSet& enabled_extensions = | |
| 273 extensions::ExtensionRegistry::Get(profile_)->enabled_extensions(); | |
| 274 for (std::set<MenuItem::ExtensionKey>::iterator iter = ids.begin(); | |
| 275 iter != ids.end(); | |
| 276 ++iter) { | |
| 277 const Extension* extension = enabled_extensions.GetByID(iter->extension_id); | |
| 278 if (extension) { | |
| 279 base::string16 menu_title = extension_items_->GetTopLevelContextMenuTitle( | |
| 280 *iter, base::EmptyString16()); | |
|
Devlin
2014/07/09 20:07:47
(Again), read the documentation for base::EmptyStr
gpdavis
2014/07/09 22:15:50
Oops. I don't mean to give you the impression tha
| |
| 281 map_ids[menu_title] = iter->extension_id; | |
| 282 sorted_menu_titles.push_back(menu_title); | |
| 283 } | |
| 284 } | |
| 285 if (sorted_menu_titles.empty()) | |
| 286 return; | |
| 287 | |
| 288 AddSeparator(ui::NORMAL_SEPARATOR); | |
| 289 | |
| 290 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | |
| 291 l10n_util::SortStrings16(app_locale, &sorted_menu_titles); | |
| 292 | |
| 293 int index = 0; | |
| 294 base::TimeTicks begin = base::TimeTicks::Now(); | |
|
Devlin
2014/07/09 20:07:47
Why do we need this?
gpdavis
2014/07/09 22:15:50
Negligence on my part. Dead code leftover from th
| |
| 295 for (size_t i = 0; i < sorted_menu_titles.size(); ++i) { | |
|
Devlin
2014/07/09 20:07:47
prefer an iterator.
gpdavis
2014/07/09 22:15:50
Done. Changed in render_view_context_menu as well
| |
| 296 const std::string& id = map_ids[sorted_menu_titles[i]]; | |
| 297 const MenuItem::ExtensionKey extension_key(id); | |
| 298 extension_items_->AppendExtensionItems( | |
| 299 extension_key, base::string16(), &index); | |
| 300 } | |
| 301 } | |
| OLD | NEW |