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/string_util.h" | |
Devlin
2014/07/09 22:42:59
Pretty sure you don't need this.
gpdavis
2014/07/10 00:22:25
Done.
| |
8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/app/chrome_command_ids.h" | |
11 #include "chrome/browser/browser_process.h" | |
9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 12 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
13 #include "chrome/browser/extensions/context_menu_matcher.h" | |
10 #include "chrome/browser/extensions/extension_action.h" | 14 #include "chrome/browser/extensions/extension_action.h" |
11 #include "chrome/browser/extensions/extension_action_manager.h" | 15 #include "chrome/browser/extensions/extension_action_manager.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/extensions/extension_tab_util.h" | 17 #include "chrome/browser/extensions/extension_tab_util.h" |
18 #include "chrome/browser/extensions/menu_manager.h" | |
14 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/chrome_pages.h" | 21 #include "chrome/browser/ui/chrome_pages.h" |
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
18 #include "chrome/common/extensions/extension_constants.h" | 23 #include "chrome/common/extensions/extension_constants.h" |
19 #include "chrome/common/extensions/manifest_url_handler.h" | 24 #include "chrome/common/extensions/manifest_url_handler.h" |
20 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
21 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
22 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
28 #include "content/public/common/context_menu_params.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 // Returns true if the given |item| is for the given |extension| and |type|. | |
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 const MenuItem::ContextList& contexts = item->contexts(); | |
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(); |
Devlin
2014/07/09 22:42:59
also not you're code... but take out the "this" he
gpdavis
2014/07/10 00:22:25
Done.
| |
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 |
74 extensions::ManifestURL::GetOptionsPage(extension).spec().length() > 0; | 115 extensions::ManifestURL::GetOptionsPage(extension).spec().length() > 0; |
75 } else if (command_id == NAME) { | 116 } else if (command_id == NAME) { |
76 // The NAME links to the Homepage URL. If the extension doesn't have a | 117 // The NAME links to the Homepage URL. If the extension doesn't have a |
77 // homepage, we just disable this menu item. | 118 // homepage, we just disable this menu item. |
78 return extensions::ManifestURL::GetHomepageURL(extension).is_valid(); | 119 return extensions::ManifestURL::GetHomepageURL(extension).is_valid(); |
(...skipping 17 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 } | 210 } |
159 | 211 |
160 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} | 212 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} |
161 | 213 |
162 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { | 214 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { |
163 DCHECK(extension); | 215 DCHECK(extension); |
164 | 216 |
165 extensions::ExtensionActionManager* extension_action_manager = | 217 extensions::ExtensionActionManager* extension_action_manager = |
166 extensions::ExtensionActionManager::Get(profile_); | 218 extensions::ExtensionActionManager::Get(profile_); |
167 extension_action_ = extension_action_manager->GetBrowserAction(*extension); | 219 extension_action_ = extension_action_manager->GetBrowserAction(*extension); |
168 if (!extension_action_) | 220 if (!extension_action_) { |
169 extension_action_ = extension_action_manager->GetPageAction(*extension); | 221 extension_action_ = extension_action_manager->GetPageAction(*extension); |
222 if (extension_action_) | |
223 action_type_ = PAGE_ACTION; | |
224 } else { | |
225 action_type_ = BROWSER_ACTION; | |
226 } | |
227 | |
228 extension_items_.reset( | |
229 new extensions::ContextMenuMatcher(profile_, | |
230 this, | |
231 this, | |
232 base::Bind(MenuItemMatchesExtension, | |
233 action_type_, | |
234 extension))); | |
170 | 235 |
171 std::string extension_name = extension->name(); | 236 std::string extension_name = extension->name(); |
172 // Ampersands need to be escaped to avoid being treated like | 237 // Ampersands need to be escaped to avoid being treated like |
173 // mnemonics in the menu. | 238 // mnemonics in the menu. |
174 base::ReplaceChars(extension_name, "&", "&&", &extension_name); | 239 base::ReplaceChars(extension_name, "&", "&&", &extension_name); |
175 AddItem(NAME, base::UTF8ToUTF16(extension_name)); | 240 AddItem(NAME, base::UTF8ToUTF16(extension_name)); |
241 AppendExtensionItems(); | |
176 AddSeparator(ui::NORMAL_SEPARATOR); | 242 AddSeparator(ui::NORMAL_SEPARATOR); |
177 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); | 243 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); |
178 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); | 244 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); |
179 if (extension_action_manager->GetBrowserAction(*extension)) | 245 if (extension_action_manager->GetBrowserAction(*extension)) |
180 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); | 246 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); |
181 AddSeparator(ui::NORMAL_SEPARATOR); | 247 AddSeparator(ui::NORMAL_SEPARATOR); |
182 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); | 248 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); |
183 } | 249 } |
184 | 250 |
185 const Extension* ExtensionContextMenuModel::GetExtension() const { | 251 const Extension* ExtensionContextMenuModel::GetExtension() const { |
186 ExtensionService* extension_service = | 252 ExtensionService* extension_service = |
187 extensions::ExtensionSystem::Get(profile_)->extension_service(); | 253 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
Devlin
2014/07/09 22:42:59
Not your code, but since you're here, can you upda
gpdavis
2014/07/10 00:22:25
Done.
| |
188 return extension_service->GetExtensionById(extension_id_, false); | 254 return extension_service->GetExtensionById(extension_id_, false); |
189 } | 255 } |
256 | |
257 void ExtensionContextMenuModel::AppendExtensionItems() { | |
258 extension_items_->Clear(); | |
259 | |
260 MenuManager* menu_manager = MenuManager::Get(profile_); | |
261 if (!menu_manager) | |
262 return; | |
263 | |
264 // Get a list of extension ids that have context menu items, and sort by the | |
265 // top level context menu title of the extension. | |
266 std::set<MenuItem::ExtensionKey> ids = menu_manager->ExtensionIds(); | |
267 std::vector<base::string16> sorted_menu_titles; | |
268 std::map<base::string16, std::string> map_ids; | |
269 const extensions::ExtensionSet& enabled_extensions = | |
270 extensions::ExtensionRegistry::Get(profile_)->enabled_extensions(); | |
271 for (std::set<MenuItem::ExtensionKey>::iterator iter = ids.begin(); | |
272 iter != ids.end(); | |
273 ++iter) { | |
274 const Extension* extension = enabled_extensions.GetByID(iter->extension_id); | |
275 if (extension) { | |
276 base::string16 menu_title = extension_items_->GetTopLevelContextMenuTitle( | |
277 *iter, base::string16()); | |
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 std::vector<base::string16>::iterator it; | |
Devlin
2014/07/09 22:42:59
scope the iterator with the for-loop.
gpdavis
2014/07/10 00:22:25
Done.
| |
292 for (it = sorted_menu_titles.begin(); it != sorted_menu_titles.end(); ++it) { | |
293 const std::string& id = map_ids[*it]; | |
294 const MenuItem::ExtensionKey extension_key(id); | |
Devlin
2014/07/09 22:42:59
You can inline extension_key.
gpdavis
2014/07/10 00:22:25
Done.
| |
295 extension_items_->AppendExtensionItems( | |
296 extension_key, base::string16(), &index); | |
297 } | |
298 } | |
OLD | NEW |