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/prefs/pref_member.h"
13 #include "chrome/browser/extensions/extension_context_menu_model.h"
14 #include "chrome/browser/ui/location_bar/location_bar.h"
15 #include "chrome/browser/ui/omnibox/chrome_omnibox_edit_controller.h"
16 #include "chrome/browser/ui/search/search_model_observer.h"
17 #include "chrome/browser/ui/toolbar/chrome_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 "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
22 #include "components/search_engines/template_url_service_observer.h"
23 #include "components/ui/zoom/zoom_event_manager_observer.h"
24 #include "ui/gfx/animation/animation_delegate.h"
25 #include "ui/gfx/animation/slide_animation.h"
26 #include "ui/gfx/font.h"
27 #include "ui/gfx/geometry/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 InstantController
;
39 class KeywordHintView
;
40 class LocationIconView
;
41 class OpenPDFInReaderView
;
42 class ManagePasswordsIconView
;
43 class PageActionWithBadgeView
;
44 class PageActionImageView
;
46 class SelectedKeywordView
;
48 class TemplateURLService
;
49 class TranslateIconView
;
57 class BubbleDelegateView
;
64 /////////////////////////////////////////////////////////////////////////////
66 // LocationBarView class
68 // The LocationBarView class is a View subclass that paints the background
69 // of the URL bar strip and contains its content.
71 /////////////////////////////////////////////////////////////////////////////
72 class LocationBarView
: public LocationBar
,
73 public LocationBarTesting
,
75 public views::ButtonListener
,
76 public views::DragController
,
77 public gfx::AnimationDelegate
,
78 public ChromeOmniboxEditController
,
79 public DropdownBarHostDelegate
,
80 public TemplateURLServiceObserver
,
81 public SearchModelObserver
,
82 public ui_zoom::ZoomEventManagerObserver
{
84 // The location bar view's class name.
85 static const char kViewClassName
[];
87 // Returns the offset used during dropdown animation.
88 int dropdown_animation_offset() const { return dropdown_animation_offset_
; }
92 // Should return the current web contents.
93 virtual content::WebContents
* GetWebContents() = 0;
95 virtual ToolbarModel
* GetToolbarModel() = 0;
96 virtual const ToolbarModel
* GetToolbarModel() const = 0;
98 // Creates Widget for the given delegate.
99 virtual views::Widget
* CreateViewsBubble(
100 views::BubbleDelegateView
* bubble_delegate
) = 0;
102 // Creates PageActionImageView. Caller gets an ownership.
103 virtual PageActionImageView
* CreatePageActionImageView(
104 LocationBarView
* owner
,
105 ExtensionAction
* action
) = 0;
107 // Returns ContentSettingBubbleModelDelegate.
108 virtual ContentSettingBubbleModelDelegate
*
109 GetContentSettingBubbleModelDelegate() = 0;
111 // Shows permissions and settings for the given web contents.
112 virtual void ShowWebsiteSettings(content::WebContents
* web_contents
,
114 const content::SSLStatus
& ssl
) = 0;
117 virtual ~Delegate() {}
128 LocationBarView(Browser
* browser
,
130 CommandUpdater
* command_updater
,
134 ~LocationBarView() override
;
136 // Initializes the LocationBarView.
139 // True if this instance has been initialized by calling Init, which can only
140 // be called when the receiving instance is attached to a view container.
141 bool IsInitialized() const;
143 // Returns the appropriate color for the desired kind, based on the user's
145 SkColor
GetColor(SecurityStateModel::SecurityLevel security_level
,
146 ColorKind kind
) const;
148 // Returns the delegate.
149 Delegate
* delegate() const { return delegate_
; }
151 // See comment in browser_window.h for more info.
152 void ZoomChangedForActiveTab(bool can_show_bubble
);
154 // The zoom icon. It may not be visible.
155 ZoomView
* zoom_view() { return zoom_view_
; }
157 // The passwords icon. It may not be visible.
158 ManagePasswordsIconView
* manage_passwords_icon_view() {
159 return manage_passwords_icon_view_
;
162 // Sets |preview_enabled| for the PageAction View associated with this
163 // |page_action|. If |preview_enabled| is true, the view will display the
164 // PageActions icon even though it has not been activated by the extension.
165 // This is used by the ExtensionInstalledBubble to preview what the icon
166 // will look like for the user upon installation of the extension.
167 void SetPreviewEnabledPageAction(ExtensionAction
* page_action
,
168 bool preview_enabled
);
170 // Retrieves the PageAction View which is associated with |page_action|.
171 PageActionWithBadgeView
* GetPageActionView(ExtensionAction
* page_action
);
173 // Toggles the star on or off.
174 void SetStarToggled(bool on
);
176 // The star. It may not be visible.
177 StarView
* star_view() { return star_view_
; }
179 // Toggles the translate icon on or off.
180 void SetTranslateIconToggled(bool on
);
182 // The translate icon. It may not be visible.
183 TranslateIconView
* translate_icon_view() { return translate_icon_view_
; }
185 // Returns the screen coordinates of the omnibox (where the URL text appears,
186 // not where the icons are shown).
187 gfx::Point
GetOmniboxViewOrigin() const;
189 // Shows |text| as an inline autocompletion. This is useful for IMEs, where
190 // we can't show the autocompletion inside the actual OmniboxView. See
191 // comments on |ime_inline_autocomplete_view_|.
192 void SetImeInlineAutocompletion(const base::string16
& text
);
194 // Invoked from OmniboxViewWin to show gray text autocompletion.
195 void SetGrayTextAutocompletion(const base::string16
& text
);
197 // Returns the current gray text autocompletion.
198 base::string16
GetGrayTextAutocompletion() const;
200 // Set if we should show a focus rect while the location entry field is
201 // focused. Used when the toolbar is in full keyboard accessibility mode.
202 // Repaints if necessary.
203 virtual void SetShowFocusRect(bool show
);
205 // Select all of the text. Needed when the user tabs through controls
206 // in the toolbar in full keyboard accessibility mode.
207 virtual void SelectAll();
209 LocationIconView
* location_icon_view() { return location_icon_view_
; }
211 // Return the point suitable for anchoring location-bar-anchored bubbles at.
212 // The point will be returned in the coordinates of the LocationBarView.
213 gfx::Point
GetLocationBarAnchorPoint() const;
215 OmniboxViewViews
* omnibox_view() { return omnibox_view_
; }
216 const OmniboxViewViews
* omnibox_view() const { return omnibox_view_
; }
218 // Returns the height of the control without the top and bottom
219 // edges(i.e. the height of the edit control inside). If
220 // |use_preferred_size| is true this will be the preferred height,
221 // otherwise it will be the current height.
222 int GetInternalHeight(bool use_preferred_size
);
224 // Returns the position and width that the popup should be, and also the left
225 // edge that the results should align themselves to (which will leave some
226 // border on the left of the popup). |top_edge_overlap| specifies the number
227 // of pixels the top edge of the popup should overlap the bottom edge of
229 void GetOmniboxPopupPositioningInfo(gfx::Point
* top_left_screen_coord
,
233 int top_edge_overlap
);
235 // Updates the controller, and, if |contents| is non-null, restores saved
236 // state that the tab holds.
237 void Update(const content::WebContents
* contents
);
239 // Clears the location bar's state for |contents|.
240 void ResetTabState(content::WebContents
* contents
);
243 void FocusLocation(bool select_all
) override
;
244 void Revert() override
;
245 OmniboxView
* GetOmniboxView() override
;
248 bool HasFocus() const override
;
249 void GetAccessibleState(ui::AXViewState
* state
) override
;
250 gfx::Size
GetPreferredSize() const override
;
251 void Layout() override
;
253 // ChromeOmniboxEditController:
254 void UpdateWithoutTabRestore() override
;
255 void ShowURL() override
;
256 ToolbarModel
* GetToolbarModel() override
;
257 content::WebContents
* GetWebContents() override
;
259 // ZoomEventManagerObserver:
260 // Updates the view for the zoom icon when default zoom levels change.
261 void OnDefaultZoomLevelChanged() override
;
264 typedef std::vector
<ContentSettingImageView
*> ContentSettingViews
;
266 friend class PageActionImageView
;
267 friend class PageActionWithBadgeView
;
268 typedef std::vector
<ExtensionAction
*> PageActions
;
269 typedef std::vector
<PageActionWithBadgeView
*> PageActionViews
;
271 // Helper for GetMinimumWidth(). Calculates the incremental minimum width
272 // |view| should add to the trailing width after the omnibox.
273 int IncrementalMinimumWidth(views::View
* view
) const;
275 // Returns the thickness of any visible left and right edge, in pixels.
276 int GetHorizontalEdgeThickness() const;
278 // The same, but for the top and bottom edges.
279 int GetVerticalEdgeThickness() const;
281 // The vertical padding to be applied to all contained views.
282 int VerticalPadding() const;
284 // Updates the visibility state of the Content Blocked icons to reflect what
285 // is actually blocked on the current page. Returns true if the visibility
286 // of at least one of the views in |content_setting_views_| changed.
287 bool RefreshContentSettingViews();
289 // Clears |page_action_views_| and removes the elements from the view
291 void DeletePageActionViews();
293 // Updates the views for the Page Actions, to reflect state changes for
294 // PageActions. Returns true if the visibility of a PageActionWithBadgeView
295 // changed, or PageActionWithBadgeView were created/destroyed.
296 bool RefreshPageActionViews();
298 // Whether the page actions represented by |page_action_views_| differ
299 // in ordering or value from |page_actions|.
300 bool PageActionsDiffer(const PageActions
& page_actions
) const;
302 // Updates the view for the zoom icon based on the current tab's zoom. Returns
303 // true if the visibility of the view changed.
304 bool RefreshZoomView();
306 // Updates the Translate icon based on the current tab's Translate status.
307 void RefreshTranslateIcon();
309 // Updates |manage_passwords_icon_view_|. Returns true if visibility changed.
310 bool RefreshManagePasswordsIconView();
312 // Helper to show the first run info bubble.
313 void ShowFirstRunBubbleInternal();
315 // Returns true if the suggest text is valid.
316 bool HasValidSuggestText() const;
318 bool ShouldShowKeywordBubble() const;
319 bool ShouldShowEVBubble() const;
321 // Used to "reverse" the URL showing/hiding animations, since we use separate
322 // animations whose curves are not true inverses of each other. Based on the
323 // current position of the omnibox, calculates what value the desired
324 // animation (|hide_url_animation_| if |hide| is true, |show_url_animation_|
325 // if it's false) should be set to in order to produce the same omnibox
326 // position. This way we can stop the old animation, set the new animation to
327 // this value, and start it running, and the text will appear to reverse
328 // directions from its current location.
329 double GetValueForAnimation(bool hide
) const;
332 void ShowFirstRunBubble() override
;
333 GURL
GetDestinationURL() const override
;
334 WindowOpenDisposition
GetWindowOpenDisposition() const override
;
335 ui::PageTransition
GetPageTransition() const override
;
336 void AcceptInput() override
;
337 void FocusSearch() override
;
338 void UpdateContentSettingsIcons() override
;
339 void UpdateManagePasswordsIconAndBubble() override
;
340 void UpdatePageActions() override
;
341 void UpdateBookmarkStarVisibility() override
;
342 void UpdateLocationBarVisibility(bool visible
, bool animation
) override
;
343 bool ShowPageActionPopup(const extensions::Extension
* extension
,
344 bool grant_active_tab
) override
;
345 void UpdateOpenPDFInReaderPrompt() override
;
346 void SaveStateToContents(content::WebContents
* contents
) override
;
347 const OmniboxView
* GetOmniboxView() const override
;
348 LocationBarTesting
* GetLocationBarForTesting() override
;
350 // LocationBarTesting:
351 int PageActionCount() override
;
352 int PageActionVisibleCount() override
;
353 ExtensionAction
* GetPageAction(size_t index
) override
;
354 ExtensionAction
* GetVisiblePageAction(size_t index
) override
;
355 void TestPageActionPressed(size_t index
) override
;
356 bool GetBookmarkStarVisibility() override
;
359 const char* GetClassName() const override
;
360 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
361 void OnFocus() override
;
362 void OnPaint(gfx::Canvas
* canvas
) override
;
363 void PaintChildren(const ui::PaintContext
& context
) override
;
365 // views::ButtonListener:
366 void ButtonPressed(views::Button
* sender
, const ui::Event
& event
) override
;
368 // views::DragController:
369 void WriteDragDataForView(View
* sender
,
370 const gfx::Point
& press_pt
,
371 OSExchangeData
* data
) override
;
372 int GetDragOperationsForView(View
* sender
, const gfx::Point
& p
) override
;
373 bool CanStartDragForView(View
* sender
,
374 const gfx::Point
& press_pt
,
375 const gfx::Point
& p
) override
;
377 // gfx::AnimationDelegate:
378 void AnimationProgressed(const gfx::Animation
* animation
) override
;
379 void AnimationEnded(const gfx::Animation
* animation
) override
;
381 // ChromeOmniboxEditController:
382 void OnChanged() override
;
383 void OnSetFocus() override
;
384 const ToolbarModel
* GetToolbarModel() const override
;
386 // DropdownBarHostDelegate:
387 void SetFocusAndSelection(bool select_all
) override
;
388 void SetAnimationOffset(int offset
) override
;
390 // TemplateURLServiceObserver:
391 void OnTemplateURLServiceChanged() override
;
393 // SearchModelObserver:
394 void ModelChanged(const SearchModel::State
& old_state
,
395 const SearchModel::State
& new_state
) override
;
397 // The Browser this LocationBarView is in. Note that at least
398 // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser
399 // window, so this may be NULL.
402 OmniboxViewViews
* omnibox_view_
;
407 // Object used to paint the border.
408 scoped_ptr
<views::Painter
> border_painter_
;
410 // An icon to the left of the edit field.
411 LocationIconView
* location_icon_view_
;
413 // A bubble displayed for EV HTTPS sites.
414 EVBubbleView
* ev_bubble_view_
;
416 // A view to show inline autocompletion when an IME is active. In this case,
417 // we shouldn't change the text or selection inside the OmniboxView itself,
418 // since this will conflict with the IME's control over the text. So instead
419 // we show any autocompletion in a separate field after the OmniboxView.
420 views::Label
* ime_inline_autocomplete_view_
;
422 // The following views are used to provide hints and remind the user as to
423 // what is going in the edit. They are all added a children of the
424 // LocationBarView. At most one is visible at a time. Preference is
425 // given to the keyword_view_, then hint_view_.
426 // These autocollapse when the edit needs the room.
428 // Shown if the user has selected a keyword.
429 SelectedKeywordView
* selected_keyword_view_
;
431 // View responsible for showing suggested text. This is NULL when there is no
433 views::Label
* suggested_text_view_
;
435 // Shown if the selected url has a corresponding keyword.
436 KeywordHintView
* keyword_hint_view_
;
438 // The voice search icon.
439 views::ImageButton
* mic_search_view_
;
441 // The content setting views.
442 ContentSettingViews content_setting_views_
;
445 ZoomView
* zoom_view_
;
447 // The icon to open a PDF in Reader.
448 OpenPDFInReaderView
* open_pdf_in_reader_view_
;
450 // The manage passwords icon.
451 ManagePasswordsIconView
* manage_passwords_icon_view_
;
453 // The page action icon views.
454 PageActionViews page_action_views_
;
456 // The icon for Translate.
457 TranslateIconView
* translate_icon_view_
;
460 StarView
* star_view_
;
462 // Animation to control showing / hiding the location bar.
463 gfx::SlideAnimation size_animation_
;
465 // Whether we're in popup mode. This value also controls whether the location
467 const bool is_popup_mode_
;
469 // True if we should show a focus rect while the location entry field is
470 // focused. Used when the toolbar is in full keyboard accessibility mode.
471 bool show_focus_rect_
;
473 // This is in case we're destroyed before the model loads. We need to make
474 // Add/RemoveObserver calls.
475 TemplateURLService
* template_url_service_
;
477 // Tracks this preference to determine whether bookmark editing is allowed.
478 BooleanPrefMember edit_bookmarks_enabled_
;
480 // During dropdown animation, the host clips the widget and draws only the
481 // bottom part of it. The view needs to know the pixel offset at which we are
482 // drawing the widget so that we can draw the curved edges that attach to the
483 // toolbar in the right location.
484 int dropdown_animation_offset_
;
486 // This is a debug state variable that stores if the WebContents was null
487 // during the last RefreshPageAction.
488 bool web_contents_null_at_last_refresh_
;
490 DISALLOW_COPY_AND_ASSIGN(LocationBarView
);
493 #endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_