1 // Copyright 2013 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.
5 #include "chrome/browser/ui/views/toolbar/browser_action_view.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/api/commands/command_service.h"
10 #include "chrome/browser/extensions/dev_mode_bubble_controller.h"
11 #include "chrome/browser/extensions/extension_action.h"
12 #include "chrome/browser/extensions/extension_action_manager.h"
13 #include "chrome/browser/extensions/extension_context_menu_model.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/themes/theme_service.h"
17 #include "chrome/browser/themes/theme_service_factory.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
20 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/manifest_constants.h"
23 #include "grit/generated_resources.h"
24 #include "grit/theme_resources.h"
25 #include "ui/base/accessibility/accessible_view_state.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/events/event.h"
29 #include "ui/gfx/image/image_skia.h"
30 #include "ui/gfx/image/image_skia_operations.h"
31 #include "ui/gfx/image/image_skia_source.h"
32 #include "ui/views/controls/menu/menu_runner.h"
34 using extensions::Extension
;
36 ////////////////////////////////////////////////////////////////////////////////
39 bool BrowserActionView::Delegate::NeedToShowMultipleIconStates() const {
43 bool BrowserActionView::Delegate::NeedToShowTooltip() const {
47 BrowserActionView::BrowserActionView(const Extension
* extension
,
49 BrowserActionView::Delegate
* delegate
)
53 extension_(extension
) {
54 button_
= new BrowserActionButton(extension_
, browser_
, delegate_
);
55 button_
->set_drag_controller(delegate_
);
56 button_
->set_owned_by_client();
57 AddChildView(button_
);
58 button_
->UpdateState();
61 BrowserActionView::~BrowserActionView() {
65 gfx::ImageSkia
BrowserActionView::GetIconWithBadge() {
66 int tab_id
= delegate_
->GetCurrentTabId();
68 const ExtensionAction
* action
=
69 extensions::ExtensionActionManager::Get(browser_
->profile())->
70 GetBrowserAction(*button_
->extension());
71 gfx::Size
spacing(0, ToolbarView::kVertSpacing
);
72 gfx::ImageSkia icon
= *button_
->icon_factory().GetIcon(tab_id
).ToImageSkia();
73 if (!button_
->IsEnabled(tab_id
))
74 icon
= gfx::ImageSkiaOperations::CreateTransparentImage(icon
, .25);
75 return action
->GetIconWithBadge(icon
, tab_id
, spacing
);
78 void BrowserActionView::Layout() {
79 // We can't rely on button_->GetPreferredSize() here because that's not set
80 // correctly until the first call to
81 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be
82 // called before that when the initial bounds are set (and then not after,
83 // since the bounds don't change). So instead of setting the height from the
84 // button's preferred size, we use IconHeight(), since that's how big the
85 // button should be regardless of what it's displaying.
86 gfx::Point offset
= delegate_
->GetViewContentOffset();
87 button_
->SetBounds(offset
.x(), offset
.y(), width() - offset
.x(),
88 BrowserActionsContainer::IconHeight());
91 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState
* state
) {
92 state
->name
= l10n_util::GetStringUTF16(
93 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION
);
94 state
->role
= ui::AccessibilityTypes::ROLE_GROUPING
;
97 gfx::Size
BrowserActionView::GetPreferredSize() {
98 return gfx::Size(BrowserActionsContainer::IconWidth(false),
99 BrowserActionsContainer::IconHeight());
102 void BrowserActionView::PaintChildren(gfx::Canvas
* canvas
) {
103 View::PaintChildren(canvas
);
104 ExtensionAction
* action
= button()->browser_action();
105 int tab_id
= delegate_
->GetCurrentTabId();
107 action
->PaintBadge(canvas
, GetLocalBounds(), tab_id
);
110 ////////////////////////////////////////////////////////////////////////////////
111 // BrowserActionButton
113 BrowserActionButton::BrowserActionButton(const Extension
* extension
,
115 BrowserActionView::Delegate
* delegate
)
116 : MenuButton(this, base::string16(), NULL
, false),
119 extensions::ExtensionActionManager::Get(browser
->profile())->
120 GetBrowserAction(*extension
)),
121 extension_(extension
),
122 icon_factory_(browser
->profile(), extension
, browser_action_
, this),
125 called_registered_extension_command_(false) {
127 set_alignment(TextButton::ALIGN_CENTER
);
128 set_context_menu_controller(this);
130 // No UpdateState() here because View hierarchy not setup yet. Our parent
131 // should call UpdateState() after creation.
133 content::NotificationSource notification_source
=
134 content::Source
<Profile
>(browser_
->profile()->GetOriginalProfile());
135 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED
,
136 content::Source
<ExtensionAction
>(browser_action_
));
137 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED
,
138 notification_source
);
139 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED
,
140 notification_source
);
142 // We also listen for browser theme changes on linux because a switch from or
143 // to GTK requires that we regrab our browser action images.
146 chrome::NOTIFICATION_BROWSER_THEME_CHANGED
,
147 content::Source
<ThemeService
>(
148 ThemeServiceFactory::GetForProfile(browser
->profile())));
151 void BrowserActionButton::Destroy() {
152 MaybeUnregisterExtensionCommand(false);
155 context_menu_
->Cancel();
156 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
162 void BrowserActionButton::ViewHierarchyChanged(
163 const ViewHierarchyChangedDetails
& details
) {
164 if (details
.is_add
&& !called_registered_extension_command_
&&
166 MaybeRegisterExtensionCommand();
167 called_registered_extension_command_
= true;
170 MenuButton::ViewHierarchyChanged(details
);
173 bool BrowserActionButton::CanHandleAccelerators() const {
174 // View::CanHandleAccelerators() checks to see if the view is visible before
175 // allowing it to process accelerators. This is not appropriate for browser
176 // actions buttons, which can be hidden inside the overflow area.
180 void BrowserActionButton::GetAccessibleState(ui::AccessibleViewState
* state
) {
181 views::MenuButton::GetAccessibleState(state
);
182 state
->role
= ui::AccessibilityTypes::ROLE_PUSHBUTTON
;
185 void BrowserActionButton::ButtonPressed(views::Button
* sender
,
186 const ui::Event
& event
) {
187 delegate_
->OnBrowserActionExecuted(this);
190 void BrowserActionButton::ShowContextMenuForView(
192 const gfx::Point
& point
,
193 ui::MenuSourceType source_type
) {
194 if (!extension()->ShowConfigureContextMenus())
199 // Reconstructs the menu every time because the menu's contents are dynamic.
200 scoped_refptr
<ExtensionContextMenuModel
> context_menu_contents_(
201 new ExtensionContextMenuModel(extension(), browser_
, delegate_
));
202 menu_runner_
.reset(new views::MenuRunner(context_menu_contents_
.get()));
204 context_menu_
= menu_runner_
->GetMenu();
205 gfx::Point screen_loc
;
206 views::View::ConvertPointToScreen(this, &screen_loc
);
207 if (menu_runner_
->RunMenuAt(GetWidget(), NULL
, gfx::Rect(screen_loc
, size()),
208 views::MenuItemView::TOPLEFT
, source_type
,
209 views::MenuRunner::HAS_MNEMONICS
| views::MenuRunner::CONTEXT_MENU
) ==
210 views::MenuRunner::MENU_DELETED
) {
214 menu_runner_
.reset();
215 SetButtonNotPushed();
216 context_menu_
= NULL
;
219 void BrowserActionButton::UpdateState() {
220 int tab_id
= delegate_
->GetCurrentTabId();
224 SetShowMultipleIconStates(delegate_
->NeedToShowMultipleIconStates());
226 if (!IsEnabled(tab_id
)) {
227 SetState(views::CustomButton::STATE_DISABLED
);
229 SetState(menu_visible_
?
230 views::CustomButton::STATE_PRESSED
:
231 views::CustomButton::STATE_NORMAL
);
234 gfx::ImageSkia icon
= *icon_factory_
.GetIcon(tab_id
).ToImageSkia();
236 if (!icon
.isNull()) {
237 if (!browser_action()->GetIsVisible(tab_id
))
238 icon
= gfx::ImageSkiaOperations::CreateTransparentImage(icon
, .25);
240 ThemeService
* theme
=
241 ThemeServiceFactory::GetForProfile(browser_
->profile());
243 int background_id
= IDR_BROWSER_ACTION
;
244 if (extensions::DevModeBubbleController::IsDevModeExtension(extension_
))
245 background_id
= IDR_BROWSER_ACTION_HIGHLIGHT
;
247 gfx::ImageSkia bg
= *theme
->GetImageSkiaNamed(background_id
);
248 SetIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg
, icon
));
250 gfx::ImageSkia bg_h
= *theme
->GetImageSkiaNamed(IDR_BROWSER_ACTION_H
);
251 SetHoverIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_h
, icon
));
253 gfx::ImageSkia bg_p
= *theme
->GetImageSkiaNamed(IDR_BROWSER_ACTION_P
);
255 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_p
, icon
));
258 // If the browser action name is empty, show the extension name instead.
259 std::string title
= browser_action()->GetTitle(tab_id
);
260 base::string16 name
=
261 base::UTF8ToUTF16(title
.empty() ? extension()->name() : title
);
262 SetTooltipText(delegate_
->NeedToShowTooltip() ? name
: base::string16());
263 SetAccessibleName(name
);
265 parent()->SchedulePaint();
268 bool BrowserActionButton::IsPopup() {
269 int tab_id
= delegate_
->GetCurrentTabId();
270 return (tab_id
< 0) ? false : browser_action_
->HasPopup(tab_id
);
273 GURL
BrowserActionButton::GetPopupUrl() {
274 int tab_id
= delegate_
->GetCurrentTabId();
275 return (tab_id
< 0) ? GURL() : browser_action_
->GetPopupUrl(tab_id
);
278 void BrowserActionButton::Observe(int type
,
279 const content::NotificationSource
& source
,
280 const content::NotificationDetails
& details
) {
282 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED
:
284 // The browser action may have become visible/hidden so we need to make
285 // sure the state gets updated.
286 delegate_
->OnBrowserActionVisibilityChanged();
288 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED
:
289 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED
: {
290 std::pair
<const std::string
, const std::string
>* payload
=
291 content::Details
<std::pair
<const std::string
, const std::string
> >(
293 if (extension_
->id() == payload
->first
&&
295 extensions::manifest_values::kBrowserActionCommandEvent
) {
296 if (type
== chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED
)
297 MaybeRegisterExtensionCommand();
299 MaybeUnregisterExtensionCommand(true);
303 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED
:
312 void BrowserActionButton::OnIconUpdated() {
316 bool BrowserActionButton::Activate() {
320 delegate_
->OnBrowserActionExecuted(this);
322 // TODO(erikkay): Run a nested modal loop while the mouse is down to
323 // enable menu-like drag-select behavior.
325 // The return value of this method is returned via OnMousePressed.
326 // We need to return false here since we're handing off focus to another
327 // widget/view, and true will grab it right back and try to send events
332 bool BrowserActionButton::OnMousePressed(const ui::MouseEvent
& event
) {
333 if (!event
.IsRightMouseButton()) {
334 return IsPopup() ? MenuButton::OnMousePressed(event
) :
335 TextButton::OnMousePressed(event
);
338 if (!views::View::ShouldShowContextMenuOnMousePress()) {
339 // See comments in MenuButton::Activate() as to why this is needed.
340 SetMouseHandler(NULL
);
342 ShowContextMenu(gfx::Point(), ui::MENU_SOURCE_MOUSE
);
347 void BrowserActionButton::OnMouseReleased(const ui::MouseEvent
& event
) {
348 if (IsPopup() || context_menu_
) {
349 // TODO(erikkay) this never actually gets called (probably because of the
351 MenuButton::OnMouseReleased(event
);
353 TextButton::OnMouseReleased(event
);
357 void BrowserActionButton::OnMouseExited(const ui::MouseEvent
& event
) {
358 if (IsPopup() || context_menu_
)
359 MenuButton::OnMouseExited(event
);
361 TextButton::OnMouseExited(event
);
364 bool BrowserActionButton::OnKeyReleased(const ui::KeyEvent
& event
) {
365 return IsPopup() ? MenuButton::OnKeyReleased(event
) :
366 TextButton::OnKeyReleased(event
);
369 void BrowserActionButton::OnGestureEvent(ui::GestureEvent
* event
) {
371 MenuButton::OnGestureEvent(event
);
373 TextButton::OnGestureEvent(event
);
376 bool BrowserActionButton::AcceleratorPressed(
377 const ui::Accelerator
& accelerator
) {
378 delegate_
->OnBrowserActionExecuted(this);
382 void BrowserActionButton::SetButtonPushed() {
383 SetState(views::CustomButton::STATE_PRESSED
);
384 menu_visible_
= true;
387 void BrowserActionButton::SetButtonNotPushed() {
388 SetState(views::CustomButton::STATE_NORMAL
);
389 menu_visible_
= false;
392 bool BrowserActionButton::IsEnabled(int tab_id
) const {
393 return browser_action_
->GetIsVisible(tab_id
);
396 gfx::ImageSkia
BrowserActionButton::GetIconForTest() {
400 BrowserActionButton::~BrowserActionButton() {
403 void BrowserActionButton::MaybeRegisterExtensionCommand() {
404 extensions::CommandService
* command_service
=
405 extensions::CommandService::Get(browser_
->profile());
406 extensions::Command browser_action_command
;
407 if (command_service
->GetBrowserActionCommand(
409 extensions::CommandService::ACTIVE_ONLY
,
410 &browser_action_command
,
412 keybinding_
.reset(new ui::Accelerator(
413 browser_action_command
.accelerator()));
414 GetFocusManager()->RegisterAccelerator(
415 *keybinding_
.get(), ui::AcceleratorManager::kHighPriority
, this);
419 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active
) {
420 if (!keybinding_
.get() || !GetFocusManager())
423 extensions::CommandService
* command_service
=
424 extensions::CommandService::Get(browser_
->profile());
426 extensions::Command browser_action_command
;
427 if (!only_if_active
|| !command_service
->GetBrowserActionCommand(
429 extensions::CommandService::ACTIVE_ONLY
,
430 &browser_action_command
,
432 GetFocusManager()->UnregisterAccelerator(*keybinding_
.get(), this);
433 keybinding_
.reset(NULL
);