| 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/extensions/extension_action_view_controller.h" | 5 #include "chrome/browser/ui/extensions/extension_action_view_controller.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/extensions/extension_view.h" | 12 #include "chrome/browser/extensions/extension_view.h" |
| 13 #include "chrome/browser/extensions/extension_view_host.h" | 13 #include "chrome/browser/extensions/extension_view_host.h" |
| 14 #include "chrome/browser/extensions/extension_view_host_factory.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/sessions/session_tab_helper.h" | 16 #include "chrome/browser/sessions/session_tab_helper.h" |
| 16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/extensions/accelerator_priority.h" | 18 #include "chrome/browser/ui/extensions/accelerator_priority.h" |
| 18 #include "chrome/browser/ui/extensions/extension_action_platform_delegate.h" | 19 #include "chrome/browser/ui/extensions/extension_action_platform_delegate.h" |
| 19 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" | 20 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" |
| 20 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" | 21 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" |
| 22 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" |
| 21 #include "chrome/common/extensions/api/extension_action/action_info.h" | 23 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| 22 #include "extensions/browser/extension_host.h" | 24 #include "extensions/browser/extension_host.h" |
| 23 #include "extensions/browser/extension_registry.h" | 25 #include "extensions/browser/extension_registry.h" |
| 24 #include "extensions/common/extension.h" | 26 #include "extensions/common/extension.h" |
| 27 #include "extensions/common/feature_switch.h" |
| 25 #include "extensions/common/manifest_constants.h" | 28 #include "extensions/common/manifest_constants.h" |
| 26 #include "ui/gfx/image/image_skia.h" | 29 #include "ui/gfx/image/image_skia.h" |
| 27 #include "ui/gfx/image/image_skia_operations.h" | 30 #include "ui/gfx/image/image_skia_operations.h" |
| 28 | 31 |
| 29 using extensions::ActionInfo; | 32 using extensions::ActionInfo; |
| 30 using extensions::CommandService; | 33 using extensions::CommandService; |
| 31 | 34 |
| 32 ExtensionActionViewController::ExtensionActionViewController( | 35 ExtensionActionViewController::ExtensionActionViewController( |
| 33 const extensions::Extension* extension, | 36 const extensions::Extension* extension, |
| 34 Browser* browser, | 37 Browser* browser, |
| 35 ExtensionAction* extension_action, | 38 ExtensionAction* extension_action, |
| 36 ToolbarActionsBar* toolbar_actions_bar) | 39 ToolbarActionsBar* toolbar_actions_bar) |
| 37 : extension_(extension), | 40 : extension_(extension), |
| 38 browser_(browser), | 41 browser_(browser), |
| 39 extension_action_(extension_action), | 42 extension_action_(extension_action), |
| 40 toolbar_actions_bar_(toolbar_actions_bar), | 43 toolbar_actions_bar_(toolbar_actions_bar), |
| 41 popup_host_(nullptr), | 44 popup_host_(nullptr), |
| 42 view_delegate_(nullptr), | 45 view_delegate_(nullptr), |
| 43 platform_delegate_(ExtensionActionPlatformDelegate::Create(this)), | 46 platform_delegate_(ExtensionActionPlatformDelegate::Create(this)), |
| 44 icon_factory_(browser->profile(), extension, extension_action, this), | 47 icon_factory_(browser->profile(), extension, extension_action, this), |
| 45 icon_observer_(nullptr), | 48 icon_observer_(nullptr), |
| 46 extension_registry_( | 49 extension_registry_( |
| 47 extensions::ExtensionRegistry::Get(browser_->profile())), | 50 extensions::ExtensionRegistry::Get(browser_->profile())), |
| 48 popup_host_observer_(this) { | 51 popup_host_observer_(this), |
| 52 weak_factory_(this) { |
| 49 DCHECK(extension_action); | 53 DCHECK(extension_action); |
| 50 DCHECK(extension_action->action_type() == ActionInfo::TYPE_PAGE || | 54 DCHECK(extension_action->action_type() == ActionInfo::TYPE_PAGE || |
| 51 extension_action->action_type() == ActionInfo::TYPE_BROWSER); | 55 extension_action->action_type() == ActionInfo::TYPE_BROWSER); |
| 52 DCHECK(extension); | 56 DCHECK(extension); |
| 53 } | 57 } |
| 54 | 58 |
| 55 ExtensionActionViewController::~ExtensionActionViewController() { | 59 ExtensionActionViewController::~ExtensionActionViewController() { |
| 56 DCHECK(!is_showing_popup()); | 60 DCHECK(!is_showing_popup()); |
| 57 } | 61 } |
| 58 | 62 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 if (!ExtensionIsValid()) | 196 if (!ExtensionIsValid()) |
| 193 return false; | 197 return false; |
| 194 | 198 |
| 195 if (extensions::ExtensionActionAPI::Get(browser_->profile()) | 199 if (extensions::ExtensionActionAPI::Get(browser_->profile()) |
| 196 ->ExecuteExtensionAction( | 200 ->ExecuteExtensionAction( |
| 197 extension_, browser_, grant_tab_permissions) == | 201 extension_, browser_, grant_tab_permissions) == |
| 198 ExtensionAction::ACTION_SHOW_POPUP) { | 202 ExtensionAction::ACTION_SHOW_POPUP) { |
| 199 GURL popup_url = extension_action_->GetPopupUrl( | 203 GURL popup_url = extension_action_->GetPopupUrl( |
| 200 SessionTabHelper::IdForTab(view_delegate_->GetCurrentWebContents())); | 204 SessionTabHelper::IdForTab(view_delegate_->GetCurrentWebContents())); |
| 201 return GetPreferredPopupViewController() | 205 return GetPreferredPopupViewController() |
| 202 ->ShowPopupWithUrl(show_action, popup_url, grant_tab_permissions); | 206 ->TriggerPopupWithUrl(show_action, popup_url, grant_tab_permissions); |
| 203 } | 207 } |
| 204 return false; | 208 return false; |
| 205 } | 209 } |
| 206 | 210 |
| 207 void ExtensionActionViewController::PaintExtra( | 211 void ExtensionActionViewController::PaintExtra( |
| 208 gfx::Canvas* canvas, | 212 gfx::Canvas* canvas, |
| 209 const gfx::Rect& bounds, | 213 const gfx::Rect& bounds, |
| 210 content::WebContents* web_contents) const { | 214 content::WebContents* web_contents) const { |
| 211 if (!ExtensionIsValid()) | 215 if (!ExtensionIsValid()) |
| 212 return; | 216 return; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 if (toolbar_actions_bar_) { | 251 if (toolbar_actions_bar_) { |
| 248 toolbar_actions_bar_->HideActivePopup(); | 252 toolbar_actions_bar_->HideActivePopup(); |
| 249 } else { | 253 } else { |
| 250 DCHECK_EQ(ActionInfo::TYPE_PAGE, extension_action_->action_type()); | 254 DCHECK_EQ(ActionInfo::TYPE_PAGE, extension_action_->action_type()); |
| 251 // In the traditional toolbar, page actions only know how to close their own | 255 // In the traditional toolbar, page actions only know how to close their own |
| 252 // popups. | 256 // popups. |
| 253 HidePopup(); | 257 HidePopup(); |
| 254 } | 258 } |
| 255 } | 259 } |
| 256 | 260 |
| 261 void ExtensionActionViewController::OnMenuClosed() { |
| 262 if (toolbar_actions_bar_->popped_out_action() == this && |
| 263 !is_showing_popup()) |
| 264 toolbar_actions_bar_->PopInAction(); |
| 265 } |
| 266 |
| 257 bool ExtensionActionViewController::GetExtensionCommand( | 267 bool ExtensionActionViewController::GetExtensionCommand( |
| 258 extensions::Command* command) { | 268 extensions::Command* command) { |
| 259 DCHECK(command); | 269 DCHECK(command); |
| 260 if (!ExtensionIsValid()) | 270 if (!ExtensionIsValid()) |
| 261 return false; | 271 return false; |
| 262 | 272 |
| 263 CommandService* command_service = CommandService::Get(browser_->profile()); | 273 CommandService* command_service = CommandService::Get(browser_->profile()); |
| 264 if (extension_action_->action_type() == ActionInfo::TYPE_PAGE) { | 274 if (extension_action_->action_type() == ActionInfo::TYPE_PAGE) { |
| 265 return command_service->GetPageActionCommand( | 275 return command_service->GetPageActionCommand( |
| 266 extension_->id(), CommandService::ACTIVE, command, NULL); | 276 extension_->id(), CommandService::ACTIVE, command, NULL); |
| 267 } | 277 } |
| 268 return command_service->GetBrowserActionCommand( | 278 return command_service->GetBrowserActionCommand( |
| 269 extension_->id(), CommandService::ACTIVE, command, NULL); | 279 extension_->id(), CommandService::ACTIVE, command, NULL); |
| 270 } | 280 } |
| 271 | 281 |
| 272 ExtensionActionViewController* | 282 ExtensionActionViewController* |
| 273 ExtensionActionViewController::GetPreferredPopupViewController() { | 283 ExtensionActionViewController::GetPreferredPopupViewController() { |
| 274 if (toolbar_actions_bar_ && toolbar_actions_bar_->in_overflow_mode()) { | 284 if (toolbar_actions_bar_ && toolbar_actions_bar_->in_overflow_mode()) { |
| 275 return static_cast<ExtensionActionViewController*>( | 285 return static_cast<ExtensionActionViewController*>( |
| 276 toolbar_actions_bar_->GetMainControllerForAction(this)); | 286 toolbar_actions_bar_->GetMainControllerForAction(this)); |
| 277 } | 287 } |
| 278 | 288 |
| 279 return this; | 289 return this; |
| 280 } | 290 } |
| 281 | 291 |
| 282 bool ExtensionActionViewController::ShowPopupWithUrl( | 292 bool ExtensionActionViewController::TriggerPopupWithUrl( |
| 283 PopupShowAction show_action, | 293 PopupShowAction show_action, |
| 284 const GURL& popup_url, | 294 const GURL& popup_url, |
| 285 bool grant_tab_permissions) { | 295 bool grant_tab_permissions) { |
| 286 if (!ExtensionIsValid()) | 296 if (!ExtensionIsValid()) |
| 287 return false; | 297 return false; |
| 288 | 298 |
| 289 bool already_showing = is_showing_popup(); | 299 bool already_showing = is_showing_popup(); |
| 290 | 300 |
| 291 // Always hide the current popup, even if it's not owned by this extension. | 301 // Always hide the current popup, even if it's not owned by this extension. |
| 292 // Only one popup should be visible at a time. | 302 // Only one popup should be visible at a time. |
| 293 HideActivePopup(); | 303 HideActivePopup(); |
| 294 | 304 |
| 295 // If we were showing a popup already, then we treat the action to open the | 305 // If we were showing a popup already, then we treat the action to open the |
| 296 // same one as a desire to close it (like clicking a menu button that was | 306 // same one as a desire to close it (like clicking a menu button that was |
| 297 // already open). | 307 // already open). |
| 298 if (already_showing) | 308 if (already_showing) |
| 299 return false; | 309 return false; |
| 300 | 310 |
| 301 popup_host_ = platform_delegate_->ShowPopupWithUrl( | 311 scoped_ptr<extensions::ExtensionViewHost> host( |
| 302 show_action, popup_url, grant_tab_permissions); | 312 extensions::ExtensionViewHostFactory::CreatePopupHost(popup_url, |
| 303 if (popup_host_) { | 313 browser_)); |
| 304 popup_host_observer_.Add(popup_host_); | 314 if (!host) |
| 305 if (toolbar_actions_bar_) | 315 return false; |
| 306 toolbar_actions_bar_->SetPopupOwner(this); | 316 |
| 307 view_delegate_->OnPopupShown(grant_tab_permissions); | 317 popup_host_ = host.get(); |
| 318 popup_host_observer_.Add(popup_host_); |
| 319 if (toolbar_actions_bar_) |
| 320 toolbar_actions_bar_->SetPopupOwner(this); |
| 321 |
| 322 if (toolbar_actions_bar_ && |
| 323 !toolbar_actions_bar_->IsActionVisible(this) && |
| 324 extensions::FeatureSwitch::extension_action_redesign()->IsEnabled()) { |
| 325 platform_delegate_->CloseOverflowMenu(); |
| 326 toolbar_actions_bar_->PopOutAction( |
| 327 this, |
| 328 base::Bind(&ExtensionActionViewController::ShowPopup, |
| 329 weak_factory_.GetWeakPtr(), |
| 330 base::Passed(host.Pass()), |
| 331 grant_tab_permissions, |
| 332 show_action)); |
| 333 } else { |
| 334 ShowPopup(host.Pass(), grant_tab_permissions, show_action); |
| 308 } | 335 } |
| 309 return is_showing_popup(); | 336 |
| 337 return true; |
| 338 } |
| 339 |
| 340 void ExtensionActionViewController::ShowPopup( |
| 341 scoped_ptr<extensions::ExtensionViewHost> popup_host, |
| 342 bool grant_tab_permissions, |
| 343 PopupShowAction show_action) { |
| 344 // It's possible that the popup should be closed before it finishes opening |
| 345 // (since it can open asynchronously). Check before proceeding. |
| 346 if (!popup_host_) |
| 347 return; |
| 348 platform_delegate_->ShowPopup( |
| 349 popup_host.Pass(), grant_tab_permissions, show_action); |
| 350 view_delegate_->OnPopupShown(grant_tab_permissions); |
| 310 } | 351 } |
| 311 | 352 |
| 312 void ExtensionActionViewController::OnPopupClosed() { | 353 void ExtensionActionViewController::OnPopupClosed() { |
| 313 popup_host_observer_.Remove(popup_host_); | 354 popup_host_observer_.Remove(popup_host_); |
| 314 popup_host_ = nullptr; | 355 popup_host_ = nullptr; |
| 315 if (toolbar_actions_bar_) | 356 if (toolbar_actions_bar_) { |
| 316 toolbar_actions_bar_->SetPopupOwner(nullptr); | 357 toolbar_actions_bar_->SetPopupOwner(nullptr); |
| 358 if (toolbar_actions_bar_->popped_out_action() == this && |
| 359 !platform_delegate_->IsMenuRunning()) |
| 360 toolbar_actions_bar_->PopInAction(); |
| 361 } |
| 317 view_delegate_->OnPopupClosed(); | 362 view_delegate_->OnPopupClosed(); |
| 318 } | 363 } |
| OLD | NEW |