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