Home | History | Annotate | Download | only in location_bar
      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 #ifndef CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
      6 #define CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/prefs/pref_member.h"
     14 #include "chrome/browser/extensions/extension_context_menu_model.h"
     15 #include "chrome/browser/search_engines/template_url_service_observer.h"
     16 #include "chrome/browser/ui/omnibox/location_bar.h"
     17 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
     18 #include "chrome/browser/ui/search/search_model_observer.h"
     19 #include "chrome/browser/ui/toolbar/toolbar_model.h"
     20 #include "chrome/browser/ui/views/dropdown_bar_host.h"
     21 #include "chrome/browser/ui/views/dropdown_bar_host_delegate.h"
     22 #include "chrome/browser/ui/views/extensions/extension_popup.h"
     23 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
     24 #include "content/public/browser/notification_observer.h"
     25 #include "content/public/browser/notification_registrar.h"
     26 #include "ui/gfx/font.h"
     27 #include "ui/gfx/rect.h"
     28 #include "ui/views/controls/button/button.h"
     29 #include "ui/views/drag_controller.h"
     30 
     31 class ActionBoxButtonView;
     32 class CommandUpdater;
     33 class ContentSettingBubbleModelDelegate;
     34 class ContentSettingImageView;
     35 class EVBubbleView;
     36 class ExtensionAction;
     37 class GURL;
     38 class GeneratedCreditCardView;
     39 class InstantController;
     40 class KeywordHintView;
     41 class LocationIconView;
     42 class OpenPDFInReaderView;
     43 class ManagePasswordsIconView;
     44 class PageActionWithBadgeView;
     45 class PageActionImageView;
     46 class Profile;
     47 class ScriptBubbleIconView;
     48 class SelectedKeywordView;
     49 class SiteChipView;
     50 class StarView;
     51 class TemplateURLService;
     52 class TranslateIconView;
     53 class ZoomView;
     54 
     55 namespace content {
     56 struct SSLStatus;
     57 }
     58 
     59 namespace views {
     60 class BubbleDelegateView;
     61 class ImageButton;
     62 class Label;
     63 class LabelButton;
     64 class Widget;
     65 }
     66 
     67 /////////////////////////////////////////////////////////////////////////////
     68 //
     69 // LocationBarView class
     70 //
     71 //   The LocationBarView class is a View subclass that paints the background
     72 //   of the URL bar strip and contains its content.
     73 //
     74 /////////////////////////////////////////////////////////////////////////////
     75 class LocationBarView : public LocationBar,
     76                         public LocationBarTesting,
     77                         public views::View,
     78                         public views::ButtonListener,
     79                         public views::DragController,
     80                         public OmniboxEditController,
     81                         public DropdownBarHostDelegate,
     82                         public TemplateURLServiceObserver,
     83                         public content::NotificationObserver,
     84                         public SearchModelObserver {
     85  public:
     86   // The location bar view's class name.
     87   static const char kViewClassName[];
     88 
     89   // DropdownBarHostDelegate:
     90   virtual void SetFocusAndSelection(bool select_all) OVERRIDE;
     91   virtual void SetAnimationOffset(int offset) OVERRIDE;
     92 
     93   // Returns the offset used while animating.
     94   int animation_offset() const { return animation_offset_; }
     95 
     96   class Delegate {
     97    public:
     98     // Should return the current web contents.
     99     virtual content::WebContents* GetWebContents() = 0;
    100 
    101     // Returns the InstantController, or NULL if there isn't one.
    102     virtual InstantController* GetInstant() = 0;
    103 
    104     virtual ToolbarModel* GetToolbarModel() = 0;
    105     virtual const ToolbarModel* GetToolbarModel() const = 0;
    106 
    107     // Creates Widget for the given delegate.
    108     virtual views::Widget* CreateViewsBubble(
    109         views::BubbleDelegateView* bubble_delegate) = 0;
    110 
    111     // Creates PageActionImageView. Caller gets an ownership.
    112     virtual PageActionImageView* CreatePageActionImageView(
    113         LocationBarView* owner,
    114         ExtensionAction* action) = 0;
    115 
    116     // Returns ContentSettingBubbleModelDelegate.
    117     virtual ContentSettingBubbleModelDelegate*
    118         GetContentSettingBubbleModelDelegate() = 0;
    119 
    120     // Shows permissions and settings for the given web contents.
    121     virtual void ShowWebsiteSettings(content::WebContents* web_contents,
    122                                      const GURL& url,
    123                                      const content::SSLStatus& ssl) = 0;
    124 
    125    protected:
    126     virtual ~Delegate() {}
    127   };
    128 
    129   enum ColorKind {
    130     BACKGROUND = 0,
    131     TEXT,
    132     SELECTED_TEXT,
    133     DEEMPHASIZED_TEXT,
    134     SECURITY_TEXT,
    135   };
    136 
    137   LocationBarView(Browser* browser,
    138                   Profile* profile,
    139                   CommandUpdater* command_updater,
    140                   Delegate* delegate,
    141                   bool is_popup_mode);
    142 
    143   virtual ~LocationBarView();
    144 
    145   // Uses GetBuiltInHorizontalPaddingForChildViews() to optionally add
    146   // additional padding (via an empty border) to |view|. This should be called
    147   // during creation on all child views which are potentially touchable so that
    148   // when touch is enabled they will have sufficient padding.
    149   static void InitTouchableLocationBarChildView(views::View* view);
    150 
    151   // Initializes the LocationBarView.
    152   void Init();
    153 
    154   // True if this instance has been initialized by calling Init, which can only
    155   // be called when the receiving instance is attached to a view container.
    156   bool IsInitialized() const;
    157 
    158   // Returns the appropriate color for the desired kind, based on the user's
    159   // system theme.
    160   SkColor GetColor(ToolbarModel::SecurityLevel security_level,
    161                    ColorKind kind) const;
    162 
    163   // Returns corresponding profile.
    164   Profile* profile() const { return profile_; }
    165 
    166   // Returns the delegate.
    167   Delegate* delegate() const { return delegate_; }
    168 
    169   // See comment in browser_window.h for more info.
    170   void ZoomChangedForActiveTab(bool can_show_bubble);
    171 
    172   // The zoom icon. It may not be visible.
    173   ZoomView* zoom_view() { return zoom_view_; }
    174 
    175   // The passwords icon. It may not be visible.
    176   ManagePasswordsIconView* manage_passwords_icon_view() {
    177     return manage_passwords_icon_view_;
    178   }
    179 
    180   // Sets |preview_enabled| for the PageAction View associated with this
    181   // |page_action|. If |preview_enabled| is true, the view will display the
    182   // PageActions icon even though it has not been activated by the extension.
    183   // This is used by the ExtensionInstalledBubble to preview what the icon
    184   // will look like for the user upon installation of the extension.
    185   void SetPreviewEnabledPageAction(ExtensionAction* page_action,
    186                                    bool preview_enabled);
    187 
    188   // Retrieves the PageAction View which is associated with |page_action|.
    189   views::View* GetPageActionView(ExtensionAction* page_action);
    190 
    191   // Toggles the star on or off.
    192   void SetStarToggled(bool on);
    193 
    194   // The star. It may not be visible.
    195   StarView* star_view() { return star_view_; }
    196 
    197   // Toggles the translate icon on or off.
    198   void SetTranslateIconToggled(bool on);
    199 
    200   // The translate icon. It may not be visible.
    201   TranslateIconView* translate_icon_view() { return translate_icon_view_; }
    202 
    203   void set_site_chip_view(SiteChipView* site_chip_view) {
    204     site_chip_view_ = site_chip_view;
    205   }
    206 
    207   // Shows the bookmark prompt.
    208   void ShowBookmarkPrompt();
    209 
    210   // Returns the screen coordinates of the omnibox (where the URL text appears,
    211   // not where the icons are shown).
    212   gfx::Point GetOmniboxViewOrigin() const;
    213 
    214   // Shows |text| as an inline autocompletion.  This is useful for IMEs, where
    215   // we can't show the autocompletion inside the actual OmniboxView.  See
    216   // comments on |ime_inline_autocomplete_view_|.
    217   void SetImeInlineAutocompletion(const base::string16& text);
    218 
    219   // Invoked from OmniboxViewWin to show gray text autocompletion.
    220   void SetGrayTextAutocompletion(const base::string16& text);
    221 
    222   // Returns the current gray text autocompletion.
    223   base::string16 GetGrayTextAutocompletion() const;
    224 
    225   // Sizing functions
    226   virtual gfx::Size GetPreferredSize() OVERRIDE;
    227 
    228   // Layout and Painting functions
    229   virtual void Layout() OVERRIDE;
    230   virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE;
    231   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
    232 
    233   // Set if we should show a focus rect while the location entry field is
    234   // focused. Used when the toolbar is in full keyboard accessibility mode.
    235   // Repaints if necessary.
    236   virtual void SetShowFocusRect(bool show);
    237 
    238   // Select all of the text. Needed when the user tabs through controls
    239   // in the toolbar in full keyboard accessibility mode.
    240   virtual void SelectAll();
    241 
    242   views::ImageView* GetLocationIconView();
    243   const views::ImageView* GetLocationIconView() const;
    244 
    245   // Return a view suitable for anchoring location-bar-anchored bubbles to.
    246   views::View* GetLocationBarAnchor();
    247   // Return the point suitable for anchoring location-bar-anchored bubbles at.
    248   // The point will be returned in the coordinates of the LocationBarView.
    249   gfx::Point GetLocationBarAnchorPoint() const;
    250 
    251   OmniboxViewViews* omnibox_view() { return omnibox_view_; }
    252 
    253   views::View* generated_credit_card_view();
    254 
    255   // OmniboxEditController:
    256   virtual void Update(const content::WebContents* contents) OVERRIDE;
    257   virtual void OnChanged() OVERRIDE;
    258   virtual void OnSetFocus() OVERRIDE;
    259   virtual InstantController* GetInstant() OVERRIDE;
    260   virtual content::WebContents* GetWebContents() OVERRIDE;
    261   virtual ToolbarModel* GetToolbarModel() OVERRIDE;
    262   virtual const ToolbarModel* GetToolbarModel() const OVERRIDE;
    263 
    264   // views::View:
    265   virtual const char* GetClassName() const OVERRIDE;
    266   virtual bool HasFocus() const OVERRIDE;
    267   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
    268   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
    269 
    270   // views::ButtonListener:
    271   virtual void ButtonPressed(views::Button* sender,
    272                              const ui::Event& event) OVERRIDE;
    273 
    274   // views::DragController:
    275   virtual void WriteDragDataForView(View* sender,
    276                                     const gfx::Point& press_pt,
    277                                     OSExchangeData* data) OVERRIDE;
    278   virtual int GetDragOperationsForView(View* sender,
    279                                        const gfx::Point& p) OVERRIDE;
    280   virtual bool CanStartDragForView(View* sender,
    281                                    const gfx::Point& press_pt,
    282                                    const gfx::Point& p) OVERRIDE;
    283 
    284   // LocationBar:
    285   virtual void ShowFirstRunBubble() OVERRIDE;
    286   virtual GURL GetDestinationURL() const OVERRIDE;
    287   virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE;
    288   virtual content::PageTransition GetPageTransition() const OVERRIDE;
    289   virtual void AcceptInput() OVERRIDE;
    290   virtual void FocusLocation(bool select_all) OVERRIDE;
    291   virtual void FocusSearch() OVERRIDE;
    292   virtual void UpdateContentSettingsIcons() OVERRIDE;
    293   virtual void UpdateManagePasswordsIconAndBubble() OVERRIDE;
    294   virtual void UpdatePageActions() OVERRIDE;
    295   virtual void InvalidatePageActions() OVERRIDE;
    296   virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE;
    297   virtual void UpdateGeneratedCreditCardView() OVERRIDE;
    298   virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE;
    299   virtual void Revert() OVERRIDE;
    300   virtual const OmniboxView* GetOmniboxView() const OVERRIDE;
    301   virtual OmniboxView* GetOmniboxView() OVERRIDE;
    302   virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE;
    303 
    304   // LocationBarTesting:
    305   virtual int PageActionCount() OVERRIDE;
    306   virtual int PageActionVisibleCount() OVERRIDE;
    307   virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE;
    308   virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE;
    309   virtual void TestPageActionPressed(size_t index) OVERRIDE;
    310   virtual bool GetBookmarkStarVisibility() OVERRIDE;
    311 
    312   // TemplateURLServiceObserver:
    313   virtual void OnTemplateURLServiceChanged() OVERRIDE;
    314 
    315   // content::NotificationObserver:
    316   virtual void Observe(int type,
    317                        const content::NotificationSource& source,
    318                        const content::NotificationDetails& details) OVERRIDE;
    319 
    320   // SearchModelObserver:
    321   virtual void ModelChanged(const SearchModel::State& old_state,
    322                             const SearchModel::State& new_state) OVERRIDE;
    323 
    324   // Returns the height of the control without the top and bottom
    325   // edges(i.e.  the height of the edit control inside).  If
    326   // |use_preferred_size| is true this will be the preferred height,
    327   // otherwise it will be the current height.
    328   int GetInternalHeight(bool use_preferred_size);
    329 
    330   // Returns the position and width that the popup should be, and also the left
    331   // edge that the results should align themselves to (which will leave some
    332   // border on the left of the popup).
    333   void GetOmniboxPopupPositioningInfo(gfx::Point* top_left_screen_coord,
    334                                       int* popup_width,
    335                                       int* left_margin,
    336                                       int* right_margin);
    337 
    338   // Space between items in the location bar, as well as between items and the
    339   // edges.
    340   static int GetItemPadding();
    341 
    342   // Thickness of the edges of the omnibox background images, in normal mode.
    343   static const int kNormalEdgeThickness;
    344   // The same, but for popup mode.
    345   static const int kPopupEdgeThickness;
    346   // Amount of padding built into the standard omnibox icons.
    347   static const int kIconInternalPadding;
    348   // Space between the edge and a bubble.
    349   static const int kBubblePadding;
    350 
    351  protected:
    352   virtual void OnFocus() OVERRIDE;
    353 
    354  private:
    355   typedef std::vector<ContentSettingImageView*> ContentSettingViews;
    356 
    357   friend class PageActionImageView;
    358   friend class PageActionWithBadgeView;
    359   typedef std::vector<ExtensionAction*> PageActions;
    360   typedef std::vector<PageActionWithBadgeView*> PageActionViews;
    361 
    362   // Returns the number of pixels of built-in padding to the left and right for
    363   // child views. This is nonzero when touch UI is enabled so as to space out
    364   // child views for easier targeting. See InitTouchableLocationBarChildView().
    365   static int GetBuiltInHorizontalPaddingForChildViews();
    366 
    367   // Returns the thickness of any visible left and right edge, in pixels.
    368   int GetHorizontalEdgeThickness() const;
    369 
    370   // The same, but for the top and bottom edges.
    371   int vertical_edge_thickness() const {
    372     return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness;
    373   }
    374 
    375   // Updates the visibility state of the Content Blocked icons to reflect what
    376   // is actually blocked on the current page. Returns true if the visibility
    377   // of at least one of the views in |content_setting_views_| changed.
    378   bool RefreshContentSettingViews();
    379 
    380   // Deletes all page action views that we have created.
    381   void DeletePageActionViews();
    382 
    383   // Updates the views for the Page Actions, to reflect state changes for
    384   // PageActions. Returns true if the visibility of a PageActionWithBadgeView
    385   // changed, or PageActionWithBadgeView were created/destroyed.
    386   bool RefreshPageActionViews();
    387 
    388   // Returns the number of scripts currently running on the page.
    389   size_t ScriptBubbleScriptsRunning();
    390 
    391   // Updates the Script Bubble Icon, to reflect the number of content scripts
    392   // running on the page. Returns true if the visibility of the bubble changed.
    393   bool RefreshScriptBubble();
    394 
    395   // Updates the view for the zoom icon based on the current tab's zoom. Returns
    396   // true if the visibility of the view changed.
    397   bool RefreshZoomView();
    398 
    399   // Updates the Translate icon based on the current tab's Translate status.
    400   void RefreshTranslateIcon();
    401 
    402   // Sets the visibility of view to new_vis.
    403   void ToggleVisibility(bool new_vis, views::View* view);
    404 
    405   // Updates |manage_passwords_icon_view_|. Returns true if visibility changed.
    406   bool RefreshManagePasswordsIconView();
    407 
    408   // Shows the manage passwords bubble if there is a savable password.
    409   void ShowManagePasswordsBubbleIfNeeded();
    410 
    411   // Returns true if the suggest text is valid.
    412   bool HasValidSuggestText() const;
    413 
    414   // Helper to show the first run info bubble.
    415   void ShowFirstRunBubbleInternal();
    416 
    417   // Draws backgrounds and borders for page actions.  Must be called
    418   // after layout, so the |page_action_views_| have their bounds.
    419   void PaintPageActionBackgrounds(gfx::Canvas* canvas);
    420 
    421   // Handles a request to change the value of this text field from software
    422   // using an accessibility API (typically automation software, screen readers
    423   // don't normally use this). Sets the value and clears the selection.
    424   void AccessibilitySetValue(const base::string16& new_value);
    425 
    426   // Checks if an extension has specified the bookmark star be hidden.
    427   bool IsBookmarkStarHiddenByExtension();
    428 
    429   // The Browser this LocationBarView is in.  Note that at least
    430   // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser
    431   // window, so this may be NULL.
    432   Browser* browser_;
    433 
    434   OmniboxViewViews* omnibox_view_;
    435 
    436   // The profile which corresponds to this View.
    437   Profile* profile_;
    438 
    439   // Our delegate.
    440   Delegate* delegate_;
    441 
    442   // Objects used to paint the normal-mode background.
    443   scoped_ptr<views::Painter> background_border_painter_;
    444   scoped_ptr<views::Painter> background_filling_painter_;
    445 
    446   // An icon to the left of the edit field.
    447   LocationIconView* location_icon_view_;
    448 
    449   // A bubble displayed for EV HTTPS sites.
    450   EVBubbleView* ev_bubble_view_;
    451 
    452   // A view to show inline autocompletion when an IME is active.  In this case,
    453   // we shouldn't change the text or selection inside the OmniboxView itself,
    454   // since this will conflict with the IME's control over the text.  So instead
    455   // we show any autocompletion in a separate field after the OmniboxView.
    456   views::Label* ime_inline_autocomplete_view_;
    457 
    458   // The following views are used to provide hints and remind the user as to
    459   // what is going in the edit. They are all added a children of the
    460   // LocationBarView. At most one is visible at a time. Preference is
    461   // given to the keyword_view_, then hint_view_.
    462   // These autocollapse when the edit needs the room.
    463 
    464   // Shown if the user has selected a keyword.
    465   SelectedKeywordView* selected_keyword_view_;
    466 
    467   // View responsible for showing suggested text. This is NULL when there is no
    468   // suggested text.
    469   views::Label* suggested_text_view_;
    470 
    471   // Shown if the selected url has a corresponding keyword.
    472   KeywordHintView* keyword_hint_view_;
    473 
    474   // The voice search icon.
    475   views::ImageButton* mic_search_view_;
    476 
    477   // The content setting views.
    478   ContentSettingViews content_setting_views_;
    479 
    480   // The zoom icon.
    481   ZoomView* zoom_view_;
    482 
    483   // A bubble that shows after successfully generating a new credit card number.
    484   GeneratedCreditCardView* generated_credit_card_view_;
    485 
    486   // The icon to open a PDF in Reader.
    487   OpenPDFInReaderView* open_pdf_in_reader_view_;
    488 
    489   // The manage passwords icon.
    490   ManagePasswordsIconView* manage_passwords_icon_view_;
    491 
    492   // The current page actions.
    493   PageActions page_actions_;
    494 
    495   // The page action icon views.
    496   PageActionViews page_action_views_;
    497 
    498   // The script bubble.
    499   ScriptBubbleIconView* script_bubble_icon_view_;
    500 
    501   // The Site Chip.
    502   SiteChipView* site_chip_view_;
    503 
    504   // The icon for Translate.
    505   TranslateIconView* translate_icon_view_;
    506 
    507   // The star.
    508   StarView* star_view_;
    509 
    510   // The search/go button.
    511   views::LabelButton* search_button_;
    512 
    513   // Whether we're in popup mode. This value also controls whether the location
    514   // bar is read-only.
    515   const bool is_popup_mode_;
    516 
    517   // True if we should show a focus rect while the location entry field is
    518   // focused. Used when the toolbar is in full keyboard accessibility mode.
    519   bool show_focus_rect_;
    520 
    521   // This is in case we're destroyed before the model loads. We need to make
    522   // Add/RemoveObserver calls.
    523   TemplateURLService* template_url_service_;
    524 
    525   // Tracks this preference to determine whether bookmark editing is allowed.
    526   BooleanPrefMember edit_bookmarks_enabled_;
    527 
    528   // While animating, the host clips the widget and draws only the bottom
    529   // part of it. The view needs to know the pixel offset at which we are drawing
    530   // the widget so that we can draw the curved edges that attach to the toolbar
    531   // in the right location.
    532   int animation_offset_;
    533 
    534   // Used to register for notifications received by NotificationObserver.
    535   content::NotificationRegistrar registrar_;
    536 
    537   // Used to bind callback functions to this object.
    538   base::WeakPtrFactory<LocationBarView> weak_ptr_factory_;
    539 
    540   DISALLOW_COPY_AND_ASSIGN(LocationBarView);
    541 };
    542 
    543 #endif  // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
    544