Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: chrome/browser/ui/cocoa/extensions/browser_actions_controller.mm

Issue 10824307: Port Extension Commands to Mac. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "browser_actions_controller.h" 5 #import "browser_actions_controller.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 #include <string> 8 #include <string>
9 9
10 #include "base/sys_string_conversions.h" 10 #include "base/sys_string_conversions.h"
11 #include "chrome/browser/extensions/extension_service.h" 11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_toolbar_model.h" 12 #include "chrome/browser/extensions/extension_toolbar_model.h"
13 #include "chrome/browser/prefs/pref_service.h" 13 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/sessions/restore_tab_helper.h" 15 #include "chrome/browser/sessions/restore_tab_helper.h"
16 #include "chrome/browser/ui/browser.h" 16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_tabstrip.h" 17 #include "chrome/browser/ui/browser_tabstrip.h"
18 #include "chrome/browser/ui/browser_window.h"
18 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h" 19 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h"
19 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h" 20 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h"
20 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h" 21 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
21 #import "chrome/browser/ui/cocoa/image_button_cell.h" 22 #import "chrome/browser/ui/cocoa/image_button_cell.h"
22 #import "chrome/browser/ui/cocoa/menu_button.h" 23 #import "chrome/browser/ui/cocoa/menu_button.h"
23 #include "chrome/browser/ui/tab_contents/tab_contents.h" 24 #include "chrome/browser/ui/tab_contents/tab_contents.h"
24 #include "chrome/common/chrome_notification_types.h" 25 #include "chrome/common/chrome_notification_types.h"
25 #include "chrome/common/extensions/extension_action.h" 26 #include "chrome/common/extensions/extension_action.h"
26 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
27 #include "content/public/browser/notification_observer.h" 28 #include "content/public/browser/notification_observer.h"
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 // Returns the ID of the currently selected tab or -1 if none exists. 169 // Returns the ID of the currently selected tab or -1 if none exists.
169 - (int)currentTabId; 170 - (int)currentTabId;
170 @end 171 @end
171 172
172 // A helper class to proxy extension notifications to the view controller's 173 // A helper class to proxy extension notifications to the view controller's
173 // appropriate methods. 174 // appropriate methods.
174 class ExtensionServiceObserverBridge : public content::NotificationObserver, 175 class ExtensionServiceObserverBridge : public content::NotificationObserver,
175 public ExtensionToolbarModel::Observer { 176 public ExtensionToolbarModel::Observer {
176 public: 177 public:
177 ExtensionServiceObserverBridge(BrowserActionsController* owner, 178 ExtensionServiceObserverBridge(BrowserActionsController* owner,
178 Profile* profile) : owner_(owner) { 179 Browser* browser)
180 : owner_(owner), browser_(browser) {
179 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, 181 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE,
180 content::Source<Profile>(profile)); 182 content::Source<Profile>(browser->profile()));
183 registrar_.Add(this,
184 chrome::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC,
185 content::Source<Profile>(browser->profile()));
181 } 186 }
182 187
183 // Overridden from content::NotificationObserver. 188 // Overridden from content::NotificationObserver.
184 void Observe(int type, 189 void Observe(int type,
185 const content::NotificationSource& source, 190 const content::NotificationSource& source,
186 const content::NotificationDetails& details) { 191 const content::NotificationDetails& details) {
187 switch (type) { 192 switch (type) {
188 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: { 193 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: {
189 ExtensionPopupController* popup = [ExtensionPopupController popup]; 194 ExtensionPopupController* popup = [ExtensionPopupController popup];
190 if (popup && ![popup isClosing]) 195 if (popup && ![popup isClosing])
191 [popup close]; 196 [popup close];
192 197
193 break; 198 break;
194 } 199 }
200 case chrome::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 ExtensionService* service = browser_->profile()->GetExtensionService();
209 if (!service)
210 break;
211 const Extension* extension = service->GetExtensionById(extension_id,
212 false);
213 if (!extension)
214 break;
215 BrowserActionButton* button = [owner_ buttonForExtension:extension];
216 [owner_ browserActionClicked:button];
217 break;
218 }
195 default: 219 default:
196 NOTREACHED() << L"Unexpected notification"; 220 NOTREACHED() << L"Unexpected notification";
197 } 221 }
198 } 222 }
199 223
200 // ExtensionToolbarModel::Observer implementation. 224 // ExtensionToolbarModel::Observer implementation.
201 void BrowserActionAdded(const Extension* extension, int index) { 225 void BrowserActionAdded(const Extension* extension, int index) {
202 [owner_ createActionButtonForExtension:extension withIndex:index]; 226 [owner_ createActionButtonForExtension:extension withIndex:index];
203 [owner_ resizeContainerAndAnimate:NO]; 227 [owner_ resizeContainerAndAnimate:NO];
204 } 228 }
205 229
206 void BrowserActionRemoved(const Extension* extension) { 230 void BrowserActionRemoved(const Extension* extension) {
207 [owner_ removeActionButtonForExtension:extension]; 231 [owner_ removeActionButtonForExtension:extension];
208 [owner_ resizeContainerAndAnimate:NO]; 232 [owner_ resizeContainerAndAnimate:NO];
209 } 233 }
210 234
211 private: 235 private:
212 // The object we need to inform when we get a notification. Weak. Owns us. 236 // The object we need to inform when we get a notification. Weak. Owns us.
213 BrowserActionsController* owner_; 237 BrowserActionsController* owner_;
214 238
239 // The browser we listen for events from. Weak.
240 Browser* browser_;
241
215 // Used for registering to receive notifications and automatic clean up. 242 // Used for registering to receive notifications and automatic clean up.
216 content::NotificationRegistrar registrar_; 243 content::NotificationRegistrar registrar_;
217 244
218 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceObserverBridge); 245 DISALLOW_COPY_AND_ASSIGN(ExtensionServiceObserverBridge);
219 }; 246 };
220 247
221 @implementation BrowserActionsController 248 @implementation BrowserActionsController
222 249
223 @synthesize containerView = containerView_; 250 @synthesize containerView = containerView_;
224 251
225 #pragma mark - 252 #pragma mark -
226 #pragma mark Public Methods 253 #pragma mark Public Methods
227 254
228 - (id)initWithBrowser:(Browser*)browser 255 - (id)initWithBrowser:(Browser*)browser
229 containerView:(BrowserActionsContainerView*)container { 256 containerView:(BrowserActionsContainerView*)container {
230 DCHECK(browser && container); 257 DCHECK(browser && container);
231 258
232 if ((self = [super init])) { 259 if ((self = [super init])) {
233 browser_ = browser; 260 browser_ = browser;
234 profile_ = browser->profile(); 261 profile_ = browser->profile();
235 262
236 if (!profile_->GetPrefs()->FindPreference( 263 if (!profile_->GetPrefs()->FindPreference(
237 prefs::kBrowserActionContainerWidth)) 264 prefs::kBrowserActionContainerWidth))
238 [BrowserActionsController registerUserPrefs:profile_->GetPrefs()]; 265 [BrowserActionsController registerUserPrefs:profile_->GetPrefs()];
239 266
240 observer_.reset(new ExtensionServiceObserverBridge(self, profile_)); 267 observer_.reset(new ExtensionServiceObserverBridge(self, browser_));
241 ExtensionService* extensionService = profile_->GetExtensionService(); 268 ExtensionService* extensionService = profile_->GetExtensionService();
242 // |extensionService| can be NULL in Incognito. 269 // |extensionService| can be NULL in Incognito.
243 if (extensionService) { 270 if (extensionService) {
244 toolbarModel_ = extensionService->toolbar_model(); 271 toolbarModel_ = extensionService->toolbar_model();
245 toolbarModel_->AddObserver(observer_.get()); 272 toolbarModel_->AddObserver(observer_.get());
246 } 273 }
247 274
248 containerView_ = container; 275 containerView_ = container;
249 [containerView_ setPostsFrameChangedNotifications:YES]; 276 [containerView_ setPostsFrameChangedNotifications:YES];
250 [[NSNotificationCenter defaultCenter] 277 [[NSNotificationCenter defaultCenter]
(...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 const extensions::ExtensionList& toolbar_items = 860 const extensions::ExtensionList& toolbar_items =
834 toolbarModel_->toolbar_items(); 861 toolbarModel_->toolbar_items();
835 if (index < toolbar_items.size()) { 862 if (index < toolbar_items.size()) {
836 const Extension* extension = toolbar_items[index]; 863 const Extension* extension = toolbar_items[index];
837 return [buttons_ objectForKey:base::SysUTF8ToNSString(extension->id())]; 864 return [buttons_ objectForKey:base::SysUTF8ToNSString(extension->id())];
838 } 865 }
839 return nil; 866 return nil;
840 } 867 }
841 868
842 @end 869 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698