IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / frame_host / render_frame_host_manager.h
blob6e787f5955d30b302c1d00330d59fc1380a6c436
1 // Copyright 2013 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.
5 #ifndef CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_MANAGER_H_
6 #define CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_MANAGER_H_
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"
20 namespace content {
21 class BrowserContext;
22 class InterstitialPageImpl;
23 class FrameTreeNode;
24 class NavigationControllerImpl;
25 class NavigationEntry;
26 class NavigationEntryImpl;
27 class RenderFrameHostDelegate;
28 class RenderFrameHostImpl;
29 class RenderFrameHostManagerTest;
30 class RenderViewHost;
31 class RenderViewHostImpl;
32 class RenderWidgetHostDelegate;
33 class RenderWidgetHostView;
34 class TestWebContents;
35 class WebUIImpl;
37 // Manages RenderFrameHosts for a FrameTreeNode. This class acts as a state
38 // machine to make cross-process navigations in a frame possible.
39 class CONTENT_EXPORT RenderFrameHostManager
40 : public RenderViewHostDelegate::RendererManagement,
41 public NotificationObserver {
42 public:
43 // Functions implemented by our owner that we need.
45 // TODO(brettw) Clean this up! These are all the functions in WebContentsImpl
46 // that are required to run this class. The design should probably be better
47 // such that these are more clear.
49 // There is additional complexity that some of the functions we need in
50 // WebContentsImpl are inherited and non-virtual. These are named with
51 // "RenderManager" so that the duplicate implementation of them will be clear.
52 class CONTENT_EXPORT Delegate {
53 public:
54 // Initializes the given renderer if necessary and creates the view ID
55 // corresponding to this view host. If this method is not called and the
56 // process is not shared, then the WebContentsImpl will act as though the
57 // renderer is not running (i.e., it will render "sad tab"). This method is
58 // automatically called from LoadURL.
60 // If you are attaching to an already-existing RenderView, you should call
61 // InitWithExistingID.
62 virtual bool CreateRenderViewForRenderManager(
63 RenderViewHost* render_view_host, int opener_route_id) = 0;
64 virtual void BeforeUnloadFiredFromRenderManager(
65 bool proceed, const base::TimeTicks& proceed_time,
66 bool* proceed_to_fire_unload) = 0;
67 virtual void RenderProcessGoneFromRenderManager(
68 RenderViewHost* render_view_host) = 0;
69 virtual void UpdateRenderViewSizeForRenderManager() = 0;
70 virtual void CancelModalDialogsForRenderManager() = 0;
71 virtual void NotifySwappedFromRenderManager(
72 RenderViewHost* old_host, RenderViewHost* new_host) = 0;
73 virtual NavigationControllerImpl&
74 GetControllerForRenderManager() = 0;
76 // Create swapped out RenderViews in the given SiteInstance for each tab in
77 // the opener chain of this tab, if any. This allows the current tab to
78 // make cross-process script calls to its opener(s). Returns the route ID
79 // of the immediate opener, if one exists (otherwise MSG_ROUTING_NONE).
80 virtual int CreateOpenerRenderViewsForRenderManager(
81 SiteInstance* instance) = 0;
83 // Creates a WebUI object for the given URL if one applies. Ownership of the
84 // returned pointer will be passed to the caller. If no WebUI applies,
85 // returns NULL.
86 virtual WebUIImpl* CreateWebUIForRenderManager(const GURL& url) = 0;
88 // Returns the navigation entry of the current navigation, or NULL if there
89 // is none.
90 virtual NavigationEntry*
91 GetLastCommittedNavigationEntryForRenderManager() = 0;
93 // Returns true if the location bar should be focused by default rather than
94 // the page contents. The view calls this function when the tab is focused
95 // to see what it should do.
96 virtual bool FocusLocationBarByDefault() = 0;
98 // Focuses the location bar.
99 virtual void SetFocusToLocationBar(bool select_all) = 0;
101 // Creates a view and sets the size for the specified RVH.
102 virtual void CreateViewAndSetSizeForRVH(RenderViewHost* rvh) = 0;
104 // Returns true if views created for this delegate should be created in a
105 // hidden state.
106 virtual bool IsHidden() = 0;
108 protected:
109 virtual ~Delegate() {}
112 // All three delegate pointers must be non-NULL and are not owned by this
113 // class. They must outlive this class. The RenderViewHostDelegate and
114 // RenderWidgetHostDelegate are what will be installed into all
115 // RenderViewHosts that are created.
117 // You must call Init() before using this class.
118 RenderFrameHostManager(
119 FrameTreeNode* frame_tree_node,
120 RenderFrameHostDelegate* render_frame_delegate,
121 RenderViewHostDelegate* render_view_delegate,
122 RenderWidgetHostDelegate* render_widget_delegate,
123 Delegate* delegate);
124 virtual ~RenderFrameHostManager();
126 // For arguments, see WebContentsImpl constructor.
127 void Init(BrowserContext* browser_context,
128 SiteInstance* site_instance,
129 int view_routing_id,
130 int frame_routing_id);
132 // Returns the currently active RenderFrameHost.
134 // This will be non-NULL between Init() and Shutdown(). You may want to NULL
135 // check it in many cases, however. Windows can send us messages during the
136 // destruction process after it has been shut down.
137 RenderFrameHostImpl* current_frame_host() const {
138 return render_frame_host_;
141 // TODO(creis): Remove this when we no longer use RVH for navigation.
142 RenderViewHostImpl* current_host() const;
144 // Returns the view associated with the current RenderViewHost, or NULL if
145 // there is no current one.
146 RenderWidgetHostView* GetRenderWidgetHostView() const;
148 // Returns the pending RenderFrameHost, or NULL if there is no pending one.
149 RenderFrameHostImpl* pending_frame_host() const {
150 return pending_render_frame_host_;
153 // TODO(creis): Remove this when we no longer use RVH for navigation.
154 RenderViewHostImpl* pending_render_view_host() const;
156 // Returns the current committed Web UI or NULL if none applies.
157 WebUIImpl* web_ui() const { return web_ui_.get(); }
159 // Returns the Web UI for the pending navigation, or NULL of none applies.
160 WebUIImpl* pending_web_ui() const {
161 return pending_web_ui_.get() ? pending_web_ui_.get() :
162 pending_and_current_web_ui_.get();
165 // Sets the pending Web UI for the pending navigation, ensuring that the
166 // bindings are appropriate for the given NavigationEntry.
167 void SetPendingWebUI(const NavigationEntryImpl& entry);
169 // Called when we want to instruct the renderer to navigate to the given
170 // navigation entry. It may create a new RenderViewHost or re-use an existing
171 // one. The RenderViewHost to navigate will be returned. Returns NULL if one
172 // could not be created.
173 RenderViewHostImpl* Navigate(const NavigationEntryImpl& entry);
175 // Instructs the various live views to stop. Called when the user directed the
176 // page to stop loading.
177 void Stop();
179 // Notifies the regular and pending RenderViewHosts that a load is or is not
180 // happening. Even though the message is only for one of them, we don't know
181 // which one so we tell both.
182 void SetIsLoading(bool is_loading);
184 // Whether to close the tab or not when there is a hang during an unload
185 // handler. If we are mid-crosssite navigation, then we should proceed
186 // with the navigation instead of closing the tab.
187 bool ShouldCloseTabOnUnresponsiveRenderer();
189 // The RenderViewHost has been swapped out, so we should resume the pending
190 // network response and allow the pending RenderViewHost to commit.
191 void SwappedOut(RenderViewHost* render_view_host);
193 // The RenderFrameHost has been swapped out, so we should resume the pending
194 // network response and allow the pending RenderFrameHost to commit.
195 void SwappedOutFrame(RenderFrameHostImpl* render_frame_host);
197 // Called when a renderer's main frame navigates.
198 void DidNavigateMainFrame(RenderViewHost* render_view_host);
200 // Called when a renderer sets its opener to null.
201 void DidDisownOpener(RenderViewHost* render_view_host);
203 // Helper method to create and initialize a RenderFrameHost. If |swapped_out|
204 // is true, it will be initially placed on the swapped out hosts list.
205 // Otherwise, it will be used for a pending cross-site navigation.
206 int CreateRenderFrame(SiteInstance* instance,
207 int opener_route_id,
208 bool swapped_out,
209 bool hidden);
211 // Sets the passed passed interstitial as the currently showing interstitial.
212 // |interstitial_page| should be non NULL (use the remove_interstitial_page
213 // method to unset the interstitial) and no interstitial page should be set
214 // when there is already a non NULL interstitial page set.
215 void set_interstitial_page(InterstitialPageImpl* interstitial_page) {
216 DCHECK(!interstitial_page_ && interstitial_page);
217 interstitial_page_ = interstitial_page;
220 // Unsets the currently showing interstitial.
221 void remove_interstitial_page() {
222 DCHECK(interstitial_page_);
223 interstitial_page_ = NULL;
226 // Returns the currently showing interstitial, NULL if no interstitial is
227 // showing.
228 InterstitialPageImpl* interstitial_page() const { return interstitial_page_; }
230 // RenderViewHostDelegate::RendererManagement implementation.
231 virtual void ShouldClosePage(
232 bool for_cross_site_transition,
233 bool proceed,
234 const base::TimeTicks& proceed_time) OVERRIDE;
235 virtual void OnCrossSiteResponse(
236 RenderViewHost* pending_render_view_host,
237 const GlobalRequestID& global_request_id,
238 bool is_transfer,
239 const std::vector<GURL>& transfer_url_chain,
240 const Referrer& referrer,
241 PageTransition page_transition,
242 int64 frame_id,
243 bool should_replace_current_entry) OVERRIDE;
245 // NotificationObserver implementation.
246 virtual void Observe(int type,
247 const NotificationSource& source,
248 const NotificationDetails& details) OVERRIDE;
250 // Called when a RenderViewHost is about to be deleted.
251 void RenderViewDeleted(RenderViewHost* rvh);
253 // Returns whether the given RenderFrameHost (or its associated
254 // RenderViewHost) is on the list of swapped out RenderFrameHosts.
255 bool IsRVHOnSwappedOutList(RenderViewHostImpl* rvh) const;
256 bool IsOnSwappedOutList(RenderFrameHostImpl* rfh) const;
258 // Returns the swapped out RenderViewHost or RenderFrameHost for the given
259 // SiteInstance, if any.
260 RenderViewHostImpl* GetSwappedOutRenderViewHost(SiteInstance* instance) const;
261 RenderFrameHostImpl* GetSwappedOutRenderFrameHost(
262 SiteInstance* instance) const;
264 // Runs the unload handler in the current page, when we know that a pending
265 // cross-process navigation is going to commit. We may initiate a transfer
266 // to a new process after this completes or times out.
267 void SwapOutOldPage();
269 private:
270 friend class RenderFrameHostManagerTest;
271 friend class TestWebContents;
273 // Tracks information about a navigation while a cross-process transition is
274 // in progress, in case we need to transfer it to a new RenderFrameHost.
275 struct PendingNavigationParams {
276 PendingNavigationParams();
277 PendingNavigationParams(const GlobalRequestID& global_request_id,
278 bool is_transfer,
279 const std::vector<GURL>& transfer_url,
280 Referrer referrer,
281 PageTransition page_transition,
282 int64 frame_id,
283 bool should_replace_current_entry);
284 ~PendingNavigationParams();
286 // The child ID and request ID for the pending navigation. Present whether
287 // |is_transfer| is true or false.
288 GlobalRequestID global_request_id;
290 // Whether this pending navigation needs to be transferred to another
291 // process than the one it was going to commit in. If so, the
292 // |transfer_url|, |referrer|, and |frame_id| parameters will be set.
293 bool is_transfer;
295 // If |is_transfer|, this is the URL chain of the request. The first entry
296 // is the original request URL, and the last entry is the destination URL to
297 // request in the new process.
298 std::vector<GURL> transfer_url_chain;
300 // If |is_transfer|, this is the referrer to use for the request in the new
301 // process.
302 Referrer referrer;
304 // If |is_transfer|, this is the transition type for the original
305 // navigation.
306 PageTransition page_transition;
308 // If |is_transfer|, this is the frame ID to use in RequestTransferURL.
309 int64 frame_id;
311 // If |is_transfer|, this is whether the navigation should replace the
312 // current history entry.
313 bool should_replace_current_entry;
316 // Used with FrameTree::ForEach to erase inactive RenderFrameHosts from a
317 // FrameTreeNode's RenderFrameHostManager.
318 static bool ClearSwappedOutRFHsInSiteInstance(int32 site_instance_id,
319 FrameTreeNode* node);
321 // Returns whether this tab should transition to a new renderer for
322 // cross-site URLs. Enabled unless we see the --process-per-tab command line
323 // switch. Can be overridden in unit tests.
324 bool ShouldTransitionCrossSite();
326 // Returns true if for the navigation from |current_entry| to |new_entry|,
327 // a new SiteInstance and BrowsingInstance should be created (even if we are
328 // in a process model that doesn't usually swap). This forces a process swap
329 // and severs script connections with existing tabs. Cases where this can
330 // happen include transitions between WebUI and regular web pages.
331 // Either of the entries may be NULL.
332 bool ShouldSwapBrowsingInstancesForNavigation(
333 const NavigationEntry* current_entry,
334 const NavigationEntryImpl* new_entry) const;
336 // Returns true if it is safe to reuse the current WebUI when navigating from
337 // |current_entry| to |new_entry|.
338 bool ShouldReuseWebUI(
339 const NavigationEntry* current_entry,
340 const NavigationEntryImpl* new_entry) const;
342 // Returns an appropriate SiteInstance object for the given NavigationEntry,
343 // possibly reusing the current SiteInstance. If --process-per-tab is used,
344 // this is only called when ShouldSwapBrowsingInstancesForNavigation returns
345 // true.
346 SiteInstance* GetSiteInstanceForEntry(
347 const NavigationEntryImpl& entry,
348 SiteInstance* current_instance,
349 bool force_browsing_instance_swap);
351 // Creates a RenderFrameHost and corresponding RenderViewHost if necessary.
352 RenderFrameHostImpl* CreateRenderFrameHost(SiteInstance* instance,
353 int view_routing_id,
354 int frame_routing_id,
355 bool swapped_out,
356 bool hidden);
358 // Sets up the necessary state for a new RenderViewHost with the given opener,
359 // if necessary. Returns early if the RenderViewHost has already been
360 // initialized for another RenderFrameHost.
361 // TODO(creis): opener_route_id is currently for the RenderViewHost but should
362 // be for the RenderFrame, since frames can have openers.
363 bool InitRenderView(RenderViewHost* render_view_host, int opener_route_id);
365 // Sets the pending RenderFrameHost/WebUI to be the active one. Note that this
366 // doesn't require the pending render_frame_host_ pointer to be non-NULL,
367 // since there could be Web UI switching as well. Call this for every commit.
368 void CommitPending();
370 // Shutdown all RenderFrameHosts in a SiteInstance. This is called to shutdown
371 // frames when all the frames in a SiteInstance are confirmed to be swapped
372 // out.
373 void ShutdownRenderFrameHostsInSiteInstance(int32 site_instance_id);
375 // Helper method to terminate the pending RenderViewHost.
376 void CancelPending();
378 RenderFrameHostImpl* UpdateRendererStateForNavigate(
379 const NavigationEntryImpl& entry);
381 // Called when a renderer process is starting to close. We should not
382 // schedule new navigations in its swapped out RenderFrameHosts after this.
383 void RendererProcessClosing(RenderProcessHost* render_process_host);
385 // For use in creating RenderFrameHosts.
386 FrameTreeNode* frame_tree_node_;
388 // Our delegate, not owned by us. Guaranteed non-NULL.
389 Delegate* delegate_;
391 // Whether a navigation requiring different RenderFrameHosts is pending. This
392 // is either for cross-site requests or when required for the process type
393 // (like WebUI).
394 bool cross_navigation_pending_;
396 // Implemented by the owner of this class. These delegates are installed into
397 // all the RenderFrameHosts that we create.
398 RenderFrameHostDelegate* render_frame_delegate_;
399 RenderViewHostDelegate* render_view_delegate_;
400 RenderWidgetHostDelegate* render_widget_delegate_;
402 // Our RenderFrameHost and its associated Web UI (if any, will be NULL for
403 // non-WebUI pages). This object is responsible for all communication with
404 // a child RenderFrame instance.
405 // For now, RenderFrameHost keeps a RenderViewHost in its SiteInstance alive.
406 // Eventually, RenderViewHost will be replaced with a page context.
407 RenderFrameHostImpl* render_frame_host_;
408 scoped_ptr<WebUIImpl> web_ui_;
410 // A RenderFrameHost used to load a cross-site page. This remains hidden
411 // while a cross-site request is pending until it calls DidNavigate. It may
412 // have an associated Web UI, in which case the Web UI pointer will be non-
413 // NULL.
415 // The |pending_web_ui_| may be non-NULL even when the
416 // |pending_render_frame_host_| is NULL. This will happen when we're
417 // transitioning between two Web UI pages: the RFH won't be swapped, so the
418 // pending pointer will be unused, but there will be a pending Web UI
419 // associated with the navigation.
420 RenderFrameHostImpl* pending_render_frame_host_;
422 // Tracks information about any current pending cross-process navigation.
423 scoped_ptr<PendingNavigationParams> pending_nav_params_;
425 // If either of these is non-NULL, the pending navigation is to a chrome:
426 // page. The scoped_ptr is used if pending_web_ui_ != web_ui_, the WeakPtr is
427 // used for when they reference the same object. If either is non-NULL, the
428 // other should be NULL.
429 scoped_ptr<WebUIImpl> pending_web_ui_;
430 base::WeakPtr<WebUIImpl> pending_and_current_web_ui_;
432 // A map of site instance ID to swapped out RenderFrameHosts. This may
433 // include pending_render_frame_host_ for navigations to existing entries.
434 typedef base::hash_map<int32, RenderFrameHostImpl*> RenderFrameHostMap;
435 RenderFrameHostMap swapped_out_hosts_;
437 // The intersitial page currently shown if any, not own by this class
438 // (the InterstitialPage is self-owned, it deletes itself when hidden).
439 InterstitialPageImpl* interstitial_page_;
441 NotificationRegistrar registrar_;
443 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostManager);
446 } // namespace content
448 #endif // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_MANAGER_H_