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" | |
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" |
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" |
23 #include "extensions/browser/extension_prefs.h" | 29 #include "extensions/browser/extension_prefs.h" |
30 #include "extensions/browser/extension_registry.h" | |
24 #include "extensions/browser/extension_system.h" | 31 #include "extensions/browser/extension_system.h" |
25 #include "extensions/browser/management_policy.h" | 32 #include "extensions/browser/management_policy.h" |
26 #include "extensions/common/extension.h" | 33 #include "extensions/common/extension.h" |
27 #include "grit/chromium_strings.h" | 34 #include "grit/chromium_strings.h" |
28 #include "grit/generated_resources.h" | 35 #include "grit/generated_resources.h" |
29 #include "ui/base/l10n/l10n_util.h" | 36 #include "ui/base/l10n/l10n_util.h" |
30 | 37 |
31 using content::OpenURLParams; | 38 using content::OpenURLParams; |
32 using content::Referrer; | 39 using content::Referrer; |
33 using content::WebContents; | 40 using content::WebContents; |
34 using extensions::Extension; | 41 using extensions::Extension; |
42 using extensions::MenuItem; | |
43 using extensions::MenuManager; | |
44 | |
45 namespace { | |
46 | |
47 // Matches menu items with the context menu's extension. | |
Devlin
2014/07/07 20:20:57
What does "matches" mean? How about,
"Returns tru
gpdavis
2014/07/09 02:13:53
Done.
| |
48 bool MenuItemMatchesExtension( | |
49 ExtensionContextMenuModel::ActionType type, | |
50 const Extension* extension, | |
51 const MenuItem* item) { | |
52 if (type == ExtensionContextMenuModel::NO_ACTION || | |
53 item->extension_id() != extension->id()) | |
54 return false; | |
55 | |
56 MenuItem::ContextList contexts = item->contexts(); | |
Devlin
2014/07/07 20:20:58
While you're in the area, would you mind updating
gpdavis
2014/07/09 02:13:53
Done.
| |
57 | |
58 if (contexts.Contains(MenuItem::ALL)) | |
59 return true; | |
60 if (contexts.Contains(MenuItem::PAGE_ACTION) && | |
61 (type == ExtensionContextMenuModel::PAGE_ACTION)) | |
62 return true; | |
63 if (contexts.Contains(MenuItem::BROWSER_ACTION) && | |
64 (type == ExtensionContextMenuModel::BROWSER_ACTION)) | |
65 return true; | |
66 | |
67 return false; | |
68 } | |
69 | |
70 } // namespace | |
35 | 71 |
36 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, | 72 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
37 Browser* browser, | 73 Browser* browser, |
38 PopupDelegate* delegate) | 74 PopupDelegate* delegate) |
39 : SimpleMenuModel(this), | 75 : SimpleMenuModel(this), |
40 extension_id_(extension->id()), | 76 extension_id_(extension->id()), |
41 browser_(browser), | 77 browser_(browser), |
42 profile_(browser->profile()), | 78 profile_(browser->profile()), |
43 delegate_(delegate) { | 79 delegate_(delegate), |
80 action_type_(NO_ACTION) { | |
44 InitMenu(extension); | 81 InitMenu(extension); |
45 | 82 |
46 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && | 83 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && |
47 delegate_) { | 84 delegate_) { |
48 AddSeparator(ui::NORMAL_SEPARATOR); | 85 AddSeparator(ui::NORMAL_SEPARATOR); |
49 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); | 86 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); |
50 } | 87 } |
51 } | 88 } |
52 | 89 |
53 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, | 90 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
54 Browser* browser) | 91 Browser* browser) |
55 : SimpleMenuModel(this), | 92 : SimpleMenuModel(this), |
56 extension_id_(extension->id()), | 93 extension_id_(extension->id()), |
57 browser_(browser), | 94 browser_(browser), |
58 profile_(browser->profile()), | 95 profile_(browser->profile()), |
59 delegate_(NULL) { | 96 delegate_(NULL), |
97 action_type_(NO_ACTION) { | |
60 InitMenu(extension); | 98 InitMenu(extension); |
61 } | 99 } |
62 | 100 |
63 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { | 101 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { |
102 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | |
103 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) | |
104 return extension_items_->IsCommandIdChecked(command_id); | |
64 return false; | 105 return false; |
65 } | 106 } |
66 | 107 |
67 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { | 108 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { |
68 const Extension* extension = this->GetExtension(); | 109 const Extension* extension = this->GetExtension(); |
69 if (!extension) | 110 if (!extension) |
70 return false; | 111 return false; |
71 | 112 |
72 if (command_id == CONFIGURE) { | 113 if (command_id == CONFIGURE) { |
73 return | 114 return |
(...skipping 22 matching lines...) Expand all Loading... | |
96 int command_id, ui::Accelerator* accelerator) { | 137 int command_id, ui::Accelerator* accelerator) { |
97 return false; | 138 return false; |
98 } | 139 } |
99 | 140 |
100 void ExtensionContextMenuModel::ExecuteCommand(int command_id, | 141 void ExtensionContextMenuModel::ExecuteCommand(int command_id, |
101 int event_flags) { | 142 int event_flags) { |
102 const Extension* extension = GetExtension(); | 143 const Extension* extension = GetExtension(); |
103 if (!extension) | 144 if (!extension) |
104 return; | 145 return; |
105 | 146 |
147 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | |
148 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) { | |
149 WebContents* web_contents = | |
150 browser_->tab_strip_model()->GetActiveWebContents(); | |
151 DCHECK(extension_items_); | |
152 extension_items_->ExecuteCommand(command_id, | |
153 web_contents, | |
154 content::ContextMenuParams()); | |
155 return; | |
156 } | |
157 | |
106 switch (command_id) { | 158 switch (command_id) { |
107 case NAME: { | 159 case NAME: { |
108 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), | 160 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), |
109 Referrer(), NEW_FOREGROUND_TAB, | 161 Referrer(), NEW_FOREGROUND_TAB, |
110 content::PAGE_TRANSITION_LINK, false); | 162 content::PAGE_TRANSITION_LINK, false); |
111 browser_->OpenURL(params); | 163 browser_->OpenURL(params); |
112 break; | 164 break; |
113 } | 165 } |
114 case CONFIGURE: | 166 case CONFIGURE: |
115 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); | 167 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 } | 207 } |
156 | 208 |
157 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} | 209 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} |
158 | 210 |
159 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { | 211 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { |
160 DCHECK(extension); | 212 DCHECK(extension); |
161 | 213 |
162 extensions::ExtensionActionManager* extension_action_manager = | 214 extensions::ExtensionActionManager* extension_action_manager = |
163 extensions::ExtensionActionManager::Get(profile_); | 215 extensions::ExtensionActionManager::Get(profile_); |
164 extension_action_ = extension_action_manager->GetBrowserAction(*extension); | 216 extension_action_ = extension_action_manager->GetBrowserAction(*extension); |
165 if (!extension_action_) | 217 if (!extension_action_) { |
166 extension_action_ = extension_action_manager->GetPageAction(*extension); | 218 extension_action_ = extension_action_manager->GetPageAction(*extension); |
219 if (extension_action_) | |
220 action_type_ = PAGE_ACTION; | |
221 } else { | |
222 action_type_ = BROWSER_ACTION; | |
223 } | |
224 | |
225 extension_items_.reset( | |
226 new extensions::ContextMenuMatcher(profile_, | |
227 this, | |
228 this, | |
229 base::Bind(MenuItemMatchesExtension, | |
230 action_type_, | |
231 extension))); | |
167 | 232 |
168 std::string extension_name = extension->name(); | 233 std::string extension_name = extension->name(); |
169 // Ampersands need to be escaped to avoid being treated like | 234 // Ampersands need to be escaped to avoid being treated like |
170 // mnemonics in the menu. | 235 // mnemonics in the menu. |
171 base::ReplaceChars(extension_name, "&", "&&", &extension_name); | 236 base::ReplaceChars(extension_name, "&", "&&", &extension_name); |
172 AddItem(NAME, base::UTF8ToUTF16(extension_name)); | 237 AddItem(NAME, base::UTF8ToUTF16(extension_name)); |
238 AppendExtensionItems(); | |
173 AddSeparator(ui::NORMAL_SEPARATOR); | 239 AddSeparator(ui::NORMAL_SEPARATOR); |
174 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); | 240 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); |
175 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); | 241 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); |
176 if (extension_action_manager->GetBrowserAction(*extension)) | 242 if (extension_action_manager->GetBrowserAction(*extension)) |
177 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); | 243 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); |
178 AddSeparator(ui::NORMAL_SEPARATOR); | 244 AddSeparator(ui::NORMAL_SEPARATOR); |
179 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); | 245 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); |
180 } | 246 } |
181 | 247 |
182 const Extension* ExtensionContextMenuModel::GetExtension() const { | 248 const Extension* ExtensionContextMenuModel::GetExtension() const { |
183 ExtensionService* extension_service = | 249 ExtensionService* extension_service = |
184 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 250 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
185 return extension_service->GetExtensionById(extension_id_, false); | 251 return extension_service->GetExtensionById(extension_id_, false); |
186 } | 252 } |
253 | |
254 void ExtensionContextMenuModel::AppendExtensionItems() { | |
255 extension_items_->Clear(); | |
256 ExtensionService* service = | |
Devlin
2014/07/07 20:20:57
We don't use this anymore, right?
gpdavis
2014/07/09 02:13:54
Negative. I just put the method call into the con
| |
257 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
258 if (!service) | |
259 return; // In unit-tests, we may not have an ExtensionService. | |
260 | |
261 MenuManager* menu_manager = MenuManager::Get(profile_); | |
262 if (!menu_manager) | |
263 return; | |
264 | |
265 // Get a list of extension ids that have context menu items, and sort by the | |
266 // top level context menu title of the extension. | |
267 std::set<MenuItem::ExtensionKey> ids = menu_manager->ExtensionIds(); | |
268 std::vector<base::string16> sorted_menu_titles; | |
269 std::map<base::string16, std::string> map_ids; | |
270 for (std::set<MenuItem::ExtensionKey>::iterator iter = ids.begin(); | |
271 iter != ids.end(); | |
272 ++iter) { | |
273 const Extension* extension = extensions::ExtensionRegistry::Get( | |
Devlin
2014/07/07 20:20:57
cache ExtensionRegistry, or better yet, enabled_ex
gpdavis
2014/07/09 02:13:54
Done.
| |
274 profile_)->enabled_extensions().GetByID(iter->extension_id); | |
275 if (extension) { | |
276 base::string16 menu_title = extension_items_->GetTopLevelContextMenuTitle( | |
277 *iter, base::EmptyString16()); | |
278 map_ids[menu_title] = iter->extension_id; | |
279 sorted_menu_titles.push_back(menu_title); | |
280 } | |
281 } | |
282 if (sorted_menu_titles.empty()) | |
283 return; | |
284 | |
285 AddSeparator(ui::NORMAL_SEPARATOR); | |
286 | |
287 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | |
288 l10n_util::SortStrings16(app_locale, &sorted_menu_titles); | |
289 | |
290 int index = 0; | |
291 base::TimeTicks begin = base::TimeTicks::Now(); | |
292 for (size_t i = 0; i < sorted_menu_titles.size(); ++i) { | |
293 const std::string& id = map_ids[sorted_menu_titles[i]]; | |
294 const MenuItem::ExtensionKey extension_key(id); | |
295 extension_items_->AppendExtensionItems( | |
296 extension_key, base::string16(), &index); | |
297 } | |
298 | |
299 UMA_HISTOGRAM_TIMES("Extensions.ActionContextMenus_BuildTime", | |
300 base::TimeTicks::Now() - begin); | |
301 UMA_HISTOGRAM_COUNTS("Extensions.ActionContextMenus_ItemCount", index); | |
302 } | |
OLD | NEW |