Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_context_menu_model.cc
blobc2addc1f7697e85630f61f4e2f6bd466e5516319
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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/extension_context_menu_model.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
10 #include "chrome/browser/extensions/extension_action.h"
11 #include "chrome/browser/extensions/extension_action_manager.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_system.h"
14 #include "chrome/browser/extensions/extension_tab_util.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/chrome_pages.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/extensions/extension_constants.h"
20 #include "chrome/common/extensions/manifest_url_handler.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/common/url_constants.h"
23 #include "content/public/browser/web_contents.h"
24 #include "extensions/browser/management_policy.h"
25 #include "extensions/common/extension.h"
26 #include "grit/chromium_strings.h"
27 #include "grit/generated_resources.h"
28 #include "ui/base/l10n/l10n_util.h"
30 using content::OpenURLParams;
31 using content::Referrer;
32 using content::WebContents;
33 using extensions::Extension;
35 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension,
36 Browser* browser,
37 PopupDelegate* delegate)
38 : SimpleMenuModel(this),
39 extension_id_(extension->id()),
40 browser_(browser),
41 profile_(browser->profile()),
42 delegate_(delegate) {
43 InitMenu(extension);
45 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) &&
46 delegate_) {
47 AddSeparator(ui::NORMAL_SEPARATOR);
48 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP);
52 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension* extension,
53 Browser* browser)
54 : SimpleMenuModel(this),
55 extension_id_(extension->id()),
56 browser_(browser),
57 profile_(browser->profile()),
58 delegate_(NULL) {
59 InitMenu(extension);
62 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id) const {
63 return false;
66 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id) const {
67 const Extension* extension = this->GetExtension();
68 if (!extension)
69 return false;
71 if (command_id == CONFIGURE) {
72 return
73 extensions::ManifestURL::GetOptionsPage(extension).spec().length() > 0;
74 } else if (command_id == NAME) {
75 // The NAME links to the Homepage URL. If the extension doesn't have a
76 // homepage, we just disable this menu item.
77 return extensions::ManifestURL::GetHomepageURL(extension).is_valid();
78 } else if (command_id == INSPECT_POPUP) {
79 WebContents* web_contents =
80 browser_->tab_strip_model()->GetActiveWebContents();
81 if (!web_contents)
82 return false;
84 return extension_action_ &&
85 extension_action_->HasPopup(SessionID::IdForTab(web_contents));
86 } else if (command_id == UNINSTALL) {
87 // Some extension types can not be uninstalled.
88 return extensions::ExtensionSystem::Get(
89 profile_)->management_policy()->UserMayModifySettings(extension, NULL);
91 return true;
94 bool ExtensionContextMenuModel::GetAcceleratorForCommandId(
95 int command_id, ui::Accelerator* accelerator) {
96 return false;
99 void ExtensionContextMenuModel::ExecuteCommand(int command_id,
100 int event_flags) {
101 const Extension* extension = GetExtension();
102 if (!extension)
103 return;
105 switch (command_id) {
106 case NAME: {
107 OpenURLParams params(extensions::ManifestURL::GetHomepageURL(extension),
108 Referrer(), NEW_FOREGROUND_TAB,
109 content::PAGE_TRANSITION_LINK, false);
110 browser_->OpenURL(params);
111 break;
113 case CONFIGURE:
114 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension).is_empty());
115 extensions::ExtensionTabUtil::OpenOptionsPage(extension, browser_);
116 break;
117 case HIDE: {
118 extensions::ExtensionActionAPI::SetBrowserActionVisibility(
119 extensions::ExtensionSystem::Get(profile_)->
120 extension_service()->extension_prefs(),
121 extension->id(),
122 false);
123 break;
125 case UNINSTALL: {
126 AddRef(); // Balanced in Accepted() and Canceled()
127 extension_uninstall_dialog_.reset(
128 ExtensionUninstallDialog::Create(profile_, browser_, this));
129 extension_uninstall_dialog_->ConfirmUninstall(extension);
130 break;
132 case MANAGE: {
133 chrome::ShowExtensions(browser_, extension->id());
134 break;
136 case INSPECT_POPUP: {
137 delegate_->InspectPopup(extension_action_);
138 break;
140 default:
141 NOTREACHED() << "Unknown option";
142 break;
146 void ExtensionContextMenuModel::ExtensionUninstallAccepted() {
147 if (GetExtension()) {
148 extensions::ExtensionSystem::Get(profile_)->extension_service()->
149 UninstallExtension(extension_id_, false, NULL);
151 Release();
154 void ExtensionContextMenuModel::ExtensionUninstallCanceled() {
155 Release();
158 ExtensionContextMenuModel::~ExtensionContextMenuModel() {}
160 void ExtensionContextMenuModel::InitMenu(const Extension* extension) {
161 DCHECK(extension);
163 extensions::ExtensionActionManager* extension_action_manager =
164 extensions::ExtensionActionManager::Get(profile_);
165 extension_action_ = extension_action_manager->GetBrowserAction(*extension);
166 if (!extension_action_)
167 extension_action_ = extension_action_manager->GetPageAction(*extension);
169 std::string extension_name = extension->name();
170 // Ampersands need to be escaped to avoid being treated like
171 // mnemonics in the menu.
172 base::ReplaceChars(extension_name, "&", "&&", &extension_name);
173 AddItem(NAME, base::UTF8ToUTF16(extension_name));
174 AddSeparator(ui::NORMAL_SEPARATOR);
175 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS_MENU_ITEM);
176 AddItem(UNINSTALL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL));
177 if (extension_action_manager->GetBrowserAction(*extension))
178 AddItemWithStringId(HIDE, IDS_EXTENSIONS_HIDE_BUTTON);
179 AddSeparator(ui::NORMAL_SEPARATOR);
180 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSION);
183 const Extension* ExtensionContextMenuModel::GetExtension() const {
184 ExtensionService* extension_service =
185 extensions::ExtensionSystem::Get(profile_)->extension_service();
186 return extension_service->GetExtensionById(extension_id_, false);