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