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