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" | 9 #include "chrome/app/chrome_command_ids.h" |
10 #include "chrome/browser/extensions/active_script_controller.h" | 10 #include "chrome/browser/extensions/active_script_controller.h" |
11 #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" | 12 #include "chrome/browser/extensions/context_menu_matcher.h" |
13 #include "chrome/browser/extensions/extension_action.h" | 13 #include "chrome/browser/extensions/extension_action.h" |
14 #include "chrome/browser/extensions/extension_action_manager.h" | 14 #include "chrome/browser/extensions/extension_action_manager.h" |
15 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
16 #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" | 17 #include "chrome/browser/extensions/menu_manager.h" |
18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/sessions/session_tab_helper.h" | 19 #include "chrome/browser/sessions/session_tab_helper.h" |
20 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
21 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
22 #include "chrome/browser/ui/chrome_pages.h" | 22 #include "chrome/browser/ui/chrome_pages.h" |
23 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
24 #include "chrome/common/extensions/extension_constants.h" | 24 #include "chrome/common/extensions/extension_constants.h" |
25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
26 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
27 #include "chrome/grit/chromium_strings.h" | 27 #include "chrome/grit/chromium_strings.h" |
28 #include "chrome/grit/generated_resources.h" | 28 #include "chrome/grit/generated_resources.h" |
| 29 #include "chrome/grit/theme_resources.h" |
29 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
30 #include "content/public/common/context_menu_params.h" | 31 #include "content/public/common/context_menu_params.h" |
31 #include "extensions/browser/extension_prefs.h" | 32 #include "extensions/browser/extension_prefs.h" |
32 #include "extensions/browser/extension_registry.h" | 33 #include "extensions/browser/extension_registry.h" |
33 #include "extensions/browser/extension_system.h" | 34 #include "extensions/browser/extension_system.h" |
34 #include "extensions/browser/management_policy.h" | 35 #include "extensions/browser/management_policy.h" |
35 #include "extensions/browser/uninstall_reason.h" | 36 #include "extensions/browser/uninstall_reason.h" |
36 #include "extensions/common/extension.h" | 37 #include "extensions/common/extension.h" |
37 #include "extensions/common/feature_switch.h" | 38 #include "extensions/common/feature_switch.h" |
38 #include "extensions/common/manifest_handlers/options_page_info.h" | 39 #include "extensions/common/manifest_handlers/options_page_info.h" |
39 #include "extensions/common/manifest_url_handlers.h" | 40 #include "extensions/common/manifest_url_handlers.h" |
40 #include "ui/base/l10n/l10n_util.h" | 41 #include "ui/base/l10n/l10n_util.h" |
| 42 #include "ui/base/resource/resource_bundle.h" |
| 43 #include "ui/gfx/image/image.h" |
41 | 44 |
42 using content::OpenURLParams; | 45 using content::OpenURLParams; |
43 using content::Referrer; | 46 using content::Referrer; |
44 using content::WebContents; | 47 using content::WebContents; |
45 using extensions::Extension; | 48 using extensions::Extension; |
46 using extensions::ExtensionActionAPI; | 49 using extensions::ExtensionActionAPI; |
47 using extensions::ExtensionPrefs; | 50 using extensions::ExtensionPrefs; |
48 using extensions::MenuItem; | 51 using extensions::MenuItem; |
49 using extensions::MenuManager; | 52 using extensions::MenuManager; |
50 | 53 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 | 109 |
107 } // namespace | 110 } // namespace |
108 | 111 |
109 ExtensionContextMenuModel::ExtensionContextMenuModel( | 112 ExtensionContextMenuModel::ExtensionContextMenuModel( |
110 const Extension* extension, | 113 const Extension* extension, |
111 Browser* browser, | 114 Browser* browser, |
112 ButtonVisibility button_visibility, | 115 ButtonVisibility button_visibility, |
113 PopupDelegate* delegate) | 116 PopupDelegate* delegate) |
114 : SimpleMenuModel(this), | 117 : SimpleMenuModel(this), |
115 extension_id_(extension->id()), | 118 extension_id_(extension->id()), |
| 119 is_component_(extensions::Manifest::IsComponentLocation( |
| 120 extension->location())), |
116 browser_(browser), | 121 browser_(browser), |
117 profile_(browser->profile()), | 122 profile_(browser->profile()), |
118 delegate_(delegate), | 123 delegate_(delegate), |
119 action_type_(NO_ACTION), | 124 action_type_(NO_ACTION), |
120 extension_items_count_(0) { | 125 extension_items_count_(0) { |
121 InitMenu(extension, button_visibility); | 126 InitMenu(extension, button_visibility); |
122 | 127 |
123 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && | 128 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && |
124 delegate_) { | 129 delegate_ && |
| 130 !is_component_) { |
125 AddSeparator(ui::NORMAL_SEPARATOR); | 131 AddSeparator(ui::NORMAL_SEPARATOR); |
126 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); | 132 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP); |
127 } | 133 } |
128 } | 134 } |
129 | 135 |
130 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, | 136 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension, |
131 Browser* browser) | 137 Browser* browser) |
132 : SimpleMenuModel(this), | 138 : ExtensionContextMenuModel(extension, browser, VISIBLE, nullptr) { |
133 extension_id_(extension->id()), | |
134 browser_(browser), | |
135 profile_(browser->profile()), | |
136 delegate_(NULL), | |
137 action_type_(NO_ACTION), | |
138 extension_items_count_(0) { | |
139 InitMenu(extension, VISIBLE); | |
140 } | 139 } |
141 | 140 |
142 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { | 141 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const { |
143 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | 142 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && |
144 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) | 143 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) |
145 return extension_items_->IsCommandIdChecked(command_id); | 144 return extension_items_->IsCommandIdChecked(command_id); |
146 return false; | 145 return false; |
147 } | 146 } |
148 | 147 |
149 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { | 148 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const { |
150 const Extension* extension = GetExtension(); | 149 const Extension* extension = GetExtension(); |
151 if (!extension) | 150 if (!extension) |
152 return false; | 151 return false; |
153 | 152 |
154 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && | 153 if (command_id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && |
155 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) { | 154 command_id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) { |
156 return extension_items_->IsCommandIdEnabled(command_id); | 155 return extension_items_->IsCommandIdEnabled(command_id); |
157 } else if (command_id == CONFIGURE) { | 156 } else if (command_id == CONFIGURE) { |
158 return extensions::OptionsPageInfo::HasOptionsPage(extension); | 157 return extensions::OptionsPageInfo::HasOptionsPage(extension); |
159 } else if (command_id == NAME) { | 158 } else if (command_id == NAME) { |
160 // The NAME links to the Homepage URL. If the extension doesn't have a | 159 // The NAME links to the Homepage URL. If the extension doesn't have a |
161 // homepage, we just disable this menu item. | 160 // homepage, we just disable this menu item. We also disable for component |
162 return extensions::ManifestURL::GetHomepageURL(extension).is_valid(); | 161 // extensions, because it doesn't make sense to link to a webstore page or |
| 162 // chrome://extensions. |
| 163 return extensions::ManifestURL::GetHomepageURL(extension).is_valid() && |
| 164 !is_component_; |
163 } else if (command_id == INSPECT_POPUP) { | 165 } else if (command_id == INSPECT_POPUP) { |
164 WebContents* web_contents = GetActiveWebContents(); | 166 WebContents* web_contents = GetActiveWebContents(); |
165 if (!web_contents) | 167 if (!web_contents) |
166 return false; | 168 return false; |
167 | 169 |
168 return extension_action_ && | 170 return extension_action_ && |
169 extension_action_->HasPopup(SessionTabHelper::IdForTab(web_contents)); | 171 extension_action_->HasPopup(SessionTabHelper::IdForTab(web_contents)); |
170 } else if (command_id == UNINSTALL) { | 172 } else if (command_id == UNINSTALL) { |
171 // Some extension types can not be uninstalled. | 173 // Some extension types can not be uninstalled. |
172 extensions::ManagementPolicy* policy = | 174 extensions::ManagementPolicy* policy = |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 // injections if there is an active action for this extension. Note that this | 296 // injections if there is an active action for this extension. Note that this |
295 // will add it to *all* extension action context menus, not just the one | 297 // will add it to *all* extension action context menus, not just the one |
296 // attached to the script injection request icon, but that's okay. | 298 // attached to the script injection request icon, but that's okay. |
297 WebContents* web_contents = GetActiveWebContents(); | 299 WebContents* web_contents = GetActiveWebContents(); |
298 if (web_contents && | 300 if (web_contents && |
299 extensions::ActiveScriptController::GetForWebContents(web_contents) | 301 extensions::ActiveScriptController::GetForWebContents(web_contents) |
300 ->WantsToRun(extension)) { | 302 ->WantsToRun(extension)) { |
301 AddItemWithStringId(ALWAYS_RUN, IDS_EXTENSIONS_ALWAYS_RUN); | 303 AddItemWithStringId(ALWAYS_RUN, IDS_EXTENSIONS_ALWAYS_RUN); |
302 } | 304 } |
303 | 305 |
304 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); | 306 if (!is_component_ || extensions::OptionsPageInfo::HasOptionsPage(extension)) |
305 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); | 307 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM); |
| 308 |
| 309 if (!is_component_) |
| 310 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); |
306 | 311 |
307 // Add a toggle visibility (show/hide) if the extension icon is shown on the | 312 // Add a toggle visibility (show/hide) if the extension icon is shown on the |
308 // toolbar. | 313 // toolbar. |
309 int visibility_string_id = | 314 int visibility_string_id = |
310 GetVisibilityStringId(profile_, extension, button_visibility); | 315 GetVisibilityStringId(profile_, extension, button_visibility); |
311 if (visibility_string_id != -1) | 316 if (visibility_string_id != -1) |
312 AddItemWithStringId(TOGGLE_VISIBILITY, visibility_string_id); | 317 AddItemWithStringId(TOGGLE_VISIBILITY, visibility_string_id); |
313 | 318 |
314 AddSeparator(ui::NORMAL_SEPARATOR); | 319 if (!is_component_) { |
315 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); | 320 AddSeparator(ui::NORMAL_SEPARATOR); |
| 321 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION); |
| 322 } |
316 } | 323 } |
317 | 324 |
318 const Extension* ExtensionContextMenuModel::GetExtension() const { | 325 const Extension* ExtensionContextMenuModel::GetExtension() const { |
319 return extensions::ExtensionRegistry::Get(profile_) | 326 return extensions::ExtensionRegistry::Get(profile_) |
320 ->enabled_extensions() | 327 ->enabled_extensions() |
321 .GetByID(extension_id_); | 328 .GetByID(extension_id_); |
322 } | 329 } |
323 | 330 |
324 void ExtensionContextMenuModel::AppendExtensionItems() { | 331 void ExtensionContextMenuModel::AppendExtensionItems() { |
325 extension_items_->Clear(); | 332 extension_items_->Clear(); |
326 | 333 |
327 MenuManager* menu_manager = MenuManager::Get(profile_); | 334 MenuManager* menu_manager = MenuManager::Get(profile_); |
328 if (!menu_manager || | 335 if (!menu_manager || |
329 !menu_manager->MenuItems(MenuItem::ExtensionKey(extension_id_))) | 336 !menu_manager->MenuItems(MenuItem::ExtensionKey(extension_id_))) |
330 return; | 337 return; |
331 | 338 |
332 AddSeparator(ui::NORMAL_SEPARATOR); | 339 AddSeparator(ui::NORMAL_SEPARATOR); |
333 | 340 |
334 extension_items_count_ = 0; | 341 extension_items_count_ = 0; |
335 extension_items_->AppendExtensionItems(MenuItem::ExtensionKey(extension_id_), | 342 extension_items_->AppendExtensionItems(MenuItem::ExtensionKey(extension_id_), |
336 base::string16(), | 343 base::string16(), |
337 &extension_items_count_, | 344 &extension_items_count_, |
338 true); // is_action_menu | 345 true); // is_action_menu |
339 } | 346 } |
340 | 347 |
341 content::WebContents* ExtensionContextMenuModel::GetActiveWebContents() const { | 348 content::WebContents* ExtensionContextMenuModel::GetActiveWebContents() const { |
342 return browser_->tab_strip_model()->GetActiveWebContents(); | 349 return browser_->tab_strip_model()->GetActiveWebContents(); |
343 } | 350 } |
OLD | NEW |