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
|
// Copyright (c) 2012 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 "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_tab_util.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_finder.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"
#include "chrome/browser/ui/cocoa/last_active_browser_cocoa.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/chrome_notification_types.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/public/browser/notification_observer.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_mac.h"
using content::OpenURLParams;
using content::Referrer;
using content::WebContents;
using extensions::Extension;
// 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(
ExtensionUninstallDialog::Create(profile, this));
extension_uninstall_dialog_->ConfirmUninstall(extension_);
}
~AsyncUninstaller() {}
// ExtensionUninstallDialog::Delegate:
virtual void ExtensionUninstallAccepted() {
profile_->GetExtensionService()->
UninstallExtension(extension_->id(), false, NULL);
}
virtual void ExtensionUninstallCanceled() {}
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);
};
@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,
};
int CurrentTabId() {
Browser* browser = browser::GetLastActiveBrowser();
if(!browser)
return -1;
WebContents* contents = browser->GetActiveWebContents();
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_MENU_ITEM),
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]];
// 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];
}
}
}
return self;
}
return nil;
}
- (void)dispatch:(id)menuItem {
Browser* browser = browser::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());
OpenURLParams params(
url, Referrer(), NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_LINK,
false);
browser->OpenURL(params);
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(),
Extension::DISABLE_USER_ACTION);
break;
}
case kExtensionContextUninstall: {
uninstaller_.reset(new AsyncUninstaller(extension_, profile_));
break;
}
case kExtensionContextHide: {
ExtensionService* extension_service = profile_->GetExtensionService();
extension_service->extension_prefs()->
SetBrowserActionVisibility(extension_, false);
break;
}
case kExtensionContextManage: {
browser->ShowExtensionsTab();
break;
}
default:
NOTREACHED();
break;
}
}
- (BOOL)validateMenuItem:(NSMenuItem*)menuItem {
if ([menuItem tag] == kExtensionContextOptions) {
// Disable 'Options' if there are no options to set.
return extension_->options_url().spec().length() > 0;
}
return YES;
}
@end
|