location_bar_view.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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/prefs/pref_member.h"
13#include "chrome/browser/extensions/extension_context_menu_model.h"
14#include "chrome/browser/search_engines/template_url_service_observer.h"
15#include "chrome/browser/ui/omnibox/location_bar.h"
16#include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
17#include "chrome/browser/ui/toolbar/toolbar_model.h"
18#include "chrome/browser/ui/views/dropdown_bar_host.h"
19#include "chrome/browser/ui/views/dropdown_bar_host_delegate.h"
20#include "chrome/browser/ui/views/extensions/extension_popup.h"
21#include "content/public/browser/notification_observer.h"
22#include "content/public/browser/notification_registrar.h"
23#include "ui/gfx/font.h"
24#include "ui/gfx/rect.h"
25#include "ui/views/controls/native/native_view_host.h"
26#include "ui/views/drag_controller.h"
27
28#if defined(USE_AURA)
29#include "ui/compositor/layer_animation_observer.h"
30#endif
31
32class ActionBoxButtonView;
33class CommandUpdater;
34class ContentSettingBubbleModelDelegate;
35class ContentSettingImageView;
36class EVBubbleView;
37class ExtensionAction;
38class GURL;
39class InstantController;
40class KeywordHintView;
41class LocationBarSeparatorView;
42class LocationIconView;
43class OpenPDFInReaderView;
44class PageActionWithBadgeView;
45class PageActionImageView;
46class Profile;
47class ScriptBubbleIconView;
48class SelectedKeywordView;
49class StarView;
50class TemplateURLService;
51class ZoomView;
52
53namespace views {
54class BubbleDelegateView;
55class Label;
56class Widget;
57}
58
59/////////////////////////////////////////////////////////////////////////////
60//
61// LocationBarView class
62//
63//   The LocationBarView class is a View subclass that paints the background
64//   of the URL bar strip and contains its content.
65//
66/////////////////////////////////////////////////////////////////////////////
67class LocationBarView : public LocationBar,
68                        public LocationBarTesting,
69                        public views::View,
70                        public views::DragController,
71                        public OmniboxEditController,
72                        public DropdownBarHostDelegate,
73                        public TemplateURLServiceObserver,
74                        public content::NotificationObserver {
75 public:
76  // The location bar view's class name.
77  static const char kViewClassName[];
78
79  // DropdownBarHostDelegate:
80  virtual void SetFocusAndSelection(bool select_all) OVERRIDE;
81  virtual void SetAnimationOffset(int offset) OVERRIDE;
82
83  // Returns the offset used while animating.
84  int animation_offset() const { return animation_offset_; }
85
86  class Delegate {
87   public:
88    // Should return the current web contents.
89    virtual content::WebContents* GetWebContents() const = 0;
90
91    // Returns the InstantController, or NULL if there isn't one.
92    virtual InstantController* GetInstant() = 0;
93
94    // Creates Widget for the given delegate.
95    virtual views::Widget* CreateViewsBubble(
96        views::BubbleDelegateView* bubble_delegate) = 0;
97
98    // Creates PageActionImageView. Caller gets an ownership.
99    virtual PageActionImageView* CreatePageActionImageView(
100        LocationBarView* owner,
101        ExtensionAction* action) = 0;
102
103    // Returns ContentSettingBubbleModelDelegate.
104    virtual ContentSettingBubbleModelDelegate*
105        GetContentSettingBubbleModelDelegate() = 0;
106
107    // Shows permissions and settings for the given web contents.
108    virtual void ShowWebsiteSettings(content::WebContents* web_contents,
109                                     const GURL& url,
110                                     const content::SSLStatus& ssl,
111                                     bool show_history) = 0;
112
113    // Called by the location bar view when the user starts typing in the edit.
114    // This forces our security style to be UNKNOWN for the duration of the
115    // editing.
116    virtual void OnInputInProgress(bool in_progress) = 0;
117
118   protected:
119    virtual ~Delegate() {}
120  };
121
122  enum ColorKind {
123    BACKGROUND = 0,
124    TEXT,
125    SELECTED_TEXT,
126    DEEMPHASIZED_TEXT,
127    SECURITY_TEXT,
128  };
129
130  LocationBarView(Browser* browser,
131                  Profile* profile,
132                  CommandUpdater* command_updater,
133                  ToolbarModel* model,
134                  Delegate* delegate,
135                  bool is_popup_mode);
136
137  virtual ~LocationBarView();
138
139  // Initializes the LocationBarView.
140  void Init();
141
142  // True if this instance has been initialized by calling Init, which can only
143  // be called when the receiving instance is attached to a view container.
144  bool IsInitialized() const;
145
146  // Returns the appropriate color for the desired kind, based on the user's
147  // system theme.
148  SkColor GetColor(ToolbarModel::SecurityLevel security_level,
149                   ColorKind kind) const;
150
151  // Updates the location bar.  We also reset the bar's permanent text and
152  // security style, and, if |tab_for_state_restoring| is non-NULL, also restore
153  // saved state that the tab holds.
154  void Update(const content::WebContents* tab_for_state_restoring);
155
156  // Returns corresponding profile.
157  Profile* profile() const { return profile_; }
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 view. It may not be visible.
166  ZoomView* zoom_view() { return zoom_view_; }
167
168  // Sets |preview_enabled| for the PageAction View associated with this
169  // |page_action|. If |preview_enabled| is true, the view will display the
170  // PageActions icon even though it has not been activated by the extension.
171  // This is used by the ExtensionInstalledBubble to preview what the icon
172  // will look like for the user upon installation of the extension.
173  void SetPreviewEnabledPageAction(ExtensionAction* page_action,
174                                   bool preview_enabled);
175
176  // Retrieves the PageAction View which is associated with |page_action|.
177  views::View* GetPageActionView(ExtensionAction* page_action);
178
179  // Toggles the star on or off.
180  void SetStarToggled(bool on);
181
182  // Returns the star view. It may not be visible.
183  StarView* star_view() { return star_view_; }
184
185  // Shows the bookmark prompt.
186  void ShowBookmarkPrompt();
187
188  // Shows the Chrome To Mobile bubble.
189  void ShowChromeToMobileBubble();
190
191  // Returns the screen coordinates of the location entry (where the URL text
192  // appears, not where the icons are shown).
193  gfx::Point GetLocationEntryOrigin() const;
194
195  // Invoked from OmniboxViewWin to show the instant suggestion.
196  void SetInstantSuggestion(const string16& text);
197
198  // Returns the current instant suggestion text.
199  string16 GetInstantSuggestion() const;
200
201  // Sets whether the location entry can accept focus.
202  void SetLocationEntryFocusable(bool focusable);
203
204  // Returns true if the location entry is focusable and visible in
205  // the root view.
206  bool IsLocationEntryFocusableInRootView() const;
207
208  // Sizing functions
209  virtual gfx::Size GetPreferredSize() OVERRIDE;
210
211  // Layout and Painting functions
212  virtual void Layout() OVERRIDE;
213  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
214
215  // No focus border for the location bar, the caret is enough.
216  virtual void OnPaintFocusBorder(gfx::Canvas* canvas) OVERRIDE { }
217
218  // Set if we should show a focus rect while the location entry field is
219  // focused. Used when the toolbar is in full keyboard accessibility mode.
220  // Repaints if necessary.
221  virtual void SetShowFocusRect(bool show);
222
223  // Select all of the text. Needed when the user tabs through controls
224  // in the toolbar in full keyboard accessibility mode.
225  virtual void SelectAll();
226
227#if defined(OS_WIN) && !defined(USE_AURA)
228  // Event Handlers
229  virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
230  virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
231  virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
232  virtual void OnMouseCaptureLost() OVERRIDE;
233#endif
234
235  LocationIconView* location_icon_view() { return location_icon_view_; }
236  const LocationIconView* location_icon_view() const {
237    return location_icon_view_;
238  }
239
240  views::View* location_entry_view() const { return location_entry_view_; }
241
242  // OmniboxEditController:
243  virtual void OnAutocompleteAccept(const GURL& url,
244                                    WindowOpenDisposition disposition,
245                                    content::PageTransition transition,
246                                    const GURL& alternate_nav_url) OVERRIDE;
247  virtual void OnChanged() OVERRIDE;
248  virtual void OnSelectionBoundsChanged() OVERRIDE;
249  virtual void OnInputInProgress(bool in_progress) OVERRIDE;
250  virtual void OnKillFocus() OVERRIDE;
251  virtual void OnSetFocus() OVERRIDE;
252  virtual gfx::Image GetFavicon() const OVERRIDE;
253  virtual string16 GetTitle() const OVERRIDE;
254  virtual InstantController* GetInstant() OVERRIDE;
255  virtual content::WebContents* GetWebContents() const OVERRIDE;
256
257  // views::View:
258  virtual const char* GetClassName() const OVERRIDE;
259  virtual bool SkipDefaultKeyEventProcessing(
260      const ui::KeyEvent& event) OVERRIDE;
261  virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
262  virtual bool HasFocus() const OVERRIDE;
263  virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
264
265  // views::DragController:
266  virtual void WriteDragDataForView(View* sender,
267                                    const gfx::Point& press_pt,
268                                    OSExchangeData* data) OVERRIDE;
269  virtual int GetDragOperationsForView(View* sender,
270                                       const gfx::Point& p) OVERRIDE;
271  virtual bool CanStartDragForView(View* sender,
272                                   const gfx::Point& press_pt,
273                                   const gfx::Point& p) OVERRIDE;
274
275  // LocationBar:
276  virtual void ShowFirstRunBubble() OVERRIDE;
277  virtual void SetInstantSuggestion(
278      const InstantSuggestion& suggestion) OVERRIDE;
279  virtual string16 GetInputString() const OVERRIDE;
280  virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE;
281  virtual content::PageTransition GetPageTransition() const OVERRIDE;
282  virtual void AcceptInput() OVERRIDE;
283  virtual void FocusLocation(bool select_all) OVERRIDE;
284  virtual void FocusSearch() OVERRIDE;
285  virtual void UpdateContentSettingsIcons() OVERRIDE;
286  virtual void UpdatePageActions() OVERRIDE;
287  virtual void InvalidatePageActions() OVERRIDE;
288  virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE;
289  virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE;
290  virtual void Revert() OVERRIDE;
291  virtual const OmniboxView* GetLocationEntry() const OVERRIDE;
292  virtual OmniboxView* GetLocationEntry() OVERRIDE;
293  virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE;
294
295  // LocationBarTesting:
296  virtual int PageActionCount() OVERRIDE;
297  virtual int PageActionVisibleCount() OVERRIDE;
298  virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE;
299  virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE;
300  virtual void TestPageActionPressed(size_t index) OVERRIDE;
301  virtual void TestActionBoxMenuItemSelected(int command_id) OVERRIDE;
302  virtual bool GetBookmarkStarVisibility() OVERRIDE;
303
304  // TemplateURLServiceObserver:
305  virtual void OnTemplateURLServiceChanged() OVERRIDE;
306
307  // content::NotificationObserver:
308  virtual void Observe(int type,
309                       const content::NotificationSource& source,
310                       const content::NotificationDetails& details) OVERRIDE;
311
312  // Returns the height of the control without the top and bottom
313  // edges(i.e.  the height of the edit control inside).  If
314  // |use_preferred_size| is true this will be the preferred height,
315  // otherwise it will be the current height.
316  int GetInternalHeight(bool use_preferred_size);
317
318  // Space between items in the location bar.
319  static int GetItemPadding();
320
321  // Space between the edges and the items next to them.
322  static int GetEdgeItemPadding();
323
324  // Thickness of the left and right edges of the omnibox, in normal mode.
325  static const int kNormalHorizontalEdgeThickness;
326  // The same, but for popup mode.
327  static const int kPopupEdgeThickness;
328  // Thickness of the top and bottom edges of the omnibox.
329  static const int kNormalVerticalEdgeThickness;
330  // Amount of padding built into the standard omnibox icons.
331  static const int kIconInternalPadding;
332  // Space between the edge and a bubble.
333  static const int kBubblePadding;
334
335 protected:
336  virtual void OnFocus() OVERRIDE;
337
338 private:
339  typedef std::vector<ContentSettingImageView*> ContentSettingViews;
340
341  friend class PageActionImageView;
342  friend class PageActionWithBadgeView;
343  typedef std::vector<PageActionWithBadgeView*> PageActionViews;
344
345  // Returns the thickness of any visible left and right edge, in pixels.
346  int GetHorizontalEdgeThickness() const;
347
348  // The same, but for the top and bottom edges.
349  int vertical_edge_thickness() const {
350    return is_popup_mode_ ? kPopupEdgeThickness : kNormalVerticalEdgeThickness;
351  }
352
353  // Update the visibility state of the Content Blocked icons to reflect what is
354  // actually blocked on the current page.
355  void RefreshContentSettingViews();
356
357  // Delete all page action views that we have created.
358  void DeletePageActionViews();
359
360  // Update the views for the Page Actions, to reflect state changes for
361  // PageActions.
362  void RefreshPageActionViews();
363
364  // Returns the number of scripts currently running on the page.
365  size_t ScriptBubbleScriptsRunning();
366
367  // Update the Script Bubble Icon, to reflect the number of content scripts
368  // running on the page.
369  void RefreshScriptBubble();
370
371  // Update the view for the zoom icon based on the current tab's zoom.
372  void RefreshZoomView();
373
374  // Sets the visibility of view to new_vis.
375  void ToggleVisibility(bool new_vis, views::View* view);
376
377#if !defined(USE_AURA)
378  // Helper for the Mouse event handlers that does all the real work.
379  void OnMouseEvent(const ui::MouseEvent& event, UINT msg);
380#endif
381
382  // Returns true if the suggest text is valid.
383  bool HasValidSuggestText() const;
384
385  // Helper to show the first run info bubble.
386  void ShowFirstRunBubbleInternal();
387
388  // Draw backgrounds and borders for page actions.  Must be called
389  // after layout, so the |page_action_views_| have their bounds.
390  void PaintPageActionBackgrounds(gfx::Canvas* canvas);
391
392  // The Browser this LocationBarView is in.  Note that at least
393  // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser
394  // window, so this may be NULL.
395  Browser* browser_;
396
397  // The Autocomplete Edit field.
398  scoped_ptr<OmniboxView> location_entry_;
399
400  // The profile which corresponds to this View.
401  Profile* profile_;
402
403  // Command updater which corresponds to this View.
404  CommandUpdater* command_updater_;
405
406  // The model.
407  ToolbarModel* model_;
408
409  // Our delegate.
410  Delegate* delegate_;
411
412  // This is the string of text from the autocompletion session that the user
413  // entered or selected.
414  string16 location_input_;
415
416  // The user's desired disposition for how their input should be opened
417  WindowOpenDisposition disposition_;
418
419  // The transition type to use for the navigation
420  content::PageTransition transition_;
421
422  // An object used to paint the normal-mode background.
423  scoped_ptr<views::Painter> background_painter_;
424
425  // An icon to the left of the edit field.
426  LocationIconView* location_icon_view_;
427
428  // A bubble displayed for EV HTTPS sites.
429  EVBubbleView* ev_bubble_view_;
430
431  // Location_entry view
432  views::View* location_entry_view_;
433
434  // The following views are used to provide hints and remind the user as to
435  // what is going in the edit. They are all added a children of the
436  // LocationBarView. At most one is visible at a time. Preference is
437  // given to the keyword_view_, then hint_view_.
438  // These autocollapse when the edit needs the room.
439
440  // Shown if the user has selected a keyword.
441  SelectedKeywordView* selected_keyword_view_;
442
443  // View responsible for showing suggested text. This is NULL when there is no
444  // suggested text.
445  views::Label* suggested_text_view_;
446
447  // Shown if the selected url has a corresponding keyword.
448  KeywordHintView* keyword_hint_view_;
449
450  // View responsible for showing text "<Search provider> Search", which appears
451  // when omnibox replaces the URL with its query terms and there's enough space
452  // in omnibox.
453  views::Label* search_token_view_;
454  LocationBarSeparatorView* search_token_separator_view_;
455
456  // The content setting views.
457  ContentSettingViews content_setting_views_;
458
459  // The zoom icon.
460  ZoomView* zoom_view_;
461
462  // The icon to open a PDF in Reader.
463  OpenPDFInReaderView* open_pdf_in_reader_view_;
464
465  // The current page actions.
466  std::vector<ExtensionAction*> page_actions_;
467
468  // The page action icon views.
469  PageActionViews page_action_views_;
470
471  // The script bubble.
472  ScriptBubbleIconView* script_bubble_icon_view_;
473
474  // The star.
475  StarView* star_view_;
476
477  // The action box button (plus).
478  ActionBoxButtonView* action_box_button_view_;
479
480  // Whether we're in popup mode.
481  const bool is_popup_mode_;
482
483  // True if we should show a focus rect while the location entry field is
484  // focused. Used when the toolbar is in full keyboard accessibility mode.
485  bool show_focus_rect_;
486
487  // This is in case we're destroyed before the model loads. We need to make
488  // Add/RemoveObserver calls.
489  TemplateURLService* template_url_service_;
490
491  // Tracks this preference to determine whether bookmark editing is allowed.
492  BooleanPrefMember edit_bookmarks_enabled_;
493
494  // While animating, the host clips the widget and draws only the bottom
495  // part of it. The view needs to know the pixel offset at which we are drawing
496  // the widget so that we can draw the curved edges that attach to the toolbar
497  // in the right location.
498  int animation_offset_;
499
500  // Used to register for notifications received by NotificationObserver.
501  content::NotificationRegistrar registrar_;
502
503  DISALLOW_COPY_AND_ASSIGN(LocationBarView);
504};
505
506#endif  // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
507