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/browser_process.h" | |
9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 11 #include "chrome/browser/extensions/api/extension_action/extension_action_api.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" |
14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/chrome_pages.h" | 18 #include "chrome/browser/ui/chrome_pages.h" |
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
18 #include "chrome/common/extensions/extension_constants.h" | 20 #include "chrome/common/extensions/extension_constants.h" |
19 #include "chrome/common/extensions/manifest_url_handler.h" | 21 #include "chrome/common/extensions/manifest_url_handler.h" |
20 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
21 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
22 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
25 #include "content/public/common/context_menu_params.h" | |
23 #include "extensions/browser/extension_prefs.h" | 26 #include "extensions/browser/extension_prefs.h" |
24 #include "extensions/browser/extension_system.h" | 27 #include "extensions/browser/extension_system.h" |
25 #include "extensions/browser/management_policy.h" | 28 #include "extensions/browser/management_policy.h" |
26 #include "extensions/common/extension.h" | 29 #include "extensions/common/extension.h" |
27 #include "grit/chromium_strings.h" | 30 #include "grit/chromium_strings.h" |
28 #include "grit/generated_resources.h" | 31 #include "grit/generated_resources.h" |
29 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
33 #include "ui/gfx/text_elider.h" | |
30 | 34 |
31 using content::OpenURLParams; | 35 using content::OpenURLParams; |
32 using content::Referrer; | 36 using content::Referrer; |
33 using content::WebContents; | 37 using content::WebContents; |
34 using extensions::Extension; | 38 using extensions::Extension; |
39 using extensions::MenuItem; | |
40 using extensions::MenuManager; | |
35 | 41 |
36 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, | 42 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
37 Browser* browser, | 43 Browser* browser, |
38 PopupDelegate* delegate) | 44 PopupDelegate* delegate) |
39 : SimpleMenuModel(this), | 45 : SimpleMenuModel(this), |
40 extension_id_(extension->id()), | 46 extension_id_(extension->id()), |
41 browser_(browser), | 47 browser_(browser), |
42 profile_(browser->profile()), | 48 profile_(browser->profile()), |
43 delegate_(delegate) { | 49 delegate_(delegate), |
50 extension_items_(profile_, | |
51 this, | |
52 this, | |
53 base::Bind(MenuItemMatchesParams)) { | |
44 InitMenu(extension); | 54 InitMenu(extension); |
45 | 55 |
46 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && | 56 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && |
47 delegate_) { | 57 delegate_) { |
48 AddSeparator(ui::NORMAL_SEPARATOR); | 58 AddSeparator(ui::NORMAL_SEPARATOR); |
49 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); | 59 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); |
50 } | 60 } |
51 } | 61 } |
52 | 62 |
53 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, | 63 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
54 Browser* browser) | 64 Browser* browser) |
55 : SimpleMenuModel(this), | 65 : SimpleMenuModel(this), |
56 extension_id_(extension->id()), | 66 extension_id_(extension->id()), |
57 browser_(browser), | 67 browser_(browser), |
58 profile_(browser->profile()), | 68 profile_(browser->profile()), |
59 delegate_(NULL) { | 69 delegate_(NULL), |
70 extension_items_(profile_, | |
71 this, | |
72 this, | |
73 base::Bind(MenuItemMatchesParams)) { | |
60 InitMenu(extension); | 74 InitMenu(extension); |
61 } | 75 } |
62 | 76 |
63 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { | 77 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { |
78 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | |
79 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) | |
80 return extension_items_.IsCommandIdChecked(command_id); | |
64 return false; | 81 return false; |
65 } | 82 } |
66 | 83 |
67 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { | 84 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { |
68 const Extension* extension = this->GetExtension(); | 85 const Extension* extension = this->GetExtension(); |
69 if (!extension) | 86 if (!extension) |
70 return false; | 87 return false; |
71 | 88 |
72 if (command_id == CONFIGURE) { | 89 if (command_id == CONFIGURE) { |
73 return | 90 return |
(...skipping 22 matching lines...) Expand all Loading... | |
96 int command_id, ui::Accelerator* accelerator) { | 113 int command_id, ui::Accelerator* accelerator) { |
97 return false; | 114 return false; |
98 } | 115 } |
99 | 116 |
100 void ExtensionContextMenuModel::ExecuteCommand(int command_id, | 117 void ExtensionContextMenuModel::ExecuteCommand(int command_id, |
101 int event_flags) { | 118 int event_flags) { |
102 const Extension* extension = GetExtension(); | 119 const Extension* extension = GetExtension(); |
103 if (!extension) | 120 if (!extension) |
104 return; | 121 return; |
105 | 122 |
123 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | |
124 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) { | |
125 WebContents* web_contents = | |
126 browser_->tab_strip_model()->GetActiveWebContents(); | |
127 content::ContextMenuParams *params = new content::ContextMenuParams(); | |
128 extension_items_.ExecuteCommand(command_id, web_contents, *params); | |
gpdavis
2014/06/26 18:16:02
I had to remove params when I was porting over all
| |
129 return; | |
130 } | |
131 | |
106 switch (command_id) { | 132 switch (command_id) { |
107 case NAME: { | 133 case NAME: { |
108 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), | 134 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), |
109 Referrer(), NEW_FOREGROUND_TAB, | 135 Referrer(), NEW_FOREGROUND_TAB, |
110 content::PAGE_TRANSITION_LINK, false); | 136 content::PAGE_TRANSITION_LINK, false); |
111 browser_->OpenURL(params); | 137 browser_->OpenURL(params); |
112 break; | 138 break; |
113 } | 139 } |
114 case CONFIGURE: | 140 case CONFIGURE: |
115 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); | 141 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 // mnemonics in the menu. | 196 // mnemonics in the menu. |
171 base::ReplaceChars(extension_name, "&", "&&", &extension_name); | 197 base::ReplaceChars(extension_name, "&", "&&", &extension_name); |
172 AddItem(NAME, base::UTF8ToUTF16(extension_name)); | 198 AddItem(NAME, base::UTF8ToUTF16(extension_name)); |
173 AddSeparator(ui::NORMAL_SEPARATOR); | 199 AddSeparator(ui::NORMAL_SEPARATOR); |
174 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); | 200 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); |
175 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); | 201 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); |
176 if (extension_action_manager->GetBrowserAction(*extension)) | 202 if (extension_action_manager->GetBrowserAction(*extension)) |
177 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); | 203 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); |
178 AddSeparator(ui::NORMAL_SEPARATOR); | 204 AddSeparator(ui::NORMAL_SEPARATOR); |
179 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); | 205 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); |
206 AppendExtensionItems(); | |
180 } | 207 } |
181 | 208 |
182 const Extension* ExtensionContextMenuModel::GetExtension() const { | 209 const Extension* ExtensionContextMenuModel::GetExtension() const { |
183 ExtensionService* extension_service = | 210 ExtensionService* extension_service = |
184 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 211 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
185 return extension_service->GetExtensionById(extension_id_, false); | 212 return extension_service->GetExtensionById(extension_id_, false); |
186 } | 213 } |
214 | |
215 void ExtensionContextMenuModel::AppendExtensionItems() { | |
216 LOG(WARNING) << "AppendExtensionItems"; | |
217 extension_items_.Clear(); | |
218 ExtensionService* service = | |
219 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
220 if (!service) | |
221 return; // In unit-tests, we may not have an ExtensionService. | |
222 | |
223 MenuManager* menu_manager = MenuManager::Get(profile_); | |
224 if (!menu_manager) | |
225 return; | |
226 | |
227 base::string16 *null_string = new base::string16(); | |
228 | |
229 // Get a list of extension id's that have context menu items, and sort by the | |
230 // top level context menu title of the extension. | |
231 std::set<MenuItem::ExtensionKey> ids = menu_manager->ExtensionIds(); | |
232 std::vector<base::string16> sorted_menu_titles; | |
233 std::map<base::string16, std::string> map_ids; | |
234 for (std::set<MenuItem::ExtensionKey>::iterator i = ids.begin(); | |
235 i != ids.end(); | |
236 ++i) { | |
237 const Extension* extension = | |
238 service->GetExtensionById(i->extension_id, false); | |
239 // Platform apps have their context menus created directly in | |
240 // AppendPlatformAppItems. | |
241 if (extension && !extension->is_platform_app()) { | |
242 base::string16 menu_title = extension_items_.GetTopLevelContextMenuTitle( | |
243 *i, *null_string); | |
244 map_ids[menu_title] = i->extension_id; | |
245 sorted_menu_titles.push_back(menu_title); | |
246 } | |
247 } | |
248 if (sorted_menu_titles.empty()) | |
249 return; | |
250 | |
251 const std::string app_locale = g_browser_process->GetApplicationLocale(); | |
252 l10n_util::SortStrings16(app_locale, &sorted_menu_titles); | |
253 | |
254 int index = 0; | |
255 // base::TimeTicks begin = base::TimeTicks::Now(); | |
256 for (size_t i = 0; i < sorted_menu_titles.size(); ++i) { | |
257 LOG(WARNING) << "LOOP " << sorted_menu_titles[i]; | |
258 const std::string& id = map_ids[sorted_menu_titles[i]]; | |
259 const MenuItem::ExtensionKey extension_key(id); | |
260 extension_items_.AppendExtensionItems( | |
261 extension_key, *null_string, &index); | |
262 } | |
263 LOG(WARNING) << "COMPLETE"; | |
264 /* | |
265 UMA_HISTOGRAM_TIMES("Extensions.ContextMenus_BuildTime", | |
266 base::TimeTicks::Now() - begin); | |
267 UMA_HISTOGRAM_COUNTS("Extensions.ContextMenus_ItemCount", index); | |
gpdavis
2014/06/26 18:16:02
I wanted to get some clarification on that to make
| |
268 */ | |
269 } | |
270 | |
271 bool ExtensionContextMenuModel::MenuItemMatchesParams( | |
272 const extensions::MenuItem* item) { | |
273 // MenuItem::ContextList contexts = item->contexts(); | |
274 | |
275 // if (contexts.Contains(MenuItem::ALL) | |
276 // || context.Contains(MenuItem::BROWSERACTION) | |
277 // ) | |
278 return true; | |
gpdavis
2014/06/26 18:16:02
I haven't added a browser_context string yet-- ins
| |
279 | |
280 return false; | |
281 } | |
282 | |
283 // Helper function to escape "&" as "&&". | |
284 void ExtensionContextMenuModel::EscapeAmpersands(base::string16* text) { | |
285 base::ReplaceChars(*text, base::ASCIIToUTF16("&"), base::ASCIIToUTF16("&&"), | |
286 text); | |
287 } | |
OLD | NEW |