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/ssl/security_state_model.h"
15 #include "chrome/browser/ui/location_bar/location_bar.h"
16 #include "chrome/browser/ui/omnibox/chrome_omnibox_edit_controller.h"
17 #include "chrome/browser/ui/search/search_model_observer.h"
18 #include "chrome/browser/ui/toolbar/chrome_toolbar_model.h"
19 #include "chrome/browser/ui/views/dropdown_bar_host.h"
20 #include "chrome/browser/ui/views/dropdown_bar_host_delegate.h"
21 #include "chrome/browser/ui/views/extensions/extension_popup.h"
22 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
23 #include "components/search_engines/template_url_service_observer.h"
24 #include "components/ui/zoom/zoom_event_manager_observer.h"
25 #include "ui/gfx/animation/animation_delegate.h"
26 #include "ui/gfx/animation/slide_animation.h"
27 #include "ui/gfx/font.h"
28 #include "ui/gfx/geometry/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 InstantController
;
40 class KeywordHintView
;
41 class LocationIconView
;
42 class OpenPDFInReaderView
;
43 class ManagePasswordsIconView
;
44 class PageActionWithBadgeView
;
45 class PageActionImageView
;
47 class SelectedKeywordView
;
49 class TemplateURLService
;
50 class TranslateIconView
;
54 class BubbleDelegateView
;
61 /////////////////////////////////////////////////////////////////////////////
63 // LocationBarView class
65 // The LocationBarView class is a View subclass that paints the background
66 // of the URL bar strip and contains its content.
68 /////////////////////////////////////////////////////////////////////////////
69 class LocationBarView
: public LocationBar
,
70 public LocationBarTesting
,
72 public views::ButtonListener
,
73 public views::DragController
,
74 public gfx::AnimationDelegate
,
75 public ChromeOmniboxEditController
,
76 public DropdownBarHostDelegate
,
77 public TemplateURLServiceObserver
,
78 public SearchModelObserver
,
79 public ui_zoom::ZoomEventManagerObserver
{
81 // The location bar view's class name.
82 static const char kViewClassName
[];
84 // Returns the offset used during dropdown animation.
85 int dropdown_animation_offset() const { return dropdown_animation_offset_
; }
89 // Should return the current web contents.
90 virtual content::WebContents
* GetWebContents() = 0;
92 virtual ToolbarModel
* GetToolbarModel() = 0;
93 virtual const ToolbarModel
* GetToolbarModel() const = 0;
95 // Creates Widget for the given delegate.
96 virtual views::Widget
* CreateViewsBubble(
97 views::BubbleDelegateView
* bubble_delegate
) = 0;
99 // Creates PageActionImageView. Caller gets an ownership.
100 virtual PageActionImageView
* CreatePageActionImageView(
101 LocationBarView
* owner
,
102 ExtensionAction
* action
) = 0;
104 // Returns ContentSettingBubbleModelDelegate.
105 virtual ContentSettingBubbleModelDelegate
*
106 GetContentSettingBubbleModelDelegate() = 0;
108 // Shows permissions and settings for the given web contents.
109 virtual void ShowWebsiteSettings(
110 content::WebContents
* web_contents
,
112 const SecurityStateModel::SecurityInfo
& security_info
) = 0;
115 virtual ~Delegate() {}
126 LocationBarView(Browser
* browser
,
128 CommandUpdater
* command_updater
,
132 ~LocationBarView() override
;
134 // Initializes the LocationBarView.
137 // True if this instance has been initialized by calling Init, which can only
138 // be called when the receiving instance is attached to a view container.
139 bool IsInitialized() const;
141 // Returns the appropriate color for the desired kind, based on the user's
143 SkColor
GetColor(SecurityStateModel::SecurityLevel security_level
,
144 ColorKind kind
) const;
146 // Returns the delegate.
147 Delegate
* delegate() const { return delegate_
; }
149 // See comment in browser_window.h for more info.
150 void ZoomChangedForActiveTab(bool can_show_bubble
);
152 // The zoom icon. It may not be visible.
153 ZoomView
* zoom_view() { return zoom_view_
; }
155 // The passwords icon. It may not be visible.
156 ManagePasswordsIconView
* manage_passwords_icon_view() {
157 return manage_passwords_icon_view_
;
160 // Sets |preview_enabled| for the PageAction View associated with this
161 // |page_action|. If |preview_enabled| is true, the view will display the
162 // PageActions icon even though it has not been activated by the extension.
163 // This is used by the ExtensionInstalledBubble to preview what the icon
164 // will look like for the user upon installation of the extension.
165 void SetPreviewEnabledPageAction(ExtensionAction
* page_action
,
166 bool preview_enabled
);
168 // Retrieves the PageAction View which is associated with |page_action|.
169 PageActionWithBadgeView
* GetPageActionView(ExtensionAction
* page_action
);
171 // Toggles the star on or off.
172 void SetStarToggled(bool on
);
174 // The star. It may not be visible.
175 StarView
* star_view() { return star_view_
; }
177 // Toggles the translate icon on or off.
178 void SetTranslateIconToggled(bool on
);
180 // The translate icon. It may not be visible.
181 TranslateIconView
* translate_icon_view() { return translate_icon_view_
; }
183 // Returns the screen coordinates of the omnibox (where the URL text appears,
184 // not where the icons are shown).
185 gfx::Point
GetOmniboxViewOrigin() const;
187 // Shows |text| as an inline autocompletion. This is useful for IMEs, where
188 // we can't show the autocompletion inside the actual OmniboxView. See
189 // comments on |ime_inline_autocomplete_view_|.
190 void SetImeInlineAutocompletion(const base::string16
& text
);
192 // Invoked from OmniboxViewWin to show gray text autocompletion.
193 void SetGrayTextAutocompletion(const base::string16
& text
);
195 // Returns the current gray text autocompletion.
196 base::string16
GetGrayTextAutocompletion() const;
198 // Set if we should show a focus rect while the location entry field is
199 // focused. Used when the toolbar is in full keyboard accessibility mode.
200 // Repaints if necessary.
201 virtual void SetShowFocusRect(bool show
);
203 // Select all of the text. Needed when the user tabs through controls
204 // in the toolbar in full keyboard accessibility mode.
205 virtual void SelectAll();
207 LocationIconView
* location_icon_view() { return location_icon_view_
; }
209 // Return the point suitable for anchoring location-bar-anchored bubbles at.
210 // The point will be returned in the coordinates of the LocationBarView.
211 gfx::Point
GetLocationBarAnchorPoint() const;
213 OmniboxViewViews
* omnibox_view() { return omnibox_view_
; }
214 const OmniboxViewViews
* omnibox_view() const { return omnibox_view_
; }
216 // Returns the height of the control without the top and bottom
217 // edges(i.e. the height of the edit control inside). If
218 // |use_preferred_size| is true this will be the preferred height,
219 // otherwise it will be the current height.
220 int GetInternalHeight(bool use_preferred_size
);
222 // Returns the position and width that the popup should be, and also the left
223 // edge that the results should align themselves to (which will leave some
224 // border on the left of the popup). |top_edge_overlap| specifies the number
225 // of pixels the top edge of the popup should overlap the bottom edge of
227 void GetOmniboxPopupPositioningInfo(gfx::Point
* top_left_screen_coord
,
231 int top_edge_overlap
);
233 // Updates the controller, and, if |contents| is non-null, restores saved
234 // state that the tab holds.
235 void Update(const content::WebContents
* contents
);
237 // Clears the location bar's state for |contents|.
238 void ResetTabState(content::WebContents
* contents
);
241 void FocusLocation(bool select_all
) override
;
242 void Revert() override
;
243 OmniboxView
* GetOmniboxView() override
;
246 bool HasFocus() const override
;
247 void GetAccessibleState(ui::AXViewState
* state
) override
;
248 gfx::Size
GetPreferredSize() const override
;
249 void Layout() override
;
251 // ChromeOmniboxEditController:
252 void UpdateWithoutTabRestore() override
;
253 void ShowURL() override
;
254 ToolbarModel
* GetToolbarModel() override
;
255 content::WebContents
* GetWebContents() override
;
257 // ZoomEventManagerObserver:
258 // Updates the view for the zoom icon when default zoom levels change.
259 void OnDefaultZoomLevelChanged() override
;
262 typedef std::vector
<ContentSettingImageView
*> ContentSettingViews
;
264 friend class PageActionImageView
;
265 friend class PageActionWithBadgeView
;
266 typedef std::vector
<ExtensionAction
*> PageActions
;
267 typedef std::vector
<PageActionWithBadgeView
*> PageActionViews
;
269 // Helper for GetMinimumWidth(). Calculates the incremental minimum width
270 // |view| should add to the trailing width after the omnibox.
271 int IncrementalMinimumWidth(views::View
* view
) const;
273 // Returns the thickness of any visible left and right edge, in pixels.
274 int GetHorizontalEdgeThickness() const;
276 // The same, but for the top and bottom edges.
277 int GetVerticalEdgeThickness() const;
279 // The vertical padding to be applied to all contained views.
280 int VerticalPadding() const;
282 // Updates the visibility state of the Content Blocked icons to reflect what
283 // is actually blocked on the current page. Returns true if the visibility
284 // of at least one of the views in |content_setting_views_| changed.
285 bool RefreshContentSettingViews();
287 // Clears |page_action_views_| and removes the elements from the view
289 void DeletePageActionViews();
291 // Updates the views for the Page Actions, to reflect state changes for
292 // PageActions. Returns true if the visibility of a PageActionWithBadgeView
293 // changed, or PageActionWithBadgeView were created/destroyed.
294 bool RefreshPageActionViews();
296 // Whether the page actions represented by |page_action_views_| differ
297 // in ordering or value from |page_actions|.
298 bool PageActionsDiffer(const PageActions
& page_actions
) const;
300 // Updates the view for the zoom icon based on the current tab's zoom. Returns
301 // true if the visibility of the view changed.
302 bool RefreshZoomView();
304 // Updates the Translate icon based on the current tab's Translate status.
305 void RefreshTranslateIcon();
307 // Updates |manage_passwords_icon_view_|. Returns true if visibility changed.
308 bool RefreshManagePasswordsIconView();
310 // Helper to show the first run info bubble.
311 void ShowFirstRunBubbleInternal();
313 // Returns true if the suggest text is valid.
314 bool HasValidSuggestText() const;
316 bool ShouldShowKeywordBubble() const;
317 bool ShouldShowEVBubble() const;
319 // Used to "reverse" the URL showing/hiding animations, since we use separate
320 // animations whose curves are not true inverses of each other. Based on the
321 // current position of the omnibox, calculates what value the desired
322 // animation (|hide_url_animation_| if |hide| is true, |show_url_animation_|
323 // if it's false) should be set to in order to produce the same omnibox
324 // position. This way we can stop the old animation, set the new animation to
325 // this value, and start it running, and the text will appear to reverse
326 // directions from its current location.
327 double GetValueForAnimation(bool hide
) const;
330 void ShowFirstRunBubble() override
;
331 GURL
GetDestinationURL() const override
;
332 WindowOpenDisposition
GetWindowOpenDisposition() const override
;
333 ui::PageTransition
GetPageTransition() const override
;
334 void AcceptInput() override
;
335 void FocusSearch() override
;
336 void UpdateContentSettingsIcons() override
;
337 void UpdateManagePasswordsIconAndBubble() override
;
338 void UpdatePageActions() override
;
339 void UpdateBookmarkStarVisibility() override
;
340 void UpdateLocationBarVisibility(bool visible
, bool animation
) override
;
341 bool ShowPageActionPopup(const extensions::Extension
* extension
,
342 bool grant_active_tab
) override
;
343 void UpdateOpenPDFInReaderPrompt() override
;
344 void SaveStateToContents(content::WebContents
* contents
) override
;
345 const OmniboxView
* GetOmniboxView() const override
;
346 LocationBarTesting
* GetLocationBarForTesting() override
;
348 // LocationBarTesting:
349 int PageActionCount() override
;
350 int PageActionVisibleCount() override
;
351 ExtensionAction
* GetPageAction(size_t index
) override
;
352 ExtensionAction
* GetVisiblePageAction(size_t index
) override
;
353 void TestPageActionPressed(size_t index
) override
;
354 bool GetBookmarkStarVisibility() override
;
357 const char* GetClassName() const override
;
358 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
359 void OnFocus() override
;
360 void OnPaint(gfx::Canvas
* canvas
) override
;
361 void PaintChildren(const ui::PaintContext
& context
) override
;
363 // views::ButtonListener:
364 void ButtonPressed(views::Button
* sender
, const ui::Event
& event
) override
;
366 // views::DragController:
367 void WriteDragDataForView(View
* sender
,
368 const gfx::Point
& press_pt
,
369 OSExchangeData
* data
) override
;
370 int GetDragOperationsForView(View
* sender
, const gfx::Point
& p
) override
;
371 bool CanStartDragForView(View
* sender
,
372 const gfx::Point
& press_pt
,
373 const gfx::Point
& p
) override
;
375 // gfx::AnimationDelegate:
376 void AnimationProgressed(const gfx::Animation
* animation
) override
;
377 void AnimationEnded(const gfx::Animation
* animation
) override
;
379 // ChromeOmniboxEditController:
380 void OnChanged() override
;
381 void OnSetFocus() override
;
382 const ToolbarModel
* GetToolbarModel() const override
;
384 // DropdownBarHostDelegate:
385 void SetFocusAndSelection(bool select_all
) override
;
386 void SetAnimationOffset(int offset
) override
;
388 // TemplateURLServiceObserver:
389 void OnTemplateURLServiceChanged() override
;
391 // SearchModelObserver:
392 void ModelChanged(const SearchModel::State
& old_state
,
393 const SearchModel::State
& new_state
) override
;
395 // The Browser this LocationBarView is in. Note that at least
396 // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser
397 // window, so this may be NULL.
400 OmniboxViewViews
* omnibox_view_
;
405 // Object used to paint the border.
406 scoped_ptr
<views::Painter
> border_painter_
;
408 // An icon to the left of the edit field.
409 LocationIconView
* location_icon_view_
;
411 // A bubble displayed for EV HTTPS sites.
412 EVBubbleView
* ev_bubble_view_
;
414 // A view to show inline autocompletion when an IME is active. In this case,
415 // we shouldn't change the text or selection inside the OmniboxView itself,
416 // since this will conflict with the IME's control over the text. So instead
417 // we show any autocompletion in a separate field after the OmniboxView.
418 views::Label
* ime_inline_autocomplete_view_
;
420 // The following views are used to provide hints and remind the user as to
421 // what is going in the edit. They are all added a children of the
422 // LocationBarView. At most one is visible at a time. Preference is
423 // given to the keyword_view_, then hint_view_.
424 // These autocollapse when the edit needs the room.
426 // Shown if the user has selected a keyword.
427 SelectedKeywordView
* selected_keyword_view_
;
429 // View responsible for showing suggested text. This is NULL when there is no
431 views::Label
* suggested_text_view_
;
433 // Shown if the selected url has a corresponding keyword.
434 KeywordHintView
* keyword_hint_view_
;
436 // The voice search icon.
437 views::ImageButton
* mic_search_view_
;
439 // The content setting views.
440 ContentSettingViews content_setting_views_
;
443 ZoomView
* zoom_view_
;
445 // The icon to open a PDF in Reader.
446 OpenPDFInReaderView
* open_pdf_in_reader_view_
;
448 // The manage passwords icon.
449 ManagePasswordsIconView
* manage_passwords_icon_view_
;
451 // The page action icon views.
452 PageActionViews page_action_views_
;
454 // The icon for Translate.
455 TranslateIconView
* translate_icon_view_
;
458 StarView
* star_view_
;
460 // Animation to control showing / hiding the location bar.
461 gfx::SlideAnimation size_animation_
;
463 // Whether we're in popup mode. This value also controls whether the location
465 const bool is_popup_mode_
;
467 // True if we should show a focus rect while the location entry field is
468 // focused. Used when the toolbar is in full keyboard accessibility mode.
469 bool show_focus_rect_
;
471 // This is in case we're destroyed before the model loads. We need to make
472 // Add/RemoveObserver calls.
473 TemplateURLService
* template_url_service_
;
475 // Tracks this preference to determine whether bookmark editing is allowed.
476 BooleanPrefMember edit_bookmarks_enabled_
;
478 // During dropdown animation, the host clips the widget and draws only the
479 // bottom part of it. The view needs to know the pixel offset at which we are
480 // drawing the widget so that we can draw the curved edges that attach to the
481 // toolbar in the right location.
482 int dropdown_animation_offset_
;
484 // This is a debug state variable that stores if the WebContents was null
485 // during the last RefreshPageAction.
486 bool web_contents_null_at_last_refresh_
;
488 DISALLOW_COPY_AND_ASSIGN(LocationBarView
);
491 #endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_