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
|
// Copyright (c) 2010 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 "browser_actions_controller.h"
#include <string>
#include "base/sys_string_conversions.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/cocoa/extensions/browser_action_button.h"
#include "chrome/browser/cocoa/extensions/browser_actions_container_view.h"
#include "chrome/browser/cocoa/extensions/extension_popup_controller.h"
#include "chrome/browser/extensions/extension_browser_event_router.h"
#include "chrome/browser/extensions/extension_toolbar_model.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_registrar.h"
// The padding between browser action buttons.
extern const CGFloat kBrowserActionButtonPadding = 3;
NSString* const kBrowserActionsChangedNotification = @"BrowserActionsChanged";
@interface BrowserActionsController(Private)
- (void)createActionButtonForExtension:(Extension*)extension
withIndex:(int)index;
- (void)removeActionButtonForExtension:(Extension*)extension;
- (void)repositionActionButtons;
- (int)currentTabId;
- (bool)shouldDisplayBrowserAction:(Extension*)extension;
@end
// A helper class to proxy extension notifications to the view controller's
// appropriate methods.
class ExtensionsServiceObserverBridge : public NotificationObserver,
public ExtensionToolbarModel::Observer {
public:
ExtensionsServiceObserverBridge(BrowserActionsController* owner,
Profile* profile) : owner_(owner) {
registrar_.Add(this, NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE,
Source<Profile>(profile));
}
// Overridden from NotificationObserver.
void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE: {
ExtensionPopupController* popup = [ExtensionPopupController popup];
if (popup && ![popup isClosing])
[popup close];
break;
}
default:
NOTREACHED() << L"Unexpected notification";
}
}
// ExtensionToolbarModel::Observer implementation.
void BrowserActionAdded(Extension* extension, int index) {
[owner_ createActionButtonForExtension:extension withIndex:index];
}
void BrowserActionRemoved(Extension* extension) {
[owner_ removeActionButtonForExtension:extension];
}
private:
// The object we need to inform when we get a notification. Weak. Owns us.
BrowserActionsController* owner_;
// Used for registering to receive notifications and automatic clean up.
NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(ExtensionsServiceObserverBridge);
};
@implementation BrowserActionsController
- (id)initWithBrowser:(Browser*)browser
containerView:(BrowserActionsContainerView*)container {
DCHECK(browser && container);
if ((self = [super init])) {
browser_ = browser;
profile_ = browser->profile();
observer_.reset(new ExtensionsServiceObserverBridge(self, profile_));
ExtensionsService* extensionsService = profile_->GetExtensionsService();
// |extensionsService| can be NULL in Incognito.
if (extensionsService) {
toolbarModel_ = extensionsService->toolbar_model();
toolbarModel_->AddObserver(observer_.get());
}
containerView_ = container;
[containerView_ setHidden:YES];
buttons_.reset([[NSMutableDictionary alloc] init]);
buttonOrder_.reset([[NSMutableArray alloc] init]);
}
return self;
}
- (void)dealloc {
if (toolbarModel_)
toolbarModel_->RemoveObserver(observer_.get());
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)update {
for (BrowserActionButton* button in [buttons_ allValues]) {
[button setTabId:[self currentTabId]];
[button updateState];
}
}
- (void)createButtons {
// No extensions in incognito mode.
if (!toolbarModel_)
return;
int i = 0;
for (ExtensionList::iterator iter = toolbarModel_->begin();
iter != toolbarModel_->end(); ++iter) {
[self createActionButtonForExtension:*iter withIndex:i++];
}
}
- (void)createActionButtonForExtension:(Extension*)extension
withIndex:(int)index {
if (!extension->browser_action())
return;
if (![self shouldDisplayBrowserAction:extension])
return;
// Show the container if it's the first button. Otherwise it will be shown
// already.
if ([buttons_ count] == 0)
[containerView_ setHidden:NO];
BrowserActionButton* newButton = [[[BrowserActionButton alloc]
initWithExtension:extension
profile:profile_
tabId:[self currentTabId]] autorelease];
[newButton setTarget:self];
[newButton setAction:@selector(browserActionClicked:)];
NSString* buttonKey = base::SysUTF8ToNSString(extension->id());
[buttons_ setObject:newButton forKey:buttonKey];
[buttonOrder_ insertObject:newButton atIndex:index];
[containerView_ addSubview:newButton];
[self repositionActionButtons];
[[NSNotificationCenter defaultCenter]
postNotificationName:kBrowserActionsChangedNotification object:self];
[containerView_ setNeedsDisplay:YES];
}
- (void)removeActionButtonForExtension:(Extension*)extension {
if (!extension->browser_action())
return;
NSString* buttonKey = base::SysUTF8ToNSString(extension->id());
BrowserActionButton* button = [buttons_ objectForKey:buttonKey];
if (!button) {
NOTREACHED();
return;
}
[button removeFromSuperview];
[buttons_ removeObjectForKey:buttonKey];
[buttonOrder_ removeObject:button];
if ([buttons_ count] == 0) {
// No more buttons? Hide the container.
[containerView_ setHidden:YES];
} else {
[self repositionActionButtons];
}
[[NSNotificationCenter defaultCenter]
postNotificationName:kBrowserActionsChangedNotification object:self];
[containerView_ setNeedsDisplay:YES];
}
- (void)repositionActionButtons {
for (NSUInteger i = 0; i < [buttonOrder_ count]; ++i) {
CGFloat xOffset = i * (kBrowserActionWidth + kBrowserActionButtonPadding);
BrowserActionButton* button = [buttonOrder_ objectAtIndex:i];
NSRect buttonFrame = [button frame];
buttonFrame.origin.x = xOffset;
[button setFrame:buttonFrame];
}
}
- (int)buttonCount {
return [buttons_ count];
}
- (int)visibleButtonCount {
int count = 0;
for (BrowserActionButton* button in [buttons_ allValues]) {
if (![button isHidden])
++count;
}
return count;
}
- (void)browserActionClicked:(BrowserActionButton*)sender {
int tabId = [self currentTabId];
if (tabId < 0) {
NOTREACHED() << "No current tab.";
return;
}
ExtensionAction* action = [sender extension]->browser_action();
if (action->HasPopup(tabId)) {
NSString* extensionId = base::SysUTF8ToNSString([sender extension]->id());
// If the extension ID is not valid UTF-8, then the NSString will be nil
// and an exception will be thrown when calling objectForKey below, hosing
// the browser. Check it.
DCHECK(extensionId);
if (!extensionId)
return;
BrowserActionButton* actionButton = [buttons_ objectForKey:extensionId];
NSRect relativeButtonBounds = [[[actionButton window] contentView]
convertRect:[actionButton bounds]
fromView:actionButton];
NSPoint arrowPoint = [[actionButton window] convertBaseToScreen:NSMakePoint(
NSMinX(relativeButtonBounds),
NSMinY(relativeButtonBounds))];
// Adjust the anchor point to be at the center of the browser action button.
arrowPoint.x += kBrowserActionWidth / 2;
[ExtensionPopupController showURL:action->GetPopupUrl(tabId)
inBrowser:browser_
anchoredAt:arrowPoint
arrowLocation:kTopRight];
} else {
ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted(
profile_, action->extension_id(), browser_);
}
}
- (int)currentTabId {
TabContents* selected_tab = browser_->GetSelectedTabContents();
if (!selected_tab)
return -1;
return selected_tab->controller().session_id().id();
}
- (NSView*)browserActionViewForExtension:(Extension*)extension {
for (BrowserActionButton* button in buttonOrder_.get()) {
if ([button extension] == extension)
return button;
}
NOTREACHED();
return nil;
}
- (NSButton*)buttonWithIndex:(int)index {
return [buttonOrder_ objectAtIndex:(NSUInteger)index];
}
- (bool)shouldDisplayBrowserAction:(Extension*)extension {
return (!profile_->IsOffTheRecord() ||
profile_->GetExtensionsService()->
IsIncognitoEnabled(extension->id()));
}
@end
|