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 "chrome/browser/api/prefs/pref_member.h"
13 #include "chrome/browser/extensions/extension_context_menu_model.h"
14 #include "chrome/browser/search_engines/template_url_service_observer.h"
15 #include "chrome/browser/ui/omnibox/location_bar.h"
16 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
17 #include "chrome/browser/ui/search/search_model_observer.h"
18 #include "chrome/browser/ui/toolbar/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 "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
24 #include "ui/gfx/font.h"
25 #include "ui/gfx/rect.h"
26 #include "ui/views/controls/native/native_view_host.h"
27 #include "ui/views/drag_controller.h"
30 #include "ui/compositor/layer_animation_observer.h"
33 class ActionBoxButtonView
;
35 class ContentSettingBubbleModelDelegate
;
36 class ContentSettingImageView
;
38 class ExtensionAction
;
40 class InstantController
;
41 class KeywordHintView
;
42 class LocationIconView
;
43 class OpenPDFInReaderView
;
44 class PageActionWithBadgeView
;
45 class PageActionImageView
;
47 class SelectedKeywordView
;
50 class TemplateURLService
;
51 class WebIntentsButtonView
;
61 class BubbleDelegateView
;
66 /////////////////////////////////////////////////////////////////////////////
68 // LocationBarView class
70 // The LocationBarView class is a View subclass that paints the background
71 // of the URL bar strip and contains its content.
73 /////////////////////////////////////////////////////////////////////////////
74 class LocationBarView
: public LocationBar
,
75 public LocationBarTesting
,
77 public views::DragController
,
78 public OmniboxEditController
,
79 public DropdownBarHostDelegate
,
80 public chrome::search::SearchModelObserver
,
81 public TemplateURLServiceObserver
,
82 public content::NotificationObserver
{
84 // The location bar view's class name.
85 static const char kViewClassName
[];
87 // DropdownBarHostDelegate
88 virtual void SetFocusAndSelection(bool select_all
) OVERRIDE
;
89 virtual void SetAnimationOffset(int offset
) OVERRIDE
;
91 // chrome::search::SearchModelObserver:
92 virtual void ModeChanged(const chrome::search::Mode
& old_mode
,
93 const chrome::search::Mode
& new_mode
) OVERRIDE
;
95 // Returns the offset used while animating.
96 int animation_offset() const { return animation_offset_
; }
100 // Should return the current tab contents.
101 virtual TabContents
* GetTabContents() const = 0;
103 // Returns the InstantController, or NULL if there isn't one.
104 virtual InstantController
* GetInstant() = 0;
106 // Creates Widget for the given delegate.
107 virtual views::Widget
* CreateViewsBubble(
108 views::BubbleDelegateView
* bubble_delegate
) = 0;
110 // Creates PageActionImageView. Caller gets an ownership.
111 virtual PageActionImageView
* CreatePageActionImageView(
112 LocationBarView
* owner
,
113 ExtensionAction
* action
) = 0;
115 // Returns ContentSettingBubbleModelDelegate.
116 virtual ContentSettingBubbleModelDelegate
*
117 GetContentSettingBubbleModelDelegate() = 0;
119 // Shows page information in the given web contents.
120 virtual void ShowPageInfo(content::WebContents
* web_contents
,
122 const content::SSLStatus
& ssl
,
123 bool show_history
) = 0;
125 // Called by the location bar view when the user starts typing in the edit.
126 // This forces our security style to be UNKNOWN for the duration of the
128 virtual void OnInputInProgress(bool in_progress
) = 0;
131 virtual ~Delegate() {}
142 // The modes reflect the different scenarios where a location bar can be used.
143 // The normal mode is the mode used in a regular browser window.
144 // In popup mode, the location bar view is read only and has a slightly
145 // different presentation (font size / color).
146 // In app launcher mode, the location bar is empty and no security states or
147 // page/browser actions are displayed.
154 LocationBarView(Browser
* browser
,
156 CommandUpdater
* command_updater
,
159 chrome::search::SearchModel
* search_model
,
162 virtual ~LocationBarView();
164 // Initializes the LocationBarView.
167 // True if this instance has been initialized by calling Init, which can only
168 // be called when the receiving instance is attached to a view container.
169 bool IsInitialized() const;
171 // Returns the appropriate color for the desired kind, based on the user's
173 static SkColor
GetColor(bool instant_extended_api_enabled
,
174 ToolbarModel::SecurityLevel security_level
,
177 // Updates the location bar. We also reset the bar's permanent text and
178 // security style, and, if |tab_for_state_restoring| is non-NULL, also restore
179 // saved state that the tab holds.
180 void Update(const content::WebContents
* tab_for_state_restoring
);
182 // Returns corresponding profile.
183 Profile
* profile() const { return profile_
; }
185 // Returns the delegate.
186 Delegate
* delegate() const { return delegate_
; }
188 // See comment in browser_window.h for more info.
189 void ZoomChangedForActiveTab(bool can_show_bubble
);
191 // Sets |preview_enabled| for the PageAction View associated with this
192 // |page_action|. If |preview_enabled| is true, the view will display the
193 // PageActions icon even though it has not been activated by the extension.
194 // This is used by the ExtensionInstalledBubble to preview what the icon
195 // will look like for the user upon installation of the extension.
196 void SetPreviewEnabledPageAction(ExtensionAction
* page_action
,
197 bool preview_enabled
);
199 // Retrieves the PageAction View which is associated with |page_action|.
200 views::View
* GetPageActionView(ExtensionAction
* page_action
);
202 // Toggles the star on or off.
203 void SetStarToggled(bool on
);
205 // Returns the star view. It may not be visible.
206 StarView
* star_view() { return star_view_
; }
208 // Shows the Chrome To Mobile bubble.
209 void ShowChromeToMobileBubble();
211 // Returns the screen coordinates of the location entry (where the URL text
212 // appears, not where the icons are shown).
213 gfx::Point
GetLocationEntryOrigin() const;
215 // Invoked from OmniboxViewWin to show the instant suggestion.
216 void SetInstantSuggestion(const string16
& text
);
218 // Returns the current instant suggestion text.
219 string16
GetInstantSuggestion() const;
221 // Sets whether the location entry can accept focus.
222 void SetLocationEntryFocusable(bool focusable
);
224 // Returns true if the location entry is focusable and visible in
226 bool IsLocationEntryFocusableInRootView() const;
229 virtual gfx::Size
GetPreferredSize() OVERRIDE
;
231 // Layout and Painting functions
232 virtual void Layout() OVERRIDE
;
233 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
235 // No focus border for the location bar, the caret is enough.
236 virtual void OnPaintFocusBorder(gfx::Canvas
* canvas
) OVERRIDE
{ }
238 // Set if we should show a focus rect while the location entry field is
239 // focused. Used when the toolbar is in full keyboard accessibility mode.
240 // Repaints if necessary.
241 virtual void SetShowFocusRect(bool show
);
243 // Select all of the text. Needed when the user tabs through controls
244 // in the toolbar in full keyboard accessibility mode.
245 virtual void SelectAll();
247 const gfx::Font
& font() const { return font_
; }
249 #if defined(OS_WIN) && !defined(USE_AURA)
251 virtual bool OnMousePressed(const ui::MouseEvent
& event
) OVERRIDE
;
252 virtual bool OnMouseDragged(const ui::MouseEvent
& event
) OVERRIDE
;
253 virtual void OnMouseReleased(const ui::MouseEvent
& event
) OVERRIDE
;
254 virtual void OnMouseCaptureLost() OVERRIDE
;
257 LocationIconView
* location_icon_view() { return location_icon_view_
; }
258 const LocationIconView
* location_icon_view() const {
259 return location_icon_view_
;
262 views::View
* location_entry_view() const { return location_entry_view_
; }
264 chrome::search::SearchModel
* search_model() const {
265 return search_model_
;
268 // Overridden from OmniboxEditController:
269 virtual void OnAutocompleteAccept(const GURL
& url
,
270 WindowOpenDisposition disposition
,
271 content::PageTransition transition
,
272 const GURL
& alternate_nav_url
) OVERRIDE
;
273 virtual void OnChanged() OVERRIDE
;
274 virtual void OnSelectionBoundsChanged() OVERRIDE
;
275 virtual void OnInputInProgress(bool in_progress
) OVERRIDE
;
276 virtual void OnKillFocus() OVERRIDE
;
277 virtual void OnSetFocus() OVERRIDE
;
278 virtual gfx::Image
GetFavicon() const OVERRIDE
;
279 virtual string16
GetTitle() const OVERRIDE
;
280 virtual InstantController
* GetInstant() OVERRIDE
;
281 virtual TabContents
* GetTabContents() const OVERRIDE
;
283 // Overridden from views::View:
284 virtual std::string
GetClassName() const OVERRIDE
;
285 virtual bool SkipDefaultKeyEventProcessing(
286 const ui::KeyEvent
& event
) OVERRIDE
;
287 virtual void GetAccessibleState(ui::AccessibleViewState
* state
) OVERRIDE
;
288 virtual bool HasFocus() const OVERRIDE
;
290 // Overridden from views::DragController:
291 virtual void WriteDragDataForView(View
* sender
,
292 const gfx::Point
& press_pt
,
293 OSExchangeData
* data
) OVERRIDE
;
294 virtual int GetDragOperationsForView(View
* sender
,
295 const gfx::Point
& p
) OVERRIDE
;
296 virtual bool CanStartDragForView(View
* sender
,
297 const gfx::Point
& press_pt
,
298 const gfx::Point
& p
) OVERRIDE
;
300 // Overridden from LocationBar:
301 virtual void ShowFirstRunBubble() OVERRIDE
;
302 virtual void SetInstantSuggestion(
303 const InstantSuggestion
& suggestion
) OVERRIDE
;
304 virtual string16
GetInputString() const OVERRIDE
;
305 virtual WindowOpenDisposition
GetWindowOpenDisposition() const OVERRIDE
;
306 virtual content::PageTransition
GetPageTransition() const OVERRIDE
;
307 virtual void AcceptInput() OVERRIDE
;
308 virtual void FocusLocation(bool select_all
) OVERRIDE
;
309 virtual void FocusSearch() OVERRIDE
;
310 virtual void UpdateContentSettingsIcons() OVERRIDE
;
311 virtual void UpdatePageActions() OVERRIDE
;
312 virtual void InvalidatePageActions() OVERRIDE
;
313 virtual void UpdateWebIntentsButton() OVERRIDE
;
314 virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE
;
315 virtual void SaveStateToContents(content::WebContents
* contents
) OVERRIDE
;
316 virtual void Revert() OVERRIDE
;
317 virtual const OmniboxView
* GetLocationEntry() const OVERRIDE
;
318 virtual OmniboxView
* GetLocationEntry() OVERRIDE
;
319 virtual LocationBarTesting
* GetLocationBarForTesting() OVERRIDE
;
321 // Overridden from LocationBarTesting:
322 virtual int PageActionCount() OVERRIDE
;
323 virtual int PageActionVisibleCount() OVERRIDE
;
324 virtual ExtensionAction
* GetPageAction(size_t index
) OVERRIDE
;
325 virtual ExtensionAction
* GetVisiblePageAction(size_t index
) OVERRIDE
;
326 virtual void TestPageActionPressed(size_t index
) OVERRIDE
;
327 virtual void TestActionBoxMenuItemSelected(int command_id
) OVERRIDE
;
328 virtual bool GetBookmarkStarVisibility() OVERRIDE
;
330 // Overridden from TemplateURLServiceObserver
331 virtual void OnTemplateURLServiceChanged() OVERRIDE
;
333 // Overridden from content::NotificationObserver
334 virtual void Observe(int type
,
335 const content::NotificationSource
& source
,
336 const content::NotificationDetails
& details
) OVERRIDE
;
338 // Returns the height of the control without the top and bottom
339 // edges(i.e. the height of the edit control inside). If
340 // |use_preferred_size| is true this will be the preferred height,
341 // otherwise it will be the current height.
342 int GetInternalHeight(bool use_preferred_size
);
344 // Space between items in the location bar.
345 static int GetItemPadding();
347 // Space between the edges and the items next to them.
348 static int GetEdgeItemPadding();
350 // Thickness of the left and right edges of the omnibox, in normal mode.
351 static const int kNormalHorizontalEdgeThickness
;
352 // Thickness of the top and bottom edges of the omnibox.
353 static const int kVerticalEdgeThickness
;
354 // Amount of padding built into the standard omnibox icons.
355 static const int kIconInternalPadding
;
356 // Space between the edge and a bubble.
357 static const int kBubbleHorizontalPadding
;
360 virtual void OnFocus() OVERRIDE
;
363 typedef std::vector
<ContentSettingImageView
*> ContentSettingViews
;
365 friend class PageActionImageView
;
366 friend class PageActionWithBadgeView
;
367 typedef std::vector
<PageActionWithBadgeView
*> PageActionViews
;
369 #if defined(USE_AURA)
370 // Observer that informs the LocationBarView when the animation is done.
371 class FadeAnimationObserver
: public ui::ImplicitAnimationObserver
{
373 explicit FadeAnimationObserver(LocationBarView
* location_bar_view
);
374 virtual ~FadeAnimationObserver();
376 // ui::ImplicitAnimationObserver overrides:
377 virtual void OnImplicitAnimationsCompleted() OVERRIDE
;
380 // The location bar view being animated. Not owned.
381 LocationBarView
* location_bar_view_
;
383 DISALLOW_COPY_AND_ASSIGN(FadeAnimationObserver
);
387 // Returns the amount of horizontal space (in pixels) out of
388 // |location_bar_width| that is not taken up by the actual text in
390 int AvailableWidth(int location_bar_width
);
392 // If |view| fits in |available_width|, it is made visible and positioned at
393 // the leading or trailing end of |bounds|, which are then shrunk
394 // appropriately. Otherwise |view| is made invisible.
395 // Note: |view| is expected to have already been positioned and sized
397 void LayoutView(views::View
* view
,
403 // Update the visibility state of the Content Blocked icons to reflect what is
404 // actually blocked on the current page.
405 void RefreshContentSettingViews();
407 // Delete all page action views that we have created.
408 void DeletePageActionViews();
410 // Update the views for the Page Actions, to reflect state changes for
412 void RefreshPageActionViews();
414 // Update the view for the zoom icon based on the current tab's zoom.
415 void RefreshZoomView();
417 // Sets the visibility of view to new_vis.
418 void ToggleVisibility(bool new_vis
, views::View
* view
);
420 #if !defined(USE_AURA)
421 // Helper for the Mouse event handlers that does all the real work.
422 void OnMouseEvent(const ui::MouseEvent
& event
, UINT msg
);
425 // Returns true if the suggest text is valid.
426 bool HasValidSuggestText() const;
428 // Helper to show the first run info bubble.
429 void ShowFirstRunBubbleInternal();
431 // Draw backgrounds and borders for page actions. Must be called
432 // after layout, so the |page_action_views_| have their bounds.
433 void PaintPageActionBackgrounds(gfx::Canvas
* canvas
);
435 // Draw the focus border when the search mode is |NTP|.
436 void PaintSearchNTPFocusBorder(gfx::Canvas
* canvas
);
438 #if defined(USE_AURA)
439 // Fade in the location bar view so the icons come in gradually.
440 void StartFadeAnimation();
442 // Stops the fade animation, if it is playing. Otherwise does nothing.
443 void StopFadeAnimation();
445 // Cleans up layers used for the animation.
446 void CleanupFadeAnimation();
449 // The Browser this LocationBarView is in. Note that at least
450 // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser
451 // window, so this may be NULL.
454 // The Autocomplete Edit field.
455 scoped_ptr
<OmniboxView
> location_entry_
;
457 // The profile which corresponds to this View.
460 // Command updater which corresponds to this View.
461 CommandUpdater
* command_updater_
;
464 ToolbarModel
* model_
;
469 // Weak, owned by browser.
470 // This is null if there is no browser instance.
471 chrome::search::SearchModel
* search_model_
;
473 // This is the string of text from the autocompletion session that the user
474 // entered or selected.
475 string16 location_input_
;
477 // The user's desired disposition for how their input should be opened
478 WindowOpenDisposition disposition_
;
480 // The transition type to use for the navigation
481 content::PageTransition transition_
;
483 // Font used by edit and some of the hints.
486 // An object used to paint the normal-mode background.
487 scoped_ptr
<views::Painter
> background_painter_
;
489 // An object used to paint the focus border when search mode is |NTP|.
490 scoped_ptr
<views::Painter
> search_focus_painter_
;
492 // An icon to the left of the edit field.
493 LocationIconView
* location_icon_view_
;
495 // A bubble displayed for EV HTTPS sites.
496 EVBubbleView
* ev_bubble_view_
;
498 // Location_entry view
499 views::View
* location_entry_view_
;
501 // The following views are used to provide hints and remind the user as to
502 // what is going in the edit. They are all added a children of the
503 // LocationBarView. At most one is visible at a time. Preference is
504 // given to the keyword_view_, then hint_view_.
505 // These autocollapse when the edit needs the room.
507 // Shown if the user has selected a keyword.
508 SelectedKeywordView
* selected_keyword_view_
;
510 // View responsible for showing suggested text. This is NULL when there is no
512 views::Label
* suggested_text_view_
;
514 // Shown if the selected url has a corresponding keyword.
515 KeywordHintView
* keyword_hint_view_
;
517 // The content setting views.
518 ContentSettingViews content_setting_views_
;
521 ZoomView
* zoom_view_
;
523 // The icon to open a PDF in Reader.
524 OpenPDFInReaderView
* open_pdf_in_reader_view_
;
526 // The current page actions.
527 std::vector
<ExtensionAction
*> page_actions_
;
529 // The page action icon views.
530 PageActionViews page_action_views_
;
533 StarView
* star_view_
;
535 // The web intents choose-another-service button
536 WebIntentsButtonView
* web_intents_button_view_
;
538 // The action box button (plus).
539 ActionBoxButtonView
* action_box_button_view_
;
541 // The mode that dictates how the bar shows.
544 // True if we should show a focus rect while the location entry field is
545 // focused. Used when the toolbar is in full keyboard accessibility mode.
546 bool show_focus_rect_
;
548 // True if Instant Extended API is enabled.
549 const bool instant_extended_api_enabled_
;
551 // This is in case we're destroyed before the model loads. We need to make
552 // Add/RemoveObserver calls.
553 TemplateURLService
* template_url_service_
;
555 // Tracks this preference to determine whether bookmark editing is allowed.
556 BooleanPrefMember edit_bookmarks_enabled_
;
558 // While animating, the host clips the widget and draws only the bottom
559 // part of it. The view needs to know the pixel offset at which we are drawing
560 // the widget so that we can draw the curved edges that attach to the toolbar
561 // in the right location.
562 int animation_offset_
;
564 // Used to register for notifications received by NotificationObserver.
565 content::NotificationRegistrar registrar_
;
567 #if defined(USE_AURA)
568 // Observer for a fade-in animation.
569 scoped_ptr
<FadeAnimationObserver
> fade_animation_observer_
;
572 DISALLOW_IMPLICIT_CONSTRUCTORS(LocationBarView
);
575 #endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_