Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: chrome/browser/extensions/extension_context_menu_model.cc

Issue 359493005: Extend contextMenus API to support browser/page actions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed yoyo's comments, restricted items to relevant extension Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
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"
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"
24 #include "extensions/browser/extension_system.h" 29 #include "extensions/browser/extension_system.h"
25 #include "extensions/browser/management_policy.h" 30 #include "extensions/browser/management_policy.h"
26 #include "extensions/common/extension.h" 31 #include "extensions/common/extension.h"
27 #include "grit/chromium_strings.h" 32 #include "grit/chromium_strings.h"
28 #include "grit/generated_resources.h" 33 #include "grit/generated_resources.h"
29 #include "ui/base/l10n/l10n_util.h" 34 #include "ui/base/l10n/l10n_util.h"
30 35
31 using content::OpenURLParams; 36 using content::OpenURLParams;
32 using content::Referrer; 37 using content::Referrer;
33 using content::WebContents; 38 using content::WebContents;
34 using extensions::Extension; 39 using extensions::Extension;
40 using extensions::MenuItem;
41 using extensions::MenuManager;
35 42
36 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, 43 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension,
37 Browser* browser, 44 Browser* browser,
38 PopupDelegate* delegate) 45 PopupDelegate* delegate)
39 : SimpleMenuModel(this), 46 : SimpleMenuModel(this),
40 extension_id_(extension->id()), 47 extension_id_(extension->id()),
41 browser_(browser), 48 browser_(browser),
42 profile_(browser->profile()), 49 profile_(browser->profile()),
43 delegate_(delegate) { 50 delegate_(delegate),
51 action_type_(NO_ACTION) {
44 InitMenu(extension); 52 InitMenu(extension);
45 53
46 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && 54 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) &&
47 delegate_) { 55 delegate_) {
48 AddSeparator(ui::NORMAL_SEPARATOR); 56 AddSeparator(ui::NORMAL_SEPARATOR);
49 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); 57 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP);
50 } 58 }
51 } 59 }
52 60
53 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, 61 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension,
54 Browser* browser) 62 Browser* browser)
55 : SimpleMenuModel(this), 63 : SimpleMenuModel(this),
56 extension_id_(extension->id()), 64 extension_id_(extension->id()),
57 browser_(browser), 65 browser_(browser),
58 profile_(browser->profile()), 66 profile_(browser->profile()),
59 delegate_(NULL) { 67 delegate_(NULL),
68 action_type_(NO_ACTION) {
60 InitMenu(extension); 69 InitMenu(extension);
61 } 70 }
62 71
63 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { 72 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const {
73 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
74 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST)
75 return extension_items_->IsCommandIdChecked(command_id);
64 return false; 76 return false;
65 } 77 }
66 78
67 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { 79 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const {
68 const Extension* extension = this->GetExtension(); 80 const Extension* extension = this->GetExtension();
69 if (!extension) 81 if (!extension)
70 return false; 82 return false;
71 83
72 if (command_id == CONFIGURE) { 84 if (command_id == CONFIGURE) {
73 return 85 return
(...skipping 22 matching lines...) Expand all
96 int command_id, ui::Accelerator* accelerator) { 108 int command_id, ui::Accelerator* accelerator) {
97 return false; 109 return false;
98 } 110 }
99 111
100 void ExtensionContextMenuModel::ExecuteCommand(int command_id, 112 void ExtensionContextMenuModel::ExecuteCommand(int command_id,
101 int event_flags) { 113 int event_flags) {
102 const Extension* extension = GetExtension(); 114 const Extension* extension = GetExtension();
103 if (!extension) 115 if (!extension)
104 return; 116 return;
105 117
118 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
119 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
120 WebContents* web_contents =
121 browser_->tab_strip_model()->GetActiveWebContents();
122 content::ContextMenuParams *params = new content::ContextMenuParams();
Devlin 2014/07/01 20:54:23 content::ContextMenuParams* params (note asterisk
Devlin 2014/07/01 20:54:24 Gah! Leak!
gpdavis 2014/07/02 01:17:46 I'm ashamed of all the leaks you've found in my co
123 params->extension_id = extension->id();
124 extension_items_->ExecuteCommand(command_id, web_contents, *params);
125 return;
126 }
127
106 switch (command_id) { 128 switch (command_id) {
107 case NAME: { 129 case NAME: {
108 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), 130 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension),
109 Referrer(), NEW_FOREGROUND_TAB, 131 Referrer(), NEW_FOREGROUND_TAB,
110 content::PAGE_TRANSITION_LINK, false); 132 content::PAGE_TRANSITION_LINK, false);
111 browser_->OpenURL(params); 133 browser_->OpenURL(params);
112 break; 134 break;
113 } 135 }
114 case CONFIGURE: 136 case CONFIGURE:
115 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); 137 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty());
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 } 177 }
156 178
157 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} 179 ExtensionContextMenuModel::~ExtensionContextMenuModel() {}
158 180
159 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { 181 void ExtensionContextMenuModel::InitMenu(const Extension* extension) {
160 DCHECK(extension); 182 DCHECK(extension);
161 183
162 extensions::ExtensionActionManager* extension_action_manager = 184 extensions::ExtensionActionManager* extension_action_manager =
163 extensions::ExtensionActionManager::Get(profile_); 185 extensions::ExtensionActionManager::Get(profile_);
164 extension_action_ = extension_action_manager->GetBrowserAction(*extension); 186 extension_action_ = extension_action_manager->GetBrowserAction(*extension);
165 if (!extension_action_) 187 if (!extension_action_) {
166 extension_action_ = extension_action_manager->GetPageAction(*extension); 188 extension_action_ = extension_action_manager->GetPageAction(*extension);
189 if (extension_action_)
190 action_type_ = PAGE_ACTION;
191 } else {
192 action_type_ = BROWSER_ACTION;
193 }
194
195 extension_items_.reset(new extensions::ContextMenuMatcher(profile_,
Devlin 2014/07/01 20:54:24 nit: fix param wrapping.
gpdavis 2014/07/02 01:17:46 Done.
196 this,
197 this,
198 base::Bind(MenuItemMatchesExtension,
199 action_type_,
200 extension)));
167 201
168 std::string extension_name = extension->name(); 202 std::string extension_name = extension->name();
169 // Ampersands need to be escaped to avoid being treated like 203 // Ampersands need to be escaped to avoid being treated like
170 // mnemonics in the menu. 204 // mnemonics in the menu.
171 base::ReplaceChars(extension_name, "&", "&&", &extension_name); 205 base::ReplaceChars(extension_name, "&", "&&", &extension_name);
172 AddItem(NAME, base::UTF8ToUTF16(extension_name)); 206 AddItem(NAME, base::UTF8ToUTF16(extension_name));
207 AppendExtensionItems();
173 AddSeparator(ui::NORMAL_SEPARATOR); 208 AddSeparator(ui::NORMAL_SEPARATOR);
174 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); 209 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM);
175 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); 210 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL));
176 if (extension_action_manager->GetBrowserAction(*extension)) 211 if (extension_action_manager->GetBrowserAction(*extension))
177 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); 212 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON);
178 AddSeparator(ui::NORMAL_SEPARATOR); 213 AddSeparator(ui::NORMAL_SEPARATOR);
179 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); 214 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION);
180 } 215 }
181 216
182 const Extension* ExtensionContextMenuModel::GetExtension() const { 217 const Extension* ExtensionContextMenuModel::GetExtension() const {
183 ExtensionService* extension_service = 218 ExtensionService* extension_service =
184 extensions::ExtensionSystem::Get(profile_)->extension_service(); 219 extensions::ExtensionSystem::Get(profile_)->extension_service();
185 return extension_service->GetExtensionById(extension_id_, false); 220 return extension_service->GetExtensionById(extension_id_, false);
186 } 221 }
222
223 void ExtensionContextMenuModel::AppendExtensionItems() {
Devlin 2014/07/01 20:54:24 The fact that 90% of this is verbatim copied code
224 extension_items_->Clear();
225 ExtensionService* service =
226 extensions::ExtensionSystem::Get(profile_)->extension_service();
227 if (!service)
228 return; // In unit-tests, we may not have an ExtensionService.
229
230 MenuManager* menu_manager = MenuManager::Get(profile_);
231 if (!menu_manager)
232 return;
233
234 // Get a list of extension id's that have context menu items, and sort by the
Devlin 2014/07/01 20:54:24 extension ids, not extension id's.
gpdavis 2014/07/02 01:17:46 Done.
235 // top level context menu title of the extension.
236 std::set<MenuItem::ExtensionKey> ids = menu_manager->ExtensionIds();
237 std::vector<base::string16> sorted_menu_titles;
238 std::map<base::string16, std::string> map_ids;
239 for (std::set<MenuItem::ExtensionKey>::iterator i = ids.begin();
Devlin 2014/07/01 20:54:24 nit: I don't know why, but we seem to have the pat
gpdavis 2014/07/02 01:17:46 Done.
240 i != ids.end();
241 ++i) {
242 const Extension* extension =
243 service->GetExtensionById(i->extension_id, false);
Devlin 2014/07/01 20:54:23 ExtensionService::GetExtensionById is deprecated.
gpdavis 2014/07/02 01:17:46 It is? I don't see any indication of that in the
Devlin 2014/07/02 17:25:21 Huh. There should be a comment there. Instead, us
gpdavis 2014/07/03 01:46:58 Done.
244 if (extension) {
245 base::string16 menu_title = extension_items_->GetTopLevelContextMenuTitle(
246 *i, base::EmptyString16());
247 map_ids[menu_title] = i->extension_id;
248 sorted_menu_titles.push_back(menu_title);
249 }
250 }
251 if (sorted_menu_titles.empty())
252 return;
253
254 AddSeparator(ui::NORMAL_SEPARATOR);
255
256 const std::string app_locale = g_browser_process->GetApplicationLocale();
Devlin 2014/07/01 20:54:24 Make this a const &, not just const.
gpdavis 2014/07/02 01:17:46 Done. Can you explain exactly why this ought to b
Devlin 2014/07/02 17:25:21 I believe it :) There's no guarantee that the cod
257 l10n_util::SortStrings16(app_locale, &sorted_menu_titles);
258
259 int index = 0;
260 base::TimeTicks begin = base::TimeTicks::Now();
261 for (size_t i = 0; i < sorted_menu_titles.size(); ++i) {
262 const std::string& id = map_ids[sorted_menu_titles[i]];
263 const MenuItem::ExtensionKey extension_key(id);
264 extension_items_->AppendExtensionItems(
265 extension_key, base::EmptyString16(), &index);
Devlin 2014/07/01 20:54:23 See comment for base::EmptyString16 where its decl
gpdavis 2014/07/02 01:17:46 I was following yoyo's suggestion to use this. Ho
Devlin 2014/07/02 17:25:21 Yeah, we should only use base::EmptyString[16] whe
gpdavis 2014/07/03 01:46:58 Done.
266 }
267
268 UMA_HISTOGRAM_TIMES("Extensions.ActionContextMenus_BuildTime",
Devlin 2014/07/01 20:54:23 If you don't add these to histograms.xml, we'll ne
gpdavis 2014/07/02 01:17:46 How exactly do I add items to this file without bl
Devlin 2014/07/02 17:25:20 Did something blow up? Huh... usually that doesn'
gpdavis 2014/07/03 01:46:58 Well, it didn't actually blow up because I was app
Devlin 2014/07/07 20:20:57 If there's no entry in histograms.xml, then we don
gpdavis 2014/07/09 02:13:53 Sounds good to me! I'll go ahead and remove the u
269 base::TimeTicks::Now() - begin);
270 UMA_HISTOGRAM_COUNTS("Extensions.ActionContextMenus_ItemCount", index);
271 }
272
273 bool ExtensionContextMenuModel::MenuItemMatchesExtension(
274 ActionType type,
275 const Extension* extension,
276 const extensions::MenuItem* item) {
277 if (type == NO_ACTION || item->extension_id() != extension->id())
278 return false;
279
280 MenuItem::ContextList contexts = item->contexts();
281
282 if (contexts.Contains(MenuItem::ALL))
283 return true;
284 if (contexts.Contains(MenuItem::PAGE_ACTION) && (type == PAGE_ACTION))
285 return true;
286 if (contexts.Contains(MenuItem::BROWSER_ACTION) && (type == BROWSER_ACTION))
287 return true;
288
289 return false;
290 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698