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