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