page_action_image_view.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2012 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/views/location_bar/page_action_image_view.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "chrome/browser/extensions/api/commands/command_service.h"
9#include "chrome/browser/extensions/extension_action.h"
10#include "chrome/browser/extensions/extension_action_icon_factory.h"
11#include "chrome/browser/extensions/extension_action_manager.h"
12#include "chrome/browser/extensions/extension_context_menu_model.h"
13#include "chrome/browser/extensions/extension_service.h"
14#include "chrome/browser/extensions/extension_tab_util.h"
15#include "chrome/browser/extensions/location_bar_controller.h"
16#include "chrome/browser/extensions/tab_helper.h"
17#include "chrome/browser/platform_util.h"
18#include "chrome/browser/profiles/profile.h"
19#include "chrome/browser/sessions/session_id.h"
20#include "chrome/browser/ui/browser_list.h"
21#include "chrome/browser/ui/views/frame/browser_view.h"
22#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
23#include "chrome/browser/ui/webui/extensions/extension_info_ui.h"
24#include "chrome/common/extensions/extension.h"
25#include "chrome/common/extensions/extension_manifest_constants.h"
26#include "ui/base/accessibility/accessible_view_state.h"
27#include "ui/base/events/event.h"
28#include "ui/gfx/canvas.h"
29#include "ui/gfx/image/image.h"
30#include "ui/views/controls/menu/menu_item_view.h"
31#include "ui/views/controls/menu/menu_runner.h"
32
33using content::WebContents;
34using extensions::LocationBarController;
35using extensions::Extension;
36
37PageActionImageView::PageActionImageView(LocationBarView* owner,
38                                         ExtensionAction* page_action,
39                                         Browser* browser)
40    : owner_(owner),
41      page_action_(page_action),
42      browser_(browser),
43      current_tab_id_(-1),
44      preview_enabled_(false),
45      popup_(NULL),
46      scoped_icon_animation_observer_(
47          page_action->GetIconAnimation(
48              SessionID::IdForTab(owner->GetWebContents())),
49          this) {
50  const Extension* extension = owner_->profile()->GetExtensionService()->
51      GetExtensionById(page_action->extension_id(), false);
52  DCHECK(extension);
53
54  icon_factory_.reset(
55      new ExtensionActionIconFactory(
56          owner_->profile(), extension, page_action, this));
57
58  set_accessibility_focusable(true);
59  set_context_menu_controller(this);
60
61  extensions::CommandService* command_service =
62      extensions::CommandService::Get(browser_->profile());
63  extensions::Command page_action_command;
64  if (command_service->GetPageActionCommand(
65          extension->id(),
66          extensions::CommandService::ACTIVE_ONLY,
67          &page_action_command,
68          NULL)) {
69    page_action_keybinding_.reset(
70        new ui::Accelerator(page_action_command.accelerator()));
71    owner_->GetFocusManager()->RegisterAccelerator(
72        *page_action_keybinding_.get(),
73        ui::AcceleratorManager::kHighPriority,
74        this);
75  }
76
77  extensions::Command script_badge_command;
78  if (command_service->GetScriptBadgeCommand(
79          extension->id(),
80          extensions::CommandService::ACTIVE_ONLY,
81          &script_badge_command,
82          NULL)) {
83    script_badge_keybinding_.reset(
84        new ui::Accelerator(script_badge_command.accelerator()));
85    owner_->GetFocusManager()->RegisterAccelerator(
86        *script_badge_keybinding_.get(),
87        ui::AcceleratorManager::kHighPriority,
88        this);
89  }
90}
91
92PageActionImageView::~PageActionImageView() {
93  if (owner_->GetFocusManager()) {
94    if (page_action_keybinding_.get()) {
95      owner_->GetFocusManager()->UnregisterAccelerator(
96          *page_action_keybinding_.get(), this);
97    }
98
99    if (script_badge_keybinding_.get()) {
100      owner_->GetFocusManager()->UnregisterAccelerator(
101          *script_badge_keybinding_.get(), this);
102    }
103  }
104
105  if (popup_)
106    popup_->GetWidget()->RemoveObserver(this);
107  HidePopup();
108}
109
110void PageActionImageView::ExecuteAction(
111    ExtensionPopup::ShowAction show_action) {
112  WebContents* web_contents = owner_->GetWebContents();
113  if (!web_contents)
114    return;
115
116  extensions::TabHelper* extensions_tab_helper =
117      extensions::TabHelper::FromWebContents(web_contents);
118  LocationBarController* controller =
119      extensions_tab_helper->location_bar_controller();
120
121  switch (controller->OnClicked(page_action_->extension_id(), 1)) {
122    case LocationBarController::ACTION_NONE:
123      break;
124
125    case LocationBarController::ACTION_SHOW_POPUP:
126      ShowPopupWithURL(page_action_->GetPopupUrl(current_tab_id_), show_action);
127      break;
128
129    case LocationBarController::ACTION_SHOW_CONTEXT_MENU:
130      // We are never passing OnClicked a right-click button, so assume that
131      // we're never going to be asked to show a context menu.
132      // TODO(kalman): if this changes, update this class to pass the real
133      // mouse button through to the LocationBarController.
134      NOTREACHED();
135      break;
136
137    case LocationBarController::ACTION_SHOW_SCRIPT_POPUP:
138      ShowPopupWithURL(ExtensionInfoUI::GetURL(page_action_->extension_id()),
139                       show_action);
140      break;
141  }
142}
143
144void PageActionImageView::GetAccessibleState(ui::AccessibleViewState* state) {
145  state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
146  state->name = UTF8ToUTF16(tooltip_);
147}
148
149bool PageActionImageView::OnMousePressed(const ui::MouseEvent& event) {
150  // We want to show the bubble on mouse release; that is the standard behavior
151  // for buttons.  (Also, triggering on mouse press causes bugs like
152  // http://crbug.com/33155.)
153  return true;
154}
155
156void PageActionImageView::OnMouseReleased(const ui::MouseEvent& event) {
157  if (!HitTestPoint(event.location()))
158    return;
159
160  if (event.IsRightMouseButton()) {
161    // Don't show a menu here, its handled in View::ProcessMouseReleased. We
162    // show the context menu by way of being the ContextMenuController.
163    return;
164  }
165
166  ExecuteAction(ExtensionPopup::SHOW);
167}
168
169bool PageActionImageView::OnKeyPressed(const ui::KeyEvent& event) {
170  if (event.key_code() == ui::VKEY_SPACE ||
171      event.key_code() == ui::VKEY_RETURN) {
172    ExecuteAction(ExtensionPopup::SHOW);
173    return true;
174  }
175  return false;
176}
177
178void PageActionImageView::ShowContextMenuForView(View* source,
179                                                 const gfx::Point& point) {
180  const Extension* extension = owner_->profile()->GetExtensionService()->
181      GetExtensionById(page_action()->extension_id(), false);
182  if (!extension->ShowConfigureContextMenus())
183    return;
184
185  scoped_refptr<ExtensionContextMenuModel> context_menu_model(
186      new ExtensionContextMenuModel(extension, browser_, this));
187  menu_runner_.reset(new views::MenuRunner(context_menu_model.get()));
188  gfx::Point screen_loc;
189  views::View::ConvertPointToScreen(this, &screen_loc);
190  if (menu_runner_->RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()),
191          views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS |
192          views::MenuRunner::CONTEXT_MENU) ==
193      views::MenuRunner::MENU_DELETED)
194    return;
195}
196
197bool PageActionImageView::AcceleratorPressed(
198    const ui::Accelerator& accelerator) {
199  DCHECK(visible());  // Should not have happened due to CanHandleAccelerator.
200
201  ExecuteAction(ExtensionPopup::SHOW);
202  return true;
203}
204
205bool PageActionImageView::CanHandleAccelerators() const {
206  // While visible, we don't handle accelerators and while so we also don't
207  // count as a priority accelerator handler.
208  return visible();
209}
210
211void PageActionImageView::UpdateVisibility(WebContents* contents,
212                                           const GURL& url) {
213  // Save this off so we can pass it back to the extension when the action gets
214  // executed. See PageActionImageView::OnMousePressed.
215  current_tab_id_ = contents ? ExtensionTabUtil::GetTabId(contents) : -1;
216  current_url_ = url;
217
218  if (!contents ||
219      (!preview_enabled_ && !page_action_->GetIsVisible(current_tab_id_))) {
220    SetVisible(false);
221    return;
222  }
223
224  // Set the tooltip.
225  tooltip_ = page_action_->GetTitle(current_tab_id_);
226  SetTooltipText(UTF8ToUTF16(tooltip_));
227
228  // Set the image.
229  gfx::Image icon = icon_factory_->GetIcon(current_tab_id_);
230  if (!icon.IsEmpty())
231    SetImage(*icon.ToImageSkia());
232
233  SetVisible(true);
234}
235
236void PageActionImageView::InspectPopup(ExtensionAction* action) {
237  ExecuteAction(ExtensionPopup::SHOW_AND_INSPECT);
238}
239
240void PageActionImageView::OnWidgetDestroying(views::Widget* widget) {
241  DCHECK_EQ(popup_->GetWidget(), widget);
242  popup_->GetWidget()->RemoveObserver(this);
243  popup_ = NULL;
244}
245
246void PageActionImageView::OnIconUpdated() {
247  WebContents* web_contents = owner_->GetWebContents();
248  if (web_contents)
249    UpdateVisibility(web_contents, current_url_);
250}
251
252void PageActionImageView::OnIconChanged() {
253  OnIconUpdated();
254}
255
256void PageActionImageView::PaintChildren(gfx::Canvas* canvas) {
257  View::PaintChildren(canvas);
258  if (current_tab_id_ >= 0)
259    page_action_->PaintBadge(canvas, GetLocalBounds(), current_tab_id_);
260}
261
262void PageActionImageView::ShowPopupWithURL(
263    const GURL& popup_url,
264    ExtensionPopup::ShowAction show_action) {
265  bool popup_showing = popup_ != NULL;
266
267  // Always hide the current popup. Only one popup at a time.
268  HidePopup();
269
270  // If we were already showing, then treat this click as a dismiss.
271  if (popup_showing)
272    return;
273
274  views::BubbleBorder::Arrow arrow = base::i18n::IsRTL() ?
275      views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT;
276
277  popup_ = ExtensionPopup::ShowPopup(popup_url, browser_, this, arrow,
278                                     show_action);
279  popup_->GetWidget()->AddObserver(this);
280}
281
282void PageActionImageView::HidePopup() {
283  if (popup_)
284    popup_->GetWidget()->Close();
285}
286