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

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: Added context strings, page action implementation 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"
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"
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"
14 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/chrome_pages.h" 19 #include "chrome/browser/ui/chrome_pages.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" 20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/extensions/extension_constants.h" 21 #include "chrome/common/extensions/extension_constants.h"
19 #include "chrome/common/extensions/manifest_url_handler.h" 22 #include "chrome/common/extensions/manifest_url_handler.h"
20 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
21 #include "chrome/common/url_constants.h" 24 #include "chrome/common/url_constants.h"
22 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
26 #include "content/public/common/context_menu_params.h"
23 #include "extensions/browser/extension_prefs.h" 27 #include "extensions/browser/extension_prefs.h"
24 #include "extensions/browser/extension_system.h" 28 #include "extensions/browser/extension_system.h"
25 #include "extensions/browser/management_policy.h" 29 #include "extensions/browser/management_policy.h"
26 #include "extensions/common/extension.h" 30 #include "extensions/common/extension.h"
27 #include "grit/chromium_strings.h" 31 #include "grit/chromium_strings.h"
28 #include "grit/generated_resources.h" 32 #include "grit/generated_resources.h"
29 #include "ui/base/l10n/l10n_util.h" 33 #include "ui/base/l10n/l10n_util.h"
34 #include "ui/gfx/text_elider.h"
Yoyo Zhou 2014/06/27 23:37:57 Is this used?
gpdavis 2014/06/28 00:05:41 Ah, no, it isn't. I had it included because of a
gpdavis 2014/06/30 21:11:11 Done.
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_(GetActionType(extension)),
52 extension_items_(profile_,
53 this,
54 this,
55 base::Bind(MenuItemMatchesContext, action_type_)) {
44 InitMenu(extension); 56 InitMenu(extension);
45 57
46 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && 58 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) &&
47 delegate_) { 59 delegate_) {
48 AddSeparator(ui::NORMAL_SEPARATOR); 60 AddSeparator(ui::NORMAL_SEPARATOR);
49 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); 61 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP);
50 } 62 }
51 } 63 }
52 64
53 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, 65 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension,
54 Browser* browser) 66 Browser* browser)
55 : SimpleMenuModel(this), 67 : SimpleMenuModel(this),
56 extension_id_(extension->id()), 68 extension_id_(extension->id()),
57 browser_(browser), 69 browser_(browser),
58 profile_(browser->profile()), 70 profile_(browser->profile()),
59 delegate_(NULL) { 71 delegate_(NULL),
72 action_type_(GetActionType(extension)),
73 extension_items_(profile_,
74 this,
75 this,
76 base::Bind(MenuItemMatchesContext, action_type_)) {
60 InitMenu(extension); 77 InitMenu(extension);
61 } 78 }
62 79
63 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { 80 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const {
81 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
82 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST)
83 return extension_items_.IsCommandIdChecked(command_id);
64 return false; 84 return false;
65 } 85 }
66 86
67 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { 87 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const {
68 const Extension* extension = this->GetExtension(); 88 const Extension* extension = this->GetExtension();
69 if (!extension) 89 if (!extension)
70 return false; 90 return false;
71 91
72 if (command_id == CONFIGURE) { 92 if (command_id == CONFIGURE) {
73 return 93 return
(...skipping 22 matching lines...) Expand all
96 int command_id, ui::Accelerator* accelerator) { 116 int command_id, ui::Accelerator* accelerator) {
97 return false; 117 return false;
98 } 118 }
99 119
100 void ExtensionContextMenuModel::ExecuteCommand(int command_id, 120 void ExtensionContextMenuModel::ExecuteCommand(int command_id,
101 int event_flags) { 121 int event_flags) {
102 const Extension* extension = GetExtension(); 122 const Extension* extension = GetExtension();
103 if (!extension) 123 if (!extension)
104 return; 124 return;
105 125
126 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
127 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
128 WebContents* web_contents =
129 browser_->tab_strip_model()->GetActiveWebContents();
130 content::ContextMenuParams *params = new content::ContextMenuParams();
131 extension_items_.ExecuteCommand(command_id, web_contents, *params);
132 return;
133 }
134
106 switch (command_id) { 135 switch (command_id) {
107 case NAME: { 136 case NAME: {
108 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), 137 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension),
109 Referrer(), NEW_FOREGROUND_TAB, 138 Referrer(), NEW_FOREGROUND_TAB,
110 content::PAGE_TRANSITION_LINK, false); 139 content::PAGE_TRANSITION_LINK, false);
111 browser_->OpenURL(params); 140 browser_->OpenURL(params);
112 break; 141 break;
113 } 142 }
114 case CONFIGURE: 143 case CONFIGURE:
115 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); 144 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty());
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 Release(); 183 Release();
155 } 184 }
156 185
157 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} 186 ExtensionContextMenuModel::~ExtensionContextMenuModel() {}
158 187
159 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { 188 void ExtensionContextMenuModel::InitMenu(const Extension* extension) {
160 DCHECK(extension); 189 DCHECK(extension);
161 190
162 extensions::ExtensionActionManager* extension_action_manager = 191 extensions::ExtensionActionManager* extension_action_manager =
163 extensions::ExtensionActionManager::Get(profile_); 192 extensions::ExtensionActionManager::Get(profile_);
164 extension_action_ = extension_action_manager->GetBrowserAction(*extension);
165 if (!extension_action_)
166 extension_action_ = extension_action_manager->GetPageAction(*extension);
167 193
168 std::string extension_name = extension->name(); 194 std::string extension_name = extension->name();
169 // Ampersands need to be escaped to avoid being treated like 195 // Ampersands need to be escaped to avoid being treated like
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);
200 AppendExtensionItems();
201 AddSeparator(ui::NORMAL_SEPARATOR);
Yoyo Zhou 2014/06/27 23:37:57 We don't want to do this if there are no extension
gpdavis 2014/06/28 00:05:41 I'll go ahead and put the AddSeparator call into A
gpdavis 2014/06/30 21:11:11 Done.
174 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); 202 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM);
175 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); 203 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL));
176 if (extension_action_manager->GetBrowserAction(*extension)) 204 if (extension_action_manager->GetBrowserAction(*extension))
177 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); 205 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON);
178 AddSeparator(ui::NORMAL_SEPARATOR); 206 AddSeparator(ui::NORMAL_SEPARATOR);
179 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); 207 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION);
180 } 208 }
181 209
182 const Extension* ExtensionContextMenuModel::GetExtension() const { 210 const Extension* ExtensionContextMenuModel::GetExtension() const {
183 ExtensionService* extension_service = 211 ExtensionService* extension_service =
184 extensions::ExtensionSystem::Get(profile_)->extension_service(); 212 extensions::ExtensionSystem::Get(profile_)->extension_service();
185 return extension_service->GetExtensionById(extension_id_, false); 213 return extension_service->GetExtensionById(extension_id_, false);
186 } 214 }
215
216 void ExtensionContextMenuModel::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();
Yoyo Zhou 2014/06/27 23:37:57 This is a leak. Use EmptyString16 from string_util
gpdavis 2014/06/28 00:05:41 Oh, wonderful. I was wondering what I should use
gpdavis 2014/06/30 21:11:11 Done.
228
229 // Get a list of extension id's that have context menu items, and sort by the
Yoyo Zhou 2014/06/27 23:37:57 Wait a minute. Here's my understanding of the worl
gpdavis 2014/06/28 00:05:41 I wasn't sure which way to do it, and since the ch
Yoyo Zhou 2014/06/28 00:29:44 Yes, please. I don't think it makes sense for an e
gpdavis 2014/06/30 21:11:11 Done.
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()) {
Yoyo Zhou 2014/06/27 23:37:57 I don't think platform apps can have extension UI
gpdavis 2014/06/30 21:11:11 Done.
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 const std::string& id = map_ids[sorted_menu_titles[i]];
258 const MenuItem::ExtensionKey extension_key(id);
259 extension_items_.AppendExtensionItems(
260 extension_key, *null_string, &index);
261 }
262
263 UMA_HISTOGRAM_TIMES("Extensions.ContextMenus_BuildTime",
Yoyo Zhou 2014/06/27 23:37:57 You can look at the comments in base/metrics/histo
gpdavis 2014/06/28 00:05:41 Sounds good to me. I'll take a look at that file
gpdavis 2014/06/30 21:11:11 Done.
264 base::TimeTicks::Now() - begin);
265 UMA_HISTOGRAM_COUNTS("Extensions.ContextMenus_ItemCount", index);
266 }
267
268 ExtensionContextMenuModel::ActionType ExtensionContextMenuModel::GetActionType(
Yoyo Zhou 2014/06/27 23:37:57 This is not a good style. A name like "GetActionTy
gpdavis 2014/06/28 00:27:02 Oops-- I meant to address this comment but I must
gpdavis 2014/06/30 21:11:11 Done.
269 const Extension* extension) {
270 extensions::ExtensionActionManager* extension_action_manager =
271 extensions::ExtensionActionManager::Get(profile_);
272 extension_action_ = extension_action_manager->GetBrowserAction(*extension);
273 if (!extension_action_) {
274 extension_action_ = extension_action_manager->GetPageAction(*extension);
275 return PAGE_ACTION;
276 }
277 return BROWSER_ACTION;
278 }
279
280 bool ExtensionContextMenuModel::MenuItemMatchesContext(
281 ActionType type,
282 const extensions::MenuItem* item) {
283 MenuItem::ContextList contexts = item->contexts();
284
285 if (contexts.Contains(MenuItem::ALL))
286 return true;
287 if (contexts.Contains(MenuItem::PAGE_ACTION) && (type == PAGE_ACTION))
288 return true;
289 if (contexts.Contains(MenuItem::BROWSER_ACTION) && (type == BROWSER_ACTION))
290 return true;
291
292 return false;
293 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698