| OLD | NEW |
| (Empty) |
| 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_RENDER_VIEW_HOST_MANAGER_H_ | |
| 6 #define CONTENT_BROWSER_WEB_CONTENTS_RENDER_VIEW_HOST_MANAGER_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "content/browser/renderer_host/render_view_host_delegate.h" | |
| 13 #include "content/browser/site_instance_impl.h" | |
| 14 #include "content/common/content_export.h" | |
| 15 #include "content/public/browser/notification_observer.h" | |
| 16 #include "content/public/browser/notification_registrar.h" | |
| 17 #include "content/public/common/referrer.h" | |
| 18 | |
| 19 | |
| 20 namespace content { | |
| 21 class BrowserContext; | |
| 22 class InterstitialPageImpl; | |
| 23 class NavigationControllerImpl; | |
| 24 class NavigationEntry; | |
| 25 class NavigationEntryImpl; | |
| 26 class RenderViewHost; | |
| 27 class RenderViewHostImpl; | |
| 28 class RenderViewHostManagerTest; | |
| 29 class RenderWidgetHostDelegate; | |
| 30 class RenderWidgetHostView; | |
| 31 class TestWebContents; | |
| 32 class WebUIImpl; | |
| 33 | |
| 34 // Manages RenderViewHosts for a WebContentsImpl. Normally there is only one and | |
| 35 // it is easy to do. But we can also have transitions of processes (and hence | |
| 36 // RenderViewHosts) that can get complex. | |
| 37 class CONTENT_EXPORT RenderViewHostManager | |
| 38 : public RenderViewHostDelegate::RendererManagement, | |
| 39 public NotificationObserver { | |
| 40 public: | |
| 41 // Functions implemented by our owner that we need. | |
| 42 // | |
| 43 // TODO(brettw) Clean this up! These are all the functions in WebContentsImpl | |
| 44 // that are required to run this class. The design should probably be better | |
| 45 // such that these are more clear. | |
| 46 // | |
| 47 // There is additional complexity that some of the functions we need in | |
| 48 // WebContentsImpl are inherited and non-virtual. These are named with | |
| 49 // "RenderManager" so that the duplicate implementation of them will be clear. | |
| 50 class CONTENT_EXPORT Delegate { | |
| 51 public: | |
| 52 // Initializes the given renderer if necessary and creates the view ID | |
| 53 // corresponding to this view host. If this method is not called and the | |
| 54 // process is not shared, then the WebContentsImpl will act as though the | |
| 55 // renderer is not running (i.e., it will render "sad tab"). This method is | |
| 56 // automatically called from LoadURL. | |
| 57 // | |
| 58 // If you are attaching to an already-existing RenderView, you should call | |
| 59 // InitWithExistingID. | |
| 60 virtual bool CreateRenderViewForRenderManager( | |
| 61 RenderViewHost* render_view_host, int opener_route_id) = 0; | |
| 62 virtual void BeforeUnloadFiredFromRenderManager( | |
| 63 bool proceed, const base::TimeTicks& proceed_time, | |
| 64 bool* proceed_to_fire_unload) = 0; | |
| 65 virtual void RenderProcessGoneFromRenderManager( | |
| 66 RenderViewHost* render_view_host) = 0; | |
| 67 virtual void UpdateRenderViewSizeForRenderManager() = 0; | |
| 68 virtual void CancelModalDialogsForRenderManager() = 0; | |
| 69 virtual void NotifySwappedFromRenderManager( | |
| 70 RenderViewHost* old_host, RenderViewHost* new_host) = 0; | |
| 71 virtual NavigationControllerImpl& | |
| 72 GetControllerForRenderManager() = 0; | |
| 73 | |
| 74 // Create swapped out RenderViews in the given SiteInstance for each tab in | |
| 75 // the opener chain of this tab, if any. This allows the current tab to | |
| 76 // make cross-process script calls to its opener(s). Returns the route ID | |
| 77 // of the immediate opener, if one exists (otherwise MSG_ROUTING_NONE). | |
| 78 virtual int CreateOpenerRenderViewsForRenderManager( | |
| 79 SiteInstance* instance) = 0; | |
| 80 | |
| 81 // Creates a WebUI object for the given URL if one applies. Ownership of the | |
| 82 // returned pointer will be passed to the caller. If no WebUI applies, | |
| 83 // returns NULL. | |
| 84 virtual WebUIImpl* CreateWebUIForRenderManager(const GURL& url) = 0; | |
| 85 | |
| 86 // Returns the navigation entry of the current navigation, or NULL if there | |
| 87 // is none. | |
| 88 virtual NavigationEntry* | |
| 89 GetLastCommittedNavigationEntryForRenderManager() = 0; | |
| 90 | |
| 91 // Returns true if the location bar should be focused by default rather than | |
| 92 // the page contents. The view calls this function when the tab is focused | |
| 93 // to see what it should do. | |
| 94 virtual bool FocusLocationBarByDefault() = 0; | |
| 95 | |
| 96 // Focuses the location bar. | |
| 97 virtual void SetFocusToLocationBar(bool select_all) = 0; | |
| 98 | |
| 99 // Creates a view and sets the size for the specified RVH. | |
| 100 virtual void CreateViewAndSetSizeForRVH(RenderViewHost* rvh) = 0; | |
| 101 | |
| 102 // Returns true if views created for this delegate should be created in a | |
| 103 // hidden state. | |
| 104 virtual bool IsHidden() = 0; | |
| 105 | |
| 106 protected: | |
| 107 virtual ~Delegate() {} | |
| 108 }; | |
| 109 | |
| 110 // All three delegate pointers must be non-NULL and are not owned by this | |
| 111 // class. They must outlive this class. The RenderViewHostDelegate and | |
| 112 // RenderWidgetHostDelegate are what will be installed into all | |
| 113 // RenderViewHosts that are created. | |
| 114 // | |
| 115 // You must call Init() before using this class. | |
| 116 RenderViewHostManager( | |
| 117 RenderViewHostDelegate* render_view_delegate, | |
| 118 RenderWidgetHostDelegate* render_widget_delegate, | |
| 119 Delegate* delegate); | |
| 120 virtual ~RenderViewHostManager(); | |
| 121 | |
| 122 // For arguments, see WebContentsImpl constructor. | |
| 123 void Init(BrowserContext* browser_context, | |
| 124 SiteInstance* site_instance, | |
| 125 int routing_id, | |
| 126 int main_frame_routing_id); | |
| 127 | |
| 128 // Returns the currently active RenderViewHost. | |
| 129 // | |
| 130 // This will be non-NULL between Init() and Shutdown(). You may want to NULL | |
| 131 // check it in many cases, however. Windows can send us messages during the | |
| 132 // destruction process after it has been shut down. | |
| 133 RenderViewHostImpl* current_host() const; | |
| 134 | |
| 135 // Returns the view associated with the current RenderViewHost, or NULL if | |
| 136 // there is no current one. | |
| 137 RenderWidgetHostView* GetRenderWidgetHostView() const; | |
| 138 | |
| 139 // Returns the pending render view host, or NULL if there is no pending one. | |
| 140 RenderViewHostImpl* pending_render_view_host() const; | |
| 141 | |
| 142 // Returns the current committed Web UI or NULL if none applies. | |
| 143 WebUIImpl* web_ui() const { return web_ui_.get(); } | |
| 144 | |
| 145 // Returns the Web UI for the pending navigation, or NULL of none applies. | |
| 146 WebUIImpl* pending_web_ui() const { | |
| 147 return pending_web_ui_.get() ? pending_web_ui_.get() : | |
| 148 pending_and_current_web_ui_.get(); | |
| 149 } | |
| 150 | |
| 151 // Sets the pending Web UI for the pending navigation, ensuring that the | |
| 152 // bindings are appropriate for the given NavigationEntry. | |
| 153 void SetPendingWebUI(const NavigationEntryImpl& entry); | |
| 154 | |
| 155 // Called when we want to instruct the renderer to navigate to the given | |
| 156 // navigation entry. It may create a new RenderViewHost or re-use an existing | |
| 157 // one. The RenderViewHost to navigate will be returned. Returns NULL if one | |
| 158 // could not be created. | |
| 159 RenderViewHostImpl* Navigate(const NavigationEntryImpl& entry); | |
| 160 | |
| 161 // Instructs the various live views to stop. Called when the user directed the | |
| 162 // page to stop loading. | |
| 163 void Stop(); | |
| 164 | |
| 165 // Notifies the regular and pending RenderViewHosts that a load is or is not | |
| 166 // happening. Even though the message is only for one of them, we don't know | |
| 167 // which one so we tell both. | |
| 168 void SetIsLoading(bool is_loading); | |
| 169 | |
| 170 // Whether to close the tab or not when there is a hang during an unload | |
| 171 // handler. If we are mid-crosssite navigation, then we should proceed | |
| 172 // with the navigation instead of closing the tab. | |
| 173 bool ShouldCloseTabOnUnresponsiveRenderer(); | |
| 174 | |
| 175 // The RenderViewHost has been swapped out, so we should resume the pending | |
| 176 // network response and allow the pending RenderViewHost to commit. | |
| 177 void SwappedOut(RenderViewHost* render_view_host); | |
| 178 | |
| 179 // Called when a renderer's main frame navigates. | |
| 180 void DidNavigateMainFrame(RenderViewHost* render_view_host); | |
| 181 | |
| 182 // Called when a renderer sets its opener to null. | |
| 183 void DidDisownOpener(RenderViewHost* render_view_host); | |
| 184 | |
| 185 // Helper method to create a RenderViewHost. If |swapped_out| is true, it | |
| 186 // will be initially placed on the swapped out hosts list. Otherwise, it | |
| 187 // will be used for a pending cross-site navigation. | |
| 188 int CreateRenderView(SiteInstance* instance, | |
| 189 int opener_route_id, | |
| 190 bool swapped_out, | |
| 191 bool hidden); | |
| 192 | |
| 193 // Called when a provisional load on the given renderer is aborted. | |
| 194 void RendererAbortedProvisionalLoad(RenderViewHost* render_view_host); | |
| 195 | |
| 196 // Sets the passed passed interstitial as the currently showing interstitial. | |
| 197 // |interstitial_page| should be non NULL (use the remove_interstitial_page | |
| 198 // method to unset the interstitial) and no interstitial page should be set | |
| 199 // when there is already a non NULL interstitial page set. | |
| 200 void set_interstitial_page(InterstitialPageImpl* interstitial_page) { | |
| 201 DCHECK(!interstitial_page_ && interstitial_page); | |
| 202 interstitial_page_ = interstitial_page; | |
| 203 } | |
| 204 | |
| 205 // Unsets the currently showing interstitial. | |
| 206 void remove_interstitial_page() { | |
| 207 DCHECK(interstitial_page_); | |
| 208 interstitial_page_ = NULL; | |
| 209 } | |
| 210 | |
| 211 // Returns the currently showing interstitial, NULL if no interstitial is | |
| 212 // showing. | |
| 213 InterstitialPageImpl* interstitial_page() const { return interstitial_page_; } | |
| 214 | |
| 215 // RenderViewHostDelegate::RendererManagement implementation. | |
| 216 virtual void ShouldClosePage( | |
| 217 bool for_cross_site_transition, | |
| 218 bool proceed, | |
| 219 const base::TimeTicks& proceed_time) OVERRIDE; | |
| 220 virtual void OnCrossSiteResponse( | |
| 221 RenderViewHost* pending_render_view_host, | |
| 222 const GlobalRequestID& global_request_id, | |
| 223 bool is_transfer, | |
| 224 const GURL& transfer_url, | |
| 225 const Referrer& referrer, | |
| 226 int64 frame_id) OVERRIDE; | |
| 227 | |
| 228 // NotificationObserver implementation. | |
| 229 virtual void Observe(int type, | |
| 230 const NotificationSource& source, | |
| 231 const NotificationDetails& details) OVERRIDE; | |
| 232 | |
| 233 // Called when a RenderViewHost is about to be deleted. | |
| 234 void RenderViewDeleted(RenderViewHost* rvh); | |
| 235 | |
| 236 // Returns whether the given RenderViewHost is on the list of swapped out | |
| 237 // RenderViewHosts. | |
| 238 bool IsOnSwappedOutList(RenderViewHost* rvh) const; | |
| 239 | |
| 240 // Returns the swapped out RenderViewHost for the given SiteInstance, if any. | |
| 241 RenderViewHostImpl* GetSwappedOutRenderViewHost(SiteInstance* instance); | |
| 242 | |
| 243 // Runs the unload handler in the current page, when we know that a pending | |
| 244 // cross-process navigation is going to commit. We may initiate a transfer | |
| 245 // to a new process after this completes or times out. | |
| 246 void SwapOutOldPage(); | |
| 247 | |
| 248 private: | |
| 249 friend class RenderViewHostManagerTest; | |
| 250 friend class TestWebContents; | |
| 251 | |
| 252 // Tracks information about a navigation while a cross-process transition is | |
| 253 // in progress, in case we need to transfer it to a new RenderViewHost. | |
| 254 struct PendingNavigationParams { | |
| 255 PendingNavigationParams(); | |
| 256 PendingNavigationParams(const GlobalRequestID& global_request_id, | |
| 257 bool is_transfer, | |
| 258 const GURL& transfer_url, | |
| 259 Referrer referrer, | |
| 260 int64 frame_id); | |
| 261 | |
| 262 // The child ID and request ID for the pending navigation. Present whether | |
| 263 // |is_transfer| is true or false. | |
| 264 GlobalRequestID global_request_id; | |
| 265 | |
| 266 // Whether this pending navigation needs to be transferred to another | |
| 267 // process than the one it was going to commit in. If so, the | |
| 268 // |transfer_url|, |referrer|, and |frame_id| parameters will be set. | |
| 269 bool is_transfer; | |
| 270 | |
| 271 // If |is_transfer|, this is the destination URL to request in the new | |
| 272 // process. | |
| 273 GURL transfer_url; | |
| 274 | |
| 275 // If |is_transfer|, this is the referrer to use for the request in the new | |
| 276 // process. | |
| 277 Referrer referrer; | |
| 278 | |
| 279 // If |is_transfer|, this is the frame ID to use in RequestTransferURL. | |
| 280 int64 frame_id; | |
| 281 }; | |
| 282 | |
| 283 // Returns whether this tab should transition to a new renderer for | |
| 284 // cross-site URLs. Enabled unless we see the --process-per-tab command line | |
| 285 // switch. Can be overridden in unit tests. | |
| 286 bool ShouldTransitionCrossSite(); | |
| 287 | |
| 288 // Returns true if the two navigation entries are incompatible in some way | |
| 289 // other than site instances. Cases where this can happen include Web UI | |
| 290 // to regular web pages. It will cause us to swap RenderViewHosts (and hence | |
| 291 // RenderProcessHosts) even if the site instance would otherwise be the same. | |
| 292 // As part of this, we'll also force new SiteInstances and BrowsingInstances. | |
| 293 // Either of the entries may be NULL. | |
| 294 bool ShouldSwapProcessesForNavigation( | |
| 295 const NavigationEntry* curr_entry, | |
| 296 const NavigationEntryImpl* new_entry) const; | |
| 297 | |
| 298 bool ShouldReuseWebUI( | |
| 299 const NavigationEntry* curr_entry, | |
| 300 const NavigationEntryImpl* new_entry) const; | |
| 301 | |
| 302 // Returns an appropriate SiteInstance object for the given NavigationEntry, | |
| 303 // possibly reusing the current SiteInstance. | |
| 304 // Never called if --process-per-tab is used. | |
| 305 SiteInstance* GetSiteInstanceForEntry( | |
| 306 const NavigationEntryImpl& entry, | |
| 307 SiteInstance* curr_instance); | |
| 308 | |
| 309 // Sets up the necessary state for a new RenderViewHost with the given opener. | |
| 310 bool InitRenderView(RenderViewHost* render_view_host, int opener_route_id); | |
| 311 | |
| 312 // Sets the pending RenderViewHost/WebUI to be the active one. Note that this | |
| 313 // doesn't require the pending render_view_host_ pointer to be non-NULL, since | |
| 314 // there could be Web UI switching as well. Call this for every commit. | |
| 315 void CommitPending(); | |
| 316 | |
| 317 // Shutdown all RenderViewHosts in a SiteInstance. This is called | |
| 318 // to shutdown views when all the views in a SiteInstance are | |
| 319 // confirmed to be swapped out. | |
| 320 void ShutdownRenderViewHostsInSiteInstance(int32 site_instance_id); | |
| 321 | |
| 322 // Helper method to terminate the pending RenderViewHost. | |
| 323 void CancelPending(); | |
| 324 | |
| 325 RenderViewHostImpl* UpdateRendererStateForNavigate( | |
| 326 const NavigationEntryImpl& entry); | |
| 327 | |
| 328 // Called when a renderer process is starting to close. We should not | |
| 329 // schedule new navigations in its swapped out RenderViewHosts after this. | |
| 330 void RendererProcessClosing(RenderProcessHost* render_process_host); | |
| 331 | |
| 332 // Our delegate, not owned by us. Guaranteed non-NULL. | |
| 333 Delegate* delegate_; | |
| 334 | |
| 335 // Whether a navigation requiring different RenderView's is pending. This is | |
| 336 // either cross-site request is (in the new process model), or when required | |
| 337 // for the view type (like view source versus not). | |
| 338 bool cross_navigation_pending_; | |
| 339 | |
| 340 // Implemented by the owner of this class, these delegates are installed into | |
| 341 // all the RenderViewHosts that we create. | |
| 342 RenderViewHostDelegate* render_view_delegate_; | |
| 343 RenderWidgetHostDelegate* render_widget_delegate_; | |
| 344 | |
| 345 // Our RenderView host and its associated Web UI (if any, will be NULL for | |
| 346 // non-DOM-UI pages). This object is responsible for all communication with | |
| 347 // a child RenderView instance. | |
| 348 RenderViewHostImpl* render_view_host_; | |
| 349 scoped_ptr<WebUIImpl> web_ui_; | |
| 350 | |
| 351 // A RenderViewHost used to load a cross-site page. This remains hidden | |
| 352 // while a cross-site request is pending until it calls DidNavigate. It may | |
| 353 // have an associated Web UI, in which case the Web UI pointer will be non- | |
| 354 // NULL. | |
| 355 // | |
| 356 // The |pending_web_ui_| may be non-NULL even when the | |
| 357 // |pending_render_view_host_| is NULL. This will happen when we're | |
| 358 // transitioning between two Web UI pages: the RVH won't be swapped, so the | |
| 359 // pending pointer will be unused, but there will be a pending Web UI | |
| 360 // associated with the navigation. | |
| 361 RenderViewHostImpl* pending_render_view_host_; | |
| 362 | |
| 363 // Tracks information about any current pending cross-process navigation. | |
| 364 scoped_ptr<PendingNavigationParams> pending_nav_params_; | |
| 365 | |
| 366 // If either of these is non-NULL, the pending navigation is to a chrome: | |
| 367 // page. The scoped_ptr is used if pending_web_ui_ != web_ui_, the WeakPtr is | |
| 368 // used for when they reference the same object. If either is non-NULL, the | |
| 369 // other should be NULL. | |
| 370 scoped_ptr<WebUIImpl> pending_web_ui_; | |
| 371 base::WeakPtr<WebUIImpl> pending_and_current_web_ui_; | |
| 372 | |
| 373 // A map of site instance ID to swapped out RenderViewHosts. This may include | |
| 374 // pending_render_view_host_ for navigations to existing entries. | |
| 375 typedef base::hash_map<int32, RenderViewHostImpl*> RenderViewHostMap; | |
| 376 RenderViewHostMap swapped_out_hosts_; | |
| 377 | |
| 378 // The intersitial page currently shown if any, not own by this class | |
| 379 // (the InterstitialPage is self-owned, it deletes itself when hidden). | |
| 380 InterstitialPageImpl* interstitial_page_; | |
| 381 | |
| 382 NotificationRegistrar registrar_; | |
| 383 | |
| 384 DISALLOW_COPY_AND_ASSIGN(RenderViewHostManager); | |
| 385 }; | |
| 386 | |
| 387 } // namespace content | |
| 388 | |
| 389 #endif // CONTENT_BROWSER_WEB_CONTENTS_RENDER_VIEW_HOST_MANAGER_H_ | |
| OLD | NEW |