location_bar_view.h revision 9ab5563a3196760eb381d102cbb2bc0f7abc6a50
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/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/button/button.h" 27#include "ui/views/controls/native/native_view_host.h" 28#include "ui/views/drag_controller.h" 29 30#if defined(USE_AURA) 31#include "ui/compositor/layer_animation_observer.h" 32#endif 33 34class ActionBoxButtonView; 35class AutofillCreditCardView; 36class CommandUpdater; 37class ContentSettingBubbleModelDelegate; 38class ContentSettingImageView; 39class EVBubbleView; 40class ExtensionAction; 41class GURL; 42class InstantController; 43class KeywordHintView; 44class LocationIconView; 45class MicSearchView; 46class OpenPDFInReaderView; 47class PageActionWithBadgeView; 48class PageActionImageView; 49class Profile; 50class ScriptBubbleIconView; 51class SelectedKeywordView; 52class StarView; 53class TemplateURLService; 54class ZoomView; 55 56namespace views { 57class BubbleDelegateView; 58class Label; 59class Widget; 60} 61 62///////////////////////////////////////////////////////////////////////////// 63// 64// LocationBarView class 65// 66// The LocationBarView class is a View subclass that paints the background 67// of the URL bar strip and contains its content. 68// 69///////////////////////////////////////////////////////////////////////////// 70class LocationBarView : public LocationBar, 71 public LocationBarTesting, 72 public views::View, 73 public views::ButtonListener, 74 public views::DragController, 75 public OmniboxEditController, 76 public DropdownBarHostDelegate, 77 public TemplateURLServiceObserver, 78 public content::NotificationObserver, 79 public SearchModelObserver { 80 public: 81 // The location bar view's class name. 82 static const char kViewClassName[]; 83 84 // DropdownBarHostDelegate: 85 virtual void SetFocusAndSelection(bool select_all) OVERRIDE; 86 virtual void SetAnimationOffset(int offset) OVERRIDE; 87 88 // Returns the offset used while animating. 89 int animation_offset() const { return animation_offset_; } 90 91 class Delegate { 92 public: 93 // Should return the current web contents. 94 virtual content::WebContents* GetWebContents() const = 0; 95 96 // Returns the InstantController, or NULL if there isn't one. 97 virtual InstantController* GetInstant() = 0; 98 99 // Creates Widget for the given delegate. 100 virtual views::Widget* CreateViewsBubble( 101 views::BubbleDelegateView* bubble_delegate) = 0; 102 103 // Creates PageActionImageView. Caller gets an ownership. 104 virtual PageActionImageView* CreatePageActionImageView( 105 LocationBarView* owner, 106 ExtensionAction* action) = 0; 107 108 // Returns ContentSettingBubbleModelDelegate. 109 virtual ContentSettingBubbleModelDelegate* 110 GetContentSettingBubbleModelDelegate() = 0; 111 112 // Shows permissions and settings for the given web contents. 113 virtual void ShowWebsiteSettings(content::WebContents* web_contents, 114 const GURL& url, 115 const content::SSLStatus& ssl) = 0; 116 117 // Called by the location bar view when the user starts typing in the edit. 118 // This forces our security style to be UNKNOWN for the duration of the 119 // editing. 120 virtual void OnInputInProgress(bool in_progress) = 0; 121 122 protected: 123 virtual ~Delegate() {} 124 }; 125 126 enum ColorKind { 127 BACKGROUND = 0, 128 TEXT, 129 SELECTED_TEXT, 130 DEEMPHASIZED_TEXT, 131 SECURITY_TEXT, 132 }; 133 134 LocationBarView(Browser* browser, 135 Profile* profile, 136 CommandUpdater* command_updater, 137 ToolbarModel* model, 138 Delegate* delegate, 139 bool is_popup_mode); 140 141 virtual ~LocationBarView(); 142 143 // Initializes the LocationBarView. 144 void Init(); 145 146 // True if this instance has been initialized by calling Init, which can only 147 // be called when the receiving instance is attached to a view container. 148 bool IsInitialized() const; 149 150 // Returns the appropriate color for the desired kind, based on the user's 151 // system theme. 152 SkColor GetColor(ToolbarModel::SecurityLevel security_level, 153 ColorKind kind) const; 154 155 // Updates the location bar. We also reset the bar's permanent text and 156 // security style, and, if |tab_for_state_restoring| is non-NULL, also restore 157 // saved state that the tab holds. 158 void Update(const content::WebContents* tab_for_state_restoring); 159 160 // Returns corresponding profile. 161 Profile* profile() const { return profile_; } 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 view. It may not be visible. 170 ZoomView* zoom_view() { return zoom_view_; } 171 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); 179 180 // Retrieves the PageAction View which is associated with |page_action|. 181 views::View* GetPageActionView(ExtensionAction* page_action); 182 183 // Toggles the star on or off. 184 void SetStarToggled(bool on); 185 186 // Returns the star view. It may not be visible. 187 StarView* star_view() { return star_view_; } 188 189 // Shows the bookmark prompt. 190 void ShowBookmarkPrompt(); 191 192 // Returns the screen coordinates of the location entry (where the URL text 193 // appears, not where the icons are shown). 194 gfx::Point GetLocationEntryOrigin() const; 195 196 // Shows |text| as an inline autocompletion. This is useful for IMEs, where 197 // we can't show the autocompletion inside the actual OmniboxView. See 198 // comments on |ime_inline_autocomplete_view_|. 199 void SetImeInlineAutocompletion(const string16& text); 200 201 // Invoked from OmniboxViewWin to show gray text autocompletion. 202 void SetGrayTextAutocompletion(const string16& text); 203 204 // Returns the current gray text autocompletion. 205 string16 GetGrayTextAutocompletion() const; 206 207 // Sets whether the location entry can accept focus. 208 void SetLocationEntryFocusable(bool focusable); 209 210 // Returns true if the location entry is focusable and visible in 211 // the root view. 212 bool IsLocationEntryFocusableInRootView() const; 213 214 // Sizing functions 215 virtual gfx::Size GetPreferredSize() OVERRIDE; 216 217 // Layout and Painting functions 218 virtual void Layout() OVERRIDE; 219 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 220 221 // No focus border for the location bar, the caret is enough. 222 virtual void OnPaintFocusBorder(gfx::Canvas* canvas) OVERRIDE { } 223 224 // Set if we should show a focus rect while the location entry field is 225 // focused. Used when the toolbar is in full keyboard accessibility mode. 226 // Repaints if necessary. 227 virtual void SetShowFocusRect(bool show); 228 229 // Select all of the text. Needed when the user tabs through controls 230 // in the toolbar in full keyboard accessibility mode. 231 virtual void SelectAll(); 232 233#if defined(OS_WIN) && !defined(USE_AURA) 234 // Event Handlers 235 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 236 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE; 237 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 238 virtual void OnMouseCaptureLost() OVERRIDE; 239#endif 240 241 LocationIconView* location_icon_view() { return location_icon_view_; } 242 const LocationIconView* location_icon_view() const { 243 return location_icon_view_; 244 } 245 246 views::View* location_entry_view() const { return location_entry_view_; } 247 248 views::View* autofill_credit_card_view(); 249 250 // OmniboxEditController: 251 virtual void OnAutocompleteAccept(const GURL& url, 252 WindowOpenDisposition disposition, 253 content::PageTransition transition, 254 const GURL& alternate_nav_url) OVERRIDE; 255 virtual void OnChanged() OVERRIDE; 256 virtual void OnSelectionBoundsChanged() OVERRIDE; 257 virtual void OnInputInProgress(bool in_progress) OVERRIDE; 258 virtual void OnKillFocus() OVERRIDE; 259 virtual void OnSetFocus() OVERRIDE; 260 virtual gfx::Image GetFavicon() const OVERRIDE; 261 virtual string16 GetTitle() const OVERRIDE; 262 virtual InstantController* GetInstant() OVERRIDE; 263 virtual content::WebContents* GetWebContents() const OVERRIDE; 264 virtual gfx::Rect GetOmniboxBounds() const OVERRIDE; 265 266 // views::View: 267 virtual const char* GetClassName() const OVERRIDE; 268 virtual bool SkipDefaultKeyEventProcessing( 269 const ui::KeyEvent& event) OVERRIDE; 270 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 271 virtual bool HasFocus() const OVERRIDE; 272 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; 273 274 // views::ButtonListener: 275 virtual void ButtonPressed(views::Button* sender, 276 const ui::Event& event) OVERRIDE; 277 278 // views::DragController: 279 virtual void WriteDragDataForView(View* sender, 280 const gfx::Point& press_pt, 281 OSExchangeData* data) OVERRIDE; 282 virtual int GetDragOperationsForView(View* sender, 283 const gfx::Point& p) OVERRIDE; 284 virtual bool CanStartDragForView(View* sender, 285 const gfx::Point& press_pt, 286 const gfx::Point& p) OVERRIDE; 287 288 // LocationBar: 289 virtual void ShowFirstRunBubble() OVERRIDE; 290 virtual string16 GetInputString() const OVERRIDE; 291 virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE; 292 virtual content::PageTransition GetPageTransition() const OVERRIDE; 293 virtual void AcceptInput() OVERRIDE; 294 virtual void FocusLocation(bool select_all) OVERRIDE; 295 virtual void FocusSearch() OVERRIDE; 296 virtual void UpdateContentSettingsIcons() OVERRIDE; 297 virtual void UpdatePageActions() OVERRIDE; 298 virtual void InvalidatePageActions() OVERRIDE; 299 virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE; 300 virtual void UpdateAutofillCreditCardView() OVERRIDE; 301 virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE; 302 virtual void Revert() OVERRIDE; 303 virtual const OmniboxView* GetLocationEntry() const OVERRIDE; 304 virtual OmniboxView* GetLocationEntry() OVERRIDE; 305 virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE; 306 307 // LocationBarTesting: 308 virtual int PageActionCount() OVERRIDE; 309 virtual int PageActionVisibleCount() OVERRIDE; 310 virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE; 311 virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE; 312 virtual void TestPageActionPressed(size_t index) OVERRIDE; 313 virtual void TestActionBoxMenuItemSelected(int command_id) OVERRIDE; 314 virtual bool GetBookmarkStarVisibility() OVERRIDE; 315 316 // TemplateURLServiceObserver: 317 virtual void OnTemplateURLServiceChanged() OVERRIDE; 318 319 // content::NotificationObserver: 320 virtual void Observe(int type, 321 const content::NotificationSource& source, 322 const content::NotificationDetails& details) OVERRIDE; 323 324 // SearchModelObserver: 325 virtual void ModelChanged(const SearchModel::State& old_state, 326 const SearchModel::State& new_state) OVERRIDE; 327 328 // Returns the height of the control without the top and bottom 329 // edges(i.e. the height of the edit control inside). If 330 // |use_preferred_size| is true this will be the preferred height, 331 // otherwise it will be the current height. 332 int GetInternalHeight(bool use_preferred_size); 333 334 // Returns the position and width that the popup should be, and also the left 335 // edge that the results should align themselves to (which will leave some 336 // border on the left of the popup). 337 void GetOmniboxPopupPositioningInfo( 338 gfx::Point* top_left_screen_coord, 339 int* popup_width, 340 int* left_margin, 341 int* right_margin); 342 343 // Space between items in the location bar, as well as between items and the 344 // edges. 345 static int GetItemPadding(); 346 347 // Thickness of the edges of the omnibox background images, in normal mode. 348 static const int kNormalEdgeThickness; 349 // The same, but for popup mode. 350 static const int kPopupEdgeThickness; 351 // Amount of padding built into the standard omnibox icons. 352 static const int kIconInternalPadding; 353 // Space between the edge and a bubble. 354 static const int kBubblePadding; 355 356 protected: 357 virtual void OnFocus() OVERRIDE; 358 359 private: 360 typedef std::vector<ContentSettingImageView*> ContentSettingViews; 361 362 friend class PageActionImageView; 363 friend class PageActionWithBadgeView; 364 typedef std::vector<PageActionWithBadgeView*> PageActionViews; 365 366 // Returns the thickness of any visible left and right edge, in pixels. 367 int GetHorizontalEdgeThickness() const; 368 369 // The same, but for the top and bottom edges. 370 int vertical_edge_thickness() const { 371 return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness; 372 } 373 374 // Update the visibility state of the Content Blocked icons to reflect what is 375 // actually blocked on the current page. 376 void RefreshContentSettingViews(); 377 378 // Delete all page action views that we have created. 379 void DeletePageActionViews(); 380 381 // Update the views for the Page Actions, to reflect state changes for 382 // PageActions. 383 void RefreshPageActionViews(); 384 385 // Returns the number of scripts currently running on the page. 386 size_t ScriptBubbleScriptsRunning(); 387 388 // Update the Script Bubble Icon, to reflect the number of content scripts 389 // running on the page. 390 void RefreshScriptBubble(); 391 392 // Update the view for the zoom icon based on the current tab's zoom. 393 void RefreshZoomView(); 394 395 // Sets the visibility of view to new_vis. 396 void ToggleVisibility(bool new_vis, views::View* view); 397 398#if !defined(USE_AURA) 399 // Helper for the Mouse event handlers that does all the real work. 400 void OnMouseEvent(const ui::MouseEvent& event, UINT msg); 401#endif 402 403 // Returns true if the suggest text is valid. 404 bool HasValidSuggestText() const; 405 406 // Helper to show the first run info bubble. 407 void ShowFirstRunBubbleInternal(); 408 409 // Draw backgrounds and borders for page actions. Must be called 410 // after layout, so the |page_action_views_| have their bounds. 411 void PaintPageActionBackgrounds(gfx::Canvas* canvas); 412 413 // The Browser this LocationBarView is in. Note that at least 414 // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser 415 // window, so this may be NULL. 416 Browser* browser_; 417 418 // The Autocomplete Edit field. 419 scoped_ptr<OmniboxView> location_entry_; 420 421 // The profile which corresponds to this View. 422 Profile* profile_; 423 424 // Command updater which corresponds to this View. 425 CommandUpdater* command_updater_; 426 427 // The model. 428 ToolbarModel* model_; 429 430 // Our delegate. 431 Delegate* delegate_; 432 433 // This is the string of text from the autocompletion session that the user 434 // entered or selected. 435 string16 location_input_; 436 437 // The user's desired disposition for how their input should be opened 438 WindowOpenDisposition disposition_; 439 440 // The transition type to use for the navigation 441 content::PageTransition transition_; 442 443 // An object used to paint the normal-mode background. 444 scoped_ptr<views::Painter> background_border_painter_; 445 scoped_ptr<views::Painter> background_filling_painter_; 446 447 // An icon to the left of the edit field. 448 LocationIconView* location_icon_view_; 449 450 // A bubble displayed for EV HTTPS sites. 451 EVBubbleView* ev_bubble_view_; 452 453 // Location_entry view 454 views::View* location_entry_view_; 455 456 // A view to show inline autocompletion when an IME is active. In this case, 457 // we shouldn't change the text or selection inside the OmniboxView itself, 458 // since this will conflict with the IME's control over the text. So instead 459 // we show any autocompletion in a separate field after the OmniboxView. 460 views::Label* ime_inline_autocomplete_view_; 461 462 // The following views are used to provide hints and remind the user as to 463 // what is going in the edit. They are all added a children of the 464 // LocationBarView. At most one is visible at a time. Preference is 465 // given to the keyword_view_, then hint_view_. 466 // These autocollapse when the edit needs the room. 467 468 // Shown if the user has selected a keyword. 469 SelectedKeywordView* selected_keyword_view_; 470 471 // View responsible for showing suggested text. This is NULL when there is no 472 // suggested text. 473 views::Label* suggested_text_view_; 474 475 // Shown if the selected url has a corresponding keyword. 476 KeywordHintView* keyword_hint_view_; 477 478 // The voice search icon. 479 MicSearchView* mic_search_view_; 480 481 // The content setting views. 482 ContentSettingViews content_setting_views_; 483 484 // The zoom icon. 485 ZoomView* zoom_view_; 486 487 // A bubble that shows after successful submission of the Autofill dialog. 488 AutofillCreditCardView* autofill_credit_card_view_; 489 490 // The icon to open a PDF in Reader. 491 OpenPDFInReaderView* open_pdf_in_reader_view_; 492 493 // The current page actions. 494 std::vector<ExtensionAction*> page_actions_; 495 496 // The page action icon views. 497 PageActionViews page_action_views_; 498 499 // The script bubble. 500 ScriptBubbleIconView* script_bubble_icon_view_; 501 502 // The star. 503 StarView* star_view_; 504 505 // The action box button (plus). 506 ActionBoxButtonView* action_box_button_view_; 507 508 // Whether we're in popup mode. 509 const bool is_popup_mode_; 510 511 // True if we should show a focus rect while the location entry field is 512 // focused. Used when the toolbar is in full keyboard accessibility mode. 513 bool show_focus_rect_; 514 515 // This is in case we're destroyed before the model loads. We need to make 516 // Add/RemoveObserver calls. 517 TemplateURLService* template_url_service_; 518 519 // Tracks this preference to determine whether bookmark editing is allowed. 520 BooleanPrefMember edit_bookmarks_enabled_; 521 522 // While animating, the host clips the widget and draws only the bottom 523 // part of it. The view needs to know the pixel offset at which we are drawing 524 // the widget so that we can draw the curved edges that attach to the toolbar 525 // in the right location. 526 int animation_offset_; 527 528 // Used to register for notifications received by NotificationObserver. 529 content::NotificationRegistrar registrar_; 530 531 DISALLOW_COPY_AND_ASSIGN(LocationBarView); 532}; 533 534#endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_ 535