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

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: Small changes, formatting 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/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 of the given |type|.
47 bool MenuItemMatchesAction(ExtensionContextMenuModel::ActionType type,
Yoyo Zhou 2014/07/22 00:44:32 You probably don't need to match against the exten
gpdavis 2014/07/22 18:52:52 Ah, I removed the check but I left the extension p
48 const Extension* extension,
49 const MenuItem* item) {
50 if (type == ExtensionContextMenuModel::NO_ACTION)
51 return false;
52
53 const MenuItem::ContextList& contexts = item->contexts();
54
55 if (contexts.Contains(MenuItem::ALL))
56 return true;
57 if (contexts.Contains(MenuItem::PAGE_ACTION) &&
58 (type == ExtensionContextMenuModel::PAGE_ACTION))
59 return true;
60 if (contexts.Contains(MenuItem::BROWSER_ACTION) &&
61 (type == ExtensionContextMenuModel::BROWSER_ACTION))
62 return true;
63
64 return false;
65 }
66
67 } // namespace
35 68
36 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, 69 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension,
37 Browser* browser, 70 Browser* browser,
38 PopupDelegate* delegate) 71 PopupDelegate* delegate)
39 : SimpleMenuModel(this), 72 : SimpleMenuModel(this),
40 extension_id_(extension->id()), 73 extension_id_(extension->id()),
41 browser_(browser), 74 browser_(browser),
42 profile_(browser->profile()), 75 profile_(browser->profile()),
43 delegate_(delegate) { 76 delegate_(delegate),
77 action_type_(NO_ACTION) {
44 InitMenu(extension); 78 InitMenu(extension);
45 79
46 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && 80 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) &&
47 delegate_) { 81 delegate_) {
48 AddSeparator(ui::NORMAL_SEPARATOR); 82 AddSeparator(ui::NORMAL_SEPARATOR);
49 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); 83 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP);
50 } 84 }
51 } 85 }
52 86
53 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, 87 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension,
54 Browser* browser) 88 Browser* browser)
55 : SimpleMenuModel(this), 89 : SimpleMenuModel(this),
56 extension_id_(extension->id()), 90 extension_id_(extension->id()),
57 browser_(browser), 91 browser_(browser),
58 profile_(browser->profile()), 92 profile_(browser->profile()),
59 delegate_(NULL) { 93 delegate_(NULL),
94 action_type_(NO_ACTION) {
60 InitMenu(extension); 95 InitMenu(extension);
61 } 96 }
62 97
63 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { 98 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const {
99 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
100 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST)
101 return extension_items_->IsCommandIdChecked(command_id);
64 return false; 102 return false;
65 } 103 }
66 104
67 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { 105 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const {
68 const Extension* extension = this->GetExtension(); 106 const Extension* extension = GetExtension();
69 if (!extension) 107 if (!extension)
70 return false; 108 return false;
71 109
72 if (command_id == CONFIGURE) { 110 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
111 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
112 return extension_items_->IsCommandIdEnabled(command_id);
113 } else 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();
79 } else if (command_id == INSPECT_POPUP) { 120 } else if (command_id == INSPECT_POPUP) {
80 WebContents* web_contents = 121 WebContents* web_contents =
81 browser_->tab_strip_model()->GetActiveWebContents(); 122 browser_->tab_strip_model()->GetActiveWebContents();
82 if (!web_contents) 123 if (!web_contents)
(...skipping 13 matching lines...) Expand all
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(
153 command_id, web_contents, 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
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(new extensions::ContextMenuMatcher(
228 profile_,
229 this,
230 this,
231 base::Bind(MenuItemMatchesAction, action_type_, extension)));
170 232
171 std::string extension_name = extension->name(); 233 std::string extension_name = extension->name();
172 // Ampersands need to be escaped to avoid being treated like 234 // Ampersands need to be escaped to avoid being treated like
173 // mnemonics in the menu. 235 // mnemonics in the menu.
174 base::ReplaceChars(extension_name, "&", "&&", &extension_name); 236 base::ReplaceChars(extension_name, "&", "&&", &extension_name);
175 AddItem(NAME, base::UTF8ToUTF16(extension_name)); 237 AddItem(NAME, base::UTF8ToUTF16(extension_name));
238 AppendExtensionItems();
176 AddSeparator(ui::NORMAL_SEPARATOR); 239 AddSeparator(ui::NORMAL_SEPARATOR);
177 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); 240 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM);
178 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); 241 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL));
179 if (extension_action_manager->GetBrowserAction(*extension)) 242 if (extension_action_manager->GetBrowserAction(*extension))
180 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); 243 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON);
181 AddSeparator(ui::NORMAL_SEPARATOR); 244 AddSeparator(ui::NORMAL_SEPARATOR);
182 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); 245 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION);
183 } 246 }
184 247
185 const Extension* ExtensionContextMenuModel::GetExtension() const { 248 const Extension* ExtensionContextMenuModel::GetExtension() const {
186 ExtensionService* extension_service = 249 return extensions::ExtensionRegistry::Get(profile_)
187 extensions::ExtensionSystem::Get(profile_)->extension_service(); 250 ->enabled_extensions()
188 return extension_service->GetExtensionById(extension_id_, false); 251 .GetByID(extension_id_);
189 } 252 }
253
254 void ExtensionContextMenuModel::AppendExtensionItems() {
255 extension_items_->Clear();
256
257 MenuManager* menu_manager = MenuManager::Get(profile_);
258 if (!menu_manager ||
259 !menu_manager->MenuItems(MenuItem::ExtensionKey(extension_id_)))
260 return;
261
262 AddSeparator(ui::NORMAL_SEPARATOR);
263
264 extension_items_count_ = 0;
265 extension_items_->AppendExtensionItems(MenuItem::ExtensionKey(extension_id_),
266 base::string16(),
267 &extension_items_count_,
268 true); // is_action_menu
269 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698