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 "content/public/browser/notification_observer.h"
24 #include "content/public/browser/notification_registrar.h"
25 #include "ui/gfx/font.h"
26 #include "ui/gfx/rect.h"
27 #include "ui/views/controls/button/button.h"
28 #include "ui/views/controls/native/native_view_host.h"
29 #include "ui/views/drag_controller.h"
32 #include "ui/compositor/layer_animation_observer.h"
35 class ActionBoxButtonView;
37 class ContentSettingBubbleModelDelegate;
38 class ContentSettingImageView;
40 class ExtensionAction;
42 class GeneratedCreditCardView;
43 class InstantController;
44 class KeywordHintView;
45 class LocationIconView;
46 class OpenPDFInReaderView;
47 class PageActionWithBadgeView;
48 class PageActionImageView;
50 class ScriptBubbleIconView;
51 class SelectedKeywordView;
53 class TemplateURLService;
54 class TranslateIconView;
58 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 OmniboxEditController,
78 public DropdownBarHostDelegate,
79 public TemplateURLServiceObserver,
80 public content::NotificationObserver,
81 public SearchModelObserver {
83 // The location bar view's class name.
84 static const char kViewClassName[];
86 // DropdownBarHostDelegate:
87 virtual void SetFocusAndSelection(bool select_all) OVERRIDE;
88 virtual void SetAnimationOffset(int offset) OVERRIDE;
90 // Returns the offset used while animating.
91 int animation_offset() const { return animation_offset_; }
95 // Should return the current web contents.
96 virtual content::WebContents* GetWebContents() = 0;
98 // Returns the InstantController, or NULL if there isn't one.
99 virtual InstantController* GetInstant() = 0;
101 virtual ToolbarModel* GetToolbarModel() = 0;
102 virtual const ToolbarModel* GetToolbarModel() const = 0;
104 // Creates Widget for the given delegate.
105 virtual views::Widget* CreateViewsBubble(
106 views::BubbleDelegateView* bubble_delegate) = 0;
108 // Creates PageActionImageView. Caller gets an ownership.
109 virtual PageActionImageView* CreatePageActionImageView(
110 LocationBarView* owner,
111 ExtensionAction* action) = 0;
113 // Returns ContentSettingBubbleModelDelegate.
114 virtual ContentSettingBubbleModelDelegate*
115 GetContentSettingBubbleModelDelegate() = 0;
117 // Shows permissions and settings for the given web contents.
118 virtual void ShowWebsiteSettings(content::WebContents* web_contents,
120 const content::SSLStatus& ssl) = 0;
123 virtual ~Delegate() {}
134 LocationBarView(Browser* browser,
136 CommandUpdater* command_updater,
140 virtual ~LocationBarView();
142 // Uses GetBuiltInHorizontalPaddingForChildViews() to optionally add
143 // additional padding (via an empty border) to |view|. This should be called
144 // during creation on all child views which are potentially touchable so that
145 // when touch is enabled they will have sufficient padding.
146 static void InitTouchableLocationBarChildView(views::View* view);
148 // Initializes the LocationBarView.
151 // True if this instance has been initialized by calling Init, which can only
152 // be called when the receiving instance is attached to a view container.
153 bool IsInitialized() const;
155 // Returns the appropriate color for the desired kind, based on the user's
157 SkColor GetColor(ToolbarModel::SecurityLevel security_level,
158 ColorKind kind) const;
160 // Returns corresponding profile.
161 Profile* profile() const { return profile_; }
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 view. It may not be visible.
170 ZoomView* zoom_view() { return zoom_view_; }
172 // Sets |preview_enabled| for the PageAction View associated with this
173 // |page_action|. If |preview_enabled| is true, the view will display the
174 // PageActions icon even though it has not been activated by the extension.
175 // This is used by the ExtensionInstalledBubble to preview what the icon
176 // will look like for the user upon installation of the extension.
177 void SetPreviewEnabledPageAction(ExtensionAction* page_action,
178 bool preview_enabled);
180 // Retrieves the PageAction View which is associated with |page_action|.
181 views::View* GetPageActionView(ExtensionAction* page_action);
183 // Toggles the star on or off.
184 void SetStarToggled(bool on);
186 // Returns the star view. It may not be visible.
187 StarView* star_view() { return star_view_; }
189 TranslateIconView* translate_icon_view() {
190 return translate_icon_view_;
193 // Shows the bookmark prompt.
194 void ShowBookmarkPrompt();
196 // Returns the screen coordinates of the location entry (where the URL text
197 // appears, not where the icons are shown).
198 gfx::Point GetLocationEntryOrigin() 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 string16& text);
205 // Invoked from OmniboxViewWin to show gray text autocompletion.
206 void SetGrayTextAutocompletion(const string16& text);
208 // Returns the current gray text autocompletion.
209 string16 GetGrayTextAutocompletion() const;
211 // Sets whether the location entry can accept focus.
212 void SetLocationEntryFocusable(bool focusable);
214 // Returns true if the location entry is focusable and visible in
216 bool IsLocationEntryFocusableInRootView() const;
219 virtual gfx::Size GetPreferredSize() OVERRIDE;
221 // Layout and Painting functions
222 virtual void Layout() OVERRIDE;
223 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
225 // No focus border for the location bar, the caret is enough.
226 virtual void OnPaintFocusBorder(gfx::Canvas* canvas) OVERRIDE { }
228 // Set if we should show a focus rect while the location entry field is
229 // focused. Used when the toolbar is in full keyboard accessibility mode.
230 // Repaints if necessary.
231 virtual void SetShowFocusRect(bool show);
233 // Select all of the text. Needed when the user tabs through controls
234 // in the toolbar in full keyboard accessibility mode.
235 virtual void SelectAll();
237 #if defined(OS_WIN) && !defined(USE_AURA)
239 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
240 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
241 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
242 virtual void OnMouseCaptureLost() OVERRIDE;
245 LocationIconView* location_icon_view() { return location_icon_view_; }
246 const LocationIconView* location_icon_view() const {
247 return location_icon_view_;
250 views::View* location_entry_view() const { return location_entry_view_; }
252 views::View* generated_credit_card_view();
254 // OmniboxEditController:
255 virtual void Update(const content::WebContents* contents) OVERRIDE;
256 virtual void OnChanged() OVERRIDE;
257 virtual void OnSetFocus() OVERRIDE;
258 virtual InstantController* GetInstant() OVERRIDE;
259 virtual content::WebContents* GetWebContents() OVERRIDE;
260 virtual ToolbarModel* GetToolbarModel() OVERRIDE;
261 virtual const ToolbarModel* GetToolbarModel() const OVERRIDE;
264 virtual const char* GetClassName() const OVERRIDE;
265 virtual bool SkipDefaultKeyEventProcessing(
266 const ui::KeyEvent& event) OVERRIDE;
267 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
268 virtual bool HasFocus() const OVERRIDE;
269 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
271 // views::ButtonListener:
272 virtual void ButtonPressed(views::Button* sender,
273 const ui::Event& event) OVERRIDE;
275 // views::DragController:
276 virtual void WriteDragDataForView(View* sender,
277 const gfx::Point& press_pt,
278 OSExchangeData* data) OVERRIDE;
279 virtual int GetDragOperationsForView(View* sender,
280 const gfx::Point& p) OVERRIDE;
281 virtual bool CanStartDragForView(View* sender,
282 const gfx::Point& press_pt,
283 const gfx::Point& p) OVERRIDE;
286 virtual void ShowFirstRunBubble() OVERRIDE;
287 virtual GURL GetDestinationURL() const OVERRIDE;
288 virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE;
289 virtual content::PageTransition GetPageTransition() const OVERRIDE;
290 virtual void AcceptInput() OVERRIDE;
291 virtual void FocusLocation(bool select_all) OVERRIDE;
292 virtual void FocusSearch() OVERRIDE;
293 virtual void UpdateContentSettingsIcons() OVERRIDE;
294 virtual void UpdatePageActions() OVERRIDE;
295 virtual void InvalidatePageActions() OVERRIDE;
296 virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE;
297 virtual void UpdateGeneratedCreditCardView() OVERRIDE;
298 virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE;
299 virtual void Revert() OVERRIDE;
300 virtual const OmniboxView* GetLocationEntry() const OVERRIDE;
301 virtual OmniboxView* GetLocationEntry() OVERRIDE;
302 virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE;
304 // LocationBarTesting:
305 virtual int PageActionCount() OVERRIDE;
306 virtual int PageActionVisibleCount() OVERRIDE;
307 virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE;
308 virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE;
309 virtual void TestPageActionPressed(size_t index) OVERRIDE;
310 virtual bool GetBookmarkStarVisibility() OVERRIDE;
312 // TemplateURLServiceObserver:
313 virtual void OnTemplateURLServiceChanged() OVERRIDE;
315 // content::NotificationObserver:
316 virtual void Observe(int type,
317 const content::NotificationSource& source,
318 const content::NotificationDetails& details) OVERRIDE;
320 // SearchModelObserver:
321 virtual void ModelChanged(const SearchModel::State& old_state,
322 const SearchModel::State& new_state) OVERRIDE;
324 // Returns the height of the control without the top and bottom
325 // edges(i.e. the height of the edit control inside). If
326 // |use_preferred_size| is true this will be the preferred height,
327 // otherwise it will be the current height.
328 int GetInternalHeight(bool use_preferred_size);
330 // Returns the position and width that the popup should be, and also the left
331 // edge that the results should align themselves to (which will leave some
332 // border on the left of the popup).
333 void GetOmniboxPopupPositioningInfo(gfx::Point* top_left_screen_coord,
338 // Space between items in the location bar, as well as between items and the
340 static int GetItemPadding();
342 // Thickness of the edges of the omnibox background images, in normal mode.
343 static const int kNormalEdgeThickness;
344 // The same, but for popup mode.
345 static const int kPopupEdgeThickness;
346 // Amount of padding built into the standard omnibox icons.
347 static const int kIconInternalPadding;
348 // Space between the edge and a bubble.
349 static const int kBubblePadding;
352 virtual void OnFocus() OVERRIDE;
355 typedef std::vector<ContentSettingImageView*> ContentSettingViews;
357 friend class PageActionImageView;
358 friend class PageActionWithBadgeView;
359 typedef std::vector<PageActionWithBadgeView*> PageActionViews;
361 // Returns the number of pixels of built-in padding to the left and right for
362 // child views. This is nonzero when touch UI is enabled so as to space out
363 // child views for easier targeting. See InitTouchableLocationBarChildView().
364 static int GetBuiltInHorizontalPaddingForChildViews();
366 // Returns the thickness of any visible left and right edge, in pixels.
367 int GetHorizontalEdgeThickness() const;
369 // The same, but for the top and bottom edges.
370 int vertical_edge_thickness() const {
371 return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness;
374 // Updates the visibility state of the Content Blocked icons to reflect what
375 // is actually blocked on the current page. Calling this function should
376 // always eventually be followed by calling Layout() and then
377 // UpdateContentSettingViewsPostLayout(), to ensure the icons can completely
378 // update their states.
379 void UpdateContentSettingViewsPreLayout();
381 // Updates after the correct screen coordinates have been set for icons.
382 // Allows content setting icons to perform any updating which can't complete
383 // until after the icons have been correctly laid out. This should be called
384 // after UpdateContentSettingViewsPreLayout() and a subsequent Layout().
385 void UpdateContentSettingViewsPostLayout();
387 // Deletes all page action views that we have created.
388 void DeletePageActionViews();
390 // Updates the views for the Page Actions, to reflect state changes for
392 void RefreshPageActionViews();
394 // Returns the number of scripts currently running on the page.
395 size_t ScriptBubbleScriptsRunning();
397 // Updates the Script Bubble Icon, to reflect the number of content scripts
398 // running on the page.
399 void RefreshScriptBubble();
401 // Updates the view for the zoom icon based on the current tab's zoom.
402 void RefreshZoomView();
404 // Updates the Translate icon based on the current tab's Translate status.
405 void RefreshTranslateIcon();
407 // Sets the visibility of view to new_vis.
408 void ToggleVisibility(bool new_vis, views::View* view);
410 #if !defined(USE_AURA)
411 // Helper for the Mouse event handlers that does all the real work.
412 void OnMouseEvent(const ui::MouseEvent& event, UINT msg);
415 // Returns true if the suggest text is valid.
416 bool HasValidSuggestText() const;
418 // Helper to show the first run info bubble.
419 void ShowFirstRunBubbleInternal();
421 // Draws backgrounds and borders for page actions. Must be called
422 // after layout, so the |page_action_views_| have their bounds.
423 void PaintPageActionBackgrounds(gfx::Canvas* canvas);
425 // Handles a request to change the value of this text field from software
426 // using an accessibility API (typically automation software, screen readers
427 // don't normally use this). Sets the value and clears the selection.
428 void AccessibilitySetValue(const string16& new_value);
430 // The Browser this LocationBarView is in. Note that at least
431 // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser
432 // window, so this may be NULL.
435 // The Autocomplete Edit field.
436 scoped_ptr<OmniboxView> location_entry_;
438 // The profile which corresponds to this View.
444 // An object used to paint the normal-mode background.
445 scoped_ptr<views::Painter> background_border_painter_;
446 scoped_ptr<views::Painter> background_filling_painter_;
448 // An icon to the left of the edit field.
449 LocationIconView* location_icon_view_;
451 // A bubble displayed for EV HTTPS sites.
452 EVBubbleView* ev_bubble_view_;
454 // Location_entry view
455 views::View* location_entry_view_;
457 // A view to show inline autocompletion when an IME is active. In this case,
458 // we shouldn't change the text or selection inside the OmniboxView itself,
459 // since this will conflict with the IME's control over the text. So instead
460 // we show any autocompletion in a separate field after the OmniboxView.
461 views::Label* ime_inline_autocomplete_view_;
463 // The following views are used to provide hints and remind the user as to
464 // what is going in the edit. They are all added a children of the
465 // LocationBarView. At most one is visible at a time. Preference is
466 // given to the keyword_view_, then hint_view_.
467 // These autocollapse when the edit needs the room.
469 // Shown if the user has selected a keyword.
470 SelectedKeywordView* selected_keyword_view_;
472 // View responsible for showing suggested text. This is NULL when there is no
474 views::Label* suggested_text_view_;
476 // Shown if the selected url has a corresponding keyword.
477 KeywordHintView* keyword_hint_view_;
479 // The voice search icon.
480 views::ImageButton* mic_search_view_;
482 // The content setting views.
483 ContentSettingViews content_setting_views_;
486 ZoomView* zoom_view_;
488 // A bubble that shows after successfully generating a new credit card number.
489 GeneratedCreditCardView* generated_credit_card_view_;
491 // The icon to open a PDF in Reader.
492 OpenPDFInReaderView* open_pdf_in_reader_view_;
494 // The current page actions.
495 std::vector<ExtensionAction*> page_actions_;
497 // The page action icon views.
498 PageActionViews page_action_views_;
500 // The script bubble.
501 ScriptBubbleIconView* script_bubble_icon_view_;
504 StarView* star_view_;
506 // The icon for Translate.
507 TranslateIconView* translate_icon_view_;
509 // Whether we're in popup mode. This value also controls whether the location
511 const bool is_popup_mode_;
513 // True if we should show a focus rect while the location entry field is
514 // focused. Used when the toolbar is in full keyboard accessibility mode.
515 bool show_focus_rect_;
517 // This is in case we're destroyed before the model loads. We need to make
518 // Add/RemoveObserver calls.
519 TemplateURLService* template_url_service_;
521 // Tracks this preference to determine whether bookmark editing is allowed.
522 BooleanPrefMember edit_bookmarks_enabled_;
524 // While animating, the host clips the widget and draws only the bottom
525 // part of it. The view needs to know the pixel offset at which we are drawing
526 // the widget so that we can draw the curved edges that attach to the toolbar
527 // in the right location.
528 int animation_offset_;
530 // Used to register for notifications received by NotificationObserver.
531 content::NotificationRegistrar registrar_;
533 // Used to bind callback functions to this object.
534 base::WeakPtrFactory<LocationBarView> weak_ptr_factory_;
536 DISALLOW_COPY_AND_ASSIGN(LocationBarView);
539 #endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_