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