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