location_bar_view.h revision 868fa2fe829687343ffae624259930155e16dbd8
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 262 // views::View: 263 virtual const char* GetClassName() const OVERRIDE; 264 virtual bool SkipDefaultKeyEventProcessing( 265 const ui::KeyEvent& event) OVERRIDE; 266 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 267 virtual bool HasFocus() const OVERRIDE; 268 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; 269 270 // views::DragController: 271 virtual void WriteDragDataForView(View* sender, 272 const gfx::Point& press_pt, 273 OSExchangeData* data) OVERRIDE; 274 virtual int GetDragOperationsForView(View* sender, 275 const gfx::Point& p) OVERRIDE; 276 virtual bool CanStartDragForView(View* sender, 277 const gfx::Point& press_pt, 278 const gfx::Point& p) OVERRIDE; 279 280 // LocationBar: 281 virtual void ShowFirstRunBubble() OVERRIDE; 282 virtual void SetInstantSuggestion( 283 const InstantSuggestion& suggestion) OVERRIDE; 284 virtual string16 GetInputString() const OVERRIDE; 285 virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE; 286 virtual content::PageTransition GetPageTransition() const OVERRIDE; 287 virtual void AcceptInput() OVERRIDE; 288 virtual void FocusLocation(bool select_all) OVERRIDE; 289 virtual void FocusSearch() OVERRIDE; 290 virtual void UpdateContentSettingsIcons() OVERRIDE; 291 virtual void UpdatePageActions() OVERRIDE; 292 virtual void InvalidatePageActions() OVERRIDE; 293 virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE; 294 virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE; 295 virtual void Revert() OVERRIDE; 296 virtual const OmniboxView* GetLocationEntry() const OVERRIDE; 297 virtual OmniboxView* GetLocationEntry() OVERRIDE; 298 virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE; 299 300 // LocationBarTesting: 301 virtual int PageActionCount() OVERRIDE; 302 virtual int PageActionVisibleCount() OVERRIDE; 303 virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE; 304 virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE; 305 virtual void TestPageActionPressed(size_t index) OVERRIDE; 306 virtual void TestActionBoxMenuItemSelected(int command_id) 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 // Returns the height of the control without the top and bottom 318 // edges(i.e. the height of the edit control inside). If 319 // |use_preferred_size| is true this will be the preferred height, 320 // otherwise it will be the current height. 321 int GetInternalHeight(bool use_preferred_size); 322 323 // Returns the position and width that the popup should be, and also the left 324 // edge that the results should align themselves to (which will leave some 325 // border on the left of the popup). 326 void GetOmniboxPopupPositioningInfo( 327 gfx::Point* top_left_screen_coord, 328 int* popup_width, 329 int* left_margin, 330 int* right_margin); 331 332 // Space between items in the location bar, as well as between items and the 333 // edges. 334 static int GetItemPadding(); 335 336 // Thickness of the edges of the omnibox background images, in normal mode. 337 static const int kNormalEdgeThickness; 338 // The same, but for popup mode. 339 static const int kPopupEdgeThickness; 340 // Amount of padding built into the standard omnibox icons. 341 static const int kIconInternalPadding; 342 // Space between the edge and a bubble. 343 static const int kBubblePadding; 344 345 protected: 346 virtual void OnFocus() OVERRIDE; 347 348 private: 349 typedef std::vector<ContentSettingImageView*> ContentSettingViews; 350 351 friend class PageActionImageView; 352 friend class PageActionWithBadgeView; 353 typedef std::vector<PageActionWithBadgeView*> PageActionViews; 354 355 // Returns the thickness of any visible left and right edge, in pixels. 356 int GetHorizontalEdgeThickness() const; 357 358 // The same, but for the top and bottom edges. 359 int vertical_edge_thickness() const { 360 return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness; 361 } 362 363 // Update the visibility state of the Content Blocked icons to reflect what is 364 // actually blocked on the current page. 365 void RefreshContentSettingViews(); 366 367 // Delete all page action views that we have created. 368 void DeletePageActionViews(); 369 370 // Update the views for the Page Actions, to reflect state changes for 371 // PageActions. 372 void RefreshPageActionViews(); 373 374 // Returns the number of scripts currently running on the page. 375 size_t ScriptBubbleScriptsRunning(); 376 377 // Update the Script Bubble Icon, to reflect the number of content scripts 378 // running on the page. 379 void RefreshScriptBubble(); 380 381 // Update the view for the zoom icon based on the current tab's zoom. 382 void RefreshZoomView(); 383 384 // Sets the visibility of view to new_vis. 385 void ToggleVisibility(bool new_vis, views::View* view); 386 387#if !defined(USE_AURA) 388 // Helper for the Mouse event handlers that does all the real work. 389 void OnMouseEvent(const ui::MouseEvent& event, UINT msg); 390#endif 391 392 // Returns true if the suggest text is valid. 393 bool HasValidSuggestText() const; 394 395 // Helper to show the first run info bubble. 396 void ShowFirstRunBubbleInternal(); 397 398 // Draw backgrounds and borders for page actions. Must be called 399 // after layout, so the |page_action_views_| have their bounds. 400 void PaintPageActionBackgrounds(gfx::Canvas* canvas); 401 402 // The Browser this LocationBarView is in. Note that at least 403 // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser 404 // window, so this may be NULL. 405 Browser* browser_; 406 407 // The Autocomplete Edit field. 408 scoped_ptr<OmniboxView> location_entry_; 409 410 // The profile which corresponds to this View. 411 Profile* profile_; 412 413 // Command updater which corresponds to this View. 414 CommandUpdater* command_updater_; 415 416 // The model. 417 ToolbarModel* model_; 418 419 // Our delegate. 420 Delegate* delegate_; 421 422 // This is the string of text from the autocompletion session that the user 423 // entered or selected. 424 string16 location_input_; 425 426 // The user's desired disposition for how their input should be opened 427 WindowOpenDisposition disposition_; 428 429 // The transition type to use for the navigation 430 content::PageTransition transition_; 431 432 // An object used to paint the normal-mode background. 433 scoped_ptr<views::Painter> background_border_painter_; 434 scoped_ptr<views::Painter> background_filling_painter_; 435 436 // An icon to the left of the edit field. 437 LocationIconView* location_icon_view_; 438 439 // A bubble displayed for EV HTTPS sites. 440 EVBubbleView* ev_bubble_view_; 441 442 // Location_entry view 443 views::View* location_entry_view_; 444 445 // A view to show inline autocompletion when an IME is active. In this case, 446 // we shouldn't change the text or selection inside the OmniboxView itself, 447 // since this will conflict with the IME's control over the text. So instead 448 // we show any autocompletion in a separate field after the OmniboxView. 449 views::Label* ime_inline_autocomplete_view_; 450 451 // The following views are used to provide hints and remind the user as to 452 // what is going in the edit. They are all added a children of the 453 // LocationBarView. At most one is visible at a time. Preference is 454 // given to the keyword_view_, then hint_view_. 455 // These autocollapse when the edit needs the room. 456 457 // Shown if the user has selected a keyword. 458 SelectedKeywordView* selected_keyword_view_; 459 460 // View responsible for showing suggested text. This is NULL when there is no 461 // suggested text. 462 views::Label* suggested_text_view_; 463 464 // Shown if the selected url has a corresponding keyword. 465 KeywordHintView* keyword_hint_view_; 466 467 // View responsible for showing text "<Search provider> Search", which appears 468 // when omnibox replaces the URL with its query terms and there's enough space 469 // in omnibox. 470 views::Label* search_token_view_; 471 LocationBarSeparatorView* search_token_separator_view_; 472 473 // The content setting views. 474 ContentSettingViews content_setting_views_; 475 476 // The zoom icon. 477 ZoomView* zoom_view_; 478 479 // The icon to open a PDF in Reader. 480 OpenPDFInReaderView* open_pdf_in_reader_view_; 481 482 // The current page actions. 483 std::vector<ExtensionAction*> page_actions_; 484 485 // The page action icon views. 486 PageActionViews page_action_views_; 487 488 // The script bubble. 489 ScriptBubbleIconView* script_bubble_icon_view_; 490 491 // The star. 492 StarView* star_view_; 493 494 // The action box button (plus). 495 ActionBoxButtonView* action_box_button_view_; 496 497 // Whether we're in popup mode. 498 const bool is_popup_mode_; 499 500 // True if we should show a focus rect while the location entry field is 501 // focused. Used when the toolbar is in full keyboard accessibility mode. 502 bool show_focus_rect_; 503 504 // This is in case we're destroyed before the model loads. We need to make 505 // Add/RemoveObserver calls. 506 TemplateURLService* template_url_service_; 507 508 // Tracks this preference to determine whether bookmark editing is allowed. 509 BooleanPrefMember edit_bookmarks_enabled_; 510 511 // While animating, the host clips the widget and draws only the bottom 512 // part of it. The view needs to know the pixel offset at which we are drawing 513 // the widget so that we can draw the curved edges that attach to the toolbar 514 // in the right location. 515 int animation_offset_; 516 517 // Used to register for notifications received by NotificationObserver. 518 content::NotificationRegistrar registrar_; 519 520 DISALLOW_COPY_AND_ASSIGN(LocationBarView); 521}; 522 523#endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_ 524