summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/extensions/extension_action_context_menu.mm
blob: 989c472a5fedcbde6304103f365918282b7cde90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/extensions/extension_action_context_menu.h"

#include "base/sys_string_conversions.h"
#include "base/task.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_tabs_module.h"
#include "chrome/browser/extensions/extension_uninstall_dialog.h"
#include "chrome/browser/prefs/pref_change_registrar.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/cocoa/browser_window_cocoa.h"
#include "chrome/browser/ui/cocoa/browser_window_controller.h"
#include "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h"
#include "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
#include "chrome/browser/ui/cocoa/info_bubble_view.h"
#import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
#include "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_action.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "content/common/notification_details.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_source.h"
#include "content/common/notification_type.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util_mac.h"

// A class that loads the extension icon on the I/O thread before showing the
// confirmation dialog to uninstall the given extension.
// Also acts as the extension's UI delegate in order to display the dialog.
class AsyncUninstaller : public ExtensionUninstallDialog::Delegate {
 public:
  AsyncUninstaller(const Extension* extension, Profile* profile)
      : extension_(extension),
        profile_(profile) {
    extension_uninstall_dialog_.reset(new ExtensionUninstallDialog(profile));
    extension_uninstall_dialog_->ConfirmUninstall(this, extension_);
  }

  ~AsyncUninstaller() {}

  // ExtensionUninstallDialog::Delegate:
  virtual void ExtensionDialogAccepted() {
    profile_->GetExtensionService()->
        UninstallExtension(extension_->id(), false, NULL);
  }
  virtual void ExtensionDialogCanceled() {}

 private:
  // The extension that we're loading the icon for. Weak.
  const Extension* extension_;

  // The current profile. Weak.
  Profile* profile_;

  scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_;

  DISALLOW_COPY_AND_ASSIGN(AsyncUninstaller);
};

namespace extension_action_context_menu {

class DevmodeObserver : public NotificationObserver {
 public:
  DevmodeObserver(ExtensionActionContextMenu* menu,
                             PrefService* service)
      : menu_(menu), pref_service_(service) {
    registrar_.Init(pref_service_);
    registrar_.Add(prefs::kExtensionsUIDeveloperMode, this);
  }
  virtual ~DevmodeObserver() {}

  void Observe(NotificationType type,
               const NotificationSource& source,
               const NotificationDetails& details) {
    if (type == NotificationType::PREF_CHANGED)
      [menu_ updateInspectorItem];
    else
      NOTREACHED();
  }

 private:
  ExtensionActionContextMenu* menu_;
  PrefService* pref_service_;
  PrefChangeRegistrar registrar_;
};

class ProfileObserverBridge : public NotificationObserver {
 public:
  ProfileObserverBridge(ExtensionActionContextMenu* owner,
                        const Profile* profile)
      : owner_(owner),
        profile_(profile) {
    registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
                   Source<Profile>(profile));
  }

  ~ProfileObserverBridge() {}

  // Overridden from NotificationObserver
  void Observe(NotificationType type,
               const NotificationSource& source,
               const NotificationDetails& details) {
    if (type == NotificationType::PROFILE_DESTROYED &&
        source == Source<Profile>(profile_)) {
      [owner_ invalidateProfile];
    }
  }

 private:
  ExtensionActionContextMenu* owner_;
  const Profile* profile_;
  NotificationRegistrar registrar_;
};

}  // namespace extension_action_context_menu

@interface ExtensionActionContextMenu(Private)
// Callback for the context menu items.
- (void)dispatch:(id)menuItem;
@end

@implementation ExtensionActionContextMenu

namespace {
// Enum of menu item choices to their respective indices.
// NOTE: You MUST keep this in sync with the |menuItems| NSArray below.
enum {
  kExtensionContextName = 0,
  kExtensionContextOptions = 2,
  kExtensionContextDisable = 3,
  kExtensionContextUninstall = 4,
  kExtensionContextHide = 5,
  kExtensionContextManage = 7,
  kExtensionContextInspect = 8
};

int CurrentTabId() {
  Browser* browser = BrowserList::GetLastActive();
  if(!browser)
    return -1;
  TabContents* contents = browser->GetSelectedTabContents();
  if (!contents)
    return -1;
  return ExtensionTabUtil::GetTabId(contents);
}

}  // namespace

