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

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: Minor changes 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"
14 #include "chrome/browser/extensions/context_menu_matcher.h"
10 #include "chrome/browser/extensions/extension_action.h" 15 #include "chrome/browser/extensions/extension_action.h"
11 #include "chrome/browser/extensions/extension_action_manager.h" 16 #include "chrome/browser/extensions/extension_action_manager.h"
12 #include "chrome/browser/extensions/extension_service.h" 17 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_tab_util.h" 18 #include "chrome/browser/extensions/extension_tab_util.h"
19 #include "chrome/browser/extensions/menu_manager.h"
14 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h" 21 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/chrome_pages.h" 22 #include "chrome/browser/ui/chrome_pages.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h" 23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/extensions/extension_constants.h" 24 #include "chrome/common/extensions/extension_constants.h"
19 #include "chrome/common/extensions/manifest_url_handler.h" 25 #include "chrome/common/extensions/manifest_url_handler.h"
20 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
21 #include "chrome/common/url_constants.h" 27 #include "chrome/common/url_constants.h"
22 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
23 #include "extensions/browser/extension_prefs.h" 29 #include "extensions/browser/extension_prefs.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 // Matches menu items with the context menu's extension.
47 static bool MenuItemMatchesExtension(
Devlin 2014/07/02 17:25:21 static functions in anonymous namespace are redund
gpdavis 2014/07/03 01:46:58 Done.
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 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 = this->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
(...skipping 22 matching lines...) Expand all
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 extension_items_->ExecuteCommand(command_id, web_contents, params_);
Devlin 2014/07/02 17:25:21 Are we guaranteed that extension_items_ will never
gpdavis 2014/07/03 01:46:58 I don't see any way it could be null. I'll DCHECK
151 return;
152 }
153
106 switch (command_id) { 154 switch (command_id) {
107 case NAME: { 155 case NAME: {
108 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), 156 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension),
109 Referrer(), NEW_FOREGROUND_TAB, 157 Referrer(), NEW_FOREGROUND_TAB,
110 content::PAGE_TRANSITION_LINK, false); 158 content::PAGE_TRANSITION_LINK, false);
111 browser_->OpenURL(params); 159 browser_->OpenURL(params);
112 break; 160 break;
113 } 161 }
114 case CONFIGURE: 162 case CONFIGURE:
115 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); 163 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty());
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 } 203 }
156 204
157 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} 205 ExtensionContextMenuModel::~ExtensionContextMenuModel() {}
158 206
159 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { 207 void ExtensionContextMenuModel::InitMenu(const Extension* extension) {
160 DCHECK(extension); 208 DCHECK(extension);
161 209
162 extensions::ExtensionActionManager* extension_action_manager = 210 extensions::ExtensionActionManager* extension_action_manager =
163 extensions::ExtensionActionManager::Get(profile_); 211 extensions::ExtensionActionManager::Get(profile_);
164 extension_action_ = extension_action_manager->GetBrowserAction(*extension); 212 extension_action_ = extension_action_manager->GetBrowserAction(*extension);
165 if (!extension_action_) 213 if (!extension_action_) {
166 extension_action_ = extension_action_manager->GetPageAction(*extension); 214 extension_action_ = extension_action_manager->GetPageAction(*extension);
215 if (extension_action_)
216 action_type_ = PAGE_ACTION;
217 } else {
218 action_type_ = BROWSER_ACTION;
219 }
220
221 extension_items_.reset(
222 new extensions::ContextMenuMatcher(profile_,
223 this,
224 this,
225 base::Bind(MenuItemMatchesExtension,
226 action_type_,
227 extension)));
167 228
168 std::string extension_name = extension->name(); 229 std::string extension_name = extension->name();
169 // Ampersands need to be escaped to avoid being treated like 230 // Ampersands need to be escaped to avoid being treated like
170 // mnemonics in the menu. 231 // mnemonics in the menu.
171 base::ReplaceChars(extension_name, "&", "&&", &extension_name); 232 base::ReplaceChars(extension_name, "&", "&&", &extension_name);
172 AddItem(NAME, base::UTF8ToUTF16(extension_name)); 233 AddItem(NAME, base::UTF8ToUTF16(extension_name));
234 AppendExtensionItems();
173 AddSeparator(ui::NORMAL_SEPARATOR); 235 AddSeparator(ui::NORMAL_SEPARATOR);
174 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); 236 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM);
175 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); 237 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL));
176 if (extension_action_manager->GetBrowserAction(*extension)) 238 if (extension_action_manager->GetBrowserAction(*extension))
177 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); 239 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON);
178 AddSeparator(ui::NORMAL_SEPARATOR); 240 AddSeparator(ui::NORMAL_SEPARATOR);
179 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); 241 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION);
180 } 242 }
181 243
182 const Extension* ExtensionContextMenuModel::GetExtension() const { 244 const Extension* ExtensionContextMenuModel::GetExtension() const {
183 ExtensionService* extension_service = 245 ExtensionService* extension_service =
184 extensions::ExtensionSystem::Get(profile_)->extension_service(); 246 extensions::ExtensionSystem::Get(profile_)->extension_service();
185 return extension_service->GetExtensionById(extension_id_, false); 247 return extension_service->GetExtensionById(extension_id_, false);
186 } 248 }
249
250 void ExtensionContextMenuModel::AppendExtensionItems() {
251 extension_items_->Clear();
252 ExtensionService* service =
253 extensions::ExtensionSystem::Get(profile_)->extension_service();
254 if (!service)
255 return; // In unit-tests, we may not have an ExtensionService.
256
257 MenuManager* menu_manager = MenuManager::Get(profile_);
258 if (!menu_manager)
259 return;
260
261 // Get a list of extension ids that have context menu items, and sort by the
262 // top level context menu title of the extension.
263 std::set<MenuItem::ExtensionKey> ids = menu_manager->ExtensionIds();
264 std::vector<base::string16> sorted_menu_titles;
265 std::map<base::string16, std::string> map_ids;
266 for (std::set<MenuItem::ExtensionKey>::iterator iter = ids.begin();
267 iter != ids.end();
268 ++iter) {
269 const Extension* extension =
270 service->GetExtensionById(iter->extension_id, false);
271 if (extension) {
272 base::string16 menu_title = extension_items_->GetTopLevelContextMenuTitle(
273 *iter, base::EmptyString16());
274 map_ids[menu_title] = iter->extension_id;
275 sorted_menu_titles.push_back(menu_title);
276 }
277 }
278 if (sorted_menu_titles.empty())
279 return;
280
281 AddSeparator(ui::NORMAL_SEPARATOR);
282
283 const std::string& app_locale = g_browser_process->GetApplicationLocale();
284 l10n_util::SortStrings16(app_locale, &sorted_menu_titles);
285
286 int index = 0;
287 base::TimeTicks begin = base::TimeTicks::Now();
288 for (size_t i = 0; i < sorted_menu_titles.size(); ++i) {
289 const std::string& id = map_ids[sorted_menu_titles[i]];
290 const MenuItem::ExtensionKey extension_key(id);
291 extension_items_->AppendExtensionItems(
292 extension_key, base::EmptyString16(), &index);
293 }
294
295 UMA_HISTOGRAM_TIMES("Extensions.ActionContextMenus_BuildTime",
296 base::TimeTicks::Now() - begin);
297 UMA_HISTOGRAM_COUNTS("Extensions.ActionContextMenus_ItemCount", index);
298 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698