toolbar_view.h revision 116680a4aac90f2aa7413d9095a592090648e557
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.
4
5#ifndef CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_VIEW_H_
6#define CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_VIEW_H_
7
8#include <set>
9#include <string>
10
11#include "base/memory/scoped_ptr.h"
12#include "base/observer_list.h"
13#include "base/prefs/pref_member.h"
14#include "chrome/browser/command_observer.h"
15#include "chrome/browser/ui/toolbar/back_forward_menu_model.h"
16#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
17#include "ui/base/accelerators/accelerator.h"
18#include "ui/views/accessible_pane_view.h"
19#include "ui/views/controls/button/menu_button.h"
20#include "ui/views/controls/button/menu_button_listener.h"
21#include "ui/views/view.h"
22
23class BackButton;
24class BrowserActionsContainer;
25class Browser;
26class HomeButton;
27class ReloadButton;
28class ToolbarButton;
29class WrenchMenu;
30class WrenchMenuModel;
31class WrenchToolbarButton;
32
33namespace extensions {
34class Command;
35class Extension;
36class ExtensionMessageBubbleFactory;
37}
38
39namespace views {
40class MenuListener;
41}
42
43// The Browser Window's toolbar.
44class ToolbarView : public views::AccessiblePaneView,
45                    public views::MenuButtonListener,
46                    public ui::AcceleratorProvider,
47                    public LocationBarView::Delegate,
48                    public content::NotificationObserver,
49                    public CommandObserver,
50                    public views::ButtonListener,
51                    public views::WidgetObserver,
52                    public views::ViewTargeterDelegate {
53 public:
54  // The view class name.
55  static const char kViewClassName[];
56
57  explicit ToolbarView(Browser* browser);
58  virtual ~ToolbarView();
59
60  // Create the contents of the Browser Toolbar.
61  void Init();
62
63  // Forces the toolbar (and transitively the location bar) to update its
64  // current state.  If |tab| is non-NULL, we're switching (back?) to this tab
65  // and should restore any previous location bar state (such as user editing)
66  // as well.
67  void Update(content::WebContents* tab);
68
69  // Set focus to the toolbar with complete keyboard access, with the
70  // focus initially set to the app menu. Focus will be restored
71  // to the last focused view if the user escapes.
72  void SetPaneFocusAndFocusAppMenu();
73
74  // Returns true if the app menu is focused.
75  bool IsAppMenuFocused();
76
77  // Add a listener to receive a callback when the menu opens.
78  void AddMenuListener(views::MenuListener* listener);
79
80  // Remove a menu listener.
81  void RemoveMenuListener(views::MenuListener* listener);
82
83  virtual bool GetAcceleratorInfo(int id, ui::Accelerator* accel);
84
85  // Returns the view to which the bookmark bubble should be anchored.
86  views::View* GetBookmarkBubbleAnchor();
87
88  // Returns the view to which the Translate bubble should be anchored.
89  views::View* GetTranslateBubbleAnchor();
90
91  // Executes |command| registered by |extension|.
92  void ExecuteExtensionCommand(const extensions::Extension* extension,
93                               const extensions::Command& command);
94
95  // Shows the extension's page action, if present.
96  void ShowPageActionPopup(const extensions::Extension* extension);
97
98  // Shows the extension's browser action, if present.
99  void ShowBrowserActionPopup(const extensions::Extension* extension);
100
101  // Accessors.
102  Browser* browser() const { return browser_; }
103  BrowserActionsContainer* browser_actions() const { return browser_actions_; }
104  ReloadButton* reload_button() const { return reload_; }
105  LocationBarView* location_bar() const { return location_bar_; }
106  views::MenuButton* app_menu() const;
107  HomeButton* home_button() const { return home_; }
108
109  // AccessiblePaneView:
110  virtual bool SetPaneFocus(View* initial_focus) OVERRIDE;
111  virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
112
113  // views::MenuButtonListener:
114  virtual void OnMenuButtonClicked(views::View* source,
115                                   const gfx::Point& point) OVERRIDE;
116
117  // LocationBarView::Delegate:
118  virtual content::WebContents* GetWebContents() OVERRIDE;
119  virtual ToolbarModel* GetToolbarModel() OVERRIDE;
120  virtual const ToolbarModel* GetToolbarModel() const OVERRIDE;
121  virtual InstantController* GetInstant() OVERRIDE;
122  virtual views::Widget* CreateViewsBubble(
123      views::BubbleDelegateView* bubble_delegate) OVERRIDE;
124  virtual PageActionImageView* CreatePageActionImageView(
125      LocationBarView* owner, ExtensionAction* action) OVERRIDE;
126  virtual ContentSettingBubbleModelDelegate*
127      GetContentSettingBubbleModelDelegate() OVERRIDE;
128  virtual void ShowWebsiteSettings(content::WebContents* web_contents,
129                                   const GURL& url,
130                                   const content::SSLStatus& ssl) OVERRIDE;
131
132  // CommandObserver:
133  virtual void EnabledStateChangedForCommand(int id, bool enabled) OVERRIDE;
134
135  // views::ButtonListener:
136  virtual void ButtonPressed(views::Button* sender,
137                             const ui::Event& event) OVERRIDE;
138
139  // views::WidgetObserver:
140  virtual void OnWidgetVisibilityChanged(views::Widget* widget,
141                                         bool visible) OVERRIDE;
142
143  // content::NotificationObserver:
144  virtual void Observe(int type,
145                       const content::NotificationSource& source,
146                       const content::NotificationDetails& details) OVERRIDE;
147
148  // ui::AcceleratorProvider:
149  virtual bool GetAcceleratorForCommandId(
150      int command_id, ui::Accelerator* accelerator) OVERRIDE;
151
152  // views::View:
153  virtual gfx::Size GetPreferredSize() const OVERRIDE;
154  virtual gfx::Size GetMinimumSize() const OVERRIDE;
155  virtual void Layout() OVERRIDE;
156  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
157  virtual void OnThemeChanged() OVERRIDE;
158  virtual const char* GetClassName() const OVERRIDE;
159  virtual bool AcceleratorPressed(const ui::Accelerator& acc) OVERRIDE;
160
161  // Whether the wrench/hotdogs menu is currently showing.
162  bool IsWrenchMenuShowing() const;
163
164  // Whether the toolbar view needs its background painted by the
165  // BrowserNonClientFrameView.
166  bool ShouldPaintBackground() const;
167
168  enum {
169    // The apparent horizontal space between most items, and the vertical
170    // padding above and below them.
171    kStandardSpacing = 3,
172
173    // The top of the toolbar has an edge we have to skip over in addition to
174    // the standard spacing.
175    kVertSpacing = 5,
176  };
177
178 protected:
179  // AccessiblePaneView:
180  virtual bool SetPaneFocusAndFocusDefault() OVERRIDE;
181  virtual void RemovePaneFocus() OVERRIDE;
182
183 private:
184  // Types of display mode this toolbar can have.
185  enum DisplayMode {
186    DISPLAYMODE_NORMAL,       // Normal toolbar with buttons, etc.
187    DISPLAYMODE_LOCATION      // Slimline toolbar showing only compact location
188                              // bar, used for popups.
189  };
190
191  // views::ViewTargeterDelegate:
192  virtual bool DoesIntersectRect(const views::View* target,
193                                 const gfx::Rect& rect) const OVERRIDE;
194
195  // Returns true if we should show the upgrade recommended dot.
196  bool ShouldShowUpgradeRecommended();
197
198  // Returns true if we should show the background page badge.
199  bool ShouldShowBackgroundPageBadge();
200
201  // Returns true if we should show the warning for incompatible software.
202  bool ShouldShowIncompatibilityWarning();
203
204  // Returns the number of pixels above the location bar in non-normal display.
205  int PopupTopSpacing() const;
206
207  // Given toolbar contents of size |size|, returns the total toolbar size.
208  gfx::Size SizeForContentSize(gfx::Size size) const;
209
210  // Loads the images for all the child views.
211  void LoadImages();
212
213  bool is_display_mode_normal() const {
214    return display_mode_ == DISPLAYMODE_NORMAL;
215  }
216
217  // Shows the critical notification bubble against the wrench menu.
218  void ShowCriticalNotification();
219
220  // Shows the outdated install notification bubble against the wrench menu.
221  // |auto_update_enabled| is set to true when auto-upate is on.
222  void ShowOutdatedInstallNotification(bool auto_update_enabled);
223
224  // Updates the badge and the accessible name of the app menu (Wrench).
225  void UpdateAppMenuState();
226
227  // Updates the severity level on the wrench menu button.
228  void UpdateWrenchButtonSeverity();
229
230  void OnShowHomeButtonChanged();
231
232  int content_shadow_height() const;
233
234  // Controls
235  BackButton* back_;
236  ToolbarButton* forward_;
237  ReloadButton* reload_;
238  HomeButton* home_;
239  LocationBarView* location_bar_;
240  BrowserActionsContainer* browser_actions_;
241  WrenchToolbarButton* app_menu_;
242  Browser* browser_;
243
244  // Controls whether or not a home button should be shown on the toolbar.
245  BooleanPrefMember show_home_button_;
246
247  // The display mode used when laying out the toolbar.
248  DisplayMode display_mode_;
249
250  // Wrench model and menu.
251  // Note that the menu should be destroyed before the model it uses, so the
252  // menu should be listed later.
253  scoped_ptr<WrenchMenuModel> wrench_menu_model_;
254  scoped_ptr<WrenchMenu> wrench_menu_;
255
256  // The factory to create bubbles to warn about dangerous/suspicious
257  // extensions.
258  scoped_ptr<extensions::ExtensionMessageBubbleFactory>
259      extension_message_bubble_factory_;
260
261  // A list of listeners to call when the menu opens.
262  ObserverList<views::MenuListener> menu_listeners_;
263
264  content::NotificationRegistrar registrar_;
265
266  DISALLOW_IMPLICIT_CONSTRUCTORS(ToolbarView);
267};
268
269#endif  // CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_VIEW_H_
270