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