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
;
45 class PageActionWithBadgeView
;
46 class PageActionImageView
;
48 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_origin_chip_view(OriginChipView
* origin_chip_view
) {
201 origin_chip_view_
= origin_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 // Handles a request to change the value of this text field from software
408 // using an accessibility API (typically automation software, screen readers
409 // don't normally use this). Sets the value and clears the selection.
410 void AccessibilitySetValue(const base::string16
& new_value
);
412 // The Browser this LocationBarView is in. Note that at least
413 // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser
414 // window, so this may be NULL.
417 OmniboxViewViews
* omnibox_view_
;
422 // Object used to paint the border.
423 scoped_ptr
<views::Painter
> border_painter_
;
425 // An icon to the left of the edit field.
426 LocationIconView
* location_icon_view_
;
428 // A bubble displayed for EV HTTPS sites.
429 EVBubbleView
* ev_bubble_view_
;
431 // A view to show inline autocompletion when an IME is active. In this case,
432 // we shouldn't change the text or selection inside the OmniboxView itself,
433 // since this will conflict with the IME's control over the text. So instead
434 // we show any autocompletion in a separate field after the OmniboxView.
435 views::Label
* ime_inline_autocomplete_view_
;
437 // The following views are used to provide hints and remind the user as to
438 // what is going in the edit. They are all added a children of the
439 // LocationBarView. At most one is visible at a time. Preference is
440 // given to the keyword_view_, then hint_view_.
441 // These autocollapse when the edit needs the room.
443 // Shown if the user has selected a keyword.
444 SelectedKeywordView
* selected_keyword_view_
;
446 // View responsible for showing suggested text. This is NULL when there is no
448 views::Label
* suggested_text_view_
;
450 // Shown if the selected url has a corresponding keyword.
451 KeywordHintView
* keyword_hint_view_
;
453 // The voice search icon.
454 views::ImageButton
* mic_search_view_
;
456 // The content setting views.
457 ContentSettingViews content_setting_views_
;
460 ZoomView
* zoom_view_
;
462 // A bubble that shows after successfully generating a new credit card number.
463 GeneratedCreditCardView
* generated_credit_card_view_
;
465 // The icon to open a PDF in Reader.
466 OpenPDFInReaderView
* open_pdf_in_reader_view_
;
468 // The manage passwords icon.
469 ManagePasswordsIconView
* manage_passwords_icon_view_
;
471 // The current page actions.
472 PageActions page_actions_
;
474 // The page action icon views.
475 PageActionViews page_action_views_
;
478 OriginChipView
* origin_chip_view_
;
480 // The icon for Translate.
481 TranslateIconView
* translate_icon_view_
;
484 StarView
* star_view_
;
486 // The search/go button.
487 views::LabelButton
* search_button_
;
489 // Whether we're in popup mode. This value also controls whether the location
491 const bool is_popup_mode_
;
493 // True if we should show a focus rect while the location entry field is
494 // focused. Used when the toolbar is in full keyboard accessibility mode.
495 bool show_focus_rect_
;
497 // This is in case we're destroyed before the model loads. We need to make
498 // Add/RemoveObserver calls.
499 TemplateURLService
* template_url_service_
;
501 // Tracks this preference to determine whether bookmark editing is allowed.
502 BooleanPrefMember edit_bookmarks_enabled_
;
504 // While animating, the host clips the widget and draws only the bottom
505 // part of it. The view needs to know the pixel offset at which we are drawing
506 // the widget so that we can draw the curved edges that attach to the toolbar
507 // in the right location.
508 int animation_offset_
;
510 // Used to register for notifications received by NotificationObserver.
511 content::NotificationRegistrar registrar_
;
513 // Used to bind callback functions to this object.
514 base::WeakPtrFactory
<LocationBarView
> weak_ptr_factory_
;
516 DISALLOW_COPY_AND_ASSIGN(LocationBarView
);
519 #endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_