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/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"
32 class ActionBoxButtonView
;
34 class ContentSettingBubbleModelDelegate
;
35 class ContentSettingImageView
;
37 class ExtensionAction
;
39 class GeneratedCreditCardView
;
40 class InstantController
;
41 class KeywordHintView
;
42 class LocationIconView
;
43 class OpenPDFInReaderView
;
44 class ManagePasswordsIconView
;
46 class PageActionWithBadgeView
;
47 class PageActionImageView
;
49 class SelectedKeywordView
;
51 class TemplateURLService
;
52 class TranslateIconView
;
64 class BubbleDelegateView
;
72 /////////////////////////////////////////////////////////////////////////////
74 // LocationBarView class
76 // The LocationBarView class is a View subclass that paints the background
77 // of the URL bar strip and contains its content.
79 /////////////////////////////////////////////////////////////////////////////
80 class LocationBarView
: public LocationBar
,
81 public LocationBarTesting
,
83 public views::ButtonListener
,
84 public views::DragController
,
85 public OmniboxEditController
,
86 public DropdownBarHostDelegate
,
87 public gfx::AnimationDelegate
,
88 public TemplateURLServiceObserver
,
89 public content::NotificationObserver
,
90 public SearchModelObserver
{
92 // The location bar view's class name.
93 static const char kViewClassName
[];
95 // Returns the offset used during dropdown animation.
96 int dropdown_animation_offset() const { return dropdown_animation_offset_
; }
100 // Should return the current web contents.
101 virtual content::WebContents
* GetWebContents() = 0;
103 // Returns the InstantController, or NULL if there isn't one.
104 virtual InstantController
* GetInstant() = 0;
106 virtual ToolbarModel
* GetToolbarModel() = 0;
107 virtual const ToolbarModel
* GetToolbarModel() const = 0;
109 // Creates Widget for the given delegate.
110 virtual views::Widget
* CreateViewsBubble(
111 views::BubbleDelegateView
* bubble_delegate
) = 0;
113 // Creates PageActionImageView. Caller gets an ownership.
114 virtual PageActionImageView
* CreatePageActionImageView(
115 LocationBarView
* owner
,
116 ExtensionAction
* action
) = 0;
118 // Returns ContentSettingBubbleModelDelegate.
119 virtual ContentSettingBubbleModelDelegate
*
120 GetContentSettingBubbleModelDelegate() = 0;
122 // Shows permissions and settings for the given web contents.
123 virtual void ShowWebsiteSettings(content::WebContents
* web_contents
,
125 const content::SSLStatus
& ssl
) = 0;
128 virtual ~Delegate() {}
139 LocationBarView(Browser
* browser
,
141 CommandUpdater
* command_updater
,
145 virtual ~LocationBarView();
147 // Initializes the LocationBarView.
150 // True if this instance has been initialized by calling Init, which can only
151 // be called when the receiving instance is attached to a view container.
152 bool IsInitialized() const;
154 // Returns the appropriate color for the desired kind, based on the user's
156 SkColor
GetColor(ToolbarModel::SecurityLevel security_level
,
157 ColorKind kind
) const;
159 // Returns the delegate.
160 Delegate
* delegate() const { return delegate_
; }
162 // See comment in browser_window.h for more info.
163 void ZoomChangedForActiveTab(bool can_show_bubble
);
165 // The zoom icon. It may not be visible.
166 ZoomView
* zoom_view() { return zoom_view_
; }
168 // The passwords icon. It may not be visible.
169 ManagePasswordsIconView
* manage_passwords_icon_view() {
170 return manage_passwords_icon_view_
;
173 // Sets |preview_enabled| for the PageAction View associated with this
174 // |page_action|. If |preview_enabled| is true, the view will display the
175 // PageActions icon even though it has not been activated by the extension.
176 // This is used by the ExtensionInstalledBubble to preview what the icon
177 // will look like for the user upon installation of the extension.
178 void SetPreviewEnabledPageAction(ExtensionAction
* page_action
,
179 bool preview_enabled
);
181 // Retrieves the PageAction View which is associated with |page_action|.
182 PageActionWithBadgeView
* GetPageActionView(ExtensionAction
* page_action
);
184 // Toggles the star on or off.
185 void SetStarToggled(bool on
);
187 // The star. It may not be visible.
188 StarView
* star_view() { return star_view_
; }
190 // Toggles the translate icon on or off.
191 void SetTranslateIconToggled(bool on
);
193 // The translate icon. It may not be visible.
194 TranslateIconView
* translate_icon_view() { return translate_icon_view_
; }
196 // Returns the screen coordinates of the omnibox (where the URL text appears,
197 // not where the icons are shown).
198 gfx::Point
GetOmniboxViewOrigin() const;
200 // Shows |text| as an inline autocompletion. This is useful for IMEs, where
201 // we can't show the autocompletion inside the actual OmniboxView. See
202 // comments on |ime_inline_autocomplete_view_|.
203 void SetImeInlineAutocompletion(const base::string16
& text
);
205 // Invoked from OmniboxViewWin to show gray text autocompletion.
206 void SetGrayTextAutocompletion(const base::string16
& text
);
208 // Returns the current gray text autocompletion.
209 base::string16
GetGrayTextAutocompletion() const;
211 // Set if we should show a focus rect while the location entry field is
212 // focused. Used when the toolbar is in full keyboard accessibility mode.
213 // Repaints if necessary.
214 virtual void SetShowFocusRect(bool show
);
216 // Select all of the text. Needed when the user tabs through controls
217 // in the toolbar in full keyboard accessibility mode.
218 virtual void SelectAll();
220 LocationIconView
* location_icon_view() { return location_icon_view_
; }
222 // Return the point suitable for anchoring location-bar-anchored bubbles at.
223 // The point will be returned in the coordinates of the LocationBarView.
224 gfx::Point
GetLocationBarAnchorPoint() const;
226 OmniboxViewViews
* omnibox_view() { return omnibox_view_
; }
227 const OmniboxViewViews
* omnibox_view() const { return omnibox_view_
; }
229 views::View
* generated_credit_card_view();
231 // Returns the height of the control without the top and bottom
232 // edges(i.e. the height of the edit control inside). If
233 // |use_preferred_size| is true this will be the preferred height,
234 // otherwise it will be the current height.
235 int GetInternalHeight(bool use_preferred_size
);
237 // Returns the position and width that the popup should be, and also the left
238 // edge that the results should align themselves to (which will leave some
239 // border on the left of the popup).
240 void GetOmniboxPopupPositioningInfo(gfx::Point
* top_left_screen_coord
,
246 virtual void FocusLocation(bool select_all
) OVERRIDE
;
247 virtual void Revert() OVERRIDE
;
248 virtual OmniboxView
* GetOmniboxView() OVERRIDE
;
251 virtual bool HasFocus() const OVERRIDE
;
252 virtual void GetAccessibleState(ui::AXViewState
* state
) OVERRIDE
;
253 virtual gfx::Size
GetPreferredSize() const OVERRIDE
;
254 virtual void Layout() OVERRIDE
;
256 // OmniboxEditController:
257 virtual void Update(const content::WebContents
* contents
) OVERRIDE
;
258 virtual void ShowURL() OVERRIDE
;
259 virtual ToolbarModel
* GetToolbarModel() OVERRIDE
;
260 virtual content::WebContents
* GetWebContents() OVERRIDE
;
262 // Thickness of the edges of the omnibox background images, in normal mode.
263 static const int kNormalEdgeThickness
;
264 // The same, but for popup mode.
265 static const int kPopupEdgeThickness
;
266 // Space between items in the location bar, as well as between items and the
268 static const int kItemPadding
;
269 // Amount of padding built into the standard omnibox icons.
270 static const int kIconInternalPadding
;
271 // Amount of padding to place between the origin chip and the leading edge of
273 static const int kOriginChipEdgeItemPadding
;
274 // Amount of padding built into the origin chip.
275 static const int kOriginChipBuiltinPadding
;
276 // Space between the edge and a bubble.
277 static const int kBubblePadding
;
280 typedef std::vector
<ContentSettingImageView
*> ContentSettingViews
;
282 friend class PageActionImageView
;
283 friend class PageActionWithBadgeView
;
284 typedef std::vector
<ExtensionAction
*> PageActions
;
285 typedef std::vector
<PageActionWithBadgeView
*> PageActionViews
;
287 // Helper for GetMinimumWidth(). Calculates the incremental minimum width
288 // |view| should add to the trailing width after the omnibox.
289 static int IncrementalMinimumWidth(views::View
* view
);
291 // Returns the thickness of any visible left and right edge, in pixels.
292 int GetHorizontalEdgeThickness() const;
294 // The same, but for the top and bottom edges.
295 int vertical_edge_thickness() const {
296 return is_popup_mode_
? kPopupEdgeThickness
: kNormalEdgeThickness
;
299 // Updates the visibility state of the Content Blocked icons to reflect what
300 // is actually blocked on the current page. Returns true if the visibility
301 // of at least one of the views in |content_setting_views_| changed.
302 bool RefreshContentSettingViews();
304 // Deletes all page action views that we have created.
305 void DeletePageActionViews();
307 // Updates the views for the Page Actions, to reflect state changes for
308 // PageActions. Returns true if the visibility of a PageActionWithBadgeView
309 // changed, or PageActionWithBadgeView were created/destroyed.
310 bool RefreshPageActionViews();
312 // Updates the view for the zoom icon based on the current tab's zoom. Returns
313 // true if the visibility of the view changed.
314 bool RefreshZoomView();
316 // Updates the Translate icon based on the current tab's Translate status.
317 void RefreshTranslateIcon();
319 // Updates |manage_passwords_icon_view_|. Returns true if visibility changed.
320 bool RefreshManagePasswordsIconView();
322 // Shows the manage passwords bubble if there is a savable password.
323 void ShowManagePasswordsBubbleIfNeeded();
325 // Helper to show the first run info bubble.
326 void ShowFirstRunBubbleInternal();
328 // Handles a request to change the value of this text field from software
329 // using an accessibility API (typically automation software, screen readers
330 // don't normally use this). Sets the value and clears the selection.
331 void AccessibilitySetValue(const base::string16
& new_value
);
333 // Returns true if the suggest text is valid.
334 bool HasValidSuggestText() const;
336 bool ShouldShowKeywordBubble() const;
337 bool ShouldShowEVBubble() const;
339 // Origin chip animation control methods.
340 void OnShowURLAnimationEnded();
341 void OnHideURLAnimationEnded();
344 virtual void ShowFirstRunBubble() OVERRIDE
;
345 virtual GURL
GetDestinationURL() const OVERRIDE
;
346 virtual WindowOpenDisposition
GetWindowOpenDisposition() const OVERRIDE
;
347 virtual content::PageTransition
GetPageTransition() const OVERRIDE
;
348 virtual void AcceptInput() OVERRIDE
;
349 virtual void FocusSearch() OVERRIDE
;
350 virtual void UpdateContentSettingsIcons() OVERRIDE
;
351 virtual void UpdateManagePasswordsIconAndBubble() OVERRIDE
;
352 virtual void UpdatePageActions() OVERRIDE
;
353 virtual void InvalidatePageActions() OVERRIDE
;
354 virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE
;
355 virtual void UpdateGeneratedCreditCardView() OVERRIDE
;
356 virtual void SaveStateToContents(content::WebContents
* contents
) OVERRIDE
;
357 virtual const OmniboxView
* GetOmniboxView() const OVERRIDE
;
358 virtual LocationBarTesting
* GetLocationBarForTesting() OVERRIDE
;
360 // LocationBarTesting:
361 virtual int PageActionCount() OVERRIDE
;
362 virtual int PageActionVisibleCount() OVERRIDE
;
363 virtual ExtensionAction
* GetPageAction(size_t index
) OVERRIDE
;
364 virtual ExtensionAction
* GetVisiblePageAction(size_t index
) OVERRIDE
;
365 virtual void TestPageActionPressed(size_t index
) OVERRIDE
;
366 virtual bool GetBookmarkStarVisibility() OVERRIDE
;
369 virtual const char* GetClassName() const OVERRIDE
;
370 virtual void OnBoundsChanged(const gfx::Rect
& previous_bounds
) OVERRIDE
;
371 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
372 virtual void PaintChildren(gfx::Canvas
* canvas
,
373 const views::CullSet
& cull_set
) OVERRIDE
;
375 // views::ButtonListener:
376 virtual void ButtonPressed(views::Button
* sender
,
377 const ui::Event
& event
) OVERRIDE
;
379 // views::DragController:
380 virtual void WriteDragDataForView(View
* sender
,
381 const gfx::Point
& press_pt
,
382 OSExchangeData
* data
) OVERRIDE
;
383 virtual int GetDragOperationsForView(View
* sender
,
384 const gfx::Point
& p
) OVERRIDE
;
385 virtual bool CanStartDragForView(View
* sender
,
386 const gfx::Point
& press_pt
,
387 const gfx::Point
& p
) OVERRIDE
;
389 // OmniboxEditController:
390 virtual void OnChanged() OVERRIDE
;
391 virtual void OnSetFocus() OVERRIDE
;
392 virtual InstantController
* GetInstant() OVERRIDE
;
393 virtual const ToolbarModel
* GetToolbarModel() const OVERRIDE
;
394 virtual void HideURL() OVERRIDE
;
396 // DropdownBarHostDelegate:
397 virtual void SetFocusAndSelection(bool select_all
) OVERRIDE
;
398 virtual void SetAnimationOffset(int offset
) OVERRIDE
;
400 // gfx::AnimationDelegate:
401 virtual void AnimationProgressed(const gfx::Animation
* animation
) OVERRIDE
;
402 virtual void AnimationEnded(const gfx::Animation
* animation
) OVERRIDE
;
404 // TemplateURLServiceObserver:
405 virtual void OnTemplateURLServiceChanged() OVERRIDE
;
407 // content::NotificationObserver:
408 virtual void Observe(int type
,
409 const content::NotificationSource
& source
,
410 const content::NotificationDetails
& details
) OVERRIDE
;
412 // SearchModelObserver:
413 virtual void ModelChanged(const SearchModel::State
& old_state
,
414 const SearchModel::State
& new_state
) OVERRIDE
;
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 // The origin chip that may appear in the location bar.
430 OriginChipView
* origin_chip_view_
;
432 // An icon to the left of the edit field.
433 LocationIconView
* location_icon_view_
;
435 // A bubble displayed for EV HTTPS sites.
436 EVBubbleView
* ev_bubble_view_
;
438 // A view to show inline autocompletion when an IME is active. In this case,
439 // we shouldn't change the text or selection inside the OmniboxView itself,
440 // since this will conflict with the IME's control over the text. So instead
441 // we show any autocompletion in a separate field after the OmniboxView.
442 views::Label
* ime_inline_autocomplete_view_
;
444 // The following views are used to provide hints and remind the user as to
445 // what is going in the edit. They are all added a children of the
446 // LocationBarView. At most one is visible at a time. Preference is
447 // given to the keyword_view_, then hint_view_.
448 // These autocollapse when the edit needs the room.
450 // Shown if the user has selected a keyword.
451 SelectedKeywordView
* selected_keyword_view_
;
453 // View responsible for showing suggested text. This is NULL when there is no
455 views::Label
* suggested_text_view_
;
457 // Shown if the selected url has a corresponding keyword.
458 KeywordHintView
* keyword_hint_view_
;
460 // The voice search icon.
461 views::ImageButton
* mic_search_view_
;
463 // The content setting views.
464 ContentSettingViews content_setting_views_
;
467 ZoomView
* zoom_view_
;
469 // A bubble that shows after successfully generating a new credit card number.
470 GeneratedCreditCardView
* generated_credit_card_view_
;
472 // The icon to open a PDF in Reader.
473 OpenPDFInReaderView
* open_pdf_in_reader_view_
;
475 // The manage passwords icon.
476 ManagePasswordsIconView
* manage_passwords_icon_view_
;
478 // The current page actions.
479 PageActions page_actions_
;
481 // The page action icon views.
482 PageActionViews page_action_views_
;
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 // During dropdown animation, the host clips the widget and draws only the
509 // bottom part of it. The view needs to know the pixel offset at which we are
510 // drawing the widget so that we can draw the curved edges that attach to the
511 // toolbar in the right location.
512 int dropdown_animation_offset_
;
514 // Origin chip animations.
515 scoped_ptr
<gfx::SlideAnimation
> show_url_animation_
;
516 scoped_ptr
<gfx::SlideAnimation
> hide_url_animation_
;
518 // Text label shown only during origin chip animations.
519 views::Label
* animated_host_label_
;
521 // Used to register for notifications received by NotificationObserver.
522 content::NotificationRegistrar registrar_
;
524 // Used to bind callback functions to this object.
525 base::WeakPtrFactory
<LocationBarView
> weak_ptr_factory_
;
527 DISALLOW_COPY_AND_ASSIGN(LocationBarView
);
530 #endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_