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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
// Copyright (c) 2009 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 "app/gfx/canvas_paint.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/cocoa/extensions/extension_popup_controller.h"
#include "chrome/browser/cocoa/toolbar_button_cell.h"
#include "chrome/browser/extensions/extension_browser_event_router.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/extensions/extension_tabs_module.h"
#include "chrome/browser/extensions/image_loading_tracker.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"
#include "skia/ext/skia_utils_mac.h"
static const CGFloat kBrowserActionBadgeOriginYOffset = -4;
// Since the container is the maximum height of the toolbar, we have to move the
// buttons up by this amount in order to have them look vertically centered
// within the toolbar.
static const CGFloat kBrowserActionOriginYOffset = 5;
// The size of each button on the toolbar.
static const CGFloat kBrowserActionHeight = 27;
extern const CGFloat kBrowserActionWidth = 29;
// The padding between browser action buttons.
extern const CGFloat kBrowserActionButtonPadding = 3;
NSString* const kBrowserActionsChangedNotification = @"BrowserActionsChanged";
@interface BrowserActionBadgeView : NSView {
@private
// The current tab ID used when drawing the badge.
int tabId_;
// The action we're drawing the badge for. Weak.
ExtensionAction* extensionAction_;
}
@property(readwrite, nonatomic) int tabId;
@property(readwrite, nonatomic) ExtensionAction* extensionAction;
@end
@implementation BrowserActionBadgeView
- (void)drawRect:(NSRect)dirtyRect {
// CanvasPaint draws its content to the current NSGraphicsContext in its
// destructor. If anything needs to be drawn afterwards, then enclose this
// in a nested block.
NSRect badgeBounds = [self bounds];
badgeBounds.origin.y += kBrowserActionBadgeOriginYOffset;
gfx::CanvasPaint canvas(badgeBounds, false);
canvas.set_composite_alpha(true);
gfx::Rect boundingRect(NSRectToCGRect(badgeBounds));
extensionAction_->PaintBadge(&canvas, boundingRect, tabId_);
}
@synthesize tabId = tabId_;
@synthesize extensionAction = extensionAction_;
@end
class ExtensionImageTrackerBridge;
@interface BrowserActionButton : NSButton {
@private
scoped_ptr<ExtensionImageTrackerBridge> imageLoadingBridge_;
scoped_nsobject<NSImage> defaultIcon_;
scoped_nsobject<NSImage> tabSpecificIcon_;
scoped_nsobject<NSView> badgeView_;
// The extension for this button. Weak.
Extension* extension_;
// Weak. Owns us.
BrowserActionsController* controller_;
}
- (id)initWithExtension:(Extension*)extension
controller:(BrowserActionsController*)controller
xOffset:(int)xOffset;
- (void)setDefaultIcon:(NSImage*)image;
- (void)setTabSpecificIcon:(NSImage*)image;
- (void)updateState;
@property(readonly, nonatomic) Extension* extension;
@end
// A helper class to bridge the asynchronous Skia bitmap loading mechanism to
// the extension's button.
class ExtensionImageTrackerBridge : public NotificationObserver,
public ImageLoadingTracker::Observer {
public:
ExtensionImageTrackerBridge(BrowserActionButton* owner, Extension* extension)
: owner_(owner),
tracker_(NULL) {
// The Browser Action API does not allow the default icon path to be
// changed at runtime, so we can load this now and cache it.
std::string path = extension->browser_action()->default_icon_path();
if (!path.empty()) {
tracker_ = new ImageLoadingTracker(this, 1);
tracker_->PostLoadImageTask(extension->GetResource(path),
gfx::Size(Extension::kBrowserActionIconMaxSize,
Extension::kBrowserActionIconMaxSize));
}
registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
Source<ExtensionAction>(extension->browser_action()));
}
~ExtensionImageTrackerBridge() {
if (tracker_)
tracker_->StopTrackingImageLoad();
}
// ImageLoadingTracker::Observer implementation.
void OnImageLoaded(SkBitmap* image, size_t index) {
if (image)
[owner_ setDefaultIcon:gfx::SkBitmapToNSImage(*image)];
tracker_ = NULL;
[owner_ updateState];
}
// Overridden from NotificationObserver.
void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type == NotificationType::EXTENSION_BROWSER_ACTION_UPDATED)
[owner_ updateState];
else
NOTREACHED();
}
private:
// Weak. Owns us.
BrowserActionButton* owner_;
// Loads the button's icons for us on the file thread. Weak.
ImageLoadingTracker* tracker_;
// Used for registering to receive notifications and automatic clean up.
NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(ExtensionImageTrackerBridge);
};
@implementation BrowserActionButton
- (id)initWithExtension:(Extension*)extension
controller:(BrowserActionsController*)controller
xOffset:(int)xOffset {
NSRect frame = NSMakeRect(xOffset,
kBrowserActionOriginYOffset,
kBrowserActionWidth,
kBrowserActionHeight);
if ((self = [super initWithFrame:frame])) {
ToolbarButtonCell* cell = [[[ToolbarButtonCell alloc] init] autorelease];
// [NSButton setCell:] warns to NOT use setCell: other than in the
// initializer of a control. However, we are using a basic
// NSButton whose initializer does not take an NSCell as an
// object. To honor the assumed semantics, we do nothing with
// NSButton between alloc/init and setCell:.
[self setCell:cell];
[self setTitle:@""];
[self setButtonType:NSMomentaryChangeButton];
[self setShowsBorderOnlyWhileMouseInside:YES];
[self setTarget:controller];
[self setAction:@selector(browserActionClicked:)];
extension_ = extension;
controller_ = controller;
imageLoadingBridge_.reset(new ExtensionImageTrackerBridge(self, extension));
NSRect badgeFrame = [self bounds];
badgeView_.reset([[BrowserActionBadgeView alloc] initWithFrame:badgeFrame]);
[badgeView_ setTabId:[controller currentTabId]];
[badgeView_ setExtensionAction:extension->browser_action()];
[self addSubview:badgeView_];
[self updateState];
}
return self;
}
- (void)setDefaultIcon:(NSImage*)image {
defaultIcon_.reset([image retain]);
}
- (void)setTabSpecificIcon:(NSImage*)image {
tabSpecificIcon_.reset([image retain]);
}
- (void)updateState {
int tabId = [controller_ currentTabId];
if (tabId < 0)
return;
std::string tooltip = extension_->browser_action()->GetTitle(tabId);
if (tooltip.empty()) {
[self setToolTip:nil];
} else {
[self setToolTip:base::SysUTF8ToNSString(tooltip)];
}
SkBitmap image = extension_->browser_action()->GetIcon(tabId);
if (!image.isNull()) {
[self setTabSpecificIcon:gfx::SkBitmapToNSImage(image)];
[self setImage:tabSpecificIcon_];
} else if (defaultIcon_) {
[self setImage:defaultIcon_];
}
[badgeView_ setTabId:tabId];
[self setNeedsDisplay:YES];
}
@synthesize extension = extension_;
@end
@interface BrowserActionsController(Private)
- (void)createActionButtonForExtension:(Extension*)extension;
- (void)removeActionButtonForExtension:(Extension*)extension;
- (void)repositionActionButtons;
@end
// A helper class to proxy extension notifications to the view controller's
// appropriate methods.
class ExtensionsServiceObserverBridge : public NotificationObserver {
public:
ExtensionsServiceObserverBridge(BrowserActionsController* owner,
Profile* profile) : owner_(owner) {
registrar_.Add(this, NotificationType::EXTENSION_LOADED,
Source<Profile>(profile));
registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
Source<Profile>(profile));
registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
Source<Profile>(profile));
registrar_.Add(this, NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE,
Source<Profile>(profile));
}
// Runs |owner_|'s method corresponding to the event type received from the
// notification system.
// Overridden from NotificationObserver.
void Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::EXTENSION_LOADED: {
Extension* extension = Details<Extension>(details).ptr();
[owner_ createActionButtonForExtension:extension];
[owner_ browserActionVisibilityHasChanged];
break;
}
case NotificationType::EXTENSION_UNLOADED:
case NotificationType::EXTENSION_UNLOADED_DISABLED: {
Extension* extension = Details<Extension>(details).ptr();
[owner_ removeActionButtonForExtension:extension];
[owner_ browserActionVisibilityHasChanged];
break;
}
case NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE: {
ExtensionPopupController* popup = [owner_ popup];
if (popup && Details<ExtensionHost>([popup host]) == details)
[owner_ hidePopup];
break;
}
default:
NOTREACHED() << L"Unexpected notification";
}
}
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:(NSView*)container {
DCHECK(browser && container);
if ((self = [super init])) {
browser_ = browser;
profile_ = browser->profile();
containerView_ = container;
[containerView_ setHidden:YES];
observer_.reset(new ExtensionsServiceObserverBridge(self, profile_));
buttons_.reset([[NSMutableDictionary alloc] init]);
buttonOrder_.reset([[NSMutableArray alloc] init]);
}
return self;
}
- (void)update {
for (BrowserActionButton* button in [buttons_ allValues]) {
[button updateState];
}
}
- (void)hidePopup {
[popupController_ close];
popupController_ = nil;
}
- (ExtensionPopupController*)popup {
return popupController_;
}
- (void)browserActionVisibilityHasChanged {
[containerView_ setNeedsDisplay:YES];
}
- (void)createButtons {
ExtensionsService* extensionsService = profile_->GetExtensionsService();
if (!extensionsService) // |extensionsService| can be NULL in Incognito.
return;
for (size_t i = 0; i < extensionsService->extensions()->size(); ++i) {
Extension* extension = extensionsService->GetExtensionById(
extensionsService->extensions()->at(i)->id(), false);
if (extension->browser_action()) {
[self createActionButtonForExtension:extension];
}
}
}
- (void)createActionButtonForExtension:(Extension*)extension {
if (!extension->browser_action())
return;
if ([buttons_ count] == 0) {
// Only call if we're adding our first button, otherwise it will be shown
// already.
[containerView_ setHidden:NO];
}
int xOffset =
[buttons_ count] * (kBrowserActionWidth + kBrowserActionButtonPadding);
BrowserActionButton* newButton =
[[[BrowserActionButton alloc] initWithExtension:extension
controller:self
xOffset:xOffset] autorelease];
NSString* buttonKey = base::SysUTF8ToNSString(extension->id());
[buttons_ setObject:newButton forKey:buttonKey];
[buttonOrder_ addObject:newButton];
[containerView_ addSubview:newButton];
[[NSNotificationCenter defaultCenter]
postNotificationName:kBrowserActionsChangedNotification object:self];
}
- (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 {
// repositionActionButtons only needs to be called if removing a browser
// action button because adding one will always append to the end of the
// container, while removing one may require that those to the right of it
// be shifted to the left.
[self repositionActionButtons];
}
[[NSNotificationCenter defaultCenter]
postNotificationName:kBrowserActionsChangedNotification object:self];
}
- (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];
}
- (void)browserActionClicked:(BrowserActionButton*)sender {
ExtensionAction* action = [sender extension]->browser_action();
if (action->has_popup()) {
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;
// Close any existing (or loading) popup owned by this controller.
if (popupController_)
[self hidePopup];
popupController_ = [ExtensionPopupController showURL:action->popup_url()
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();
}
- (NSButton*)buttonWithIndex:(int)index {
return [buttonOrder_ objectAtIndex:(NSUInteger)index];
}
@end
|