• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 class ActionBoxButtonView;
33 class AddToAppLauncherView;
34 class CommandUpdater;
35 class ContentSettingBubbleModelDelegate;
36 class ContentSettingImageView;
37 class EVBubbleView;
38 class ExtensionAction;
39 class GURL;
40 class GeneratedCreditCardView;
41 class InstantController;
42 class KeywordHintView;
43 class LocationIconView;
44 class OpenPDFInReaderView;
45 class ManagePasswordsIconView;
46 class OriginChipView;
47 class PageActionWithBadgeView;
48 class PageActionImageView;
49 class Profile;
50 class SearchButton;
51 class SelectedKeywordView;
52 class StarView;
53 class TemplateURLService;
54 class TranslateIconView;
55 class ZoomView;
56 
57 namespace content {
58 struct SSLStatus;
59 }
60 
61 namespace gfx {
62 class SlideAnimation;
63 }
64 
65 namespace views {
66 class BubbleDelegateView;
67 class ImageButton;
68 class ImageView;
69 class Label;
70 class 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 /////////////////////////////////////////////////////////////////////////////
81 class 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.
dropdown_animation_offset()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:
~Delegate()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.
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.
zoom_view()167   ZoomView* zoom_view() { return zoom_view_; }
168 
169   // The passwords icon. It may not be visible.
manage_passwords_icon_view()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.
star_view()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.
translate_icon_view()195   TranslateIconView* translate_icon_view() { return translate_icon_view_; }
196 
197   // Returns the screen coordinates of the omnibox (where the URL text appears,
198   // not where the icons are shown).
199   gfx::Point GetOmniboxViewOrigin() const;
200 
201   // Shows |text| as an inline autocompletion.  This is useful for IMEs, where
202   // we can't show the autocompletion inside the actual OmniboxView.  See
203   // comments on |ime_inline_autocomplete_view_|.
204   void SetImeInlineAutocompletion(const base::string16& text);
205 
206   // Invoked from OmniboxViewWin to show gray text autocompletion.
207   void SetGrayTextAutocompletion(const base::string16& text);
208 
209   // Returns the current gray text autocompletion.
210   base::string16 GetGrayTextAutocompletion() const;
211 
212   // Set if we should show a focus rect while the location entry field is
213   // focused. Used when the toolbar is in full keyboard accessibility mode.
214   // Repaints if necessary.
215   virtual void SetShowFocusRect(bool show);
216 
217   // Select all of the text. Needed when the user tabs through controls
218   // in the toolbar in full keyboard accessibility mode.
219   virtual void SelectAll();
220 
location_icon_view()221   LocationIconView* location_icon_view() { return location_icon_view_; }
222 
223   // Return the point suitable for anchoring location-bar-anchored bubbles at.
224   // The point will be returned in the coordinates of the LocationBarView.
225   gfx::Point GetLocationBarAnchorPoint() const;
226 
omnibox_view()227   OmniboxViewViews* omnibox_view() { return omnibox_view_; }
omnibox_view()228   const OmniboxViewViews* omnibox_view() const { return omnibox_view_; }
229 
230   views::View* generated_credit_card_view();
231 
232   // Returns the height of the control without the top and bottom
233   // edges(i.e.  the height of the edit control inside).  If
234   // |use_preferred_size| is true this will be the preferred height,
235   // otherwise it will be the current height.
236   int GetInternalHeight(bool use_preferred_size);
237 
238   // Returns the position and width that the popup should be, and also the left
239   // edge that the results should align themselves to (which will leave some
240   // border on the left of the popup).
241   void GetOmniboxPopupPositioningInfo(gfx::Point* top_left_screen_coord,
242                                       int* popup_width,
243                                       int* left_margin,
244                                       int* right_margin);
245 
246   // LocationBar:
247   virtual void FocusLocation(bool select_all) OVERRIDE;
248   virtual void Revert() OVERRIDE;
249   virtual OmniboxView* GetOmniboxView() OVERRIDE;
250 
251   // views::View:
252   virtual bool HasFocus() const OVERRIDE;
253   virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
254   virtual gfx::Size GetPreferredSize() const OVERRIDE;
255   virtual void Layout() OVERRIDE;
256 
257   // OmniboxEditController:
258   virtual void Update(const content::WebContents* contents) OVERRIDE;
259   virtual void ShowURL() OVERRIDE;
260   virtual void EndOriginChipAnimations(bool cancel_fade) OVERRIDE;
261   virtual ToolbarModel* GetToolbarModel() OVERRIDE;
262   virtual content::WebContents* GetWebContents() OVERRIDE;
263 
264   // Thickness of the edges of the omnibox background images, in normal mode.
265   static const int kNormalEdgeThickness;
266   // The same, but for popup mode.
267   static const int kPopupEdgeThickness;
268   // Space between items in the location bar, as well as between items and the
269   // edges.
270   static const int kItemPadding;
271   // Amount of padding built into the standard omnibox icons.
272   static const int kIconInternalPadding;
273   // Amount of padding to place between the origin chip and the leading edge of
274   // the location bar.
275   static const int kOriginChipEdgeItemPadding;
276   // Amount of padding built into the origin chip.
277   static const int kOriginChipBuiltinPadding;
278   // Space between the edge and a bubble.
279   static const int kBubblePadding;
280 
281  private:
282   typedef std::vector<ContentSettingImageView*> ContentSettingViews;
283 
284   friend class PageActionImageView;
285   friend class PageActionWithBadgeView;
286   typedef std::vector<ExtensionAction*> PageActions;
287   typedef std::vector<PageActionWithBadgeView*> PageActionViews;
288 
289   // Helper for GetMinimumWidth().  Calculates the incremental minimum width
290   // |view| should add to the trailing width after the omnibox.
291   static int IncrementalMinimumWidth(views::View* view);
292 
293   // Returns the thickness of any visible left and right edge, in pixels.
294   int GetHorizontalEdgeThickness() const;
295 
296   // The same, but for the top and bottom edges.
vertical_edge_thickness()297   int vertical_edge_thickness() const {
298     return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness;
299   }
300 
301   // Updates the visibility state of the Content Blocked icons to reflect what
302   // is actually blocked on the current page. Returns true if the visibility
303   // of at least one of the views in |content_setting_views_| changed.
304   bool RefreshContentSettingViews();
305 
306   // Deletes all page action views that we have created.
307   void DeletePageActionViews();
308 
309   // Updates the views for the Page Actions, to reflect state changes for
310   // PageActions. Returns true if the visibility of a PageActionWithBadgeView
311   // changed, or PageActionWithBadgeView were created/destroyed.
312   bool RefreshPageActionViews();
313 
314   // Updates the view for the zoom icon based on the current tab's zoom. Returns
315   // true if the visibility of the view changed.
316   bool RefreshZoomView();
317 
318   // Updates the Translate icon based on the current tab's Translate status.
319   void RefreshTranslateIcon();
320 
321   // Updates |manage_passwords_icon_view_|. Returns true if visibility changed.
322   bool RefreshManagePasswordsIconView();
323 
324   // Shows the manage passwords bubble if there is a savable password.
325   void ShowManagePasswordsBubbleIfNeeded();
326 
327   // Helper to show the first run info bubble.
328   void ShowFirstRunBubbleInternal();
329 
330   // Handles a request to change the value of this text field from software
331   // using an accessibility API (typically automation software, screen readers
332   // don't normally use this). Sets the value and clears the selection.
333   void AccessibilitySetValue(const base::string16& new_value);
334 
335   // Returns true if the suggest text is valid.
336   bool HasValidSuggestText() const;
337 
338   bool ShouldShowKeywordBubble() const;
339   bool ShouldShowEVBubble() const;
340 
341   // Used to "reverse" the URL showing/hiding animations, since we use separate
342   // animations whose curves are not true inverses of each other.  Based on the
343   // current position of the omnibox, calculates what value the desired
344   // animation (|hide_url_animation_| if |hide| is true, |show_url_animation_|
345   // if it's false) should be set to in order to produce the same omnibox
346   // position.  This way we can stop the old animation, set the new animation to
347   // this value, and start it running, and the text will appear to reverse
348   // directions from its current location.
349   double GetValueForAnimation(bool hide) const;
350 
351   // Resets |show_url_animation_| and the color changes it causes.
352   void ResetShowAnimationAndColors();
353 
354   // LocationBar:
355   virtual void ShowFirstRunBubble() OVERRIDE;
356   virtual GURL GetDestinationURL() const OVERRIDE;
357   virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE;
358   virtual content::PageTransition GetPageTransition() const OVERRIDE;
359   virtual void AcceptInput() OVERRIDE;
360   virtual void FocusSearch() OVERRIDE;
361   virtual void UpdateContentSettingsIcons() OVERRIDE;
362   virtual void UpdateManagePasswordsIconAndBubble() OVERRIDE;
363   virtual void UpdatePageActions() OVERRIDE;
364   virtual void InvalidatePageActions() OVERRIDE;
365   virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE;
366   virtual void UpdateGeneratedCreditCardView() OVERRIDE;
367   virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE;
368   virtual const OmniboxView* GetOmniboxView() const OVERRIDE;
369   virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE;
370 
371   // LocationBarTesting:
372   virtual int PageActionCount() OVERRIDE;
373   virtual int PageActionVisibleCount() OVERRIDE;
374   virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE;
375   virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE;
376   virtual void TestPageActionPressed(size_t index) OVERRIDE;
377   virtual bool GetBookmarkStarVisibility() OVERRIDE;
378 
379   // views::View:
380   virtual const char* GetClassName() const OVERRIDE;
381   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
382   virtual void OnFocus() OVERRIDE;
383   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
384   virtual void PaintChildren(gfx::Canvas* canvas,
385                              const views::CullSet& cull_set) OVERRIDE;
386 
387   // views::ButtonListener:
388   virtual void ButtonPressed(views::Button* sender,
389                              const ui::Event& event) OVERRIDE;
390 
391   // views::DragController:
392   virtual void WriteDragDataForView(View* sender,
393                                     const gfx::Point& press_pt,
394                                     OSExchangeData* data) OVERRIDE;
395   virtual int GetDragOperationsForView(View* sender,
396                                        const gfx::Point& p) OVERRIDE;
397   virtual bool CanStartDragForView(View* sender,
398                                    const gfx::Point& press_pt,
399                                    const gfx::Point& p) OVERRIDE;
400 
401   // OmniboxEditController:
402   virtual void OnChanged() OVERRIDE;
403   virtual void OnSetFocus() OVERRIDE;
404   virtual InstantController* GetInstant() OVERRIDE;
405   virtual const ToolbarModel* GetToolbarModel() const OVERRIDE;
406   virtual void HideURL() OVERRIDE;
407 
408   // DropdownBarHostDelegate:
409   virtual void SetFocusAndSelection(bool select_all) OVERRIDE;
410   virtual void SetAnimationOffset(int offset) OVERRIDE;
411 
412   // gfx::AnimationDelegate:
413   virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
414   virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
415 
416   // TemplateURLServiceObserver:
417   virtual void OnTemplateURLServiceChanged() OVERRIDE;
418 
419   // content::NotificationObserver:
420   virtual void Observe(int type,
421                        const content::NotificationSource& source,
422                        const content::NotificationDetails& details) OVERRIDE;
423 
424   // SearchModelObserver:
425   virtual void ModelChanged(const SearchModel::State& old_state,
426                             const SearchModel::State& new_state) OVERRIDE;
427 
428   // The Browser this LocationBarView is in.  Note that at least
429   // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser
430   // window, so this may be NULL.
431   Browser* browser_;
432 
433   OmniboxViewViews* omnibox_view_;
434 
435   // Our delegate.
436   Delegate* delegate_;
437 
438   // Object used to paint the border.
439   scoped_ptr<views::Painter> border_painter_;
440 
441   // The origin chip that may appear in the location bar.
442   OriginChipView* origin_chip_view_;
443 
444   // An icon to the left of the edit field.
445   LocationIconView* location_icon_view_;
446 
447   // A bubble displayed for EV HTTPS sites.
448   EVBubbleView* ev_bubble_view_;
449 
450   // A view to show inline autocompletion when an IME is active.  In this case,
451   // we shouldn't change the text or selection inside the OmniboxView itself,
452   // since this will conflict with the IME's control over the text.  So instead
453   // we show any autocompletion in a separate field after the OmniboxView.
454   views::Label* ime_inline_autocomplete_view_;
455 
456   // The following views are used to provide hints and remind the user as to
457   // what is going in the edit. They are all added a children of the
458   // LocationBarView. At most one is visible at a time. Preference is
459   // given to the keyword_view_, then hint_view_.
460   // These autocollapse when the edit needs the room.
461 
462   // Shown if the user has selected a keyword.
463   SelectedKeywordView* selected_keyword_view_;
464 
465   // View responsible for showing suggested text. This is NULL when there is no
466   // suggested text.
467   views::Label* suggested_text_view_;
468 
469   // Shown if the selected url has a corresponding keyword.
470   KeywordHintView* keyword_hint_view_;
471 
472   // The voice search icon.
473   views::ImageButton* mic_search_view_;
474 
475   // The content setting views.
476   ContentSettingViews content_setting_views_;
477 
478   // The zoom icon.
479   ZoomView* zoom_view_;
480 
481   // A bubble that shows after successfully generating a new credit card number.
482   GeneratedCreditCardView* generated_credit_card_view_;
483 
484   // The icon to open a PDF in Reader.
485   OpenPDFInReaderView* open_pdf_in_reader_view_;
486 
487   // The manage passwords icon.
488   ManagePasswordsIconView* manage_passwords_icon_view_;
489 
490   // The current page actions.
491   PageActions page_actions_;
492 
493   // The page action icon views.
494   PageActionViews page_action_views_;
495 
496   // The icon for Translate.
497   TranslateIconView* translate_icon_view_;
498 
499   // The view to add pages to the app launcher.
500   AddToAppLauncherView* add_to_app_launcher_view_;
501 
502   // The star.
503   StarView* star_view_;
504 
505   // The search/go button.
506   SearchButton* search_button_;
507 
508   // Whether we're in popup mode. This value also controls whether the location
509   // bar is read-only.
510   const bool is_popup_mode_;
511 
512   // True if we should show a focus rect while the location entry field is
513   // focused. Used when the toolbar is in full keyboard accessibility mode.
514   bool show_focus_rect_;
515 
516   // This is in case we're destroyed before the model loads. We need to make
517   // Add/RemoveObserver calls.
518   TemplateURLService* template_url_service_;
519 
520   // Tracks this preference to determine whether bookmark editing is allowed.
521   BooleanPrefMember edit_bookmarks_enabled_;
522 
523   // During dropdown animation, the host clips the widget and draws only the
524   // bottom part of it. The view needs to know the pixel offset at which we are
525   // drawing the widget so that we can draw the curved edges that attach to the
526   // toolbar in the right location.
527   int dropdown_animation_offset_;
528 
529   // Origin chip animations.
530   //
531   // For the "show URL" animation, we instantly hide the origin chip and show
532   // the |omnibox_view_| in its place, containing the complete URL.  However, we
533   // clip that view (using the XXX_leading_inset_ and XXX_width_ members) so
534   // that only the hostname is visible.  We also offset the omnibox (using the
535   // XXX_offset_ members) so the hostname is in the same place as it was in the
536   // origin chip.  Finally, we set the selection text and background color of
537   // the text to match the pressed origin chip.  Then, as the animation runs,
538   // all of these values are animated to their steady-state values (no omnibox
539   // offset, no inset, width equal to the full omnibox text [which is reset to
540   // "no width clamp" after the animation ends], and standard selection colors).
541   //
542   // For the hide animation, we run the positioning and clipping parts of the
543   // animation in reverse, but instead of changing the selection color, because
544   // there usually isn't a selection when hiding, we leave the omnibox colors
545   // alone, and when the hide animation has ended, tell the origin chip to
546   // fade-in its background.
547   scoped_ptr<gfx::SlideAnimation> show_url_animation_;
548   scoped_ptr<gfx::SlideAnimation> hide_url_animation_;
549   // The omnibox offset may be positive or negative.  The starting offset is the
550   // amount necessary to shift the |omnibox_view_| by such that the hostname
551   // portion of the URL aligns with the hostname in the origin chip.  As the
552   // show animation runs, the current offset gradually moves to 0.
553   int starting_omnibox_offset_;
554   int current_omnibox_offset_;
555   // The leading inset is always positive.  The starting inset is the width of
556   // the text between the leading edge of the omnibox and the edge of the
557   // hostname, which is clipped off at the start of the show animation.  Note
558   // that in RTL mode, this will be the part of the URL that is logically after
559   // the hostname.  As the show animation runs, the current inset gradually
560   // moves to 0.
561   int starting_omnibox_leading_inset_;
562   int current_omnibox_leading_inset_;
563   // The width is always positive.  The ending width is the width of the entire
564   // omnibox URL.  As the show animation runs, the current width gradually moves
565   // from the width of the hostname to the ending value.
566   int current_omnibox_width_;
567   int ending_omnibox_width_;
568 
569   // Used to register for notifications received by NotificationObserver.
570   content::NotificationRegistrar registrar_;
571 
572   // Used to bind callback functions to this object.
573   base::WeakPtrFactory<LocationBarView> weak_ptr_factory_;
574 
575   DISALLOW_COPY_AND_ASSIGN(LocationBarView);
576 };
577 
578 #endif  // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
579