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

Side by Side Diff: chrome/browser/extensions/extension_action_context_menu_model.cc

Issue 1107007: Extension context menu refactor (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 9 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2009 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.
4
5 #include "chrome/browser/extensions/extension_action_context_menu_model.h"
6
7 #include "app/l10n_util.h"
8 #include "chrome/browser/browser_list.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/extensions/extension_tabs_module.h"
11 #include "chrome/browser/extensions/extensions_service.h"
12 #include "chrome/browser/pref_service.h"
13 #include "chrome/browser/profile.h"
14 #include "chrome/common/extensions/extension.h"
15 #include "chrome/common/extensions/extension_action.h"
16 #include "chrome/common/extensions/extension_constants.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h"
19 #include "grit/generated_resources.h"
20
21 enum MenuEntries {
22 NAME = 0,
23 CONFIGURE,
24 DISABLE,
25 UNINSTALL,
26 MANAGE,
27 INSPECT_POPUP
28 };
29
30 ExtensionActionContextMenuModel::ExtensionActionContextMenuModel(
31 Extension* extension, ExtensionAction* extension_action, PrefService* prefs,
32 MenuDelegate* delegate)
33 : ALLOW_THIS_IN_INITIALIZER_LIST(SimpleMenuModel(this)),
34 extension_(extension),
35 extension_action_(extension_action),
36 delegate_(delegate) {
37 AddItem(NAME, UTF8ToUTF16(extension->name()));
38 AddSeparator();
39 AddItemWithStringId(CONFIGURE, IDS_EXTENSIONS_OPTIONS);
40 AddItemWithStringId(DISABLE, IDS_EXTENSIONS_DISABLE);
41 AddItemWithStringId(UNINSTALL, IDS_EXTENSIONS_UNINSTALL);
42 AddSeparator();
43 AddItemWithStringId(MANAGE, IDS_MANAGE_EXTENSIONS);
44
45 if (extension_ && delegate_ && prefs &&
46 prefs->GetBoolean(prefs::kExtensionsUIDeveloperMode)) {
47 AddSeparator();
48 AddItemWithStringId(INSPECT_POPUP, IDS_EXTENSION_ACTION_INSPECT_POPUP);
49 }
50 }
51
52 ExtensionActionContextMenuModel::~ExtensionActionContextMenuModel() {
53 }
54
55 bool ExtensionActionContextMenuModel::IsCommandIdChecked(int command_id) const {
56 return false;
57 }
58
59 bool ExtensionActionContextMenuModel::IsCommandIdEnabled(int command_id) const {
60 if (command_id == CONFIGURE) {
61 return extension_->options_url().spec().length() > 0;
62 } else if (command_id == NAME) {
63 // The NAME links to the gallery page, which only makes sense if Google is
64 // hosting the extension. For other 3rd party extensions we don't have a
65 // homepage url, so we just disable this menu item on those cases, at least
66 // for now.
67 return extension_->update_url().DomainIs("google.com");
68 } else if (command_id == INSPECT_POPUP) {
69 if (!delegate_ || !extension_)
70 return false;
71 Browser* browser = BrowserList::GetLastActive();
72 if (!browser)
73 return false;
74 TabContents* contents = browser->GetSelectedTabContents();
75 if (!contents)
76 return false;
77
78 // Different tabs can have different popups set. We need to make sure we
79 // only enable the menu item if the current tab has a popup.
80 return (extension_action_->HasPopup(ExtensionTabUtil::GetTabId(contents)));
81 }
82 return true;
83 }
84
85 bool ExtensionActionContextMenuModel::GetAcceleratorForCommandId(
86 int command_id, menus::Accelerator* accelerator) {
87 return false;
88 }
89
90 void ExtensionActionContextMenuModel::ExecuteCommand(int command_id) {
91 // TODO(finnur): GetLastActive returns NULL in unit tests.
92 Browser* browser = BrowserList::GetLastActive();
93 Profile* profile = browser->profile();
94
95 switch (command_id) {
96 case NAME: {
97 GURL url(std::string(extension_urls::kGalleryBrowsePrefix) +
98 std::string("/detail/") + extension_->id());
99 browser->OpenURL(url, GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK);
100 break;
101 }
102 case CONFIGURE:
103 DCHECK(!extension_->options_url().is_empty());
104 profile->GetExtensionProcessManager()->OpenOptionsPage(extension_,
105 browser);
106 break;
107 case DISABLE: {
108 ExtensionsService* extension_service = profile->GetExtensionsService();
109 extension_service->DisableExtension(extension_->id());
110 break;
111 }
112 case UNINSTALL: {
113 scoped_ptr<SkBitmap> uninstall_icon;
114 Extension::DecodeIcon(extension_, Extension::EXTENSION_ICON_LARGE,
115 &uninstall_icon);
116
117 ExtensionInstallUI client(profile);
118 client.ConfirmUninstall(this, extension_, uninstall_icon.get());
119 break;
120 }
121 case MANAGE: {
122 browser->OpenURL(GURL(chrome::kChromeUIExtensionsURL), GURL(),
123 SINGLETON_TAB, PageTransition::LINK);
124 break;
125 }
126 case INSPECT_POPUP: {
127 if (delegate_)
128 delegate_->ShowPopupForDevToolsWindow(extension_, extension_action_);
129 break;
130 }
131 default:
132 NOTREACHED() << "Unknown option";
133 break;
134 }
135 }
136
137 void ExtensionActionContextMenuModel::InstallUIProceed(bool create_app) {
138 DCHECK(!create_app);
139
140 // TODO(finnur): GetLastActive returns NULL in unit tests.
141 Browser* browser = BrowserList::GetLastActive();
142 std::string id = extension_->id();
143 browser->profile()->GetExtensionsService()->UninstallExtension(id, false);
144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698