location_bar_view.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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. 4 5#ifndef CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_ 6#define CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_ 7 8#include <string> 9#include <vector> 10 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/font.h" 27#include "ui/gfx/rect.h" 28#include "ui/views/controls/button/button.h" 29#include "ui/views/drag_controller.h" 30 31class ActionBoxButtonView; 32class CommandUpdater; 33class ContentSettingBubbleModelDelegate; 34class ContentSettingImageView; 35class EVBubbleView; 36class ExtensionAction; 37class GURL; 38class GeneratedCreditCardView; 39class InstantController; 40class KeywordHintView; 41class LocationIconView; 42class OpenPDFInReaderView; 43class ManagePasswordsIconView; 44class OriginChipView; 45class PageActionWithBadgeView; 46class PageActionImageView; 47class Profile; 48class SelectedKeywordView; 49class StarView; 50class TemplateURLService; 51class TranslateIconView; 52class ZoomView; 53 54namespace content { 55struct SSLStatus; 56} 57 58namespace views { 59class BubbleDelegateView; 60class ImageButton; 61class ImageView; 62class Label; 63class LabelButton; 64class Widget; 65} 66 67///////////////////////////////////////////////////////////////////////////// 68// 69// LocationBarView class 70// 71// The LocationBarView class is a View subclass that paints the background 72// of the URL bar strip and contains its content. 73// 74///////////////////////////////////////////////////////////////////////////// 75class LocationBarView : public LocationBar, 76 public LocationBarTesting, 77 public views::View, 78 public views::ButtonListener, 79 public views::DragController, 80 public OmniboxEditController, 81 public DropdownBarHostDelegate, 82 public TemplateURLServiceObserver, 83 public content::NotificationObserver, 84 public SearchModelObserver { 85 public: 86 // The location bar view's class name. 87 static const char kViewClassName[]; 88 89 // DropdownBarHostDelegate: 90 virtual void SetFocusAndSelection(bool select_all) OVERRIDE; 91 virtual void SetAnimationOffset(int offset) OVERRIDE; 92 93 // Returns the offset used while animating. 94 int animation_offset() const { return animation_offset_; } 95 96 class Delegate { 97 public: 98 // Should return the current web contents. 99 virtual content::WebContents* GetWebContents() = 0; 100 101 // Returns the InstantController, or NULL if there isn't one. 102 virtual InstantController* GetInstant() = 0; 103 104 virtual ToolbarModel* GetToolbarModel() = 0; 105 virtual const ToolbarModel* GetToolbarModel() const = 0; 106 107 // Creates Widget for the given delegate. 108 virtual views::Widget* CreateViewsBubble( 109 views::BubbleDelegateView* bubble_delegate) = 0; 110 111 // Creates PageActionImageView. Caller gets an ownership. 112 virtual PageActionImageView* CreatePageActionImageView( 113 LocationBarView* owner, 114 ExtensionAction* action) = 0; 115 116 // Returns ContentSettingBubbleModelDelegate. 117 virtual ContentSettingBubbleModelDelegate* 118 GetContentSettingBubbleModelDelegate() = 0; 119 120 // Shows permissions and settings for the given web contents. 121 virtual void ShowWebsiteSettings(content::WebContents* web_contents, 122 const GURL& url, 123 const content::SSLStatus& ssl) = 0; 124 125 protected: 126 virtual ~Delegate() {} 127 }; 128 129 enum ColorKind { 130 BACKGROUND = 0, 131 TEXT, 132 SELECTED_TEXT, 133 DEEMPHASIZED_TEXT, 134 SECURITY_TEXT, 135 }; 136 137 LocationBarView(Browser* browser, 138 Profile* profile, 139 CommandUpdater* command_updater, 140 Delegate* delegate, 141 bool is_popup_mode); 142 143 virtual ~LocationBarView(); 144 145 // Uses GetBuiltInHorizontalPaddingForChildViews() to optionally add 146 // additional padding (via an empty border) to |view|. This should be called 147 // during creation on all child views which are potentially touchable so that 148 // when touch is enabled they will have sufficient padding. 149 static void InitTouchableLocationBarChildView(views::View* view); 150 151 // Initializes the LocationBarView. 152 void Init(); 153 154 // True if this instance has been initialized by calling Init, which can only 155 // be called when the receiving instance is attached to a view container. 156 bool IsInitialized() const; 157 158 // Returns the appropriate color for the desired kind, based on the user's 159 // system theme. 160 SkColor GetColor(ToolbarModel::SecurityLevel security_level, 161 ColorKind kind) const; 162 163 // Returns the delegate. 164 Delegate* delegate() const { return delegate_; } 165 166 // See comment in browser_window.h for more info. 167 void ZoomChangedForActiveTab(bool can_show_bubble); 168 169 // The zoom icon. It may not be visible. 170 ZoomView* zoom_view() { return zoom_view_; } 171 172 // The passwords icon. It may not be visible. 173 ManagePasswordsIconView* manage_passwords_icon_view() { 174 return manage_passwords_icon_view_; 175 } 176 177 // Sets |preview_enabled| for the PageAction View associated with this 178 // |page_action|. If |preview_enabled| is true, the view will display the 179 // PageActions icon even though it has not been activated by the extension. 180 // This is used by the ExtensionInstalledBubble to preview what the icon 181 // will look like for the user upon installation of the extension. 182 void SetPreviewEnabledPageAction(ExtensionAction* page_action, 183 bool preview_enabled); 184 185 // Retrieves the PageAction View which is associated with |page_action|. 186 views::View* GetPageActionView(ExtensionAction* page_action); 187 188 // Toggles the star on or off. 189 void SetStarToggled(bool on); 190 191 // The star. It may not be visible. 192 StarView* star_view() { return star_view_; } 193 194 // Toggles the translate icon on or off. 195 void SetTranslateIconToggled(bool on); 196 197 // The translate icon. It may not be visible. 198 TranslateIconView* translate_icon_view() { return translate_icon_view_; } 199 200 void set_origin_chip_view(OriginChipView* origin_chip_view) { 201 origin_chip_view_ = origin_chip_view; 202 } 203 204 // Shows the bookmark prompt. 205 void ShowBookmarkPrompt(); 206 207 // Returns the screen coordinates of the omnibox (where the URL text appears, 208 // not where the icons are shown). 209 gfx::Point GetOmniboxViewOrigin() const; 210 211 // Shows |text| as an inline autocompletion. This is useful for IMEs, where 212 // we can't show the autocompletion inside the actual OmniboxView. See 213 // comments on |ime_inline_autocomplete_view_|. 214 void SetImeInlineAutocompletion(const base::string16& text); 215 216 // Invoked from OmniboxViewWin to show gray text autocompletion. 217 void SetGrayTextAutocompletion(const base::string16& text); 218 219 // Returns the current gray text autocompletion. 220 base::string16 GetGrayTextAutocompletion() const; 221 222 // Sizing functions 223 virtual gfx::Size GetPreferredSize() OVERRIDE; 224 225 // Layout and Painting functions 226 virtual void Layout() OVERRIDE; 227 virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE; 228 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 229 230 // Set if we should show a focus rect while the location entry field is 231 // focused. Used when the toolbar is in full keyboard accessibility mode. 232 // Repaints if necessary. 233 virtual void SetShowFocusRect(bool show); 234 235 // Select all of the text. Needed when the user tabs through controls 236 // in the toolbar in full keyboard accessibility mode. 237 virtual void SelectAll(); 238 239 views::ImageView* GetLocationIconView(); 240 const views::ImageView* GetLocationIconView() const; 241 242 // Return a view suitable for anchoring location-bar-anchored bubbles to. 243 views::View* GetLocationBarAnchor(); 244 // Return the point suitable for anchoring location-bar-anchored bubbles at. 245 // The point will be returned in the coordinates of the LocationBarView. 246 gfx::Point GetLocationBarAnchorPoint() const; 247 248 OmniboxViewViews* omnibox_view() { return omnibox_view_; } 249 250 views::View* generated_credit_card_view(); 251 252 // OmniboxEditController: 253 virtual void Update(const content::WebContents* contents) OVERRIDE; 254 virtual void OnChanged() OVERRIDE; 255 virtual void OnSetFocus() OVERRIDE; 256 virtual InstantController* GetInstant() OVERRIDE; 257 virtual content::WebContents* GetWebContents() OVERRIDE; 258 virtual ToolbarModel* GetToolbarModel() OVERRIDE; 259 virtual const ToolbarModel* GetToolbarModel() const OVERRIDE; 260 261 // views::View: 262 virtual const char* GetClassName() const OVERRIDE; 263 virtual bool HasFocus() const OVERRIDE; 264 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 265 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; 266 267 // views::ButtonListener: 268 virtual void ButtonPressed(views::Button* sender, 269 const ui::Event& event) OVERRIDE; 270 271 // views::DragController: 272 virtual void WriteDragDataForView(View* sender, 273 const gfx::Point& press_pt, 274 OSExchangeData* data) OVERRIDE; 275 virtual int GetDragOperationsForView(View* sender, 276 const gfx::Point& p) OVERRIDE; 277 virtual bool CanStartDragForView(View* sender, 278 const gfx::Point& press_pt, 279 const gfx::Point& p) OVERRIDE; 280 281 // LocationBar: 282 virtual void ShowFirstRunBubble() OVERRIDE; 283 virtual GURL GetDestinationURL() const OVERRIDE; 284 virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE; 285 virtual content::PageTransition GetPageTransition() const OVERRIDE; 286 virtual void AcceptInput() OVERRIDE; 287 virtual void FocusLocation(bool select_all) OVERRIDE; 288 virtual void FocusSearch() OVERRIDE; 289 virtual void UpdateContentSettingsIcons() OVERRIDE; 290 virtual void UpdateManagePasswordsIconAndBubble() OVERRIDE; 291 virtual void UpdatePageActions() OVERRIDE; 292 virtual void InvalidatePageActions() OVERRIDE; 293 virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE; 294 virtual void UpdateGeneratedCreditCardView() OVERRIDE; 295 virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE; 296 virtual void Revert() OVERRIDE; 297 virtual const OmniboxView* GetOmniboxView() const OVERRIDE; 298 virtual OmniboxView* GetOmniboxView() OVERRIDE; 299 virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE; 300 301 // LocationBarTesting: 302 virtual int PageActionCount() OVERRIDE; 303 virtual int PageActionVisibleCount() OVERRIDE; 304 virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE; 305 virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE; 306 virtual void TestPageActionPressed(size_t index) OVERRIDE; 307 virtual bool GetBookmarkStarVisibility() OVERRIDE; 308 309 // TemplateURLServiceObserver: 310 virtual void OnTemplateURLServiceChanged() OVERRIDE; 311 312 // content::NotificationObserver: 313 virtual void Observe(int type, 314 const content::NotificationSource& source, 315 const content::NotificationDetails& details) OVERRIDE; 316 317 // SearchModelObserver: 318 virtual void ModelChanged(const SearchModel::State& old_state, 319 const SearchModel::State& new_state) OVERRIDE; 320 321 // Returns the height of the control without the top and bottom 322 // edges(i.e. the height of the edit control inside). If 323 // |use_preferred_size| is true this will be the preferred height, 324 // otherwise it will be the current height. 325 int GetInternalHeight(bool use_preferred_size); 326 327 // Returns the position and width that the popup should be, and also the left 328 // edge that the results should align themselves to (which will leave some 329 // border on the left of the popup). 330 void GetOmniboxPopupPositioningInfo(gfx::Point* top_left_screen_coord, 331 int* popup_width, 332 int* left_margin, 333 int* right_margin); 334 335 // Space between items in the location bar, as well as between items and the 336 // edges. 337 static int GetItemPadding(); 338 339 // Thickness of the edges of the omnibox background images, in normal mode. 340 static const int kNormalEdgeThickness; 341 // The same, but for popup mode. 342 static const int kPopupEdgeThickness; 343 // Amount of padding built into the standard omnibox icons. 344 static const int kIconInternalPadding; 345 // Space between the edge and a bubble. 346 static const int kBubblePadding; 347 348 protected: 349 virtual void OnFocus() OVERRIDE; 350 351 private: 352 typedef std::vector<ContentSettingImageView*> ContentSettingViews; 353 354 friend class PageActionImageView; 355 friend class PageActionWithBadgeView; 356 typedef std::vector<ExtensionAction*> PageActions; 357 typedef std::vector<PageActionWithBadgeView*> PageActionViews; 358 359 // Returns the number of pixels of built-in padding to the left and right for 360 // child views. This is nonzero when touch UI is enabled so as to space out 361 // child views for easier targeting. See InitTouchableLocationBarChildView(). 362 static int GetBuiltInHorizontalPaddingForChildViews(); 363 364 // Returns the thickness of any visible left and right edge, in pixels. 365 int GetHorizontalEdgeThickness() const; 366 367 // The same, but for the top and bottom edges. 368 int vertical_edge_thickness() const { 369 return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness; 370 } 371 372 // Updates the visibility state of the Content Blocked icons to reflect what 373 // is actually blocked on the current page. Returns true if the visibility 374 // of at least one of the views in |content_setting_views_| changed. 375 bool RefreshContentSettingViews(); 376 377 // Deletes all page action views that we have created. 378 void DeletePageActionViews(); 379 380 // Updates the views for the Page Actions, to reflect state changes for 381 // PageActions. Returns true if the visibility of a PageActionWithBadgeView 382 // changed, or PageActionWithBadgeView were created/destroyed. 383 bool RefreshPageActionViews(); 384 385 // Updates the view for the zoom icon based on the current tab's zoom. Returns 386 // true if the visibility of the view changed. 387 bool RefreshZoomView(); 388 389 // Updates the Translate icon based on the current tab's Translate status. 390 void RefreshTranslateIcon(); 391 392 // Sets the visibility of view to new_vis. 393 void ToggleVisibility(bool new_vis, views::View* view); 394 395 // Updates |manage_passwords_icon_view_|. Returns true if visibility changed. 396 bool RefreshManagePasswordsIconView(); 397 398 // Shows the manage passwords bubble if there is a savable password. 399 void ShowManagePasswordsBubbleIfNeeded(); 400 401 // Returns true if the suggest text is valid. 402 bool HasValidSuggestText() const; 403 404 // Helper to show the first run info bubble. 405 void ShowFirstRunBubbleInternal(); 406 407 // Handles a request to change the value of this text field from software 408 // using an accessibility API (typically automation software, screen readers 409 // don't normally use this). Sets the value and clears the selection. 410 void AccessibilitySetValue(const base::string16& new_value); 411 412 // The Browser this LocationBarView is in. Note that at least 413 // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser 414 // window, so this may be NULL. 415 Browser* browser_; 416 417 OmniboxViewViews* omnibox_view_; 418 419 // Our delegate. 420 Delegate* delegate_; 421 422 // Object used to paint the border. 423 scoped_ptr<views::Painter> border_painter_; 424 425 // An icon to the left of the edit field. 426 LocationIconView* location_icon_view_; 427 428 // A bubble displayed for EV HTTPS sites. 429 EVBubbleView* ev_bubble_view_; 430 431 // A view to show inline autocompletion when an IME is active. In this case, 432 // we shouldn't change the text or selection inside the OmniboxView itself, 433 // since this will conflict with the IME's control over the text. So instead 434 // we show any autocompletion in a separate field after the OmniboxView. 435 views::Label* ime_inline_autocomplete_view_; 436 437 // The following views are used to provide hints and remind the user as to 438 // what is going in the edit. They are all added a children of the 439 // LocationBarView. At most one is visible at a time. Preference is 440 // given to the keyword_view_, then hint_view_. 441 // These autocollapse when the edit needs the room. 442 443 // Shown if the user has selected a keyword. 444 SelectedKeywordView* selected_keyword_view_; 445 446 // View responsible for showing suggested text. This is NULL when there is no 447 // suggested text. 448 views::Label* suggested_text_view_; 449 450 // Shown if the selected url has a corresponding keyword. 451 KeywordHintView* keyword_hint_view_; 452 453 // The voice search icon. 454 views::ImageButton* mic_search_view_; 455 456 // The content setting views. 457 ContentSettingViews content_setting_views_; 458 459 // The zoom icon. 460 ZoomView* zoom_view_; 461 462 // A bubble that shows after successfully generating a new credit card number. 463 GeneratedCreditCardView* generated_credit_card_view_; 464 465 // The icon to open a PDF in Reader. 466 OpenPDFInReaderView* open_pdf_in_reader_view_; 467 468 // The manage passwords icon. 469 ManagePasswordsIconView* manage_passwords_icon_view_; 470 471 // The current page actions. 472 PageActions page_actions_; 473 474 // The page action icon views. 475 PageActionViews page_action_views_; 476 477 // The Origin Chip. 478 OriginChipView* origin_chip_view_; 479 480 // The icon for Translate. 481 TranslateIconView* translate_icon_view_; 482 483 // The star. 484 StarView* star_view_; 485 486 // The search/go button. 487 views::LabelButton* search_button_; 488 489 // Whether we're in popup mode. This value also controls whether the location 490 // bar is read-only. 491 const bool is_popup_mode_; 492 493 // True if we should show a focus rect while the location entry field is 494 // focused. Used when the toolbar is in full keyboard accessibility mode. 495 bool show_focus_rect_; 496 497 // This is in case we're destroyed before the model loads. We need to make 498 // Add/RemoveObserver calls. 499 TemplateURLService* template_url_service_; 500 501 // Tracks this preference to determine whether bookmark editing is allowed. 502 BooleanPrefMember edit_bookmarks_enabled_; 503 504 // While animating, the host clips the widget and draws only the bottom 505 // part of it. The view needs to know the pixel offset at which we are drawing 506 // the widget so that we can draw the curved edges that attach to the toolbar 507 // in the right location. 508 int animation_offset_; 509 510 // Used to register for notifications received by NotificationObserver. 511 content::NotificationRegistrar registrar_; 512 513 // Used to bind callback functions to this object. 514 base::WeakPtrFactory<LocationBarView> weak_ptr_factory_; 515 516 DISALLOW_COPY_AND_ASSIGN(LocationBarView); 517}; 518 519#endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_ 520