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