OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/extensions/extension_action_view_controller.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/api/commands/command_service.h" |
| 10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 11 #include "chrome/browser/extensions/extension_action.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/sessions/session_tab_helper.h" |
| 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/extensions/accelerator_priority.h" |
| 16 #include "chrome/browser/ui/extensions/extension_action_platform_delegate.h" |
| 17 #include "chrome/browser/ui/toolbar/toolbar_action_view_delegate.h" |
| 18 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| 19 #include "extensions/common/extension.h" |
| 20 #include "extensions/common/manifest_constants.h" |
| 21 #include "ui/gfx/image/image_skia.h" |
| 22 #include "ui/gfx/image/image_skia_operations.h" |
| 23 |
| 24 using extensions::ActionInfo; |
| 25 using extensions::CommandService; |
| 26 |
| 27 ExtensionActionViewController::ExtensionActionViewController( |
| 28 const extensions::Extension* extension, |
| 29 Browser* browser, |
| 30 ExtensionAction* extension_action) |
| 31 : extension_(extension), |
| 32 browser_(browser), |
| 33 extension_action_(extension_action), |
| 34 view_delegate_(nullptr), |
| 35 platform_delegate_(ExtensionActionPlatformDelegate::Create(this)), |
| 36 icon_factory_(browser->profile(), extension, extension_action, this), |
| 37 icon_observer_(nullptr) { |
| 38 DCHECK(extension_action); |
| 39 DCHECK(extension_action->action_type() == ActionInfo::TYPE_PAGE || |
| 40 extension_action->action_type() == ActionInfo::TYPE_BROWSER); |
| 41 DCHECK(extension); |
| 42 } |
| 43 |
| 44 ExtensionActionViewController::~ExtensionActionViewController() { |
| 45 } |
| 46 |
| 47 const std::string& ExtensionActionViewController::GetId() const { |
| 48 return extension_->id(); |
| 49 } |
| 50 |
| 51 void ExtensionActionViewController::SetDelegate( |
| 52 ToolbarActionViewDelegate* delegate) { |
| 53 view_delegate_ = delegate; |
| 54 platform_delegate_->OnDelegateSet(); |
| 55 } |
| 56 |
| 57 gfx::Image ExtensionActionViewController::GetIcon( |
| 58 content::WebContents* web_contents) { |
| 59 return icon_factory_.GetIcon(SessionTabHelper::IdForTab(web_contents)); |
| 60 } |
| 61 |
| 62 gfx::ImageSkia ExtensionActionViewController::GetIconWithBadge() { |
| 63 content::WebContents* web_contents = view_delegate_->GetCurrentWebContents(); |
| 64 gfx::Size spacing(0, 3); |
| 65 gfx::ImageSkia icon = *GetIcon(web_contents).ToImageSkia(); |
| 66 if (!IsEnabled(web_contents)) |
| 67 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); |
| 68 return extension_action_->GetIconWithBadge( |
| 69 icon, SessionTabHelper::IdForTab(web_contents), spacing); |
| 70 } |
| 71 |
| 72 base::string16 ExtensionActionViewController::GetActionName() const { |
| 73 return base::UTF8ToUTF16(extension_->name()); |
| 74 } |
| 75 |
| 76 base::string16 ExtensionActionViewController::GetAccessibleName( |
| 77 content::WebContents* web_contents) const { |
| 78 std::string title = |
| 79 extension_action()->GetTitle(SessionTabHelper::IdForTab(web_contents)); |
| 80 return base::UTF8ToUTF16(title.empty() ? extension()->name() : title); |
| 81 } |
| 82 |
| 83 base::string16 ExtensionActionViewController::GetTooltip( |
| 84 content::WebContents* web_contents) const { |
| 85 return GetAccessibleName(web_contents); |
| 86 } |
| 87 |
| 88 bool ExtensionActionViewController::IsEnabled( |
| 89 content::WebContents* web_contents) const { |
| 90 return extension_action_->GetIsVisible( |
| 91 SessionTabHelper::IdForTab(web_contents)); |
| 92 } |
| 93 |
| 94 bool ExtensionActionViewController::HasPopup( |
| 95 content::WebContents* web_contents) const { |
| 96 int tab_id = SessionTabHelper::IdForTab(web_contents); |
| 97 return (tab_id < 0) ? false : extension_action_->HasPopup(tab_id); |
| 98 } |
| 99 |
| 100 void ExtensionActionViewController::HidePopup() { |
| 101 if (platform_delegate_->IsShowingPopup()) |
| 102 platform_delegate_->CloseOwnPopup(); |
| 103 } |
| 104 |
| 105 gfx::NativeView ExtensionActionViewController::GetPopupNativeView() { |
| 106 return platform_delegate_->GetPopupNativeView(); |
| 107 } |
| 108 |
| 109 bool ExtensionActionViewController::IsMenuRunning() const { |
| 110 return platform_delegate_->IsMenuRunning(); |
| 111 } |
| 112 |
| 113 bool ExtensionActionViewController::CanDrag() const { |
| 114 return true; |
| 115 } |
| 116 |
| 117 bool ExtensionActionViewController::ExecuteAction(bool by_user) { |
| 118 return ExecuteAction(SHOW_POPUP, by_user); |
| 119 } |
| 120 |
| 121 bool ExtensionActionViewController::ExecuteAction(PopupShowAction show_action, |
| 122 bool grant_tab_permissions) { |
| 123 if (extensions::ExtensionActionAPI::Get(browser_->profile()) |
| 124 ->ExecuteExtensionAction( |
| 125 extension_, browser_, grant_tab_permissions) == |
| 126 ExtensionAction::ACTION_SHOW_POPUP) { |
| 127 GURL popup_url = extension_action_->GetPopupUrl( |
| 128 SessionTabHelper::IdForTab(view_delegate_->GetCurrentWebContents())); |
| 129 return static_cast<ExtensionActionViewController*>( |
| 130 view_delegate_->GetPreferredPopupViewController()) |
| 131 ->ShowPopupWithUrl(show_action, popup_url, grant_tab_permissions); |
| 132 } |
| 133 return false; |
| 134 } |
| 135 |
| 136 void ExtensionActionViewController::PaintExtra( |
| 137 gfx::Canvas* canvas, |
| 138 const gfx::Rect& bounds, |
| 139 content::WebContents* web_contents) const { |
| 140 int tab_id = SessionTabHelper::IdForTab(web_contents); |
| 141 if (tab_id >= 0) |
| 142 extension_action_->PaintBadge(canvas, bounds, tab_id); |
| 143 } |
| 144 |
| 145 void ExtensionActionViewController::RegisterCommand() { |
| 146 platform_delegate_->RegisterCommand(); |
| 147 } |
| 148 |
| 149 void ExtensionActionViewController::InspectPopup() { |
| 150 ExecuteAction(SHOW_POPUP_AND_INSPECT, true); |
| 151 } |
| 152 |
| 153 void ExtensionActionViewController::OnIconUpdated() { |
| 154 if (icon_observer_) |
| 155 icon_observer_->OnIconUpdated(); |
| 156 if (view_delegate_) |
| 157 view_delegate_->UpdateState(); |
| 158 } |
| 159 |
| 160 bool ExtensionActionViewController::GetExtensionCommand( |
| 161 extensions::Command* command) { |
| 162 DCHECK(command); |
| 163 CommandService* command_service = CommandService::Get(browser_->profile()); |
| 164 if (extension_action_->action_type() == ActionInfo::TYPE_PAGE) { |
| 165 return command_service->GetPageActionCommand( |
| 166 extension_->id(), CommandService::ACTIVE_ONLY, command, NULL); |
| 167 } |
| 168 return command_service->GetBrowserActionCommand( |
| 169 extension_->id(), CommandService::ACTIVE_ONLY, command, NULL); |
| 170 } |
| 171 |
| 172 bool ExtensionActionViewController::ShowPopupWithUrl( |
| 173 PopupShowAction show_action, |
| 174 const GURL& popup_url, |
| 175 bool grant_tab_permissions) { |
| 176 bool already_showing = platform_delegate_->IsShowingPopup(); |
| 177 |
| 178 // Always hide the current popup, even if it's not owned by this extension. |
| 179 // Only one popup should be visible at a time. |
| 180 platform_delegate_->CloseActivePopup(); |
| 181 |
| 182 // If we were showing a popup already, then we treat the action to open the |
| 183 // same one as a desire to close it (like clicking a menu button that was |
| 184 // already open). |
| 185 if (already_showing) |
| 186 return false; |
| 187 |
| 188 return platform_delegate_->ShowPopupWithUrl( |
| 189 show_action, popup_url, grant_tab_permissions); |
| 190 } |
OLD | NEW |