- (id)initWithExtension:(const Extension*)extension
                profile:(Profile*)profile
        extensionAction:(ExtensionAction*)action{
  if ((self = [super initWithTitle:@""])) {
    action_ = action;
    extension_ = extension;
    profile_ = profile;

    NSArray* menuItems = [NSArray arrayWithObjects:
        base::SysUTF8ToNSString(extension->name()),
        [NSMenuItem separatorItem],
        l10n_util::GetNSStringWithFixup(IDS_EXTENSIONS_OPTIONS),
        l10n_util::GetNSStringWithFixup(IDS_EXTENSIONS_DISABLE),
        l10n_util::GetNSStringWithFixup(IDS_EXTENSIONS_UNINSTALL),
        l10n_util::GetNSStringWithFixup(IDS_EXTENSIONS_HIDE_BUTTON),
        [NSMenuItem separatorItem],
        l10n_util::GetNSStringWithFixup(IDS_MANAGE_EXTENSIONS),
        nil];

    for (id item in menuItems) {
      if ([item isKindOfClass:[NSMenuItem class]]) {
        [self addItem:item];
      } else if ([item isKindOfClass:[NSString class]]) {
        NSMenuItem* itemObj = [self addItemWithTitle:item
                                              action:@selector(dispatch:)
                                       keyEquivalent:@""];
        // The tag should correspond to the enum above.
        // NOTE: The enum and the order of the menu items MUST be in sync.
        [itemObj setTag:[self indexOfItem:itemObj]];

        // Disable the 'Options' item if there are no options to set.
        if ([itemObj tag] == kExtensionContextOptions &&
            extension_->options_url().spec().length() <= 0) {
          // Setting the target to nil will disable the item. For some reason
          // setEnabled:NO does not work.
          [itemObj setTarget:nil];
        } else {
          [itemObj setTarget:self];
        }

        // Only browser actions can have their button hidden. Page actions
        // should never show the "Hide" menu item.
        if ([itemObj tag] == kExtensionContextHide &&
            !extension->browser_action()) {
          [itemObj setTarget:nil];  // Item is disabled.
          [itemObj setHidden:YES];  // Item is hidden.
        } else {
          [itemObj setTarget:self];
        }
      }
    }

    NSString* inspectorTitle =
        l10n_util::GetNSStringWithFixup(IDS_EXTENSION_ACTION_INSPECT_POPUP);
    inspectorItem_.reset([[NSMenuItem alloc] initWithTitle:inspectorTitle
                                                    action:@selector(dispatch:)
                                             keyEquivalent:@""]);
    [inspectorItem_.get() setTarget:self];
    [inspectorItem_.get() setTag:kExtensionContextInspect];

    PrefService* service = profile_->GetPrefs();
    observer_.reset(
        new extension_action_context_menu::DevmodeObserver(self, service));
    profile_observer_.reset(
        new extension_action_context_menu::ProfileObserverBridge(self,
                                                                 profile));

    [self updateInspectorItem];
    return self;
  }
  return nil;
}

- (void)updateInspectorItem {
  PrefService* service = profile_->GetPrefs();
  bool devmode = service->GetBoolean(prefs::kExtensionsUIDeveloperMode);
  if (devmode) {
    if ([self indexOfItem:inspectorItem_.get()] == -1)
      [self addItem:inspectorItem_.get()];
  } else {
    if ([self indexOfItem:inspectorItem_.get()] != -1)
      [self removeItem:inspectorItem_.get()];
  }
}

- (void)dispatch:(id)menuItem {
  Browser* browser = BrowserList::FindBrowserWithProfile(profile_);
  if (!browser)
    return;

  NSMenuItem* item = (NSMenuItem*)menuItem;
  switch ([item tag]) {
    case kExtensionContextName: {
      GURL url(std::string(extension_urls::kGalleryBrowsePrefix) +
               std::string("/detail/") + extension_->id());
      browser->OpenURL(url, GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK);
      break;
    }
    case kExtensionContextOptions: {
      DCHECK(!extension_->options_url().is_empty());
      profile_->GetExtensionProcessManager()->OpenOptionsPage(extension_,
                                                              browser);
      break;
    }
    case kExtensionContextDisable: {
      ExtensionService* extensionService = profile_->GetExtensionService();
      if (!extensionService)
        return; // Incognito mode.
      extensionService->DisableExtension(extension_->id());
      break;
    }
    case kExtensionContextUninstall: {
      uninstaller_.reset(new AsyncUninstaller(extension_, profile_));
      break;
    }
    case kExtensionContextHide: {
      ExtensionService* extension_service = profile_->GetExtensionService();
      extension_service->SetBrowserActionVisibility(extension_, false);
      break;
    }
    case kExtensionContextManage: {
      browser->OpenURL(GURL(chrome::kChromeUIExtensionsURL), GURL(),
                       NEW_FOREGROUND_TAB, PageTransition::LINK);
      break;
    }
    case kExtensionContextInspect: {
      BrowserWindowCocoa* window =
          static_cast<BrowserWindowCocoa*>(browser->window());
      ToolbarController* toolbarController =
          [window->cocoa_controller() toolbarController];
      LocationBarViewMac* locationBarView =
          [toolbarController locationBarBridge];

      NSPoint popupPoint = NSZeroPoint;
      if (extension_->page_action() == action_) {
        popupPoint = locationBarView->GetPageActionBubblePoint(action_);

      } else if (extension_->browser_action() == action_) {
        BrowserActionsController* controller =
            [toolbarController browserActionsController];
        popupPoint = [controller popupPointForBrowserAction:extension_];

      } else {
        NOTREACHED() << "action_ is not a page action or browser action?";
      }

      int tabId = CurrentTabId();
      GURL url = action_->GetPopupUrl(tabId);
      DCHECK(url.is_valid());
      [ExtensionPopupController showURL:url
                              inBrowser:BrowserList::GetLastActive()
                             anchoredAt:popupPoint
                          arrowLocation:info_bubble::kTopRight
                                devMode:YES];
      break;
    }
    default:
      NOTREACHED();
      break;
  }
}

- (BOOL)validateMenuItem:(NSMenuItem*)menuItem {
  if([menuItem isEqualTo:inspectorItem_.get()]) {
    return action_ && action_->HasPopup(CurrentTabId());
  }
  return YES;
}

- (void)invalidateProfile {
  observer_.reset();
  profile_ = NULL;
}

@end