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