1a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// found in the LICENSE file.
4a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
5a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#ifndef CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_PROXY_HOST_H_
6a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#define CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_PROXY_HOST_H_
7a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
8a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "base/memory/scoped_ptr.h"
9a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "content/browser/frame_host/render_frame_host_impl.h"
10a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "content/browser/site_instance_impl.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "ipc/ipc_listener.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "ipc/ipc_sender.h"
13a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)namespace content {
156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)class CrossProcessFrameConnector;
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class FrameTreeNode;
18a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochclass RenderProcessHost;
19a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochclass RenderFrameHostImpl;
20a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochclass RenderViewHostImpl;
216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)class RenderWidgetHostView;
22a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
23a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// When a page's frames are rendered by multiple processes, each renderer has a
24a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// full copy of the frame tree. It has full RenderFrames for the frames it is
25a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// responsible for rendering and placeholder objects (i.e., RenderFrameProxies)
26a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// for frames rendered by other processes.
27a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch//
28a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// This class is the browser-side host object for the placeholder. Each node in
29a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// the frame tree has a RenderFrameHost for the active SiteInstance and a set
30a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// of RenderFrameProxyHost objects - one for all other SiteInstances with
31a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// references to this frame. The proxies allow us to keep existing window
32a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// references valid over cross-process navigations and route cross-site
33a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// asynchronous JavaScript calls, such as postMessage.
34a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch//
35a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// For now, RenderFrameProxyHost is created when a RenderFrameHost is swapped
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// out and acts just as a wrapper. It is destroyed when the RenderFrameHost is
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// swapped back in or is no longer referenced and is therefore deleted.
38a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch//
39a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Long term, RenderFrameProxyHost will be created whenever a cross-site
40a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// navigation occurs and a reference to the frame navigating needs to be kept
41a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// alive. A RenderFrameProxyHost and a RenderFrameHost for the same SiteInstance
42a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// can exist at the same time, but only one will be "active" at a time.
43a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// There are two cases where the two objects will coexist:
44a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// * When navigating cross-process and there is already a RenderFrameProxyHost
45a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// for the new SiteInstance. A pending RenderFrameHost is created, but it is
46a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// not used until it commits. At that point, RenderFrameHostManager transitions
47a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// the pending RenderFrameHost to the active one and deletes the proxy.
48a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// * When navigating cross-process and the existing document has an unload
49a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// event handler. When the new navigation commits, RenderFrameHostManager
50a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// creates a RenderFrameProxyHost for the old SiteInstance and uses it going
51a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// forward. It also instructs the RenderFrameHost to run the unload event
52a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// handler and is kept alive for the duration. Once the event handling is
53a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// complete, the RenderFrameHost is deleted.
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class RenderFrameProxyHost
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : public IPC::Listener,
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      public IPC::Sender {
57a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch public:
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  static RenderFrameProxyHost* FromID(int process_id, int routing_id);
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  RenderFrameProxyHost(SiteInstance* site_instance,
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       FrameTreeNode* frame_tree_node);
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~RenderFrameProxyHost();
63a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
64a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  RenderProcessHost* GetProcess() {
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return site_instance_->GetProcess();
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Initializes the object and creates the RenderFrameProxy in the process
695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // for the SiteInstance.
705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  bool InitRenderFrameProxy();
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int GetRoutingID() {
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return routing_id_;
74a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
75a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
76a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  SiteInstance* GetSiteInstance() {
77a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    return site_instance_.get();
78a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
79a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  FrameTreeNode* frame_tree_node() const { return frame_tree_node_; };
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  void SetChildRWHView(RenderWidgetHostView* view);
836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // TODO(nasko): The following methods should be removed once we don't have a
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // swapped out state on RenderFrameHosts. See https://crbug.com/357747.
86a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  RenderFrameHostImpl* render_frame_host() {
87a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    return render_frame_host_.get();
88a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  RenderViewHostImpl* GetRenderViewHost();
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void TakeFrameHostOwnership(
92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<RenderFrameHostImpl> render_frame_host) {
93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    render_frame_host_ = render_frame_host.Pass();
94a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<RenderFrameHostImpl> PassFrameHostOwnership();
96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // IPC::Sender
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual bool Send(IPC::Message* msg) OVERRIDE;
99a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // IPC::Listener
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1036d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  CrossProcessFrameConnector* cross_process_frame_connector() {
1046d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    return cross_process_frame_connector_.get();
1056d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  }
1066d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Set the frame's opener to null in the renderer process in response to an
1081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // action in another renderer process.
1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void DisownOpener();
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) private:
112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // This RenderFrameProxyHost's routing id.
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int routing_id_;
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The SiteInstance this proxy is associated with.
116a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  scoped_refptr<SiteInstance> site_instance_;
117a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The node in the frame tree where this proxy is located.
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  FrameTreeNode* frame_tree_node_;
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // When a RenderFrameHost is in a different process from its parent in the
1226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // frame tree, this class connects its associated RenderWidgetHostView
1236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // to this RenderFrameProxyHost, which corresponds to the same frame in the
1246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // parent's renderer process.
1256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  scoped_ptr<CrossProcessFrameConnector> cross_process_frame_connector_;
1266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // TODO(nasko): This can be removed once we don't have a swapped out state on
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // RenderFrameHosts. See https://crbug.com/357747.
129a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  scoped_ptr<RenderFrameHostImpl> render_frame_host_;
130a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
131a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  DISALLOW_COPY_AND_ASSIGN(RenderFrameProxyHost);
132a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch};
133a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
134a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}  // namespace
135a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
136a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#endif  // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_PROXY_HOST_H_
137