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 CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_IMPL_H_ 6 #define CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_IMPL_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 12 #include "base/compiler_specific.h" 13 #include "base/gtest_prod_util.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/observer_list.h" 16 #include "base/process/process.h" 17 #include "base/values.h" 18 #include "content/browser/frame_host/frame_tree.h" 19 #include "content/browser/frame_host/navigation_controller_delegate.h" 20 #include "content/browser/frame_host/navigation_controller_impl.h" 21 #include "content/browser/frame_host/navigator_delegate.h" 22 #include "content/browser/frame_host/render_frame_host_delegate.h" 23 #include "content/browser/frame_host/render_frame_host_manager.h" 24 #include "content/browser/renderer_host/render_view_host_delegate.h" 25 #include "content/browser/renderer_host/render_widget_host_delegate.h" 26 #include "content/common/content_export.h" 27 #include "content/public/browser/notification_observer.h" 28 #include "content/public/browser/notification_registrar.h" 29 #include "content/public/browser/web_contents.h" 30 #include "content/public/common/page_transition_types.h" 31 #include "content/public/common/renderer_preferences.h" 32 #include "content/public/common/three_d_api_types.h" 33 #include "net/base/load_states.h" 34 #include "third_party/WebKit/public/web/WebDragOperation.h" 35 #include "ui/gfx/rect_f.h" 36 #include "ui/gfx/size.h" 37 #include "webkit/common/resource_type.h" 38 39 struct BrowserPluginHostMsg_ResizeGuest_Params; 40 struct ViewHostMsg_DateTimeDialogValue_Params; 41 struct ViewMsg_PostMessage_Params; 42 43 namespace content { 44 class BrowserPluginEmbedder; 45 class BrowserPluginGuest; 46 class BrowserPluginGuestManager; 47 class ColorChooser; 48 class DateTimeChooserAndroid; 49 class DownloadItem; 50 class InterstitialPageImpl; 51 class JavaBridgeDispatcherHostManager; 52 class JavaScriptDialogManager; 53 class PowerSaveBlocker; 54 class RenderViewHost; 55 class RenderViewHostDelegateView; 56 class RenderViewHostImpl; 57 class RenderWidgetHostImpl; 58 class RenderWidgetHostViewPort; 59 class SavePackage; 60 class SessionStorageNamespaceImpl; 61 class SiteInstance; 62 class TestWebContents; 63 class WebContentsDelegate; 64 class WebContentsImpl; 65 class WebContentsObserver; 66 class WebContentsViewPort; 67 class WebContentsViewDelegate; 68 struct ColorSuggestion; 69 struct FaviconURL; 70 struct LoadNotificationDetails; 71 72 // Factory function for the implementations that content knows about. Takes 73 // ownership of |delegate|. 74 WebContentsViewPort* CreateWebContentsView( 75 WebContentsImpl* web_contents, 76 WebContentsViewDelegate* delegate, 77 RenderViewHostDelegateView** render_view_host_delegate_view); 78 79 class CONTENT_EXPORT WebContentsImpl 80 : public NON_EXPORTED_BASE(WebContents), 81 public NON_EXPORTED_BASE(RenderFrameHostDelegate), 82 public RenderViewHostDelegate, 83 public RenderWidgetHostDelegate, 84 public RenderFrameHostManager::Delegate, 85 public NotificationObserver, 86 public NON_EXPORTED_BASE(NavigationControllerDelegate), 87 public NON_EXPORTED_BASE(NavigatorDelegate) { 88 public: 89 virtual ~WebContentsImpl(); 90 91 static WebContentsImpl* CreateWithOpener( 92 const WebContents::CreateParams& params, 93 WebContentsImpl* opener); 94 95 // Returns the opener WebContentsImpl, if any. This can be set to null if the 96 // opener is closed or the page clears its window.opener. 97 WebContentsImpl* opener() const { return opener_; } 98 99 // Creates a WebContents to be used as a browser plugin guest. 100 static BrowserPluginGuest* CreateGuest( 101 BrowserContext* browser_context, 102 content::SiteInstance* site_instance, 103 int guest_instance_id, 104 scoped_ptr<base::DictionaryValue> extra_params); 105 106 // Creates a swapped out RenderView. This is used by the browser plugin to 107 // create a swapped out RenderView in the embedder render process for the 108 // guest, to expose the guest's window object to the embedder. 109 // This returns the routing ID of the newly created swapped out RenderView. 110 int CreateSwappedOutRenderView(SiteInstance* instance); 111 112 // Complex initialization here. Specifically needed to avoid having 113 // members call back into our virtual functions in the constructor. 114 virtual void Init(const WebContents::CreateParams& params); 115 116 // Returns the SavePackage which manages the page saving job. May be NULL. 117 SavePackage* save_package() const { return save_package_.get(); } 118 119 #if defined(OS_ANDROID) 120 JavaBridgeDispatcherHostManager* java_bridge_dispatcher_host_manager() const { 121 return java_bridge_dispatcher_host_manager_.get(); 122 } 123 124 // In Android WebView, the RenderView needs created even there is no 125 // navigation entry, this allows Android WebViews to use 126 // javascript: URLs that load into the DOMWindow before the first page 127 // load. This is not safe to do in any context that a web page could get a 128 // reference to the DOMWindow before the first page load. 129 bool CreateRenderViewForInitialEmptyDocument(); 130 #endif 131 132 // Expose the render manager for testing. 133 RenderFrameHostManager* GetRenderManagerForTesting(); 134 135 // Returns guest browser plugin object, or NULL if this WebContents is not a 136 // guest. 137 BrowserPluginGuest* GetBrowserPluginGuest() const; 138 139 // Sets a BrowserPluginGuest object for this WebContents. If this WebContents 140 // has a BrowserPluginGuest then that implies that it is being hosted by 141 // a BrowserPlugin object in an embedder renderer process. 142 void SetBrowserPluginGuest(BrowserPluginGuest* guest); 143 144 // Returns embedder browser plugin object, or NULL if this WebContents is not 145 // an embedder. 146 BrowserPluginEmbedder* GetBrowserPluginEmbedder() const; 147 148 // Returns the BrowserPluginGuestManager object, or NULL if this web contents 149 // does not have a BrowserPluginGuestManager. 150 BrowserPluginGuestManager* GetBrowserPluginGuestManager() const; 151 152 // Gets the current fullscreen render widget's routing ID. Returns 153 // MSG_ROUTING_NONE when there is no fullscreen render widget. 154 int GetFullscreenWidgetRoutingID() const; 155 156 // Invoked when visible SSL state (as defined by SSLStatus) changes. 157 void DidChangeVisibleSSLState(); 158 159 // Informs the render view host and the BrowserPluginEmbedder, if present, of 160 // a Drag Source End. 161 void DragSourceEndedAt(int client_x, int client_y, int screen_x, 162 int screen_y, blink::WebDragOperation operation); 163 164 // Informs the render view host and the BrowserPluginEmbedder, if present, of 165 // a Drag Source Move. 166 void DragSourceMovedTo(int client_x, int client_y, 167 int screen_x, int screen_y); 168 169 // WebContents ------------------------------------------------------ 170 virtual WebContentsDelegate* GetDelegate() OVERRIDE; 171 virtual void SetDelegate(WebContentsDelegate* delegate) OVERRIDE; 172 virtual NavigationControllerImpl& GetController() OVERRIDE; 173 virtual const NavigationControllerImpl& GetController() const OVERRIDE; 174 virtual BrowserContext* GetBrowserContext() const OVERRIDE; 175 virtual RenderProcessHost* GetRenderProcessHost() const OVERRIDE; 176 virtual RenderFrameHost* GetMainFrame() OVERRIDE; 177 virtual RenderViewHost* GetRenderViewHost() const OVERRIDE; 178 virtual void GetRenderViewHostAtPosition( 179 int x, 180 int y, 181 const GetRenderViewHostCallback& callback) OVERRIDE; 182 virtual WebContents* GetEmbedderWebContents() const OVERRIDE; 183 virtual int GetEmbeddedInstanceID() const OVERRIDE; 184 virtual int GetRoutingID() const OVERRIDE; 185 virtual RenderWidgetHostView* GetRenderWidgetHostView() const OVERRIDE; 186 virtual RenderWidgetHostView* GetFullscreenRenderWidgetHostView() const 187 OVERRIDE; 188 virtual WebContentsView* GetView() const OVERRIDE; 189 virtual WebUI* CreateWebUI(const GURL& url) OVERRIDE; 190 virtual WebUI* GetWebUI() const OVERRIDE; 191 virtual WebUI* GetCommittedWebUI() const OVERRIDE; 192 virtual void SetUserAgentOverride(const std::string& override) OVERRIDE; 193 virtual const std::string& GetUserAgentOverride() const OVERRIDE; 194 #if defined(OS_WIN) && defined(USE_AURA) 195 virtual void SetParentNativeViewAccessible( 196 gfx::NativeViewAccessible accessible_parent) OVERRIDE; 197 #endif 198 virtual const base::string16& GetTitle() const OVERRIDE; 199 virtual int32 GetMaxPageID() OVERRIDE; 200 virtual int32 GetMaxPageIDForSiteInstance( 201 SiteInstance* site_instance) OVERRIDE; 202 virtual SiteInstance* GetSiteInstance() const OVERRIDE; 203 virtual SiteInstance* GetPendingSiteInstance() const OVERRIDE; 204 virtual bool IsLoading() const OVERRIDE; 205 virtual bool IsWaitingForResponse() const OVERRIDE; 206 virtual const net::LoadStateWithParam& GetLoadState() const OVERRIDE; 207 virtual const base::string16& GetLoadStateHost() const OVERRIDE; 208 virtual uint64 GetUploadSize() const OVERRIDE; 209 virtual uint64 GetUploadPosition() const OVERRIDE; 210 virtual std::set<GURL> GetSitesInTab() const OVERRIDE; 211 virtual const std::string& GetEncoding() const OVERRIDE; 212 virtual bool DisplayedInsecureContent() const OVERRIDE; 213 virtual void IncrementCapturerCount() OVERRIDE; 214 virtual void DecrementCapturerCount() OVERRIDE; 215 virtual int GetCapturerCount() const OVERRIDE; 216 virtual bool IsCrashed() const OVERRIDE; 217 virtual void SetIsCrashed(base::TerminationStatus status, 218 int error_code) OVERRIDE; 219 virtual base::TerminationStatus GetCrashedStatus() const OVERRIDE; 220 virtual bool IsBeingDestroyed() const OVERRIDE; 221 virtual void NotifyNavigationStateChanged(unsigned changed_flags) OVERRIDE; 222 virtual base::TimeTicks GetLastSelectedTime() const OVERRIDE; 223 virtual void WasShown() OVERRIDE; 224 virtual void WasHidden() OVERRIDE; 225 virtual bool NeedToFireBeforeUnload() OVERRIDE; 226 virtual void Stop() OVERRIDE; 227 virtual WebContents* Clone() OVERRIDE; 228 virtual void FocusThroughTabTraversal(bool reverse) OVERRIDE; 229 virtual bool ShowingInterstitialPage() const OVERRIDE; 230 virtual InterstitialPage* GetInterstitialPage() const OVERRIDE; 231 virtual bool IsSavable() OVERRIDE; 232 virtual void OnSavePage() OVERRIDE; 233 virtual bool SavePage(const base::FilePath& main_file, 234 const base::FilePath& dir_path, 235 SavePageType save_type) OVERRIDE; 236 virtual void SaveFrame(const GURL& url, 237 const Referrer& referrer) OVERRIDE; 238 virtual void GenerateMHTML( 239 const base::FilePath& file, 240 const base::Callback<void(int64)>& callback) 241 OVERRIDE; 242 virtual bool IsActiveEntry(int32 page_id) OVERRIDE; 243 244 virtual const std::string& GetContentsMimeType() const OVERRIDE; 245 virtual bool WillNotifyDisconnection() const OVERRIDE; 246 virtual void SetOverrideEncoding(const std::string& encoding) OVERRIDE; 247 virtual void ResetOverrideEncoding() OVERRIDE; 248 virtual RendererPreferences* GetMutableRendererPrefs() OVERRIDE; 249 virtual void Close() OVERRIDE; 250 virtual void SystemDragEnded() OVERRIDE; 251 virtual void UserGestureDone() OVERRIDE; 252 virtual void SetClosedByUserGesture(bool value) OVERRIDE; 253 virtual bool GetClosedByUserGesture() const OVERRIDE; 254 virtual double GetZoomLevel() const OVERRIDE; 255 virtual int GetZoomPercent(bool* enable_increment, 256 bool* enable_decrement) const OVERRIDE; 257 virtual void ViewSource() OVERRIDE; 258 virtual void ViewFrameSource(const GURL& url, 259 const PageState& page_state) OVERRIDE; 260 virtual int GetMinimumZoomPercent() const OVERRIDE; 261 virtual int GetMaximumZoomPercent() const OVERRIDE; 262 virtual gfx::Size GetPreferredSize() const OVERRIDE; 263 virtual bool GotResponseToLockMouseRequest(bool allowed) OVERRIDE; 264 virtual bool HasOpener() const OVERRIDE; 265 virtual void DidChooseColorInColorChooser(SkColor color) OVERRIDE; 266 virtual void DidEndColorChooser() OVERRIDE; 267 virtual int DownloadImage(const GURL& url, 268 bool is_favicon, 269 uint32_t max_bitmap_size, 270 const ImageDownloadCallback& callback) OVERRIDE; 271 virtual void SetZoomLevel(double level) OVERRIDE; 272 #if defined(OS_ANDROID) 273 virtual base::android::ScopedJavaLocalRef<jobject> GetJavaWebContents() 274 OVERRIDE; 275 #endif 276 277 // Implementation of PageNavigator. 278 virtual WebContents* OpenURL(const OpenURLParams& params) OVERRIDE; 279 280 // Implementation of IPC::Sender. 281 virtual bool Send(IPC::Message* message) OVERRIDE; 282 283 // RenderFrameHostDelegate --------------------------------------------------- 284 virtual bool OnMessageReceived(RenderFrameHost* render_frame_host, 285 const IPC::Message& message) OVERRIDE; 286 virtual void RenderFrameCreated(RenderFrameHost* render_frame_host) OVERRIDE; 287 virtual void RenderFrameDeleted(RenderFrameHost* render_frame_host) OVERRIDE; 288 289 // RenderViewHostDelegate ---------------------------------------------------- 290 virtual RenderViewHostDelegateView* GetDelegateView() OVERRIDE; 291 virtual RenderViewHostDelegate::RendererManagement* 292 GetRendererManagementDelegate() OVERRIDE; 293 virtual bool OnMessageReceived(RenderViewHost* render_view_host, 294 const IPC::Message& message) OVERRIDE; 295 virtual const GURL& GetURL() const OVERRIDE; 296 virtual const GURL& GetVisibleURL() const OVERRIDE; 297 virtual const GURL& GetLastCommittedURL() const OVERRIDE; 298 virtual WebContents* GetAsWebContents() OVERRIDE; 299 virtual gfx::Rect GetRootWindowResizerRect() const OVERRIDE; 300 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE; 301 virtual void RenderViewReady(RenderViewHost* render_view_host) OVERRIDE; 302 virtual void RenderViewTerminated(RenderViewHost* render_view_host, 303 base::TerminationStatus status, 304 int error_code) OVERRIDE; 305 virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE; 306 virtual void DidRedirectProvisionalLoad( 307 RenderViewHost* render_view_host, 308 int32 page_id, 309 const GURL& source_url, 310 const GURL& target_url) OVERRIDE; 311 virtual void DidFailProvisionalLoadWithError( 312 RenderViewHost* render_view_host, 313 const ViewHostMsg_DidFailProvisionalLoadWithError_Params& params) 314 OVERRIDE; 315 virtual void DidGetResourceResponseStart( 316 const ResourceRequestDetails& details) OVERRIDE; 317 virtual void DidGetRedirectForResourceRequest( 318 const ResourceRedirectDetails& details) OVERRIDE; 319 virtual void DidNavigate( 320 RenderViewHost* render_view_host, 321 const ViewHostMsg_FrameNavigate_Params& params) OVERRIDE; 322 virtual void UpdateState(RenderViewHost* render_view_host, 323 int32 page_id, 324 const PageState& page_state) OVERRIDE; 325 virtual void UpdateTitle(RenderViewHost* render_view_host, 326 int32 page_id, 327 const base::string16& title, 328 base::i18n::TextDirection title_direction) OVERRIDE; 329 virtual void UpdateEncoding(RenderViewHost* render_view_host, 330 const std::string& encoding) OVERRIDE; 331 virtual void UpdateTargetURL(int32 page_id, const GURL& url) OVERRIDE; 332 virtual void Close(RenderViewHost* render_view_host) OVERRIDE; 333 virtual void RequestMove(const gfx::Rect& new_bounds) OVERRIDE; 334 virtual void SwappedOut(RenderViewHost* render_view_host) OVERRIDE; 335 virtual void DidStartLoading(RenderViewHost* render_view_host) OVERRIDE; 336 virtual void DidStopLoading(RenderViewHost* render_view_host) OVERRIDE; 337 virtual void DidCancelLoading() OVERRIDE; 338 virtual void DidChangeLoadProgress(double progress) OVERRIDE; 339 virtual void DidDisownOpener(RenderViewHost* rvh) OVERRIDE; 340 virtual void DidAccessInitialDocument() OVERRIDE; 341 virtual void DocumentAvailableInMainFrame( 342 RenderViewHost* render_view_host) OVERRIDE; 343 virtual void DocumentOnLoadCompletedInMainFrame( 344 RenderViewHost* render_view_host, 345 int32 page_id) OVERRIDE; 346 virtual void RequestOpenURL(RenderViewHost* rvh, 347 const GURL& url, 348 const Referrer& referrer, 349 WindowOpenDisposition disposition, 350 int64 source_frame_id, 351 bool should_replace_current_entry, 352 bool user_gesture) OVERRIDE; 353 virtual void RequestTransferURL( 354 const GURL& url, 355 const std::vector<GURL>& redirect_chain, 356 const Referrer& referrer, 357 PageTransition page_transition, 358 WindowOpenDisposition disposition, 359 int64 source_frame_id, 360 const GlobalRequestID& transferred_global_request_id, 361 bool should_replace_current_entry, 362 bool user_gesture) OVERRIDE; 363 virtual void RouteCloseEvent(RenderViewHost* rvh) OVERRIDE; 364 virtual void RouteMessageEvent( 365 RenderViewHost* rvh, 366 const ViewMsg_PostMessage_Params& params) OVERRIDE; 367 virtual void RunJavaScriptMessage(RenderViewHost* rvh, 368 const base::string16& message, 369 const base::string16& default_prompt, 370 const GURL& frame_url, 371 JavaScriptMessageType type, 372 IPC::Message* reply_msg, 373 bool* did_suppress_message) OVERRIDE; 374 virtual void RunBeforeUnloadConfirm(RenderViewHost* rvh, 375 const base::string16& message, 376 bool is_reload, 377 IPC::Message* reply_msg) OVERRIDE; 378 virtual bool AddMessageToConsole(int32 level, 379 const base::string16& message, 380 int32 line_no, 381 const base::string16& source_id) OVERRIDE; 382 virtual RendererPreferences GetRendererPrefs( 383 BrowserContext* browser_context) const OVERRIDE; 384 virtual WebPreferences GetWebkitPrefs() OVERRIDE; 385 virtual void OnUserGesture() OVERRIDE; 386 virtual void OnIgnoredUIEvent() OVERRIDE; 387 virtual void RendererUnresponsive(RenderViewHost* render_view_host, 388 bool is_during_beforeunload, 389 bool is_during_unload) OVERRIDE; 390 virtual void RendererResponsive(RenderViewHost* render_view_host) OVERRIDE; 391 virtual void LoadStateChanged(const GURL& url, 392 const net::LoadStateWithParam& load_state, 393 uint64 upload_position, 394 uint64 upload_size) OVERRIDE; 395 virtual void WorkerCrashed() OVERRIDE; 396 virtual void Activate() OVERRIDE; 397 virtual void Deactivate() OVERRIDE; 398 virtual void LostCapture() OVERRIDE; 399 virtual void HandleMouseDown() OVERRIDE; 400 virtual void HandleMouseUp() OVERRIDE; 401 virtual void HandlePointerActivate() OVERRIDE; 402 virtual void HandleGestureBegin() OVERRIDE; 403 virtual void HandleGestureEnd() OVERRIDE; 404 virtual void RunFileChooser( 405 RenderViewHost* render_view_host, 406 const FileChooserParams& params) OVERRIDE; 407 virtual void ToggleFullscreenMode(bool enter_fullscreen) OVERRIDE; 408 virtual bool IsFullscreenForCurrentTab() const OVERRIDE; 409 virtual void UpdatePreferredSize(const gfx::Size& pref_size) OVERRIDE; 410 virtual void ResizeDueToAutoResize(const gfx::Size& new_size) OVERRIDE; 411 virtual void RequestToLockMouse(bool user_gesture, 412 bool last_unlocked_by_target) OVERRIDE; 413 virtual void LostMouseLock() OVERRIDE; 414 virtual void CreateNewWindow( 415 int render_process_id, 416 int route_id, 417 int main_frame_route_id, 418 const ViewHostMsg_CreateWindow_Params& params, 419 SessionStorageNamespace* session_storage_namespace) OVERRIDE; 420 virtual void CreateNewWidget(int render_process_id, 421 int route_id, 422 blink::WebPopupType popup_type) OVERRIDE; 423 virtual void CreateNewFullscreenWidget(int render_process_id, 424 int route_id) OVERRIDE; 425 virtual void ShowCreatedWindow(int route_id, 426 WindowOpenDisposition disposition, 427 const gfx::Rect& initial_pos, 428 bool user_gesture) OVERRIDE; 429 virtual void ShowCreatedWidget(int route_id, 430 const gfx::Rect& initial_pos) OVERRIDE; 431 virtual void ShowCreatedFullscreenWidget(int route_id) OVERRIDE; 432 virtual void ShowContextMenu(const ContextMenuParams& params) OVERRIDE; 433 virtual void RequestMediaAccessPermission( 434 const MediaStreamRequest& request, 435 const MediaResponseCallback& callback) OVERRIDE; 436 virtual SessionStorageNamespace* GetSessionStorageNamespace( 437 SiteInstance* instance) OVERRIDE; 438 virtual FrameTree* GetFrameTree() OVERRIDE; 439 440 // NavigatorDelegate --------------------------------------------------------- 441 442 virtual void DidStartProvisionalLoad( 443 RenderFrameHostImpl* render_frame_host, 444 int64 frame_id, 445 int64 parent_frame_id, 446 bool is_main_frame, 447 const GURL& validated_url, 448 bool is_error_page, 449 bool is_iframe_srcdoc) OVERRIDE; 450 virtual void NotifyChangedNavigationState( 451 InvalidateTypes changed_flags) OVERRIDE; 452 453 // RenderWidgetHostDelegate -------------------------------------------------- 454 455 virtual void RenderWidgetDeleted( 456 RenderWidgetHostImpl* render_widget_host) OVERRIDE; 457 virtual bool PreHandleKeyboardEvent( 458 const NativeWebKeyboardEvent& event, 459 bool* is_keyboard_shortcut) OVERRIDE; 460 virtual void HandleKeyboardEvent( 461 const NativeWebKeyboardEvent& event) OVERRIDE; 462 virtual bool PreHandleWheelEvent( 463 const blink::WebMouseWheelEvent& event) OVERRIDE; 464 virtual void DidSendScreenRects(RenderWidgetHostImpl* rwh) OVERRIDE; 465 #if defined(OS_WIN) && defined(USE_AURA) 466 virtual gfx::NativeViewAccessible GetParentNativeViewAccessible() OVERRIDE; 467 #endif 468 469 // RenderFrameHostManager::Delegate ------------------------------------------ 470 471 virtual bool CreateRenderViewForRenderManager( 472 RenderViewHost* render_view_host, int opener_route_id) OVERRIDE; 473 virtual void BeforeUnloadFiredFromRenderManager( 474 bool proceed, const base::TimeTicks& proceed_time, 475 bool* proceed_to_fire_unload) OVERRIDE; 476 virtual void RenderProcessGoneFromRenderManager( 477 RenderViewHost* render_view_host) OVERRIDE; 478 virtual void UpdateRenderViewSizeForRenderManager() OVERRIDE; 479 virtual void CancelModalDialogsForRenderManager() OVERRIDE; 480 virtual void NotifySwappedFromRenderManager( 481 RenderViewHost* old_host, RenderViewHost* new_host) OVERRIDE; 482 virtual int CreateOpenerRenderViewsForRenderManager( 483 SiteInstance* instance) OVERRIDE; 484 virtual NavigationControllerImpl& 485 GetControllerForRenderManager() OVERRIDE; 486 virtual WebUIImpl* CreateWebUIForRenderManager(const GURL& url) OVERRIDE; 487 virtual NavigationEntry* 488 GetLastCommittedNavigationEntryForRenderManager() OVERRIDE; 489 virtual bool FocusLocationBarByDefault() OVERRIDE; 490 virtual void SetFocusToLocationBar(bool select_all) OVERRIDE; 491 virtual void CreateViewAndSetSizeForRVH(RenderViewHost* rvh) OVERRIDE; 492 virtual bool IsHidden() OVERRIDE; 493 494 // NotificationObserver ------------------------------------------------------ 495 496 virtual void Observe(int type, 497 const NotificationSource& source, 498 const NotificationDetails& details) OVERRIDE; 499 500 // NavigationControllerDelegate ---------------------------------------------- 501 502 virtual WebContents* GetWebContents() OVERRIDE; 503 virtual void NotifyNavigationEntryCommitted( 504 const LoadCommittedDetails& load_details) OVERRIDE; 505 506 // Invoked before a form repost warning is shown. 507 virtual void NotifyBeforeFormRepostWarningShow() OVERRIDE; 508 509 // Activate this WebContents and show a form repost warning. 510 virtual void ActivateAndShowRepostFormWarningDialog() OVERRIDE; 511 512 // Updates the max page ID for the current SiteInstance in this 513 // WebContentsImpl to be at least |page_id|. 514 virtual void UpdateMaxPageID(int32 page_id) OVERRIDE; 515 516 // Updates the max page ID for the given SiteInstance in this WebContentsImpl 517 // to be at least |page_id|. 518 virtual void UpdateMaxPageIDForSiteInstance(SiteInstance* site_instance, 519 int32 page_id) OVERRIDE; 520 521 // Copy the current map of SiteInstance ID to max page ID from another tab. 522 // This is necessary when this tab adopts the NavigationEntries from 523 // |web_contents|. 524 virtual void CopyMaxPageIDsFrom(WebContents* web_contents) OVERRIDE; 525 526 // Called by the NavigationController to cause the WebContentsImpl to navigate 527 // to the current pending entry. The NavigationController should be called 528 // back with RendererDidNavigate on success or DiscardPendingEntry on failure. 529 // The callbacks can be inside of this function, or at some future time. 530 // 531 // The entry has a PageID of -1 if newly created (corresponding to navigation 532 // to a new URL). 533 // 534 // If this method returns false, then the navigation is discarded (equivalent 535 // to calling DiscardPendingEntry on the NavigationController). 536 virtual bool NavigateToPendingEntry( 537 NavigationController::ReloadType reload_type) OVERRIDE; 538 539 // Sets the history for this WebContentsImpl to |history_length| entries, and 540 // moves the current page_id to the last entry in the list if it's valid. 541 // This is mainly used when a prerendered page is swapped into the current 542 // tab. The method is virtual for testing. 543 virtual void SetHistoryLengthAndPrune( 544 const SiteInstance* site_instance, 545 int merge_history_length, 546 int32 minimum_page_id) OVERRIDE; 547 548 // Called by InterstitialPageImpl when it creates a RenderViewHost. 549 virtual void RenderViewForInterstitialPageCreated( 550 RenderViewHost* render_view_host) OVERRIDE; 551 552 // Sets the passed interstitial as the currently showing interstitial. 553 // No interstitial page should already be attached. 554 virtual void AttachInterstitialPage( 555 InterstitialPageImpl* interstitial_page) OVERRIDE; 556 557 // Unsets the currently showing interstitial. 558 virtual void DetachInterstitialPage() OVERRIDE; 559 560 // Changes the IsLoading state and notifies the delegate as needed. 561 // |details| is used to provide details on the load that just finished 562 // (but can be null if not applicable). 563 virtual void SetIsLoading(RenderViewHost* render_view_host, 564 bool is_loading, 565 LoadNotificationDetails* details) OVERRIDE; 566 567 typedef base::Callback<void(WebContents*)> CreatedCallback; 568 569 private: 570 friend class NavigationControllerImpl; 571 friend class TestNavigationObserver; 572 friend class WebContentsObserver; 573 friend class WebContents; // To implement factory methods. 574 575 FRIEND_TEST_ALL_PREFIXES(WebContentsImplTest, NoJSMessageOnInterstitials); 576 FRIEND_TEST_ALL_PREFIXES(WebContentsImplTest, UpdateTitle); 577 FRIEND_TEST_ALL_PREFIXES(WebContentsImplTest, FindOpenerRVHWhenPending); 578 FRIEND_TEST_ALL_PREFIXES(WebContentsImplTest, 579 CrossSiteCantPreemptAfterUnload); 580 FRIEND_TEST_ALL_PREFIXES(WebContentsImplTest, PendingContents); 581 FRIEND_TEST_ALL_PREFIXES(WebContentsImplTest, FrameTreeShape); 582 FRIEND_TEST_ALL_PREFIXES(FormStructureBrowserTest, HTMLFiles); 583 FRIEND_TEST_ALL_PREFIXES(NavigationControllerTest, HistoryNavigate); 584 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostManagerTest, PageDoesBackAndReload); 585 586 // So InterstitialPageImpl can access SetIsLoading. 587 friend class InterstitialPageImpl; 588 589 // TODO(brettw) TestWebContents shouldn't exist! 590 friend class TestWebContents; 591 592 class DestructionObserver; 593 594 // See WebContents::Create for a description of these parameters. 595 WebContentsImpl(BrowserContext* browser_context, 596 WebContentsImpl* opener); 597 598 // Add and remove observers for page navigation notifications. The order in 599 // which notifications are sent to observers is undefined. Clients must be 600 // sure to remove the observer before they go away. 601 void AddObserver(WebContentsObserver* observer); 602 void RemoveObserver(WebContentsObserver* observer); 603 604 // Clears this tab's opener if it has been closed. 605 void OnWebContentsDestroyed(WebContentsImpl* web_contents); 606 607 // Creates and adds to the map a destruction observer watching |web_contents|. 608 // No-op if such an observer already exists. 609 void AddDestructionObserver(WebContentsImpl* web_contents); 610 611 // Deletes and removes from the map a destruction observer 612 // watching |web_contents|. No-op if there is no such observer. 613 void RemoveDestructionObserver(WebContentsImpl* web_contents); 614 615 // Callback function when showing JS dialogs. 616 void OnDialogClosed(RenderViewHost* rvh, 617 IPC::Message* reply_msg, 618 bool success, 619 const base::string16& user_input); 620 621 // Callback function when requesting permission to access the PPAPI broker. 622 // |result| is true if permission was granted. 623 void OnPpapiBrokerPermissionResult(int routing_id, bool result); 624 625 bool OnMessageReceived(RenderViewHost* render_view_host, 626 RenderFrameHost* render_frame_host, 627 const IPC::Message& message); 628 629 // IPC message handlers. 630 void OnDidLoadResourceFromMemoryCache(const GURL& url, 631 const std::string& security_info, 632 const std::string& http_request, 633 const std::string& mime_type, 634 ResourceType::Type resource_type); 635 void OnDidDisplayInsecureContent(); 636 void OnDidRunInsecureContent(const std::string& security_origin, 637 const GURL& target_url); 638 void OnDocumentLoadedInFrame(int64 frame_id); 639 void OnDidFinishLoad(int64 frame_id, 640 const GURL& url, 641 bool is_main_frame); 642 void OnDidFailLoadWithError(int64 frame_id, 643 const GURL& url, 644 bool is_main_frame, 645 int error_code, 646 const base::string16& error_description); 647 void OnGoToEntryAtOffset(int offset); 648 void OnUpdateZoomLimits(int minimum_percent, 649 int maximum_percent, 650 bool remember); 651 void OnEnumerateDirectory(int request_id, const base::FilePath& path); 652 void OnJSOutOfMemory(); 653 654 void OnRegisterProtocolHandler(const std::string& protocol, 655 const GURL& url, 656 const base::string16& title, 657 bool user_gesture); 658 void OnFindReply(int request_id, 659 int number_of_matches, 660 const gfx::Rect& selection_rect, 661 int active_match_ordinal, 662 bool final_update); 663 #if defined(OS_ANDROID) 664 void OnFindMatchRectsReply(int version, 665 const std::vector<gfx::RectF>& rects, 666 const gfx::RectF& active_rect); 667 668 void OnOpenDateTimeDialog( 669 const ViewHostMsg_DateTimeDialogValue_Params& value); 670 void OnJavaBridgeGetChannelHandle(IPC::Message* reply_msg); 671 #endif 672 void OnPepperPluginHung(int plugin_child_id, 673 const base::FilePath& path, 674 bool is_hung); 675 void OnPluginCrashed(const base::FilePath& plugin_path, 676 base::ProcessId plugin_pid); 677 void OnAppCacheAccessed(const GURL& manifest_url, bool blocked_by_policy); 678 void OnOpenColorChooser(int color_chooser_id, 679 SkColor color, 680 const std::vector<ColorSuggestion>& suggestions); 681 void OnEndColorChooser(int color_chooser_id); 682 void OnSetSelectedColorInColorChooser(int color_chooser_id, SkColor color); 683 void OnWebUISend(const GURL& source_url, 684 const std::string& name, 685 const base::ListValue& args); 686 void OnRequestPpapiBrokerPermission(int routing_id, 687 const GURL& url, 688 const base::FilePath& plugin_path); 689 void OnBrowserPluginMessage(const IPC::Message& message); 690 void OnDidDownloadImage(int id, 691 int http_status_code, 692 const GURL& image_url, 693 const std::vector<SkBitmap>& bitmaps, 694 const std::vector<gfx::Size>& original_bitmap_sizes); 695 void OnUpdateFaviconURL(int32 page_id, 696 const std::vector<FaviconURL>& candidates); 697 void OnFirstVisuallyNonEmptyPaint(int32 page_id); 698 void OnMediaNotification(int64 player_cookie, 699 bool has_video, 700 bool has_audio, 701 bool is_playing); 702 void OnShowValidationMessage(const gfx::Rect& anchor_in_root_view, 703 const string16& main_text, 704 const string16& sub_text); 705 void OnHideValidationMessage(); 706 void OnMoveValidationMessage(const gfx::Rect& anchor_in_root_view); 707 708 709 // Called by derived classes to indicate that we're no longer waiting for a 710 // response. This won't actually update the throbber, but it will get picked 711 // up at the next animation step if the throbber is going. 712 void SetNotWaitingForResponse() { waiting_for_response_ = false; } 713 714 // Navigation helpers -------------------------------------------------------- 715 // 716 // These functions are helpers for Navigate() and DidNavigate(). 717 718 // Handles post-navigation tasks in DidNavigate AFTER the entry has been 719 // committed to the navigation controller. Note that the navigation entry is 720 // not provided since it may be invalid/changed after being committed. The 721 // current navigation entry is in the NavigationController at this point. 722 void DidNavigateMainFramePostCommit( 723 const LoadCommittedDetails& details, 724 const ViewHostMsg_FrameNavigate_Params& params); 725 void DidNavigateAnyFramePostCommit( 726 RenderViewHost* render_view_host, 727 const LoadCommittedDetails& details, 728 const ViewHostMsg_FrameNavigate_Params& params); 729 730 // Specifies whether the passed in URL should be assigned as the site of the 731 // current SiteInstance, if it does not yet have a site. 732 bool ShouldAssignSiteForURL(const GURL& url); 733 734 // If our controller was restored, update the max page ID associated with the 735 // given RenderViewHost to be larger than the number of restored entries. 736 // This is called in CreateRenderView before any navigations in the RenderView 737 // have begun, to prevent any races in updating RenderView::next_page_id. 738 void UpdateMaxPageIDIfNecessary(RenderViewHost* rvh); 739 740 // Saves the given title to the navigation entry and does associated work. It 741 // will update history and the view for the new title, and also synthesize 742 // titles for file URLs that have none (so we require that the URL of the 743 // entry already be set). 744 // 745 // This is used as the backend for state updates, which include a new title, 746 // or the dedicated set title message. It returns true if the new title is 747 // different and was therefore updated. 748 bool UpdateTitleForEntry(NavigationEntryImpl* entry, 749 const base::string16& title); 750 751 // Causes the WebContentsImpl to navigate in the right renderer to |entry|, 752 // which must be already part of the entries in the navigation controller. 753 // This does not change the NavigationController state. 754 bool NavigateToEntry(const NavigationEntryImpl& entry, 755 NavigationController::ReloadType reload_type); 756 757 // Recursively creates swapped out RenderViews for this tab's opener chain 758 // (including this tab) in the given SiteInstance, allowing other tabs to send 759 // cross-process JavaScript calls to their opener(s). Returns the route ID of 760 // this tab's RenderView for |instance|. 761 int CreateOpenerRenderViews(SiteInstance* instance); 762 763 // Helper for CreateNewWidget/CreateNewFullscreenWidget. 764 void CreateNewWidget(int render_process_id, 765 int route_id, 766 bool is_fullscreen, 767 blink::WebPopupType popup_type); 768 769 // Helper for ShowCreatedWidget/ShowCreatedFullscreenWidget. 770 void ShowCreatedWidget(int route_id, 771 bool is_fullscreen, 772 const gfx::Rect& initial_pos); 773 774 // Finds the new RenderWidgetHost and returns it. Note that this can only be 775 // called once as this call also removes it from the internal map. 776 RenderWidgetHostView* GetCreatedWidget(int route_id); 777 778 // Finds the new WebContentsImpl by route_id, initializes it for 779 // renderer-initiated creation, and returns it. Note that this can only be 780 // called once as this call also removes it from the internal map. 781 WebContentsImpl* GetCreatedWindow(int route_id); 782 783 // Returns the RenderWidgetHostView that is associated with a native window 784 // and can be used in showing created widgets. 785 // If this WebContents belongs to a browser plugin guest, there is no native 786 // window 'view' associated with this WebContents. This method returns the 787 // 'view' of the embedder instead. 788 RenderWidgetHostViewPort* GetRenderWidgetHostViewPort() const; 789 790 // Misc non-view stuff ------------------------------------------------------- 791 792 // Helper functions for sending notifications. 793 void NotifySwapped(RenderViewHost* old_host, RenderViewHost* new_host); 794 void NotifyDisconnected(); 795 796 void SetEncoding(const std::string& encoding); 797 798 // TODO(creis): This should take in a FrameTreeNode to know which node's 799 // render manager to return. For now, we just return the root's. 800 RenderFrameHostManager* GetRenderManager() const; 801 802 RenderViewHostImpl* GetRenderViewHostImpl(); 803 804 // Removes browser plugin embedder if there is one. 805 void RemoveBrowserPluginEmbedder(); 806 807 // Clear |render_view_host|'s PowerSaveBlockers. 808 void ClearPowerSaveBlockers(RenderViewHost* render_view_host); 809 810 // Clear all PowerSaveBlockers, leave power_save_blocker_ empty. 811 void ClearAllPowerSaveBlockers(); 812 813 // Helper function to invoke WebContentsDelegate::GetSizeForNewRenderView(). 814 gfx::Size GetSizeForNewRenderView() const; 815 816 void OnFrameRemoved(RenderViewHostImpl* render_view_host, int64 frame_id); 817 818 // Adds/removes a callback called on creation of each new WebContents. 819 // Deprecated, about to remove. 820 static void AddCreatedCallback(const CreatedCallback& callback); 821 static void RemoveCreatedCallback(const CreatedCallback& callback); 822 823 // Data for core operation --------------------------------------------------- 824 825 // Delegate for notifying our owner about stuff. Not owned by us. 826 WebContentsDelegate* delegate_; 827 828 // Handles the back/forward list and loading. 829 NavigationControllerImpl controller_; 830 831 // The corresponding view. 832 scoped_ptr<WebContentsViewPort> view_; 833 834 // The view of the RVHD. Usually this is our WebContentsView implementation, 835 // but if an embedder uses a different WebContentsView, they'll need to 836 // provide this. 837 RenderViewHostDelegateView* render_view_host_delegate_view_; 838 839 // Tracks created WebContentsImpl objects that have not been shown yet. They 840 // are identified by the route ID passed to CreateNewWindow. 841 typedef std::map<int, WebContentsImpl*> PendingContents; 842 PendingContents pending_contents_; 843 844 // These maps hold on to the widgets that we created on behalf of the renderer 845 // that haven't shown yet. 846 typedef std::map<int, RenderWidgetHostView*> PendingWidgetViews; 847 PendingWidgetViews pending_widget_views_; 848 849 typedef std::map<WebContentsImpl*, DestructionObserver*> DestructionObservers; 850 DestructionObservers destruction_observers_; 851 852 // A list of observers notified when page state changes. Weak references. 853 // This MUST be listed above frame_tree_ since at destruction time the 854 // latter might cause RenderViewHost's destructor to call us and we might use 855 // the observer list then. 856 ObserverList<WebContentsObserver> observers_; 857 858 // The tab that opened this tab, if any. Will be set to null if the opener 859 // is closed. 860 WebContentsImpl* opener_; 861 862 #if defined(OS_WIN) && defined(USE_AURA) 863 gfx::NativeViewAccessible accessible_parent_; 864 #endif 865 866 // Helper classes ------------------------------------------------------------ 867 868 // Maps the RenderViewHost to its media_player_cookie and PowerSaveBlocker 869 // pairs. Key is the RenderViewHost, value is the map which maps player_cookie 870 // on to PowerSaveBlocker. 871 typedef std::map<RenderViewHost*, std::map<int64, PowerSaveBlocker*> > 872 PowerSaveBlockerMap; 873 PowerSaveBlockerMap power_save_blockers_; 874 875 // Manages the frame tree of the page and process swaps in each node. 876 FrameTree frame_tree_; 877 878 #if defined(OS_ANDROID) 879 // Manages injecting Java objects into all RenderViewHosts associated with 880 // this WebContentsImpl. 881 scoped_ptr<JavaBridgeDispatcherHostManager> 882 java_bridge_dispatcher_host_manager_; 883 #endif 884 885 // SavePackage, lazily created. 886 scoped_refptr<SavePackage> save_package_; 887 888 // Data for loading state ---------------------------------------------------- 889 890 // Indicates whether we're currently loading a resource. 891 bool is_loading_; 892 893 // Indicates if the tab is considered crashed. 894 base::TerminationStatus crashed_status_; 895 int crashed_error_code_; 896 897 // Whether this WebContents is waiting for a first-response for the 898 // main resource of the page. This controls whether the throbber state is 899 // "waiting" or "loading." 900 bool waiting_for_response_; 901 902 // Map of SiteInstance ID to max page ID for this tab. A page ID is specific 903 // to a given tab and SiteInstance, and must be valid for the lifetime of the 904 // WebContentsImpl. 905 std::map<int32, int32> max_page_ids_; 906 907 // System time at which the current load was started. 908 base::TimeTicks current_load_start_; 909 910 // The current load state and the URL associated with it. 911 net::LoadStateWithParam load_state_; 912 base::string16 load_state_host_; 913 // Upload progress, for displaying in the status bar. 914 // Set to zero when there is no significant upload happening. 915 uint64 upload_size_; 916 uint64 upload_position_; 917 918 // Data for current page ----------------------------------------------------- 919 920 // When a title cannot be taken from any entry, this title will be used. 921 base::string16 page_title_when_no_navigation_entry_; 922 923 // When a navigation occurs, we record its contents MIME type. It can be 924 // used to check whether we can do something for some special contents. 925 std::string contents_mime_type_; 926 927 // Character encoding. 928 std::string encoding_; 929 930 // True if this is a secure page which displayed insecure content. 931 bool displayed_insecure_content_; 932 933 // Data for misc internal state ---------------------------------------------- 934 935 // When > 0, the WebContents is currently being captured (e.g., for 936 // screenshots or mirroring); and the underlying RenderWidgetHost should not 937 // be told it is hidden. 938 int capturer_count_; 939 940 // Tracks whether RWHV should be visible once capturer_count_ becomes zero. 941 bool should_normally_be_visible_; 942 943 // See getter above. 944 bool is_being_destroyed_; 945 946 // Indicates whether we should notify about disconnection of this 947 // WebContentsImpl. This is used to ensure disconnection notifications only 948 // happen if a connection notification has happened and that they happen only 949 // once. 950 bool notify_disconnection_; 951 952 // Pointer to the JavaScript dialog manager, lazily assigned. Used because the 953 // delegate of this WebContentsImpl is nulled before its destructor is called. 954 JavaScriptDialogManager* dialog_manager_; 955 956 // Set to true when there is an active "before unload" dialog. When true, 957 // we've forced the throbber to start in Navigate, and we need to remember to 958 // turn it off in OnJavaScriptMessageBoxClosed if the navigation is canceled. 959 bool is_showing_before_unload_dialog_; 960 961 // Settings that get passed to the renderer process. 962 RendererPreferences renderer_preferences_; 963 964 // The time that this tab was last selected. 965 base::TimeTicks last_selected_time_; 966 967 // See description above setter. 968 bool closed_by_user_gesture_; 969 970 // Minimum/maximum zoom percent. 971 int minimum_zoom_percent_; 972 int maximum_zoom_percent_; 973 // If true, the default zoom limits have been overriden for this tab, in which 974 // case we don't want saved settings to apply to it and we don't want to 975 // remember it. 976 bool temporary_zoom_settings_; 977 978 // The intrinsic size of the page. 979 gfx::Size preferred_size_; 980 981 #if defined(OS_ANDROID) 982 // Date time chooser opened by this tab. 983 // Only used in Android since all other platforms use a multi field UI. 984 scoped_ptr<DateTimeChooserAndroid> date_time_chooser_; 985 #endif 986 987 // Color chooser that was opened by this tab. 988 scoped_ptr<ColorChooser> color_chooser_; 989 990 // A unique identifier for the current color chooser. Identifiers are unique 991 // across a renderer process. This avoids race conditions in synchronizing 992 // the browser and renderer processes. For example, if a renderer closes one 993 // chooser and opens another, and simultaneously the user picks a color in the 994 // first chooser, the IDs can be used to drop the "chose a color" message 995 // rather than erroneously tell the renderer that the user picked a color in 996 // the second chooser. 997 int color_chooser_identifier_; 998 999 // Manages the embedder state for browser plugins, if this WebContents is an 1000 // embedder; NULL otherwise. 1001 scoped_ptr<BrowserPluginEmbedder> browser_plugin_embedder_; 1002 // Manages the guest state for browser plugin, if this WebContents is a guest; 1003 // NULL otherwise. 1004 scoped_ptr<BrowserPluginGuest> browser_plugin_guest_; 1005 1006 // This must be at the end, or else we might get notifications and use other 1007 // member variables that are gone. 1008 NotificationRegistrar registrar_; 1009 1010 // Used during IPC message dispatching from the RenderView so that the 1011 // handlers can get a pointer to the RVH through which the message was 1012 // received. 1013 RenderViewHost* render_view_message_source_; 1014 1015 // All live RenderWidgetHostImpls that are created by this object and may 1016 // outlive it. 1017 std::set<RenderWidgetHostImpl*> created_widgets_; 1018 1019 // Routing id of the shown fullscreen widget or MSG_ROUTING_NONE otherwise. 1020 int fullscreen_widget_routing_id_; 1021 1022 // Maps the ids of pending image downloads to their callbacks 1023 typedef std::map<int, ImageDownloadCallback> ImageDownloadMap; 1024 ImageDownloadMap image_download_map_; 1025 1026 DISALLOW_COPY_AND_ASSIGN(WebContentsImpl); 1027 }; 1028 1029 } // namespace content 1030 1031 #endif // CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_IMPL_H_ 1032