location_bar_view.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 ToolbarModel* GetToolbarModel() OVERRIDE;
260  virtual content::WebContents* GetWebContents() OVERRIDE;
261
262  // Thickness of the edges of the omnibox background images, in normal mode.
263  static const int kNormalEdgeThickness;
264  // The same, but for popup mode.
265  static const int kPopupEdgeThickness;
266  // Space between items in the location bar, as well as between items and the
267  // edges.
268  static const int kItemPadding;
269  // Amount of padding built into the standard omnibox icons.
270  static const int kIconInternalPadding;
271  // Amount of padding to place between the origin chip and the leading edge of
272  // the location bar.
273  static const int kOriginChipEdgeItemPadding;
274  // Amount of padding built into the origin chip.
275  static const int kOriginChipBuiltinPadding;
276  // Space between the edge and a bubble.
277  static const int kBubblePadding;
278
279 private:
280  typedef std::vector<ContentSettingImageView*> ContentSettingViews;
281
282  friend class PageActionImageView;
283  friend class PageActionWithBadgeView;
284  typedef std::vector<ExtensionAction*> PageActions;
285  typedef std::vector<PageActionWithBadgeView*> PageActionViews;
286
287  // Helper for GetMinimumWidth().  Calculates the incremental minimum width
288  // |view| should add to the trailing width after the omnibox.
289  static int IncrementalMinimumWidth(views::View* view);
290
291  // Returns the thickness of any visible left and right edge, in pixels.
292  int GetHorizontalEdgeThickness() const;
293
294  // The same, but for the top and bottom edges.
295  int vertical_edge_thickness() const {
296    return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness;
297  }
298
299  // Updates the visibility state of the Content Blocked icons to reflect what
300  // is actually blocked on the current page. Returns true if the visibility
301  // of at least one of the views in |content_setting_views_| changed.
302  bool RefreshContentSettingViews();
303
304  // Deletes all page action views that we have created.
305  void DeletePageActionViews();
306
307  // Updates the views for the Page Actions, to reflect state changes for
308  // PageActions. Returns true if the visibility of a PageActionWithBadgeView
309  // changed, or PageActionWithBadgeView were created/destroyed.
310  bool RefreshPageActionViews();
311
312  // Updates the view for the zoom icon based on the current tab's zoom. Returns
313  // true if the visibility of the view changed.
314  bool RefreshZoomView();
315
316  // Updates the Translate icon based on the current tab's Translate status.
317  void RefreshTranslateIcon();
318
319  // Updates |manage_passwords_icon_view_|. Returns true if visibility changed.
320  bool RefreshManagePasswordsIconView();
321
322  // Shows the manage passwords bubble if there is a savable password.
323  void ShowManagePasswordsBubbleIfNeeded();
324
325  // Helper to show the first run info bubble.
326  void ShowFirstRunBubbleInternal();
327
328  // Handles a request to change the value of this text field from software
329  // using an accessibility API (typically automation software, screen readers
330  // don't normally use this). Sets the value and clears the selection.
331  void AccessibilitySetValue(const base::string16& new_value);
332
333  // Returns true if the suggest text is valid.
334  bool HasValidSuggestText() const;
335
336  bool ShouldShowKeywordBubble() const;
337  bool ShouldShowEVBubble() const;
338
339  // Origin chip animation control methods.
340  void OnShowURLAnimationEnded();
341  void OnHideURLAnimationEnded();
342
343  // LocationBar:
344  virtual void ShowFirstRunBubble() OVERRIDE;
345  virtual GURL GetDestinationURL() const OVERRIDE;
346  virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE;
347  virtual content::PageTransition GetPageTransition() const OVERRIDE;
348  virtual void AcceptInput() OVERRIDE;
349  virtual void FocusSearch() OVERRIDE;
350  virtual void UpdateContentSettingsIcons() OVERRIDE;
351  virtual void UpdateManagePasswordsIconAndBubble() OVERRIDE;
352  virtual void UpdatePageActions() OVERRIDE;
353  virtual void InvalidatePageActions() OVERRIDE;
354  virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE;
355  virtual void UpdateGeneratedCreditCardView() OVERRIDE;
356  virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE;
357  virtual const OmniboxView* GetOmniboxView() const OVERRIDE;
358  virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE;
359
360  // LocationBarTesting:
361  virtual int PageActionCount() OVERRIDE;
362  virtual int PageActionVisibleCount() OVERRIDE;
363  virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE;
364  virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE;
365  virtual void TestPageActionPressed(size_t index) OVERRIDE;
366  virtual bool GetBookmarkStarVisibility() OVERRIDE;
367
368  // views::View:
369  virtual const char* GetClassName() const OVERRIDE;
370  virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
371  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
372  virtual void PaintChildren(gfx::Canvas* canvas,
373                             const views::CullSet& cull_set) OVERRIDE;
374
375  // views::ButtonListener:
376  virtual void ButtonPressed(views::Button* sender,
377                             const ui::Event& event) OVERRIDE;
378
379  // views::DragController:
380  virtual void WriteDragDataForView(View* sender,
381                                    const gfx::Point& press_pt,
382                                    OSExchangeData* data) OVERRIDE;
383  virtual int GetDragOperationsForView(View* sender,
384                                       const gfx::Point& p) OVERRIDE;
385  virtual bool CanStartDragForView(View* sender,
386                                   const gfx::Point& press_pt,
387                                   const gfx::Point& p) OVERRIDE;
388
389  // OmniboxEditController:
390  virtual void OnChanged() OVERRIDE;
391  virtual void OnSetFocus() OVERRIDE;
392  virtual InstantController* GetInstant() OVERRIDE;
393  virtual const ToolbarModel* GetToolbarModel() const OVERRIDE;
394  virtual void HideURL() OVERRIDE;
395
396  // DropdownBarHostDelegate:
397  virtual void SetFocusAndSelection(bool select_all) OVERRIDE;
398  virtual void SetAnimationOffset(int offset) OVERRIDE;
399
400  // gfx::AnimationDelegate:
401  virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
402  virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
403
404  // TemplateURLServiceObserver:
405  virtual void OnTemplateURLServiceChanged() OVERRIDE;
406
407  // content::NotificationObserver:
408  virtual void Observe(int type,
409                       const content::NotificationSource& source,
410                       const content::NotificationDetails& details) OVERRIDE;
411
412  // SearchModelObserver:
413  virtual void ModelChanged(const SearchModel::State& old_state,
414                            const SearchModel::State& new_state) OVERRIDE;
415
416  // The Browser this LocationBarView is in.  Note that at least
417  // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser
418  // window, so this may be NULL.
419  Browser* browser_;
420
421  OmniboxViewViews* omnibox_view_;
422
423  // Our delegate.
424  Delegate* delegate_;
425
426  // Object used to paint the border.
427  scoped_ptr<views::Painter> border_painter_;
428
429  // The origin chip that may appear in the location bar.
430  OriginChipView* origin_chip_view_;
431
432  // An icon to the left of the edit field.
433  LocationIconView* location_icon_view_;
434
435  // A bubble displayed for EV HTTPS sites.
436  EVBubbleView* ev_bubble_view_;
437
438  // A view to show inline autocompletion when an IME is active.  In this case,
439  // we shouldn't change the text or selection inside the OmniboxView itself,
440  // since this will conflict with the IME's control over the text.  So instead
441  // we show any autocompletion in a separate field after the OmniboxView.
442  views::Label* ime_inline_autocomplete_view_;
443
444  // The following views are used to provide hints and remind the user as to
445  // what is going in the edit. They are all added a children of the
446  // LocationBarView. At most one is visible at a time. Preference is
447  // given to the keyword_view_, then hint_view_.
448  // These autocollapse when the edit needs the room.
449
450  // Shown if the user has selected a keyword.
451  SelectedKeywordView* selected_keyword_view_;
452
453  // View responsible for showing suggested text. This is NULL when there is no
454  // suggested text.
455  views::Label* suggested_text_view_;
456
457  // Shown if the selected url has a corresponding keyword.
458  KeywordHintView* keyword_hint_view_;
459
460  // The voice search icon.
461  views::ImageButton* mic_search_view_;
462
463  // The content setting views.
464  ContentSettingViews content_setting_views_;
465
466  // The zoom icon.
467  ZoomView* zoom_view_;
468
469  // A bubble that shows after successfully generating a new credit card number.
470  GeneratedCreditCardView* generated_credit_card_view_;
471
472  // The icon to open a PDF in Reader.
473  OpenPDFInReaderView* open_pdf_in_reader_view_;
474
475  // The manage passwords icon.
476  ManagePasswordsIconView* manage_passwords_icon_view_;
477
478  // The current page actions.
479  PageActions page_actions_;
480
481  // The page action icon views.
482  PageActionViews page_action_views_;
483
484  // The icon for Translate.
485  TranslateIconView* translate_icon_view_;
486
487  // The star.
488  StarView* star_view_;
489
490  // The search/go button.
491  views::LabelButton* search_button_;
492
493  // Whether we're in popup mode. This value also controls whether the location
494  // bar is read-only.
495  const bool is_popup_mode_;
496
497  // True if we should show a focus rect while the location entry field is
498  // focused. Used when the toolbar is in full keyboard accessibility mode.
499  bool show_focus_rect_;
500
501  // This is in case we're destroyed before the model loads. We need to make
502  // Add/RemoveObserver calls.
503  TemplateURLService* template_url_service_;
504
505  // Tracks this preference to determine whether bookmark editing is allowed.
506  BooleanPrefMember edit_bookmarks_enabled_;
507
508  // During dropdown animation, the host clips the widget and draws only the
509  // bottom part of it. The view needs to know the pixel offset at which we are
510  // drawing the widget so that we can draw the curved edges that attach to the
511  // toolbar in the right location.
512  int dropdown_animation_offset_;
513
514  // Origin chip animations.
515  scoped_ptr<gfx::SlideAnimation> show_url_animation_;
516  scoped_ptr<gfx::SlideAnimation> hide_url_animation_;
517
518  // Text label shown only during origin chip animations.
519  views::Label* animated_host_label_;
520
521  // Used to register for notifications received by NotificationObserver.
522  content::NotificationRegistrar registrar_;
523
524  // Used to bind callback functions to this object.
525  base::WeakPtrFactory<LocationBarView> weak_ptr_factory_;
526
527  DISALLOW_COPY_AND_ASSIGN(LocationBarView);
528};
529
530#endif  // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
531