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