render_frame_host_impl.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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_IMPL_H_
6#define CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_IMPL_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/callback.h"
13#include "base/compiler_specific.h"
14#include "base/strings/string16.h"
15#include "base/time/time.h"
16#include "content/common/content_export.h"
17#include "content/public/browser/render_frame_host.h"
18#include "content/public/common/page_transition_types.h"
19
20class GURL;
21struct FrameHostMsg_DidFailProvisionalLoadWithError_Params;
22struct FrameHostMsg_OpenURL_Params;
23struct FrameMsg_Navigate_Params;
24
25namespace base {
26class FilePath;
27class ListValue;
28}
29
30namespace content {
31
32class CrossProcessFrameConnector;
33class CrossSiteTransferringRequest;
34class FrameTree;
35class FrameTreeNode;
36class RenderFrameHostDelegate;
37class RenderProcessHost;
38class RenderViewHostImpl;
39struct ContextMenuParams;
40struct GlobalRequestID;
41struct Referrer;
42
43class CONTENT_EXPORT RenderFrameHostImpl : public RenderFrameHost {
44 public:
45  static RenderFrameHostImpl* FromID(int process_id, int routing_id);
46
47  virtual ~RenderFrameHostImpl();
48
49  // RenderFrameHost
50  virtual int GetRoutingID() OVERRIDE;
51  virtual SiteInstance* GetSiteInstance() OVERRIDE;
52  virtual RenderProcessHost* GetProcess() OVERRIDE;
53  virtual RenderFrameHost* GetParent() OVERRIDE;
54  virtual bool IsCrossProcessSubframe() OVERRIDE;
55  virtual GURL GetLastCommittedURL() OVERRIDE;
56  virtual gfx::NativeView GetNativeView() OVERRIDE;
57  virtual void DispatchBeforeUnload(bool for_cross_site_transition) OVERRIDE;
58  virtual void NotifyContextMenuClosed(
59      const CustomContextMenuContext& context) OVERRIDE;
60  virtual void ExecuteCustomContextMenuCommand(
61      int action, const CustomContextMenuContext& context) OVERRIDE;
62  virtual void Cut() OVERRIDE;
63  virtual void Copy() OVERRIDE;
64  virtual void Paste() OVERRIDE;
65  virtual void InsertCSS(const std::string& css) OVERRIDE;
66  virtual void ExecuteJavaScript(
67      const base::string16& javascript) OVERRIDE;
68  virtual void ExecuteJavaScript(
69      const base::string16& javascript,
70      const JavaScriptResultCallback& callback) OVERRIDE;
71  virtual RenderViewHost* GetRenderViewHost() OVERRIDE;
72
73  // IPC::Sender
74  virtual bool Send(IPC::Message* msg) OVERRIDE;
75
76  // IPC::Listener
77  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
78
79  void Init();
80  int routing_id() const { return routing_id_; }
81  void OnCreateChildFrame(int new_routing_id,
82                          const std::string& frame_name);
83
84  RenderViewHostImpl* render_view_host() { return render_view_host_; }
85  RenderFrameHostDelegate* delegate() { return delegate_; }
86  FrameTreeNode* frame_tree_node() { return frame_tree_node_; }
87
88  // This function is called when this is a swapped out RenderFrameHost that
89  // lives in the same process as the parent frame. The
90  // |cross_process_frame_connector| allows the non-swapped-out
91  // RenderFrameHost for a frame to communicate with the parent process
92  // so that it may composite drawing data.
93  //
94  // Ownership is not transfered.
95  void set_cross_process_frame_connector(
96      CrossProcessFrameConnector* cross_process_frame_connector) {
97    cross_process_frame_connector_ = cross_process_frame_connector;
98  }
99
100  // Returns a bitwise OR of bindings types that have been enabled for this
101  // RenderFrameHostImpl's RenderView. See BindingsPolicy for details.
102  // TODO(creis): Make bindings frame-specific, to support cases like <webview>.
103  int GetEnabledBindings();
104
105  // Called on the pending RenderFrameHost when the network response is ready to
106  // commit.  We should ensure that the old RenderFrameHost runs its unload
107  // handler and determine whether a transfer to a different RenderFrameHost is
108  // needed.
109  void OnCrossSiteResponse(
110      const GlobalRequestID& global_request_id,
111      scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request,
112      const std::vector<GURL>& transfer_url_chain,
113      const Referrer& referrer,
114      PageTransition page_transition,
115      bool should_replace_current_entry);
116
117  // Hack to get this subframe to swap out, without yet moving over all the
118  // SwapOut state and machinery from RenderViewHost.
119  void SwapOut();
120  void OnSwappedOut(bool timed_out);
121  bool is_swapped_out() { return is_swapped_out_; }
122  void set_swapped_out(bool is_swapped_out) {
123    is_swapped_out_ = is_swapped_out;
124  }
125
126  // Sets the RVH for |this| as pending shutdown. |on_swap_out| will be called
127  // when the SwapOutACK is received.
128  void SetPendingShutdown(const base::Closure& on_swap_out);
129
130  // TODO(nasko): This method is public so RenderViewHostImpl::Navigate can
131  // call it directly. It should be made private once Navigate moves here.
132  void OnDidStartLoading();
133
134  // Sends the given navigation message. Use this rather than sending it
135  // yourself since this does the internal bookkeeping described below. This
136  // function takes ownership of the provided message pointer.
137  //
138  // If a cross-site request is in progress, we may be suspended while waiting
139  // for the onbeforeunload handler, so this function might buffer the message
140  // rather than sending it.
141  void Navigate(const FrameMsg_Navigate_Params& params);
142
143  // Load the specified URL; this is a shortcut for Navigate().
144  void NavigateToURL(const GURL& url);
145
146 protected:
147  friend class RenderFrameHostFactory;
148
149  // TODO(nasko): Remove dependency on RenderViewHost here. RenderProcessHost
150  // should be the abstraction needed here, but we need RenderViewHost to pass
151  // into WebContentsObserver::FrameDetached for now.
152  RenderFrameHostImpl(RenderViewHostImpl* render_view_host,
153                      RenderFrameHostDelegate* delegate,
154                      FrameTree* frame_tree,
155                      FrameTreeNode* frame_tree_node,
156                      int routing_id,
157                      bool is_swapped_out);
158
159 private:
160  friend class TestRenderFrameHost;
161  friend class TestRenderViewHost;
162
163  // IPC Message handlers.
164  void OnDetach();
165  void OnFrameFocused();
166  void OnOpenURL(const FrameHostMsg_OpenURL_Params& params);
167  void OnDidStartProvisionalLoadForFrame(int parent_routing_id,
168                                         bool main_frame,
169                                         const GURL& url);
170  void OnDidFailProvisionalLoadWithError(
171      const FrameHostMsg_DidFailProvisionalLoadWithError_Params& params);
172  void OnDidFailLoadWithError(
173      const GURL& url,
174      bool is_main_frame,
175      int error_code,
176      const base::string16& error_description);
177  void OnDidRedirectProvisionalLoad(int32 page_id,
178                                    const GURL& source_url,
179                                    const GURL& target_url);
180  void OnNavigate(const IPC::Message& msg);
181  void OnDidStopLoading();
182  void OnBeforeUnloadACK(
183      bool proceed,
184      const base::TimeTicks& renderer_before_unload_start_time,
185      const base::TimeTicks& renderer_before_unload_end_time);
186  void OnSwapOutACK();
187  void OnContextMenu(const ContextMenuParams& params);
188  void OnJavaScriptExecuteResponse(int id, const base::ListValue& result);
189
190  // Returns whether the given URL is allowed to commit in the current process.
191  // This is a more conservative check than RenderProcessHost::FilterURL, since
192  // it will be used to kill processes that commit unauthorized URLs.
193  bool CanCommitURL(const GURL& url);
194
195  // For now, RenderFrameHosts indirectly keep RenderViewHosts alive via a
196  // refcount that calls Shutdown when it reaches zero.  This allows each
197  // RenderFrameHostManager to just care about RenderFrameHosts, while ensuring
198  // we have a RenderViewHost for each RenderFrameHost.
199  // TODO(creis): RenderViewHost will eventually go away and be replaced with
200  // some form of page context.
201  RenderViewHostImpl* render_view_host_;
202
203  RenderFrameHostDelegate* delegate_;
204
205  // |cross_process_frame_connector_| passes messages from an out-of-process
206  // child frame to the parent process for compositing.
207  //
208  // This is only non-NULL when this is the swapped out RenderFrameHost in
209  // the same site instance as this frame's parent.
210  //
211  // See the class comment above CrossProcessFrameConnector for more
212  // information.
213  //
214  // This will move to RenderFrameProxyHost when that class is created.
215  CrossProcessFrameConnector* cross_process_frame_connector_;
216
217  // Reference to the whole frame tree that this RenderFrameHost belongs to.
218  // Allows this RenderFrameHost to add and remove nodes in response to
219  // messages from the renderer requesting DOM manipulation.
220  FrameTree* frame_tree_;
221
222  // The FrameTreeNode which this RenderFrameHostImpl is hosted in.
223  FrameTreeNode* frame_tree_node_;
224
225  // The mapping of pending JavaScript calls created by
226  // ExecuteJavaScript and their corresponding callbacks.
227  std::map<int, JavaScriptResultCallback> javascript_callbacks_;
228
229  int routing_id_;
230  bool is_swapped_out_;
231
232  // When the last BeforeUnload message was sent.
233  base::TimeTicks send_before_unload_start_time_;
234
235  DISALLOW_COPY_AND_ASSIGN(RenderFrameHostImpl);
236};
237
238}  // namespace content
239
240#endif  // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_IMPL_H_
241