Chromium Code Reviews| Index: chrome/browser/ui/cocoa/extensions/extension_action_platform_delegate_cocoa.mm |
| diff --git a/chrome/browser/ui/cocoa/extensions/extension_action_platform_delegate_cocoa.mm b/chrome/browser/ui/cocoa/extensions/extension_action_platform_delegate_cocoa.mm |
| index 74c784c286e62944d9879f554701d4952330b0bc..66ba9c8a1fc0bad7fc8bcd7a3737e2c1fba318e3 100644 |
| --- a/chrome/browser/ui/cocoa/extensions/extension_action_platform_delegate_cocoa.mm |
| +++ b/chrome/browser/ui/cocoa/extensions/extension_action_platform_delegate_cocoa.mm |
| @@ -4,10 +4,37 @@ |
| #import "chrome/browser/ui/cocoa/extensions/extension_action_platform_delegate_cocoa.h" |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "base/logging.h" |
| +#include "chrome/browser/extensions/extension_action.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#import "chrome/browser/ui/cocoa/extensions/extension_action_context_menu_controller.h" |
| #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h" |
| #import "chrome/browser/ui/cocoa/toolbar/toolbar_action_view_delegate_cocoa.h" |
| +#include "chrome/common/extensions/api/extension_action/action_info.h" |
| +#include "content/public/browser/notification_details.h" |
| +#include "content/public/browser/notification_source.h" |
| +#include "extensions/browser/notification_types.h" |
| #include "extensions/common/extension.h" |
| +namespace { |
| + |
| +// Returns the notification to listen to for activation for a particular |
| +// |extension_action|. |
| +int GetNotificationTypeForAction(const ExtensionAction& extension_action) { |
| + if (extension_action.action_type() == extensions::ActionInfo::TYPE_BROWSER) |
| + return extensions::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC; |
| + |
| + // We should only have page and browser action types. |
| + DCHECK_EQ(extensions::ActionInfo::TYPE_PAGE, extension_action.action_type()); |
| + return extensions::NOTIFICATION_EXTENSION_COMMAND_PAGE_ACTION_MAC; |
| +} |
| + |
| +} // namespace |
| + |
| // static |
| scoped_ptr<ExtensionActionPlatformDelegate> |
| ExtensionActionPlatformDelegate::Create( |
| @@ -38,6 +65,22 @@ void ExtensionActionPlatformDelegateCocoa::RegisterCommand() { |
| } |
| void ExtensionActionPlatformDelegateCocoa::OnDelegateSet() { |
| + if (controller_->extension()->ShowConfigureContextMenus()) { |
| + menuController_.reset([[ExtensionActionContextMenuController alloc] |
| + initWithExtension:controller_->extension() |
| + browser:controller_->browser() |
| + extensionAction:controller_->extension_action()]); |
| + GetDelegateCocoa()->SetContextMenuController(menuController_.get()); |
| + } |
| + |
| + registrar_.Add( |
| + this, |
| + extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
| + content::Source<Profile>(controller_->browser()->profile())); |
| + registrar_.Add( |
| + this, |
| + GetNotificationTypeForAction(*controller_->extension_action()), |
| + content::Source<Profile>(controller_->browser()->profile())); |
| } |
| bool ExtensionActionPlatformDelegateCocoa::IsShowingPopup() const { |
| @@ -46,14 +89,15 @@ bool ExtensionActionPlatformDelegateCocoa::IsShowingPopup() const { |
| void ExtensionActionPlatformDelegateCocoa::CloseActivePopup() { |
| ExtensionPopupController* popup = [ExtensionPopupController popup]; |
| - if (popup) |
| + if (popup && ![popup isClosing]) |
| [popup close]; |
| } |
| void ExtensionActionPlatformDelegateCocoa::CloseOwnPopup() { |
| ExtensionPopupController* popup = GetPopup(); |
| DCHECK(popup); |
| - [popup close]; |
| + if (popup && ![popup isClosing]) |
| + [popup close]; |
| } |
| bool ExtensionActionPlatformDelegateCocoa::ShowPopupWithUrl( |
| @@ -81,3 +125,33 @@ ExtensionPopupController* ExtensionActionPlatformDelegateCocoa::GetPopup() |
| return popup && [popup extensionId] == controller_->extension()->id() ? |
| popup : nil; |
| } |
| + |
| +void ExtensionActionPlatformDelegateCocoa::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + switch (type) { |
| + case extensions::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: |
| + CloseActivePopup(); |
| + break; |
| + case extensions::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC: |
| + case extensions::NOTIFICATION_EXTENSION_COMMAND_PAGE_ACTION_MAC: { |
|
Avi (use Gerrit)
2014/11/03 22:04:00
Not for this CL, but why don't we collapse these?
Devlin
2014/11/03 22:07:28
I can't think of a reason to not (since extensions
|
| + DCHECK_EQ(type, |
| + GetNotificationTypeForAction(*controller_->extension_action())); |
| + std::pair<const std::string, gfx::NativeWindow>* payload = |
| + content::Details<std::pair<const std::string, gfx::NativeWindow> >( |
| + details).ptr(); |
| + const std::string& extension_id = payload->first; |
| + gfx::NativeWindow window = payload->second; |
| + if (window == controller_->browser()->window()->GetNativeWindow() && |
| + extension_id == controller_->extension()->id() && |
| + controller_->IsEnabled( |
| + controller_->view_delegate()->GetCurrentWebContents())) { |
| + controller_->ExecuteAction(true); |
| + } |
| + break; |
| + } |
| + default: |
| + NOTREACHED() << L"Unexpected notification"; |
| + } |
| +} |