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

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 fixes 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/browser/uninstall_reason.h" 32 #include "extensions/browser/uninstall_reason.h"
27 #include "extensions/common/extension.h" 33 #include "extensions/common/extension.h"
28 #include "grit/chromium_strings.h" 34 #include "grit/chromium_strings.h"
29 #include "grit/generated_resources.h" 35 #include "grit/generated_resources.h"
30 #include "ui/base/l10n/l10n_util.h" 36 #include "ui/base/l10n/l10n_util.h"
31 37
32 using content::OpenURLParams; 38 using content::OpenURLParams;
33 using content::Referrer; 39 using content::Referrer;
34 using content::WebContents; 40 using content::WebContents;
35 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 of the given |type|.
48 bool MenuItemMatchesAction(ExtensionContextMenuModel::ActionType type,
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
36 68
37 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, 69 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension,
38 Browser* browser, 70 Browser* browser,
39 PopupDelegate* delegate) 71 PopupDelegate* delegate)
40 : SimpleMenuModel(this), 72 : SimpleMenuModel(this),
41 extension_id_(extension->id()), 73 extension_id_(extension->id()),
42 browser_(browser), 74 browser_(browser),
43 profile_(browser->profile()), 75 profile_(browser->profile()),
44 delegate_(delegate) { 76 delegate_(delegate),
77 action_type_(NO_ACTION) {
45 InitMenu(extension); 78 InitMenu(extension);
46 79
47 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && 80 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) &&
48 delegate_) { 81 delegate_) {
49 AddSeparator(ui::NORMAL_SEPARATOR); 82 AddSeparator(ui::NORMAL_SEPARATOR);
50 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); 83 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP);
51 } 84 }
52 } 85 }
53 86
54 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, 87 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension,
55 Browser* browser) 88 Browser* browser)
56 : SimpleMenuModel(this), 89 : SimpleMenuModel(this),
57 extension_id_(extension->id()), 90 extension_id_(extension->id()),
58 browser_(browser), 91 browser_(browser),
59 profile_(browser->profile()), 92 profile_(browser->profile()),
60 delegate_(NULL) { 93 delegate_(NULL),
94 action_type_(NO_ACTION) {
61 InitMenu(extension); 95 InitMenu(extension);
62 } 96 }
63 97
64 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);
65 return false; 102 return false;
66 } 103 }
67 104
68 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { 105 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const {
69 const Extension* extension = this->GetExtension(); 106 const Extension* extension = GetExtension();
70 if (!extension) 107 if (!extension)
71 return false; 108 return false;
72 109
73 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) {
74 return 114 return
75 extensions::ManifestURL::GetOptionsPage(extension).spec().length() > 0; 115 extensions::ManifestURL::GetOptionsPage(extension).spec().length() > 0;
76 } else if (command_id == NAME) { 116 } else if (command_id == NAME) {
77 // 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
78 // homepage, we just disable this menu item. 118 // homepage, we just disable this menu item.
79 return extensions::ManifestURL::GetHomepageURL(extension).is_valid(); 119 return extensions::ManifestURL::GetHomepageURL(extension).is_valid();
80 } else if (command_id == INSPECT_POPUP) { 120 } else if (command_id == INSPECT_POPUP) {
81 WebContents* web_contents = 121 WebContents* web_contents =
82 browser_->tab_strip_model()->GetActiveWebContents(); 122 browser_->tab_strip_model()->GetActiveWebContents();
83 if (!web_contents) 123 if (!web_contents)
(...skipping 13 matching lines...) Expand all
97 int command_id, ui::Accelerator* accelerator) { 137 int command_id, ui::Accelerator* accelerator) {
98 return false; 138 return false;
99 } 139 }
100 140
101 void ExtensionContextMenuModel::ExecuteCommand(int command_id, 141 void ExtensionContextMenuModel::ExecuteCommand(int command_id,
102 int event_flags) { 142 int event_flags) {
103 const Extension* extension = GetExtension(); 143 const Extension* extension = GetExtension();
104 if (!extension) 144 if (!extension)
105 return; 145 return;
106 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
107 switch (command_id) { 157 switch (command_id) {
108 case NAME: { 158 case NAME: {
109 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension), 159 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension),
110 Referrer(), NEW_FOREGROUND_TAB, 160 Referrer(), NEW_FOREGROUND_TAB,
111 content::PAGE_TRANSITION_LINK, false); 161 content::PAGE_TRANSITION_LINK, false);
112 browser_->OpenURL(params); 162 browser_->OpenURL(params);
113 break; 163 break;
114 } 164 }
115 case CONFIGURE: 165 case CONFIGURE:
116 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty()); 166 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty());
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 } 208 }
159 209
160 ExtensionContextMenuModel::~ExtensionContextMenuModel() {} 210 ExtensionContextMenuModel::~ExtensionContextMenuModel() {}
161 211
162 void ExtensionContextMenuModel::InitMenu(const Extension* extension) { 212 void ExtensionContextMenuModel::InitMenu(const Extension* extension) {
163 DCHECK(extension); 213 DCHECK(extension);
164 214
165 extensions::ExtensionActionManager* extension_action_manager = 215 extensions::ExtensionActionManager* extension_action_manager =
166 extensions::ExtensionActionManager::Get(profile_); 216 extensions::ExtensionActionManager::Get(profile_);
167 extension_action_ = extension_action_manager->GetBrowserAction(*extension); 217 extension_action_ = extension_action_manager->GetBrowserAction(*extension);
168 if (!extension_action_) 218 if (!extension_action_) {
169 extension_action_ = extension_action_manager->GetPageAction(*extension); 219 extension_action_ = extension_action_manager->GetPageAction(*extension);
220 if (extension_action_)
221 action_type_ = PAGE_ACTION;
222 } else {
223 action_type_ = BROWSER_ACTION;
224 }
225
226 extension_items_.reset(new extensions::ContextMenuMatcher(
227 profile_,
228 this,
229 this,
230 base::Bind(MenuItemMatchesAction, action_type_)));
170 231
171 std::string extension_name = extension->name(); 232 std::string extension_name = extension->name();
172 // Ampersands need to be escaped to avoid being treated like 233 // Ampersands need to be escaped to avoid being treated like
173 // mnemonics in the menu. 234 // mnemonics in the menu.
174 base::ReplaceChars(extension_name, "&", "&&", &extension_name); 235 base::ReplaceChars(extension_name, "&", "&&", &extension_name);
175 AddItem(NAME, base::UTF8ToUTF16(extension_name)); 236 AddItem(NAME, base::UTF8ToUTF16(extension_name));
237 AppendExtensionItems();
176 AddSeparator(ui::NORMAL_SEPARATOR); 238 AddSeparator(ui::NORMAL_SEPARATOR);
177 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); 239 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM);
178 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); 240 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL));
179 if (extension_action_manager->GetBrowserAction(*extension)) 241 if (extension_action_manager->GetBrowserAction(*extension))
180 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON); 242 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON);
181 AddSeparator(ui::NORMAL_SEPARATOR); 243 AddSeparator(ui::NORMAL_SEPARATOR);
182 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); 244 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION);
183 } 245 }
184 246
185 const Extension* ExtensionContextMenuModel::GetExtension() const { 247 const Extension* ExtensionContextMenuModel::GetExtension() const {
186 ExtensionService* extension_service = 248 return extensions::ExtensionRegistry::Get(profile_)
187 extensions::ExtensionSystem::Get(profile_)->extension_service(); 249 ->enabled_extensions()
188 return extension_service->GetExtensionById(extension_id_, false); 250 .GetByID(extension_id_);
189 } 251 }
252
253 void ExtensionContextMenuModel::AppendExtensionItems() {
254 extension_items_->Clear();
255
256 MenuManager* menu_manager = MenuManager::Get(profile_);
257 if (!menu_manager ||
258 !menu_manager->MenuItems(MenuItem::ExtensionKey(extension_id_)))
259 return;
260
261 AddSeparator(ui::NORMAL_SEPARATOR);
262
263 extension_items_count_ = 0;
264 extension_items_->AppendExtensionItems(MenuItem::ExtensionKey(extension_id_),
265 base::string16(),
266 &extension_items_count_,
267 true); // is_action_menu
268 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698