render_frame_host_impl.h revision 010d83a9304c5a91596085d917d248abff47903a
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/memory/weak_ptr.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/javascript_message_type.h"
19#include "content/public/common/page_transition_types.h"
20
21class GURL;
22struct FrameHostMsg_DidFailProvisionalLoadWithError_Params;
23struct FrameHostMsg_OpenURL_Params;
24struct FrameMsg_Navigate_Params;
25
26namespace base {
27class FilePath;
28class ListValue;
29}
30
31namespace content {
32
33class CrossProcessFrameConnector;
34class CrossSiteTransferringRequest;
35class FrameTree;
36class FrameTreeNode;
37class RenderFrameHostDelegate;
38class RenderProcessHost;
39class RenderViewHostImpl;
40struct ContextMenuParams;
41struct GlobalRequestID;
42struct Referrer;
43struct ShowDesktopNotificationHostMsgParams;
44
45class CONTENT_EXPORT RenderFrameHostImpl : public RenderFrameHost {
46 public:
47  static RenderFrameHostImpl* FromID(int process_id, int routing_id);
48
49  virtual ~RenderFrameHostImpl();
50
51  // RenderFrameHost
52  virtual int GetRoutingID() OVERRIDE;
53  virtual SiteInstance* GetSiteInstance() OVERRIDE;
54  virtual RenderProcessHost* GetProcess() OVERRIDE;
55  virtual RenderFrameHost* GetParent() OVERRIDE;
56  virtual const std::string& GetFrameName() OVERRIDE;
57  virtual bool IsCrossProcessSubframe() OVERRIDE;
58  virtual GURL GetLastCommittedURL() OVERRIDE;
59  virtual gfx::NativeView GetNativeView() OVERRIDE;
60  virtual void ExecuteJavaScript(
61      const base::string16& javascript) OVERRIDE;
62  virtual void ExecuteJavaScript(
63      const base::string16& javascript,
64      const JavaScriptResultCallback& callback) OVERRIDE;
65  virtual RenderViewHost* GetRenderViewHost() OVERRIDE;
66
67  // IPC::Sender
68  virtual bool Send(IPC::Message* msg) OVERRIDE;
69
70  // IPC::Listener
71  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
72
73  void Init();
74  int routing_id() const { return routing_id_; }
75  void OnCreateChildFrame(int new_routing_id,
76                          const std::string& frame_name);
77
78  RenderViewHostImpl* render_view_host() { return render_view_host_; }
79  RenderFrameHostDelegate* delegate() { return delegate_; }
80  FrameTreeNode* frame_tree_node() { return frame_tree_node_; }
81
82  // This function is called when this is a swapped out RenderFrameHost that
83  // lives in the same process as the parent frame. The
84  // |cross_process_frame_connector| allows the non-swapped-out
85  // RenderFrameHost for a frame to communicate with the parent process
86  // so that it may composite drawing data.
87  //
88  // Ownership is not transfered.
89  void set_cross_process_frame_connector(
90      CrossProcessFrameConnector* cross_process_frame_connector) {
91    cross_process_frame_connector_ = cross_process_frame_connector;
92  }
93
94  // Returns a bitwise OR of bindings types that have been enabled for this
95  // RenderFrameHostImpl's RenderView. See BindingsPolicy for details.
96  // TODO(creis): Make bindings frame-specific, to support cases like <webview>.
97  int GetEnabledBindings();
98
99  // Called on the pending RenderFrameHost when the network response is ready to
100  // commit.  We should ensure that the old RenderFrameHost runs its unload
101  // handler and determine whether a transfer to a different RenderFrameHost is
102  // needed.
103  void OnCrossSiteResponse(
104      const GlobalRequestID& global_request_id,
105      scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request,
106      const std::vector<GURL>& transfer_url_chain,
107      const Referrer& referrer,
108      PageTransition page_transition,
109      bool should_replace_current_entry);
110
111  // Tells the renderer that this RenderFrame is being swapped out for one in a
112  // different renderer process.  It should run its unload handler and move to
113  // a blank document.  The renderer should preserve the Frame object until it
114  // exits, in case we come back.  The renderer can exit if it has no other
115  // active RenderFrames, but not until WasSwappedOut is called (when it is no
116  // longer visible).
117  void SwapOut();
118
119  void OnSwappedOut(bool timed_out);
120  bool is_swapped_out() { return is_swapped_out_; }
121  void set_swapped_out(bool is_swapped_out) {
122    is_swapped_out_ = is_swapped_out;
123  }
124
125  // Sets the RVH for |this| as pending shutdown. |on_swap_out| will be called
126  // when the SwapOutACK is received.
127  void SetPendingShutdown(const base::Closure& on_swap_out);
128
129  // TODO(nasko): This method is public so RenderViewHostImpl::Navigate can
130  // call it directly. It should be made private once Navigate moves here.
131  // |to_different_document| will be true unless the load is a fragment
132  // navigation, or triggered by history.pushState/replaceState.
133  void OnDidStartLoading(bool to_different_document);
134
135  // Sends the given navigation message. Use this rather than sending it
136  // yourself since this does the internal bookkeeping described below. This
137  // function takes ownership of the provided message pointer.
138  //
139  // If a cross-site request is in progress, we may be suspended while waiting
140  // for the onbeforeunload handler, so this function might buffer the message
141  // rather than sending it.
142  void Navigate(const FrameMsg_Navigate_Params& params);
143
144  // Load the specified URL; this is a shortcut for Navigate().
145  void NavigateToURL(const GURL& url);
146
147  // Runs the beforeunload handler for this frame. |for_cross_site_transition|
148  // indicates whether this call is for the current frame during a cross-process
149  // navigation. False means we're closing the entire tab.
150  void DispatchBeforeUnload(bool for_cross_site_transition);
151
152  // Deletes the current selection plus the specified number of characters
153  // before and after the selection or caret.
154  void ExtendSelectionAndDelete(size_t before, size_t after);
155
156  // Notifies the RenderFrame that the JavaScript message that was shown was
157  // closed by the user.
158  void JavaScriptDialogClosed(IPC::Message* reply_msg,
159                              bool success,
160                              const base::string16& user_input,
161                              bool dialog_was_suppressed);
162
163  // Called when an HTML5 notification is closed.
164  void NotificationClosed(int notification_id);
165
166 protected:
167  friend class RenderFrameHostFactory;
168
169  // TODO(nasko): Remove dependency on RenderViewHost here. RenderProcessHost
170  // should be the abstraction needed here, but we need RenderViewHost to pass
171  // into WebContentsObserver::FrameDetached for now.
172  RenderFrameHostImpl(RenderViewHostImpl* render_view_host,
173                      RenderFrameHostDelegate* delegate,
174                      FrameTree* frame_tree,
175                      FrameTreeNode* frame_tree_node,
176                      int routing_id,
177                      bool is_swapped_out);
178
179 private:
180  friend class TestRenderFrameHost;
181  friend class TestRenderViewHost;
182
183  // IPC Message handlers.
184  void OnAddMessageToConsole(int32 level,
185                             const base::string16& message,
186                             int32 line_no,
187                             const base::string16& source_id);
188  void OnDetach();
189  void OnFrameFocused();
190  void OnOpenURL(const FrameHostMsg_OpenURL_Params& params);
191  void OnDocumentOnLoadCompleted();
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  void OnRunJavaScriptMessage(const base::string16& message,
213                              const base::string16& default_prompt,
214                              const GURL& frame_url,
215                              JavaScriptMessageType type,
216                              IPC::Message* reply_msg);
217  void OnRunBeforeUnloadConfirm(const GURL& frame_url,
218                                const base::string16& message,
219                                bool is_reload,
220                                IPC::Message* reply_msg);
221  void OnRequestDesktopNotificationPermission(const GURL& origin,
222                                              int callback_id);
223  void OnShowDesktopNotification(
224      int notification_id,
225      const ShowDesktopNotificationHostMsgParams& params);
226  void OnCancelDesktopNotification(int notification_id);
227  void OnDidAccessInitialDocument();
228  void OnDidDisownOpener();
229
230  // Returns whether the given URL is allowed to commit in the current process.
231  // This is a more conservative check than RenderProcessHost::FilterURL, since
232  // it will be used to kill processes that commit unauthorized URLs.
233  bool CanCommitURL(const GURL& url);
234
235  void DesktopNotificationPermissionRequestDone(int callback_context);
236
237  // For now, RenderFrameHosts indirectly keep RenderViewHosts alive via a
238  // refcount that calls Shutdown when it reaches zero.  This allows each
239  // RenderFrameHostManager to just care about RenderFrameHosts, while ensuring
240  // we have a RenderViewHost for each RenderFrameHost.
241  // TODO(creis): RenderViewHost will eventually go away and be replaced with
242  // some form of page context.
243  RenderViewHostImpl* render_view_host_;
244
245  RenderFrameHostDelegate* delegate_;
246
247  // |cross_process_frame_connector_| passes messages from an out-of-process
248  // child frame to the parent process for compositing.
249  //
250  // This is only non-NULL when this is the swapped out RenderFrameHost in
251  // the same site instance as this frame's parent.
252  //
253  // See the class comment above CrossProcessFrameConnector for more
254  // information.
255  //
256  // This will move to RenderFrameProxyHost when that class is created.
257  CrossProcessFrameConnector* cross_process_frame_connector_;
258
259  // Reference to the whole frame tree that this RenderFrameHost belongs to.
260  // Allows this RenderFrameHost to add and remove nodes in response to
261  // messages from the renderer requesting DOM manipulation.
262  FrameTree* frame_tree_;
263
264  // The FrameTreeNode which this RenderFrameHostImpl is hosted in.
265  FrameTreeNode* frame_tree_node_;
266
267  // The mapping of pending JavaScript calls created by
268  // ExecuteJavaScript and their corresponding callbacks.
269  std::map<int, JavaScriptResultCallback> javascript_callbacks_;
270
271  // Map from notification_id to a callback to cancel them.
272  std::map<int, base::Closure> cancel_notification_callbacks_;
273
274  int routing_id_;
275  bool is_swapped_out_;
276
277  // When the last BeforeUnload message was sent.
278  base::TimeTicks send_before_unload_start_time_;
279
280  base::WeakPtrFactory<RenderFrameHostImpl> weak_ptr_factory_;
281
282  DISALLOW_COPY_AND_ASSIGN(RenderFrameHostImpl);
283};
284
285}  // namespace content
286
287#endif  // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_IMPL_H_
288