[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / extensions / extension_action_context_menu_controller.mm
blob591b792a7b389986425a869503bc1a19c688e43a
1 // Copyright 2013 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 #import "chrome/browser/ui/cocoa/extensions/extension_action_context_menu_controller.h"
7 #include "base/strings/sys_string_conversions.h"
8 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
9 #include "chrome/browser/extensions/extension_action_manager.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_tab_util.h"
12 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/chrome_pages.h"
16 #include "chrome/browser/ui/cocoa/browser_window_cocoa.h"
17 #include "chrome/browser/ui/cocoa/browser_window_controller.h"
18 #include "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h"
19 #include "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
20 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
21 #include "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
22 #include "chrome/common/extensions/extension_constants.h"
23 #include "chrome/common/extensions/manifest_url_handler.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/common/url_constants.h"
26 #include "content/public/browser/web_contents.h"
27 #include "extensions/browser/extension_prefs.h"
28 #include "extensions/browser/extension_system.h"
29 #include "extensions/common/extension.h"
30 #include "grit/chromium_strings.h"
31 #include "grit/generated_resources.h"
32 #include "ui/base/l10n/l10n_util_mac.h"
34 using content::OpenURLParams;
35 using content::Referrer;
36 using content::WebContents;
37 using extensions::Extension;
39 // A class that shows a confirmation dialog to uninstall the given extension.
40 // Also acts as the extension's UI delegate in order to display the dialog.
41 class AsyncUninstaller : public ExtensionUninstallDialog::Delegate {
42  public:
43   AsyncUninstaller(const Extension* extension, Browser* browser)
44       : extension_(extension),
45         profile_(browser->profile()) {
46     extension_uninstall_dialog_.reset(
47         ExtensionUninstallDialog::Create(profile_, browser, this));
48     extension_uninstall_dialog_->ConfirmUninstall(extension_);
49   }
51   virtual ~AsyncUninstaller() {}
53   // ExtensionUninstallDialog::Delegate:
54   virtual void ExtensionUninstallAccepted() OVERRIDE {
55     extensions::ExtensionSystem::Get(profile_)->extension_service()->
56         UninstallExtension(extension_->id(), false, NULL);
57   }
58   virtual void ExtensionUninstallCanceled() OVERRIDE {}
60  private:
61   // The extension that's being uninstalled.
62   const Extension* extension_;
64   // The current profile. Weak.
65   Profile* profile_;
67   scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_;
69   DISALLOW_COPY_AND_ASSIGN(AsyncUninstaller);
72 @interface ExtensionActionContextMenuController ()
73 - (void)onExtensionName:(id)sender;
74 - (void)onOptions:(id)sender;
75 - (void)onUninstall:(id)sender;
76 - (void)onHide:(id)sender;
77 - (void)onManage:(id)sender;
78 - (void)onInspect:(id)sender;
79 @end
81 @implementation ExtensionActionContextMenuController
83 - (id)initWithExtension:(const Extension*)extension
84                 browser:(Browser*)browser
85         extensionAction:(ExtensionAction*)action{
86   if ((self = [super init])) {
87     action_ = action;
88     extension_ = extension;
89     browser_ = browser;
90   }
91   return self;
94 - (void)populateMenu:(NSMenu*)menu {
95   [menu setAutoenablesItems:NO];
97   // Extension name.
98   NSMenuItem* item =
99       [menu addItemWithTitle:base::SysUTF8ToNSString(extension_->name())
100                       action:@selector(onExtensionName:)
101                keyEquivalent:@""];
102   [item setTarget:self];
103   [item setEnabled:extensions::ManifestURL::GetHomepageURL(
104       extension_).is_valid()];
106   // Separator.
107   [menu addItem:[NSMenuItem separatorItem]];
109   // Options.
110   item = [menu addItemWithTitle:
111               l10n_util::GetNSStringWithFixup(IDS_EXTENSIONS_OPTIONS_MENU_ITEM)
112                          action:@selector(onOptions:)
113                   keyEquivalent:@""];
114   [item setTarget:self];
115   [item setEnabled:extensions::ManifestURL::GetOptionsPage(
116       extension_).spec().length() > 0];
119   // Uninstall.
120   item = [menu addItemWithTitle:
121               l10n_util::GetNSStringWithFixup(IDS_EXTENSIONS_UNINSTALL)
122                          action:@selector(onUninstall:)
123                   keyEquivalent:@""];
124   [item setTarget:self];
126   // Hide. Only used for browser actions.
127   if (extensions::ExtensionActionManager::Get(
128           browser_->profile())->GetBrowserAction(*extension_)) {
129     item = [menu addItemWithTitle:
130                 l10n_util::GetNSStringWithFixup(IDS_EXTENSIONS_HIDE_BUTTON)
131                            action:@selector(onHide:)
132                     keyEquivalent:@""];
133     [item setTarget:self];
134   }
136   // Separator.
137   [menu addItem:[NSMenuItem separatorItem]];
139   // Manage.
140   item = [menu addItemWithTitle:
141               l10n_util::GetNSStringWithFixup(IDS_MANAGE_EXTENSION)
142                          action:@selector(onManage:)
143                   keyEquivalent:@""];
144   [item setTarget:self];
146   // Inspect.
147   PrefService* service = browser_->profile()->GetPrefs();
148   bool devMode = service->GetBoolean(prefs::kExtensionsUIDeveloperMode);
149   if (devMode) {
150     item = [menu addItemWithTitle:l10n_util::GetNSStringWithFixup(
151                 IDS_EXTENSION_ACTION_INSPECT_POPUP)
152                            action:@selector(onInspect:)
153                     keyEquivalent:@""];
154     [item setTarget:self];
155   }
158 - (void)onExtensionName:(id)sender {
159   GURL url(std::string(extension_urls::kGalleryBrowsePrefix) +
160            std::string("/detail/") + extension_->id());
161   OpenURLParams params(url,
162                        Referrer(),
163                        NEW_FOREGROUND_TAB,
164                        content::PAGE_TRANSITION_LINK,
165                        false);
166   browser_->OpenURL(params);
169 - (void)onOptions:(id)sender {
170   DCHECK(!extensions::ManifestURL::GetOptionsPage(extension_).is_empty());
171   extensions::ExtensionTabUtil::OpenOptionsPage(extension_, browser_);
174 - (void)onUninstall:(id)sender {
175   uninstaller_.reset(new AsyncUninstaller(extension_, browser_));
178 - (void)onHide:(id)sender {
179   extensions::ExtensionActionAPI::SetBrowserActionVisibility(
180       extensions::ExtensionPrefs::Get(browser_->profile()),
181       extension_->id(),
182       false);
185 - (void)onManage:(id)sender {
186   chrome::ShowExtensions(browser_, extension_->id());
189 - (void)onInspect:(id)sender {
190   BrowserWindowCocoa* window =
191       static_cast<BrowserWindowCocoa*>(browser_->window());
192   ToolbarController* toolbarController =
193       [window->cocoa_controller() toolbarController];
194   LocationBarViewMac* locationBarView = [toolbarController locationBarBridge];
196   extensions::ExtensionActionManager* actionManager =
197       extensions::ExtensionActionManager::Get(browser_->profile());
198   NSPoint popupPoint = NSZeroPoint;
199   if (actionManager->GetPageAction(*extension_) == action_) {
200     popupPoint = locationBarView->GetPageActionBubblePoint(action_);
201   } else if (actionManager->GetBrowserAction(*extension_) == action_) {
202     BrowserActionsController* controller =
203         [toolbarController browserActionsController];
204     popupPoint = [controller popupPointForBrowserAction:extension_];
205   } else {
206     NOTREACHED() << "action_ is not a page action or browser action?";
207   }
209   content::WebContents* activeTab =
210       browser_->tab_strip_model()->GetActiveWebContents();
211   if (!activeTab)
212     return;
214   int tabId = extensions::ExtensionTabUtil::GetTabId(activeTab);
216   GURL url = action_->GetPopupUrl(tabId);
217   if (!url.is_valid())
218     return;
220   [ExtensionPopupController showURL:url
221                           inBrowser:browser_
222                          anchoredAt:popupPoint
223                       arrowLocation:info_bubble::kTopRight
224                             devMode:YES];
227 @end