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