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