| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "chrome/browser/ui/views/extensions/extension_action_platform_delegate_
views.h" | 5 #include "chrome/browser/ui/views/extensions/extension_action_platform_delegate_
views.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/api/commands/command_service.h" | 9 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 11 #include "chrome/browser/extensions/extension_action.h" | 11 #include "chrome/browser/extensions/extension_action.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/sessions/session_tab_helper.h" | 13 #include "chrome/browser/sessions/session_tab_helper.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/extensions/accelerator_priority.h" | 15 #include "chrome/browser/ui/extensions/accelerator_priority.h" |
| 16 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 17 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" |
| 16 #include "chrome/browser/ui/views/toolbar/toolbar_action_view_delegate_views.h" | 18 #include "chrome/browser/ui/views/toolbar/toolbar_action_view_delegate_views.h" |
| 17 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 19 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
| 18 #include "chrome/common/extensions/api/extension_action/action_info.h" | 20 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| 19 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
| 20 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
| 21 #include "extensions/browser/notification_types.h" | 23 #include "extensions/browser/notification_types.h" |
| 22 #include "extensions/common/extension.h" | 24 #include "extensions/common/extension.h" |
| 23 #include "extensions/common/manifest_constants.h" | 25 #include "extensions/common/manifest_constants.h" |
| 24 #include "ui/gfx/image/image_skia.h" | 26 #include "ui/gfx/image/image_skia.h" |
| 25 #include "ui/gfx/image/image_skia_operations.h" | 27 #include "ui/gfx/image/image_skia_operations.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 controller_->browser()->profile()->GetOriginalProfile()); | 59 controller_->browser()->profile()->GetOriginalProfile()); |
| 58 registrar_.Add(this, | 60 registrar_.Add(this, |
| 59 extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED, | 61 extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED, |
| 60 notification_source); | 62 notification_source); |
| 61 registrar_.Add(this, | 63 registrar_.Add(this, |
| 62 extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED, | 64 extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED, |
| 63 notification_source); | 65 notification_source); |
| 64 } | 66 } |
| 65 | 67 |
| 66 ExtensionActionPlatformDelegateViews::~ExtensionActionPlatformDelegateViews() { | 68 ExtensionActionPlatformDelegateViews::~ExtensionActionPlatformDelegateViews() { |
| 69 DCHECK(!popup_); // We should never have a visible popup at shutdown. |
| 67 if (context_menu_owner == this) | 70 if (context_menu_owner == this) |
| 68 context_menu_owner = NULL; | 71 context_menu_owner = nullptr; |
| 69 if (IsShowingPopup()) | |
| 70 CloseOwnPopup(); | |
| 71 UnregisterCommand(false); | 72 UnregisterCommand(false); |
| 72 } | 73 } |
| 73 | 74 |
| 74 gfx::NativeView ExtensionActionPlatformDelegateViews::GetPopupNativeView() { | 75 gfx::NativeView ExtensionActionPlatformDelegateViews::GetPopupNativeView() { |
| 75 return popup_ ? popup_->GetWidget()->GetNativeView() : nullptr; | 76 return popup_ ? popup_->GetWidget()->GetNativeView() : nullptr; |
| 76 } | 77 } |
| 77 | 78 |
| 78 bool ExtensionActionPlatformDelegateViews::IsMenuRunning() const { | 79 bool ExtensionActionPlatformDelegateViews::IsMenuRunning() const { |
| 79 return menu_runner_.get() != NULL; | 80 return menu_runner_.get() != NULL; |
| 80 } | 81 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 95 GetAcceleratorPriority(extension_command.accelerator(), | 96 GetAcceleratorPriority(extension_command.accelerator(), |
| 96 controller_->extension()), | 97 controller_->extension()), |
| 97 this); | 98 this); |
| 98 } | 99 } |
| 99 } | 100 } |
| 100 | 101 |
| 101 void ExtensionActionPlatformDelegateViews::OnDelegateSet() { | 102 void ExtensionActionPlatformDelegateViews::OnDelegateSet() { |
| 102 GetDelegateViews()->GetAsView()->set_context_menu_controller(this); | 103 GetDelegateViews()->GetAsView()->set_context_menu_controller(this); |
| 103 } | 104 } |
| 104 | 105 |
| 105 bool ExtensionActionPlatformDelegateViews::IsShowingPopup() const { | |
| 106 return popup_ != nullptr; | |
| 107 } | |
| 108 | |
| 109 void ExtensionActionPlatformDelegateViews::CloseActivePopup() { | 106 void ExtensionActionPlatformDelegateViews::CloseActivePopup() { |
| 110 GetDelegateViews()->HideActivePopup(); | 107 if (controller_->extension_action()->action_type() == |
| 108 ActionInfo::TYPE_BROWSER) { |
| 109 BrowserView::GetBrowserViewForBrowser(controller_->browser())->toolbar()-> |
| 110 browser_actions()->HideActivePopup(); |
| 111 } else { |
| 112 DCHECK_EQ(ActionInfo::TYPE_PAGE, |
| 113 controller_->extension_action()->action_type()); |
| 114 // Page actions only know how to close their own popups. |
| 115 controller_->HidePopup(); |
| 116 } |
| 111 } | 117 } |
| 112 | 118 |
| 113 void ExtensionActionPlatformDelegateViews::CloseOwnPopup() { | 119 void ExtensionActionPlatformDelegateViews::CloseOwnPopup() { |
| 114 // We should only be asked to close the popup if we're showing one. | 120 // It's possible that the popup is already in the process of destroying. |
| 115 DCHECK(popup_); | 121 if (popup_) |
| 116 CleanupPopup(true); | 122 CleanupPopup(true); |
| 117 } | 123 } |
| 118 | 124 |
| 119 bool ExtensionActionPlatformDelegateViews::ShowPopupWithUrl( | 125 extensions::ExtensionViewHost* |
| 126 ExtensionActionPlatformDelegateViews::ShowPopupWithUrl( |
| 120 ExtensionActionViewController::PopupShowAction show_action, | 127 ExtensionActionViewController::PopupShowAction show_action, |
| 121 const GURL& popup_url, | 128 const GURL& popup_url, |
| 122 bool grant_tab_permissions) { | 129 bool grant_tab_permissions) { |
| 123 // TOP_RIGHT is correct for both RTL and LTR, because the views platform | 130 // TOP_RIGHT is correct for both RTL and LTR, because the views platform |
| 124 // performs the flipping in RTL cases. | 131 // performs the flipping in RTL cases. |
| 125 views::BubbleBorder::Arrow arrow = views::BubbleBorder::TOP_RIGHT; | 132 views::BubbleBorder::Arrow arrow = views::BubbleBorder::TOP_RIGHT; |
| 126 | 133 |
| 127 views::View* reference_view = GetDelegateViews()->GetReferenceViewForPopup(); | 134 views::View* reference_view = GetDelegateViews()->GetReferenceViewForPopup(); |
| 128 | 135 |
| 129 ExtensionPopup::ShowAction popup_show_action = | 136 ExtensionPopup::ShowAction popup_show_action = |
| 130 show_action == ExtensionActionViewController::SHOW_POPUP ? | 137 show_action == ExtensionActionViewController::SHOW_POPUP ? |
| 131 ExtensionPopup::SHOW : ExtensionPopup::SHOW_AND_INSPECT; | 138 ExtensionPopup::SHOW : ExtensionPopup::SHOW_AND_INSPECT; |
| 132 popup_ = ExtensionPopup::ShowPopup(popup_url, | 139 popup_ = ExtensionPopup::ShowPopup(popup_url, |
| 133 controller_->browser(), | 140 controller_->browser(), |
| 134 reference_view, | 141 reference_view, |
| 135 arrow, | 142 arrow, |
| 136 popup_show_action); | 143 popup_show_action); |
| 137 popup_->GetWidget()->AddObserver(this); | 144 popup_->GetWidget()->AddObserver(this); |
| 138 | 145 |
| 139 GetDelegateViews()->OnPopupShown(grant_tab_permissions); | 146 return popup_->host(); |
| 140 | |
| 141 return true; | |
| 142 } | 147 } |
| 143 | 148 |
| 144 void ExtensionActionPlatformDelegateViews::Observe( | 149 void ExtensionActionPlatformDelegateViews::Observe( |
| 145 int type, | 150 int type, |
| 146 const content::NotificationSource& source, | 151 const content::NotificationSource& source, |
| 147 const content::NotificationDetails& details) { | 152 const content::NotificationDetails& details) { |
| 148 DCHECK(type == extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED || | 153 DCHECK(type == extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED || |
| 149 type == extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED); | 154 type == extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED); |
| 150 extensions::ExtensionCommandRemovedDetails* payload = | 155 extensions::ExtensionCommandRemovedDetails* payload = |
| 151 content::Details<extensions::ExtensionCommandRemovedDetails>(details) | 156 content::Details<extensions::ExtensionCommandRemovedDetails>(details) |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 ui::MenuSourceType source_type) { | 231 ui::MenuSourceType source_type) { |
| 227 ui::MenuModel* context_menu_model = controller_->GetContextMenu(); | 232 ui::MenuModel* context_menu_model = controller_->GetContextMenu(); |
| 228 // It's possible the extension doesn't have a context menu. | 233 // It's possible the extension doesn't have a context menu. |
| 229 if (!context_menu_model) | 234 if (!context_menu_model) |
| 230 return; | 235 return; |
| 231 | 236 |
| 232 DCHECK(!context_menu_owner); | 237 DCHECK(!context_menu_owner); |
| 233 context_menu_owner = this; | 238 context_menu_owner = this; |
| 234 | 239 |
| 235 // We shouldn't have both a popup and a context menu showing. | 240 // We shouldn't have both a popup and a context menu showing. |
| 236 GetDelegateViews()->HideActivePopup(); | 241 CloseActivePopup(); |
| 237 | 242 |
| 238 gfx::Point screen_loc; | 243 gfx::Point screen_loc; |
| 239 views::View::ConvertPointToScreen(GetDelegateViews()->GetAsView(), | 244 views::View::ConvertPointToScreen(GetDelegateViews()->GetAsView(), |
| 240 &screen_loc); | 245 &screen_loc); |
| 241 | 246 |
| 242 int run_types = | 247 int run_types = |
| 243 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; | 248 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU; |
| 244 if (GetDelegateViews()->IsShownInMenu()) | 249 if (GetDelegateViews()->IsShownInMenu()) |
| 245 run_types |= views::MenuRunner::IS_NESTED; | 250 run_types |= views::MenuRunner::IS_NESTED; |
| 246 | 251 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); | 308 menu_controller->Cancel(views::MenuController::EXIT_OUTERMOST); |
| 304 return true; | 309 return true; |
| 305 } | 310 } |
| 306 } | 311 } |
| 307 | 312 |
| 308 return false; | 313 return false; |
| 309 } | 314 } |
| 310 | 315 |
| 311 void ExtensionActionPlatformDelegateViews::CleanupPopup(bool close_widget) { | 316 void ExtensionActionPlatformDelegateViews::CleanupPopup(bool close_widget) { |
| 312 DCHECK(popup_); | 317 DCHECK(popup_); |
| 313 GetDelegateViews()->CleanupPopup(); | |
| 314 popup_->GetWidget()->RemoveObserver(this); | 318 popup_->GetWidget()->RemoveObserver(this); |
| 315 if (close_widget) | 319 if (close_widget) |
| 316 popup_->GetWidget()->Close(); | 320 popup_->GetWidget()->Close(); |
| 317 popup_ = NULL; | 321 popup_ = nullptr; |
| 318 } | 322 } |
| 319 | 323 |
| 320 ToolbarActionViewDelegateViews* | 324 ToolbarActionViewDelegateViews* |
| 321 ExtensionActionPlatformDelegateViews::GetDelegateViews() const { | 325 ExtensionActionPlatformDelegateViews::GetDelegateViews() const { |
| 322 return static_cast<ToolbarActionViewDelegateViews*>( | 326 return static_cast<ToolbarActionViewDelegateViews*>( |
| 323 controller_->view_delegate()); | 327 controller_->view_delegate()); |
| 324 } | 328 } |
| OLD | NEW |