OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h" | 5 #import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
11 #include "chrome/browser/chrome_notification_types.h" | |
12 #include "chrome/browser/extensions/extension_action.h" | 11 #include "chrome/browser/extensions/extension_action.h" |
13 #include "chrome/browser/extensions/extension_action_manager.h" | 12 #include "chrome/browser/extensions/extension_action_manager.h" |
14 #include "chrome/browser/extensions/extension_toolbar_model.h" | 13 #include "chrome/browser/extensions/extension_toolbar_model.h" |
15 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
18 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h" | 17 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h" |
19 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h" | 18 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h" |
20 #import "chrome/browser/ui/cocoa/extensions/extension_action_context_menu_contro
ller.h" | |
21 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h" | 19 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h" |
22 #import "chrome/browser/ui/cocoa/image_button_cell.h" | 20 #import "chrome/browser/ui/cocoa/image_button_cell.h" |
23 #import "chrome/browser/ui/cocoa/menu_button.h" | 21 #import "chrome/browser/ui/cocoa/menu_button.h" |
24 #include "chrome/browser/ui/extensions/extension_action_view_controller.h" | 22 #include "chrome/browser/ui/extensions/extension_action_view_controller.h" |
25 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
26 #include "content/public/browser/notification_details.h" | |
27 #include "content/public/browser/notification_observer.h" | |
28 #include "content/public/browser/notification_registrar.h" | |
29 #include "content/public/browser/notification_source.h" | |
30 #include "grit/theme_resources.h" | 24 #include "grit/theme_resources.h" |
31 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSAnimation+Duration.h
" | 25 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSAnimation+Duration.h
" |
32 | 26 |
33 using extensions::Extension; | 27 using extensions::Extension; |
34 using extensions::ExtensionList; | 28 using extensions::ExtensionList; |
35 | 29 |
36 NSString* const kBrowserActionVisibilityChangedNotification = | 30 NSString* const kBrowserActionVisibilityChangedNotification = |
37 @"BrowserActionVisibilityChangedNotification"; | 31 @"BrowserActionVisibilityChangedNotification"; |
38 | 32 |
39 namespace { | 33 namespace { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // Handles when a menu item within the chevron overflow menu is selected. | 157 // Handles when a menu item within the chevron overflow menu is selected. |
164 - (void)chevronItemSelected:(id)menuItem; | 158 - (void)chevronItemSelected:(id)menuItem; |
165 | 159 |
166 // Updates the container's grippy cursor based on the number of hidden buttons. | 160 // Updates the container's grippy cursor based on the number of hidden buttons. |
167 - (void)updateGrippyCursors; | 161 - (void)updateGrippyCursors; |
168 @end | 162 @end |
169 | 163 |
170 // A helper class to proxy extension notifications to the view controller's | 164 // A helper class to proxy extension notifications to the view controller's |
171 // appropriate methods. | 165 // appropriate methods. |
172 class ExtensionServiceObserverBridge | 166 class ExtensionServiceObserverBridge |
173 : public content::NotificationObserver, | 167 : public extensions::ExtensionToolbarModel::Observer { |
174 public extensions::ExtensionToolbarModel::Observer { | |
175 public: | 168 public: |
176 ExtensionServiceObserverBridge(BrowserActionsController* owner, | 169 ExtensionServiceObserverBridge(BrowserActionsController* owner, |
177 Browser* browser) | 170 Browser* browser) |
178 : owner_(owner), browser_(browser) { | 171 : owner_(owner), browser_(browser) { |
179 registrar_.Add(this, | |
180 extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | |
181 content::Source<Profile>(browser->profile())); | |
182 registrar_.Add( | |
183 this, | |
184 extensions::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC, | |
185 content::Source<Profile>(browser->profile())); | |
186 } | |
187 | |
188 // Overridden from content::NotificationObserver. | |
189 void Observe(int type, | |
190 const content::NotificationSource& source, | |
191 const content::NotificationDetails& details) override { | |
192 switch (type) { | |
193 case extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: { | |
194 ExtensionPopupController* popup = [ExtensionPopupController popup]; | |
195 if (popup && ![popup isClosing]) | |
196 [popup close]; | |
197 | |
198 break; | |
199 } | |
200 case extensions::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC: { | |
201 std::pair<const std::string, gfx::NativeWindow>* payload = | |
202 content::Details<std::pair<const std::string, gfx::NativeWindow> >( | |
203 details).ptr(); | |
204 std::string extension_id = payload->first; | |
205 gfx::NativeWindow window = payload->second; | |
206 if (window != browser_->window()->GetNativeWindow()) | |
207 break; | |
208 [owner_ activateBrowserAction:extension_id]; | |
209 break; | |
210 } | |
211 default: | |
212 NOTREACHED() << L"Unexpected notification"; | |
213 } | |
214 } | 172 } |
215 | 173 |
216 // extensions::ExtensionToolbarModel::Observer implementation. | 174 // extensions::ExtensionToolbarModel::Observer implementation. |
217 void ToolbarExtensionAdded(const Extension* extension, int index) override { | 175 void ToolbarExtensionAdded(const Extension* extension, int index) override { |
218 [owner_ createActionButtonForExtension:extension withIndex:index]; | 176 [owner_ createActionButtonForExtension:extension withIndex:index]; |
219 [owner_ resizeContainerAndAnimate:NO]; | 177 [owner_ resizeContainerAndAnimate:NO]; |
220 } | 178 } |
221 | 179 |
222 void ToolbarExtensionRemoved(const Extension* extension) override { | 180 void ToolbarExtensionRemoved(const Extension* extension) override { |
223 [owner_ removeActionButtonForExtension:extension]; | 181 [owner_ removeActionButtonForExtension:extension]; |
(...skipping 25 matching lines...) Expand all Loading... |
249 | 207 |
250 Browser* GetBrowser() override { return browser_; } | 208 Browser* GetBrowser() override { return browser_; } |
251 | 209 |
252 private: | 210 private: |
253 // The object we need to inform when we get a notification. Weak. Owns us. | 211 // The object we need to inform when we get a notification. Weak. Owns us. |
254 BrowserActionsController* owner_; | 212 BrowserActionsController* owner_; |
255 | 213 |
256 // The browser we listen for events from. Weak. | 214 // The browser we listen for events from. Weak. |
257 Browser* browser_; | 215 Browser* browser_; |
258 | 216 |
259 // Used for registering to receive notifications and automatic clean up. | |
260 content::NotificationRegistrar registrar_; | |
261 | |
262 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceObserverBridge); | 217 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceObserverBridge); |
263 }; | 218 }; |
264 | 219 |
265 @implementation BrowserActionsController | 220 @implementation BrowserActionsController |
266 | 221 |
267 @synthesize containerView = containerView_; | 222 @synthesize containerView = containerView_; |
268 | 223 |
269 #pragma mark - | 224 #pragma mark - |
270 #pragma mark Public Methods | 225 #pragma mark Public Methods |
271 | 226 |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 | 439 |
485 NSRect buttonFrame = NSMakeRect(0.0, kBrowserActionOriginYOffset, | 440 NSRect buttonFrame = NSMakeRect(0.0, kBrowserActionOriginYOffset, |
486 kBrowserActionWidth, kBrowserActionHeight); | 441 kBrowserActionWidth, kBrowserActionHeight); |
487 ExtensionAction* extensionAction = | 442 ExtensionAction* extensionAction = |
488 extensions::ExtensionActionManager::Get(browser_->profile())-> | 443 extensions::ExtensionActionManager::Get(browser_->profile())-> |
489 GetExtensionAction(*extension); | 444 GetExtensionAction(*extension); |
490 DCHECK(extensionAction) | 445 DCHECK(extensionAction) |
491 << "Don't create a BrowserActionButton if there is no browser action."; | 446 << "Don't create a BrowserActionButton if there is no browser action."; |
492 scoped_ptr<ToolbarActionViewController> viewController( | 447 scoped_ptr<ToolbarActionViewController> viewController( |
493 new ExtensionActionViewController(extension, browser_, extensionAction)); | 448 new ExtensionActionViewController(extension, browser_, extensionAction)); |
494 // TODO(devlin): Move ContextMenuController stuff to | |
495 // ExtensionActionViewController. | |
496 ExtensionActionContextMenuController* menuController = | |
497 [[ExtensionActionContextMenuController alloc] | |
498 initWithExtension:extension | |
499 browser:browser_ | |
500 extensionAction:extensionAction]; | |
501 BrowserActionButton* newButton = | 449 BrowserActionButton* newButton = |
502 [[[BrowserActionButton alloc] | 450 [[[BrowserActionButton alloc] |
503 initWithFrame:buttonFrame | 451 initWithFrame:buttonFrame |
504 viewController:viewController.Pass() | 452 viewController:viewController.Pass() |
505 controller:self | 453 controller:self] autorelease]; |
506 menuController:menuController] autorelease]; | |
507 [newButton setTarget:self]; | 454 [newButton setTarget:self]; |
508 [newButton setAction:@selector(browserActionClicked:)]; | 455 [newButton setAction:@selector(browserActionClicked:)]; |
509 NSString* buttonKey = base::SysUTF8ToNSString(extension->id()); | 456 NSString* buttonKey = base::SysUTF8ToNSString(extension->id()); |
510 if (!buttonKey) | 457 if (!buttonKey) |
511 return; | 458 return; |
512 [buttons_ setObject:newButton forKey:buttonKey]; | 459 [buttons_ setObject:newButton forKey:buttonKey]; |
513 | 460 |
514 [self positionActionButtonsAndAnimate:NO]; | 461 [self positionActionButtonsAndAnimate:NO]; |
515 | 462 |
516 [[NSNotificationCenter defaultCenter] | 463 [[NSNotificationCenter defaultCenter] |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
833 const extensions::ExtensionList& toolbar_items = | 780 const extensions::ExtensionList& toolbar_items = |
834 toolbarModel_->toolbar_items(); | 781 toolbarModel_->toolbar_items(); |
835 if (index < toolbar_items.size()) { | 782 if (index < toolbar_items.size()) { |
836 const Extension* extension = toolbar_items[index].get(); | 783 const Extension* extension = toolbar_items[index].get(); |
837 return [buttons_ objectForKey:base::SysUTF8ToNSString(extension->id())]; | 784 return [buttons_ objectForKey:base::SysUTF8ToNSString(extension->id())]; |
838 } | 785 } |
839 return nil; | 786 return nil; |
840 } | 787 } |
841 | 788 |
842 @end | 789 @end |
OLD | NEW |