Home | History | Annotate | Download | only in toolbar
      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 
     23 class BackButton;
     24 class BrowserActionsContainer;
     25 class Browser;
     26 class HomeButton;
     27 class ReloadButton;
     28 class ToolbarButton;
     29 class WrenchMenu;
     30 class WrenchMenuModel;
     31 class WrenchToolbarButton;
     32 
     33 namespace extensions {
     34 class Command;
     35 class Extension;
     36 class ExtensionMessageBubbleFactory;
     37 }
     38 
     39 namespace views {
     40 class MenuListener;
     41 }
     42 
     43 // The Browser Window's toolbar.
     44 class 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   views::MenuButton* app_menu() const;
    106   HomeButton* home_button() const { return home_; }
    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() const OVERRIDE;
    153   virtual gfx::Size GetMinimumSize() const OVERRIDE;
    154   virtual void Layout() OVERRIDE;
    155   virtual bool HitTestRect(const gfx::Rect& rect) const 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   // Overridden from 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   // Returns true if we should show the upgrade recommended dot.
    192   bool ShouldShowUpgradeRecommended();
    193 
    194   // Returns true if we should show the background page badge.
    195   bool ShouldShowBackgroundPageBadge();
    196 
    197   // Returns true if we should show the warning for incompatible software.
    198   bool ShouldShowIncompatibilityWarning();
    199 
    200   // Returns the number of pixels above the location bar in non-normal display.
    201   int PopupTopSpacing() const;
    202 
    203   // Given toolbar contents of size |size|, returns the total toolbar size.
    204   gfx::Size SizeForContentSize(gfx::Size size) const;
    205 
    206   // Loads the images for all the child views.
    207   void LoadImages();
    208 
    209   bool is_display_mode_normal() const {
    210     return display_mode_ == DISPLAYMODE_NORMAL;
    211   }
    212 
    213   // Shows the critical notification bubble against the wrench menu.
    214   void ShowCriticalNotification();
    215 
    216   // Shows the outdated install notification bubble against the wrench menu.
    217   // |auto_update_enabled| is set to true when auto-upate is on.
    218   void ShowOutdatedInstallNotification(bool auto_update_enabled);
    219 
    220   // Updates the badge and the accessible name of the app menu (Wrench).
    221   void UpdateAppMenuState();
    222 
    223   // Updates the severity level on the wrench menu button.
    224   void UpdateWrenchButtonSeverity();
    225 
    226   void OnShowHomeButtonChanged();
    227 
    228   int content_shadow_height() const;
    229 
    230   // Controls
    231   BackButton* back_;
    232   ToolbarButton* forward_;
    233   ReloadButton* reload_;
    234   HomeButton* home_;
    235   LocationBarView* location_bar_;
    236   BrowserActionsContainer* browser_actions_;
    237   WrenchToolbarButton* app_menu_;
    238   Browser* browser_;
    239 
    240   // Controls whether or not a home button should be shown on the toolbar.
    241   BooleanPrefMember show_home_button_;
    242 
    243   // The display mode used when laying out the toolbar.
    244   DisplayMode display_mode_;
    245 
    246   // Wrench model and menu.
    247   // Note that the menu should be destroyed before the model it uses, so the
    248   // menu should be listed later.
    249   scoped_ptr<WrenchMenuModel> wrench_menu_model_;
    250   scoped_ptr<WrenchMenu> wrench_menu_;
    251 
    252   // The factory to create bubbles to warn about dangerous/suspicious
    253   // extensions.
    254   scoped_ptr<extensions::ExtensionMessageBubbleFactory>
    255       extension_message_bubble_factory_;
    256 
    257   // A list of listeners to call when the menu opens.
    258   ObserverList<views::MenuListener> menu_listeners_;
    259 
    260   content::NotificationRegistrar registrar_;
    261 
    262   DISALLOW_IMPLICIT_CONSTRUCTORS(ToolbarView);
    263 };
    264 
    265 #endif  // CHROME_BROWSER_UI_VIEWS_TOOLBAR_TOOLBAR_VIEW_H_
    266