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_tab_util.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/chrome_pages.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/extensions/extension_constants.h"
19 #include "chrome/common/extensions/manifest_url_handler.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/common/url_constants.h"
22 #include "content/public/browser/web_contents.h"
23 #include "extensions/browser/extension_prefs.h"
24 #include "extensions/browser/extension_system.h"
25 #include "extensions/browser/management_policy.h"
26 #include "extensions/common/extension.h"
27 #include "grit/chromium_strings.h"
28 #include "grit/generated_resources.h"
29 #include "ui/base/l10n/l10n_util.h"
31 using content::OpenURLParams
;
32 using content::Referrer
;
33 using content::WebContents
;
34 using extensions::Extension
;
36 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension
* extension
,
38 PopupDelegate
* delegate
)
39 : SimpleMenuModel(this),
40 extension_id_(extension
->id()),
42 profile_(browser
->profile()),
46 if (profile_
->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode
) &&
48 AddSeparator(ui::NORMAL_SEPARATOR
);
49 AddItemWithStringId(INSPECT_POPUP
, IDS_EXTENSION_ACTION_INSPECT_POPUP
);
53 ExtensionContextMenuModel::ExtensionContextMenuModel(const Extension
* extension
,
55 : SimpleMenuModel(this),
56 extension_id_(extension
->id()),
58 profile_(browser
->profile()),
63 bool ExtensionContextMenuModel::IsCommandIdChecked(int command_id
) const {
67 bool ExtensionContextMenuModel::IsCommandIdEnabled(int command_id
) const {
68 const Extension
* extension
= this->GetExtension();
72 if (command_id
== CONFIGURE
) {
74 extensions::ManifestURL::GetOptionsPage(extension
).spec().length() > 0;
75 } else if (command_id
== NAME
) {
76 // The NAME links to the Homepage URL. If the extension doesn't have a
77 // homepage, we just disable this menu item.
78 return extensions::ManifestURL::GetHomepageURL(extension
).is_valid();
79 } else if (command_id
== INSPECT_POPUP
) {
80 WebContents
* web_contents
=
81 browser_
->tab_strip_model()->GetActiveWebContents();
85 return extension_action_
&&
86 extension_action_
->HasPopup(SessionID::IdForTab(web_contents
));
87 } else if (command_id
== UNINSTALL
) {
88 // Some extension types can not be uninstalled.
89 return extensions::ExtensionSystem::Get(
90 profile_
)->management_policy()->UserMayModifySettings(extension
, NULL
);
95 bool ExtensionContextMenuModel::GetAcceleratorForCommandId(
96 int command_id
, ui::Accelerator
* accelerator
) {
100 void ExtensionContextMenuModel::ExecuteCommand(int command_id
,
102 const Extension
* extension
= GetExtension();
106 switch (command_id
) {
108 OpenURLParams
params(extensions::ManifestURL::GetHomepageURL(extension
),
109 Referrer(), NEW_FOREGROUND_TAB
,
110 content::PAGE_TRANSITION_LINK
, false);
111 browser_
->OpenURL(params
);
115 DCHECK(!extensions::ManifestURL::GetOptionsPage(extension
).is_empty());
116 extensions::ExtensionTabUtil::OpenOptionsPage(extension
, browser_
);
119 extensions::ExtensionActionAPI::SetBrowserActionVisibility(
120 extensions::ExtensionPrefs::Get(profile_
), extension
->id(), false);
124 AddRef(); // Balanced in Accepted() and Canceled()
125 extension_uninstall_dialog_
.reset(
126 ExtensionUninstallDialog::Create(profile_
, browser_
, this));
127 extension_uninstall_dialog_
->ConfirmUninstall(extension
);
131 chrome::ShowExtensions(browser_
, extension
->id());
134 case INSPECT_POPUP
: {
135 delegate_
->InspectPopup(extension_action_
);
139 NOTREACHED() << "Unknown option";
144 void ExtensionContextMenuModel::ExtensionUninstallAccepted() {
145 if (GetExtension()) {
146 extensions::ExtensionSystem::Get(profile_
)->extension_service()->
147 UninstallExtension(extension_id_
, false, NULL
);
152 void ExtensionContextMenuModel::ExtensionUninstallCanceled() {
156 ExtensionContextMenuModel::~ExtensionContextMenuModel() {}
158 void ExtensionContextMenuModel::InitMenu(const Extension
* extension
) {
161 extensions::ExtensionActionManager
* extension_action_manager
=
162 extensions::ExtensionActionManager::Get(profile_
);
163 extension_action_
= extension_action_manager
->GetBrowserAction(*extension
);
164 if (!extension_action_
)
165 extension_action_
= extension_action_manager
->GetPageAction(*extension
);
167 std::string extension_name
= extension
->name();
168 // Ampersands need to be escaped to avoid being treated like
169 // mnemonics in the menu.
170 base::ReplaceChars(extension_name
, "&", "&&", &extension_name
);
171 AddItem(NAME
, base::UTF8ToUTF16(extension_name
));
172 AddSeparator(ui::NORMAL_SEPARATOR
);
173 AddItemWithStringId(CONFIGURE
, IDS_EXTENSIONS_OPTIONS_MENU_ITEM
);
174 AddItem(UNINSTALL
, l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL
));
175 if (extension_action_manager
->GetBrowserAction(*extension
))
176 AddItemWithStringId(HIDE
, IDS_EXTENSIONS_HIDE_BUTTON
);
177 AddSeparator(ui::NORMAL_SEPARATOR
);
178 AddItemWithStringId(MANAGE
, IDS_MANAGE_EXTENSION
);
181 const Extension
* ExtensionContextMenuModel::GetExtension() const {
182 ExtensionService
* extension_service
=
183 extensions::ExtensionSystem::Get(profile_
)->extension_service();
184 return extension_service
->GetExtensionById(extension_id_
, false);