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.
5 #ifndef CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
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"
31 class ActionBoxButtonView;
33 class ContentSettingBubbleModelDelegate;
34 class ContentSettingImageView;
36 class ExtensionAction;
38 class GeneratedCreditCardView;
39 class InstantController;
40 class KeywordHintView;
41 class LocationIconView;
42 class OpenPDFInReaderView;
43 class ManagePasswordsIconView;
44 class PageActionWithBadgeView;
45 class PageActionImageView;
47 class SelectedKeywordView;
50 class TemplateURLService;
51 class TranslateIconView;
59 class BubbleDelegateView;
67 /////////////////////////////////////////////////////////////////////////////
69 // LocationBarView class
71 // The LocationBarView class is a View subclass that paints the background
72 // of the URL bar strip and contains its content.
74 /////////////////////////////////////////////////////////////////////////////
75 class LocationBarView : public LocationBar,
76 public LocationBarTesting,
78 public views::ButtonListener,
79 public views::DragController,
80 public OmniboxEditController,
81 public DropdownBarHostDelegate,
82 public TemplateURLServiceObserver,
83 public content::NotificationObserver,
84 public SearchModelObserver {
86 // The location bar view's class name.
87 static const char kViewClassName[];
89 // DropdownBarHostDelegate:
90 virtual void SetFocusAndSelection(bool select_all) OVERRIDE;
91 virtual void SetAnimationOffset(int offset) OVERRIDE;
93 // Returns the offset used while animating.
94 int animation_offset() const { return animation_offset_; }
98 // Should return the current web contents.
99 virtual content::WebContents* GetWebContents() = 0;
101 // Returns the InstantController, or NULL if there isn't one.
102 virtual InstantController* GetInstant() = 0;
104 virtual ToolbarModel* GetToolbarModel() = 0;
105 virtual const ToolbarModel* GetToolbarModel() const = 0;
107 // Creates Widget for the given delegate.
108 virtual views::Widget* CreateViewsBubble(
109 views::BubbleDelegateView* bubble_delegate) = 0;
111 // Creates PageActionImageView. Caller gets an ownership.
112 virtual PageActionImageView* CreatePageActionImageView(
113 LocationBarView* owner,
114 ExtensionAction* action) = 0;
116 // Returns ContentSettingBubbleModelDelegate.
117 virtual ContentSettingBubbleModelDelegate*
118 GetContentSettingBubbleModelDelegate() = 0;
120 // Shows permissions and settings for the given web contents.
121 virtual void ShowWebsiteSettings(content::WebContents* web_contents,
123 const content::SSLStatus& ssl) = 0;
126 virtual ~Delegate() {}
137 LocationBarView(Browser* browser,
139 CommandUpdater* command_updater,
143 virtual ~LocationBarView();
145 // Uses GetBuiltInHorizontalPaddingForChildViews() to optionally add
146 // additional padding (via an empty border) to |view|. This should be called
147 // during creation on all child views which are potentially touchable so that
148 // when touch is enabled they will have sufficient padding.
149 static void InitTouchableLocationBarChildView(views::View* view);
151 // Initializes the LocationBarView.
154 // True if this instance has been initialized by calling Init, which can only
155 // be called when the receiving instance is attached to a view container.
156 bool IsInitialized() const;
158 // Returns the appropriate color for the desired kind, based on the user's
160 SkColor GetColor(ToolbarModel::SecurityLevel security_level,
161 ColorKind kind) const;
163 // Returns the delegate.
164 Delegate* delegate() const { return delegate_; }
166 // See comment in browser_window.h for more info.
167 void ZoomChangedForActiveTab(bool can_show_bubble);
169 // The zoom icon. It may not be visible.
170 ZoomView* zoom_view() { return zoom_view_; }
172 // The passwords icon. It may not be visible.
173 ManagePasswordsIconView* manage_passwords_icon_view() {
174 return manage_passwords_icon_view_;
177 // Sets |preview_enabled| for the PageAction View associated with this
178 // |page_action|. If |preview_enabled| is true, the view will display the
179 // PageActions icon even though it has not been activated by the extension.
180 // This is used by the ExtensionInstalledBubble to preview what the icon
181 // will look like for the user upon installation of the extension.
182 void SetPreviewEnabledPageAction(ExtensionAction* page_action,
183 bool preview_enabled);
185 // Retrieves the PageAction View which is associated with |page_action|.
186 views::View* GetPageActionView(ExtensionAction* page_action);
188 // Toggles the star on or off.
189 void SetStarToggled(bool on);
191 // The star. It may not be visible.
192 StarView* star_view() { return star_view_; }
194 // Toggles the translate icon on or off.
195 void SetTranslateIconToggled(bool on);
197 // The translate icon. It may not be visible.
198 TranslateIconView* translate_icon_view() { return translate_icon_view_; }
200 void set_site_chip_view(SiteChipView* site_chip_view) {
201 site_chip_view_ = site_chip_view;
204 // Shows the bookmark prompt.
205 void ShowBookmarkPrompt();
207 // Returns the screen coordinates of the omnibox (where the URL text appears,
208 // not where the icons are shown).
209 gfx::Point GetOmniboxViewOrigin() const;
211 // Shows |text| as an inline autocompletion. This is useful for IMEs, where
212 // we can't show the autocompletion inside the actual OmniboxView. See
213 // comments on |ime_inline_autocomplete_view_|.
214 void SetImeInlineAutocompletion(const base::string16& text);
216 // Invoked from OmniboxViewWin to show gray text autocompletion.
217 void SetGrayTextAutocompletion(const base::string16& text);
219 // Returns the current gray text autocompletion.
220 base::string16 GetGrayTextAutocompletion() const;
223 virtual gfx::Size GetPreferredSize() OVERRIDE;
225 // Layout and Painting functions
226 virtual void Layout() OVERRIDE;
227 virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE;
228 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
230 // Set if we should show a focus rect while the location entry field is
231 // focused. Used when the toolbar is in full keyboard accessibility mode.
232 // Repaints if necessary.
233 virtual void SetShowFocusRect(bool show);
235 // Select all of the text. Needed when the user tabs through controls
236 // in the toolbar in full keyboard accessibility mode.
237 virtual void SelectAll();
239 views::ImageView* GetLocationIconView();
240 const views::ImageView* GetLocationIconView() const;
242 // Return a view suitable for anchoring location-bar-anchored bubbles to.
243 views::View* GetLocationBarAnchor();
244 // Return the point suitable for anchoring location-bar-anchored bubbles at.
245 // The point will be returned in the coordinates of the LocationBarView.
246 gfx::Point GetLocationBarAnchorPoint() const;
248 OmniboxViewViews* omnibox_view() { return omnibox_view_; }
250 views::View* generated_credit_card_view();
252 // OmniboxEditController:
253 virtual void Update(const content::WebContents* contents) OVERRIDE;
254 virtual void OnChanged() OVERRIDE;
255 virtual void OnSetFocus() OVERRIDE;
256 virtual InstantController* GetInstant() OVERRIDE;
257 virtual content::WebContents* GetWebContents() OVERRIDE;
258 virtual ToolbarModel* GetToolbarModel() OVERRIDE;
259 virtual const ToolbarModel* GetToolbarModel() const OVERRIDE;
262 virtual const char* GetClassName() const OVERRIDE;
263 virtual bool HasFocus() const OVERRIDE;
264 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
265 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
267 // views::ButtonListener:
268 virtual void ButtonPressed(views::Button* sender,
269 const ui::Event& event) OVERRIDE;
271 // views::DragController:
272 virtual void WriteDragDataForView(View* sender,
273 const gfx::Point& press_pt,
274 OSExchangeData* data) OVERRIDE;
275 virtual int GetDragOperationsForView(View* sender,
276 const gfx::Point& p) OVERRIDE;
277 virtual bool CanStartDragForView(View* sender,
278 const gfx::Point& press_pt,
279 const gfx::Point& p) OVERRIDE;
282 virtual void ShowFirstRunBubble() OVERRIDE;
283 virtual GURL GetDestinationURL() const OVERRIDE;
284 virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE;
285 virtual content::PageTransition GetPageTransition() const OVERRIDE;
286 virtual void AcceptInput() OVERRIDE;
287 virtual void FocusLocation(bool select_all) OVERRIDE;
288 virtual void FocusSearch() OVERRIDE;
289 virtual void UpdateContentSettingsIcons() OVERRIDE;
290 virtual void UpdateManagePasswordsIconAndBubble() OVERRIDE;
291 virtual void UpdatePageActions() OVERRIDE;
292 virtual void InvalidatePageActions() OVERRIDE;
293 virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE;
294 virtual void UpdateGeneratedCreditCardView() OVERRIDE;
295 virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE;
296 virtual void Revert() OVERRIDE;
297 virtual const OmniboxView* GetOmniboxView() const OVERRIDE;
298 virtual OmniboxView* GetOmniboxView() OVERRIDE;
299 virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE;
301 // LocationBarTesting:
302 virtual int PageActionCount() OVERRIDE;
303 virtual int PageActionVisibleCount() OVERRIDE;
304 virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE;
305 virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE;
306 virtual void TestPageActionPressed(size_t index) OVERRIDE;
307 virtual bool GetBookmarkStarVisibility() OVERRIDE;
309 // TemplateURLServiceObserver:
310 virtual void OnTemplateURLServiceChanged() OVERRIDE;
312 // content::NotificationObserver:
313 virtual void Observe(int type,
314 const content::NotificationSource& source,
315 const content::NotificationDetails& details) OVERRIDE;
317 // SearchModelObserver:
318 virtual void ModelChanged(const SearchModel::State& old_state,
319 const SearchModel::State& new_state) OVERRIDE;
321 // Returns the height of the control without the top and bottom
322 // edges(i.e. the height of the edit control inside). If
323 // |use_preferred_size| is true this will be the preferred height,
324 // otherwise it will be the current height.
325 int GetInternalHeight(bool use_preferred_size);
327 // Returns the position and width that the popup should be, and also the left
328 // edge that the results should align themselves to (which will leave some
329 // border on the left of the popup).
330 void GetOmniboxPopupPositioningInfo(gfx::Point* top_left_screen_coord,
335 // Space between items in the location bar, as well as between items and the
337 static int GetItemPadding();
339 // Thickness of the edges of the omnibox background images, in normal mode.
340 static const int kNormalEdgeThickness;
341 // The same, but for popup mode.
342 static const int kPopupEdgeThickness;
343 // Amount of padding built into the standard omnibox icons.
344 static const int kIconInternalPadding;
345 // Space between the edge and a bubble.
346 static const int kBubblePadding;
349 virtual void OnFocus() OVERRIDE;
352 typedef std::vector<ContentSettingImageView*> ContentSettingViews;
354 friend class PageActionImageView;
355 friend class PageActionWithBadgeView;
356 typedef std::vector<ExtensionAction*> PageActions;
357 typedef std::vector<PageActionWithBadgeView*> PageActionViews;
359 // Returns the number of pixels of built-in padding to the left and right for
360 // child views. This is nonzero when touch UI is enabled so as to space out
361 // child views for easier targeting. See InitTouchableLocationBarChildView().
362 static int GetBuiltInHorizontalPaddingForChildViews();
364 // Returns the thickness of any visible left and right edge, in pixels.
365 int GetHorizontalEdgeThickness() const;
367 // The same, but for the top and bottom edges.
368 int vertical_edge_thickness() const {
369 return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness;
372 // Updates the visibility state of the Content Blocked icons to reflect what
373 // is actually blocked on the current page. Returns true if the visibility
374 // of at least one of the views in |content_setting_views_| changed.
375 bool RefreshContentSettingViews();
377 // Deletes all page action views that we have created.
378 void DeletePageActionViews();
380 // Updates the views for the Page Actions, to reflect state changes for
381 // PageActions. Returns true if the visibility of a PageActionWithBadgeView
382 // changed, or PageActionWithBadgeView were created/destroyed.
383 bool RefreshPageActionViews();
385 // Updates the view for the zoom icon based on the current tab's zoom. Returns
386 // true if the visibility of the view changed.
387 bool RefreshZoomView();
389 // Updates the Translate icon based on the current tab's Translate status.
390 void RefreshTranslateIcon();
392 // Sets the visibility of view to new_vis.
393 void ToggleVisibility(bool new_vis, views::View* view);
395 // Updates |manage_passwords_icon_view_|. Returns true if visibility changed.
396 bool RefreshManagePasswordsIconView();
398 // Shows the manage passwords bubble if there is a savable password.
399 void ShowManagePasswordsBubbleIfNeeded();
401 // Returns true if the suggest text is valid.
402 bool HasValidSuggestText() const;
404 // Helper to show the first run info bubble.
405 void ShowFirstRunBubbleInternal();
407 // Draws backgrounds and borders for page actions. Must be called
408 // after layout, so the |page_action_views_| have their bounds.
409 void PaintPageActionBackgrounds(gfx::Canvas* canvas);
411 // Handles a request to change the value of this text field from software
412 // using an accessibility API (typically automation software, screen readers
413 // don't normally use this). Sets the value and clears the selection.
414 void AccessibilitySetValue(const base::string16& new_value);
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.
421 OmniboxViewViews* omnibox_view_;
426 // Object used to paint the border.
427 scoped_ptr<views::Painter> border_painter_;
429 // An icon to the left of the edit field.
430 LocationIconView* location_icon_view_;
432 // A bubble displayed for EV HTTPS sites.
433 EVBubbleView* ev_bubble_view_;
435 // A view to show inline autocompletion when an IME is active. In this case,
436 // we shouldn't change the text or selection inside the OmniboxView itself,
437 // since this will conflict with the IME's control over the text. So instead
438 // we show any autocompletion in a separate field after the OmniboxView.
439 views::Label* ime_inline_autocomplete_view_;
441 // The following views are used to provide hints and remind the user as to
442 // what is going in the edit. They are all added a children of the
443 // LocationBarView. At most one is visible at a time. Preference is
444 // given to the keyword_view_, then hint_view_.
445 // These autocollapse when the edit needs the room.
447 // Shown if the user has selected a keyword.
448 SelectedKeywordView* selected_keyword_view_;
450 // View responsible for showing suggested text. This is NULL when there is no
452 views::Label* suggested_text_view_;
454 // Shown if the selected url has a corresponding keyword.
455 KeywordHintView* keyword_hint_view_;
457 // The voice search icon.
458 views::ImageButton* mic_search_view_;
460 // The content setting views.
461 ContentSettingViews content_setting_views_;
464 ZoomView* zoom_view_;
466 // A bubble that shows after successfully generating a new credit card number.
467 GeneratedCreditCardView* generated_credit_card_view_;
469 // The icon to open a PDF in Reader.
470 OpenPDFInReaderView* open_pdf_in_reader_view_;
472 // The manage passwords icon.
473 ManagePasswordsIconView* manage_passwords_icon_view_;
475 // The current page actions.
476 PageActions page_actions_;
478 // The page action icon views.
479 PageActionViews page_action_views_;
482 SiteChipView* site_chip_view_;
484 // The icon for Translate.
485 TranslateIconView* translate_icon_view_;
488 StarView* star_view_;
490 // The search/go button.
491 views::LabelButton* search_button_;
493 // Whether we're in popup mode. This value also controls whether the location
495 const bool is_popup_mode_;
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_;
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_;
505 // Tracks this preference to determine whether bookmark editing is allowed.
506 BooleanPrefMember edit_bookmarks_enabled_;
508 // While animating, the host clips the widget and draws only the bottom
509 // part of it. The view needs to know the pixel offset at which we are drawing
510 // the widget so that we can draw the curved edges that attach to the toolbar
511 // in the right location.
512 int animation_offset_;
514 // Used to register for notifications received by NotificationObserver.
515 content::NotificationRegistrar registrar_;
517 // Used to bind callback functions to this object.
518 base::WeakPtrFactory<LocationBarView> weak_ptr_factory_;
520 DISALLOW_COPY_AND_ASSIGN(LocationBarView);
523 #endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_