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"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/extensions/extension_action.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/themes/theme_service.h"
14 #include "chrome/browser/themes/theme_service_factory.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/view_ids.h"
17 #include "chrome/browser/ui/views/frame/browser_view.h"
18 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
19 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/manifest_constants.h"
23 #include "grit/theme_resources.h"
24 #include "ui/accessibility/ax_view_state.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/events/event.h"
28 #include "ui/gfx/image/image_skia.h"
29 #include "ui/gfx/image/image_skia_operations.h"
30 #include "ui/gfx/image/image_skia_source.h"
31 #include "ui/views/controls/button/label_button_border.h"
33 using extensions::Extension;
34 using views::LabelButtonBorder;
38 // We have smaller insets than normal STYLE_TEXTBUTTON buttons so that we can
39 // fit user supplied icons in without clipping them.
40 const int kBorderInset = 4;
44 ////////////////////////////////////////////////////////////////////////////////
47 BrowserActionView::BrowserActionView(const Extension* extension,
48 ExtensionAction* extension_action,
50 BrowserActionView::Delegate* delegate)
51 : MenuButton(this, base::string16(), NULL, false),
52 view_controller_(new ExtensionActionViewController(
58 called_registered_extension_command_(false),
59 icon_observer_(NULL) {
60 set_id(VIEW_ID_BROWSER_ACTION);
61 SetHorizontalAlignment(gfx::ALIGN_CENTER);
62 set_context_menu_controller(view_controller_.get());
63 set_drag_controller(delegate_);
65 content::NotificationSource notification_source =
66 content::Source<Profile>(browser->profile()->GetOriginalProfile());
68 extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED,
71 extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED,
74 // We also listen for browser theme changes on linux because a switch from or
75 // to GTK requires that we regrab our browser action images.
78 chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
79 content::Source<ThemeService>(
80 ThemeServiceFactory::GetForProfile(browser->profile())));
85 BrowserActionView::~BrowserActionView() {
88 void BrowserActionView::ViewHierarchyChanged(
89 const ViewHierarchyChangedDetails& details) {
90 if (details.is_add && !called_registered_extension_command_ &&
92 view_controller_->RegisterCommand();
93 called_registered_extension_command_ = true;
96 MenuButton::ViewHierarchyChanged(details);
99 void BrowserActionView::OnDragDone() {
100 delegate_->OnBrowserActionViewDragDone();
103 gfx::Size BrowserActionView::GetPreferredSize() const {
104 return gfx::Size(BrowserActionsContainer::IconWidth(false),
105 BrowserActionsContainer::IconHeight());
108 void BrowserActionView::PaintChildren(gfx::Canvas* canvas,
109 const views::CullSet& cull_set) {
110 View::PaintChildren(canvas, cull_set);
111 int tab_id = view_controller_->GetCurrentTabId();
113 extension_action()->PaintBadge(canvas, GetLocalBounds(), tab_id);
116 void BrowserActionView::GetAccessibleState(ui::AXViewState* state) {
117 views::MenuButton::GetAccessibleState(state);
118 state->name = l10n_util::GetStringUTF16(
119 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION);
120 state->role = ui::AX_ROLE_BUTTON;
123 void BrowserActionView::ButtonPressed(views::Button* sender,
124 const ui::Event& event) {
125 view_controller_->ExecuteActionByUser();
128 void BrowserActionView::UpdateState() {
129 int tab_id = view_controller_->GetCurrentTabId();
133 if (!IsEnabled(tab_id))
134 SetState(views::CustomButton::STATE_DISABLED);
135 else if (state() == views::CustomButton::STATE_DISABLED)
136 SetState(views::CustomButton::STATE_NORMAL);
138 gfx::ImageSkia icon = *view_controller_->GetIcon(tab_id).ToImageSkia();
140 if (!icon.isNull()) {
141 if (!extension_action()->GetIsVisible(tab_id))
142 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25);
144 ThemeService* theme = ThemeServiceFactory::GetForProfile(
145 view_controller_->browser()->profile());
147 gfx::ImageSkia bg = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION);
148 SetImage(views::Button::STATE_NORMAL,
149 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon));
152 // If the browser action name is empty, show the extension name instead.
153 std::string title = extension_action()->GetTitle(tab_id);
154 base::string16 name =
155 base::UTF8ToUTF16(title.empty() ? extension()->name() : title);
156 SetTooltipText(name);
157 SetAccessibleName(name);
159 Layout(); // We need to layout since we may have added an icon as a result.
163 bool BrowserActionView::IsPopup() {
164 int tab_id = view_controller_->GetCurrentTabId();
165 return (tab_id < 0) ? false : extension_action()->HasPopup(tab_id);
168 void BrowserActionView::Observe(int type,
169 const content::NotificationSource& source,
170 const content::NotificationDetails& details) {
172 case extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED:
173 case extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED: {
174 std::pair<const std::string, const std::string>* payload =
175 content::Details<std::pair<const std::string, const std::string> >(
177 if (extension()->id() == payload->first &&
179 extensions::manifest_values::kBrowserActionCommandEvent) {
180 if (type == extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED)
181 view_controller_->RegisterCommand();
183 view_controller_->UnregisterCommand(true);
187 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED:
196 bool BrowserActionView::Activate() {
200 view_controller_->ExecuteActionByUser();
202 // TODO(erikkay): Run a nested modal loop while the mouse is down to
203 // enable menu-like drag-select behavior.
205 // The return value of this method is returned via OnMousePressed.
206 // We need to return false here since we're handing off focus to another
207 // widget/view, and true will grab it right back and try to send events
212 bool BrowserActionView::OnMousePressed(const ui::MouseEvent& event) {
213 if (!event.IsRightMouseButton()) {
214 return IsPopup() ? MenuButton::OnMousePressed(event) :
215 LabelButton::OnMousePressed(event);
220 void BrowserActionView::OnMouseReleased(const ui::MouseEvent& event) {
221 if (IsPopup() || view_controller_->is_menu_running()) {
222 // TODO(erikkay) this never actually gets called (probably because of the
224 MenuButton::OnMouseReleased(event);
226 LabelButton::OnMouseReleased(event);
230 void BrowserActionView::OnMouseExited(const ui::MouseEvent& event) {
231 if (IsPopup() || view_controller_->is_menu_running())
232 MenuButton::OnMouseExited(event);
234 LabelButton::OnMouseExited(event);
237 bool BrowserActionView::OnKeyReleased(const ui::KeyEvent& event) {
238 return IsPopup() ? MenuButton::OnKeyReleased(event) :
239 LabelButton::OnKeyReleased(event);
242 void BrowserActionView::OnGestureEvent(ui::GestureEvent* event) {
244 MenuButton::OnGestureEvent(event);
246 LabelButton::OnGestureEvent(event);
249 scoped_ptr<LabelButtonBorder> BrowserActionView::CreateDefaultBorder() const {
250 scoped_ptr<LabelButtonBorder> border = LabelButton::CreateDefaultBorder();
251 border->set_insets(gfx::Insets(kBorderInset, kBorderInset,
252 kBorderInset, kBorderInset));
253 return border.Pass();
256 bool BrowserActionView::IsEnabled(int tab_id) const {
257 return view_controller_->extension_action()->GetIsVisible(tab_id);
260 gfx::ImageSkia BrowserActionView::GetIconWithBadge() {
261 int tab_id = view_controller_->GetCurrentTabId();
262 gfx::Size spacing(0, ToolbarView::kVertSpacing);
263 gfx::ImageSkia icon = *view_controller_->GetIcon(tab_id).ToImageSkia();
264 if (!IsEnabled(tab_id))
265 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25);
266 return extension_action()->GetIconWithBadge(icon, tab_id, spacing);
269 gfx::ImageSkia BrowserActionView::GetIconForTest() {
270 return GetImage(views::Button::STATE_NORMAL);
273 void BrowserActionView::OnIconUpdated() {
276 icon_observer_->OnIconUpdated(GetIconWithBadge());
279 views::View* BrowserActionView::GetAsView() {
283 bool BrowserActionView::IsShownInMenu() {
284 return delegate_->ShownInsideMenu();
287 views::FocusManager* BrowserActionView::GetFocusManagerForAccelerator() {
288 return GetFocusManager();
291 views::Widget* BrowserActionView::GetParentForContextMenu() {
292 // RunMenuAt expects a nested menu to be parented by the same widget as the
293 // already visible menu, in this case the Chrome menu.
294 return delegate_->ShownInsideMenu() ?
295 BrowserView::GetBrowserViewForBrowser(view_controller_->browser())
296 ->toolbar()->app_menu()->GetWidget() :
300 ExtensionActionViewController*
301 BrowserActionView::GetPreferredPopupViewController() {
302 return delegate_->ShownInsideMenu() ?
303 delegate_->GetMainViewForExtension(extension())->view_controller() :
307 views::View* BrowserActionView::GetReferenceViewForPopup() {
308 // Browser actions in the overflow menu can still show popups, so we may need
309 // a reference view other than this button's parent. If so, use the overflow
311 return visible() ? this : delegate_->GetOverflowReferenceView();
314 views::MenuButton* BrowserActionView::GetContextMenuButton() {
315 DCHECK(visible()); // We should never show a context menu for a hidden item.
319 content::WebContents* BrowserActionView::GetCurrentWebContents() {
320 return delegate_->GetCurrentWebContents();
323 void BrowserActionView::HideActivePopup() {
324 delegate_->HideActivePopup();
327 void BrowserActionView::OnPopupShown(bool grant_tab_permissions) {
328 delegate_->SetPopupOwner(this);
329 // If this was through direct user action, we press the menu button.
330 if (grant_tab_permissions) {
331 // We set the state of the menu button we're using as a reference view,
332 // which is either this or the overflow reference view.
333 // This cast is safe because GetReferenceViewForPopup returns either |this|
334 // or delegate_->GetOverflowReferenceView(), which returns a MenuButton.
335 views::MenuButton* reference_view =
336 static_cast<views::MenuButton*>(GetReferenceViewForPopup());
337 pressed_lock_.reset(new views::MenuButton::PressedLock(reference_view));
341 void BrowserActionView::CleanupPopup() {
342 // We need to do these actions synchronously (instead of closing and then
343 // performing the rest of the cleanup in OnWidgetDestroyed()) because
344 // OnWidgetDestroyed() can be called asynchronously from Close(), and we need
345 // to keep the delegate's popup owner up-to-date.
346 delegate_->SetPopupOwner(NULL);
347 pressed_lock_.reset(); // Unpress the menu button if it was pressed.