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