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