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/toolbar/wrench_menu_badge_controller.h"
17#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
18#include "ui/base/accelerators/accelerator.h"
19#include "ui/views/accessible_pane_view.h"
20#include "ui/views/controls/button/menu_button.h"
21#include "ui/views/controls/button/menu_button_listener.h"
22#include "ui/views/view.h"
23
24class BackButton;
25class BrowserActionsContainer;
26class Browser;
27class HomeButton;
28class ReloadButton;
29class ToolbarButton;
30class WrenchMenu;
31class WrenchMenuModel;
32class WrenchToolbarButton;
33
34namespace extensions {
35class Command;
36class Extension;
37class ExtensionMessageBubbleFactory;
38}
39
40namespace views {
41class MenuListener;
42}
43
44// The Browser Window's toolbar.
45class ToolbarView : public views::AccessiblePaneView,
46                    public views::MenuButtonListener,
47                    public ui::AcceleratorProvider,
48                    public LocationBarView::Delegate,
49                    public content::NotificationObserver,
50                    public CommandObserver,
51                    public views::ButtonListener,
52                    public views::WidgetObserver,
53                    public views::ViewTargeterDelegate,
54                    public WrenchMenuBadgeController::Delegate {
55 public:
56  // The view class name.
57  static const char kViewClassName[];
58
59  explicit ToolbarView(Browser* browser);
60  virtual ~ToolbarView();
61
62  // Create the contents of the Browser Toolbar.
63  void Init();
64
65  // Forces the toolbar (and transitively the location bar) to update its
66  // current state.  If |tab| is non-NULL, we're switching (back?) to this tab
67  // and should restore any previous location bar state (such as user editing)
68  // as well.
69  void Update(content::WebContents* tab);
70
71  // Set focus to the toolbar with complete keyboard access, with the
72  // focus initially set to the app menu. Focus will be restored
73  // to the last focused view if the user escapes.
74  void SetPaneFocusAndFocusAppMenu();
75
76  // Returns true if the app menu is focused.
77  bool IsAppMenuFocused();
78
79  // Add a listener to receive a callback when the menu opens.
80  void AddMenuListener(views::MenuListener* listener);
81
82  // Remove a menu listener.
83  void RemoveMenuListener(views::MenuListener* listener);
84
85  virtual bool GetAcceleratorInfo(int id, ui::Accelerator* accel);
86
87  // Returns the view to which the bookmark bubble should be anchored.
88  views::View* GetBookmarkBubbleAnchor();
89
90  // Returns the view to which the Translate bubble should be anchored.
91  views::View* GetTranslateBubbleAnchor();
92
93  // Executes |command| registered by |extension|.
94  void ExecuteExtensionCommand(const extensions::Extension* extension,
95                               const extensions::Command& command);
96
97  // Shows the app (wrench) menu. |for_drop| indicates whether the menu is
98  // opened for a drag-and-drop operation.
99  void ShowAppMenu(bool for_drop);
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  virtual void OnWidgetActivationChanged(views::Widget* widget,
143                                         bool active) OVERRIDE;
144
145  // content::NotificationObserver:
146  virtual void Observe(int type,
147                       const content::NotificationSource& source,
148                       const content::NotificationDetails& details) OVERRIDE;
149
150  // ui::AcceleratorProvider:
151  virtual bool GetAcceleratorForCommandId(
152      int command_id, ui::Accelerator* accelerator) OVERRIDE;
153
154  // views::View:
155  virtual gfx::Size GetPreferredSize() const OVERRIDE;
156  virtual gfx::Size GetMinimumSize() const OVERRIDE;
157  virtual void Layout() OVERRIDE;
158  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
159  virtual void OnThemeChanged() OVERRIDE;
160  virtual const char* GetClassName() const OVERRIDE;
161  virtual bool AcceleratorPressed(const ui::Accelerator& acc) OVERRIDE;
162
163  // Whether the wrench/hotdogs menu is currently showing.
164  bool IsWrenchMenuShowing() const;
165
166  // Whether the toolbar view needs its background painted by the
167  // BrowserNonClientFrameView.
168  bool ShouldPaintBackground() const;
169
170  enum {
171    // The apparent horizontal space between most items, and the vertical
172    // padding above and below them.
173    kStandardSpacing = 3,
174
175    // The top of the toolbar has an edge we have to skip over in addition to
176    // the standard spacing.
177    kVertSpacing = 5,
178  };
179
180 protected:
181  // AccessiblePaneView:
182  virtual bool SetPaneFocusAndFocusDefault() OVERRIDE;
183  virtual void RemovePaneFocus() OVERRIDE;
184
185 private:
186  // Types of display mode this toolbar can have.
187  enum DisplayMode {
188    DISPLAYMODE_NORMAL,       // Normal toolbar with buttons, etc.
189    DISPLAYMODE_LOCATION      // Slimline toolbar showing only compact location
190                              // bar, used for popups.
191  };
192
193  // views::ViewTargeterDelegate:
194  virtual bool DoesIntersectRect(const views::View* target,
195                                 const gfx::Rect& rect) const OVERRIDE;
196
197  // WrenchMenuBadgeController::Delegate:
198  virtual void UpdateBadgeSeverity(WrenchMenuBadgeController::BadgeType type,
199                                   WrenchIconPainter::Severity severity,
200                                   bool animate) OVERRIDE;
201
202  // Returns the number of pixels above the location bar in non-normal display.
203  int PopupTopSpacing() const;
204
205  // Given toolbar contents of size |size|, returns the total toolbar size.
206  gfx::Size SizeForContentSize(gfx::Size size) const;
207
208  // Loads the images for all the child views.
209  void LoadImages();
210
211  bool is_display_mode_normal() const {
212    return display_mode_ == DISPLAYMODE_NORMAL;
213  }
214
215  // Shows the critical notification bubble against the wrench menu.
216  void ShowCriticalNotification();
217
218  // Shows the outdated install notification bubble against the wrench menu.
219  // |auto_update_enabled| is set to true when auto-upate is on.
220  void ShowOutdatedInstallNotification(bool auto_update_enabled);
221
222  void OnShowHomeButtonChanged();
223
224  int content_shadow_height() const;
225
226  // Controls
227  BackButton* back_;
228  ToolbarButton* forward_;
229  ReloadButton* reload_;
230  HomeButton* home_;
231  LocationBarView* location_bar_;
232  BrowserActionsContainer* browser_actions_;
233  WrenchToolbarButton* app_menu_;
234  Browser* browser_;
235
236  WrenchMenuBadgeController badge_controller_;
237
238  // Controls whether or not a home button should be shown on the toolbar.
239  BooleanPrefMember show_home_button_;
240
241  // The display mode used when laying out the toolbar.
242  DisplayMode display_mode_;
243
244  // Wrench model and menu.
245  // Note that the menu should be destroyed before the model it uses, so the
246  // menu should be listed later.
247  scoped_ptr<WrenchMenuModel> wrench_menu_model_;
248  scoped_ptr<WrenchMenu> wrench_menu_;
249
250  // The factory to create bubbles to warn about dangerous/suspicious
251  // extensions.
252  scoped_ptr<extensions::ExtensionMessageBubbleFactory>
253      extension_message_bubble_factory_;
254
255  // A list of listeners to call when the menu opens.
256  ObserverList<views::MenuListener> menu_listeners_;
257
258  content::NotificationRegistrar registrar_;
259
260  DISALLOW_IMPLICIT_CONSTRUCTORS(ToolbarView);
261};
262
263#endif  // CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_VIEW_H_
264