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.
5 #ifndef CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_IMPL_H_
6 #define CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_IMPL_H_
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"
23 struct FrameHostMsg_DidFailProvisionalLoadWithError_Params
;
24 struct FrameHostMsg_OpenURL_Params
;
25 struct FrameMsg_Navigate_Params
;
34 class CrossProcessFrameConnector
;
35 class CrossSiteTransferringRequest
;
38 class RenderFrameHostDelegate
;
39 class RenderFrameProxyHost
;
40 class RenderProcessHost
;
41 class RenderViewHostImpl
;
42 class RenderWidgetHostImpl
;
43 struct ContextMenuParams
;
44 struct GlobalRequestID
;
46 struct ShowDesktopNotificationHostMsgParams
;
48 class CONTENT_EXPORT RenderFrameHostImpl
: public RenderFrameHost
{
50 static RenderFrameHostImpl
* FromID(int process_id
, int routing_id
);
52 virtual ~RenderFrameHostImpl();
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
;
71 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
74 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
77 int routing_id() const { return routing_id_
; }
78 void OnCreateChildFrame(int new_routing_id
,
79 const std::string
& frame_name
);
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();
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.
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
;
100 void set_render_frame_proxy_host(RenderFrameProxyHost
* proxy
) {
101 render_frame_proxy_host_
= proxy
;
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();
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
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
);
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
);
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
;
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
);
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.
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
);
148 // Load the specified URL; this is a shortcut for Navigate().
149 void NavigateToURL(const GURL
& url
);
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
);
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
);
160 // Notifies the RenderFrame that the JavaScript message that was shown was
161 // closed by the user.
162 void JavaScriptDialogClosed(IPC::Message
* reply_msg
,
164 const base::string16
& user_input
,
165 bool dialog_was_suppressed
);
167 // Called when an HTML5 notification is closed.
168 void NotificationClosed(int notification_id
);
171 friend class RenderFrameHostFactory
;
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
,
181 bool is_swapped_out
);
184 friend class TestRenderFrameHost
;
185 friend class TestRenderViewHost
;
187 // IPC Message handlers.
188 void OnAddMessageToConsole(int32 level
,
189 const base::string16
& message
,
191 const base::string16
& source_id
);
193 void OnFrameFocused();
194 void OnOpenURL(const FrameHostMsg_OpenURL_Params
& params
);
195 void OnDocumentOnLoadCompleted();
196 void OnDidStartProvisionalLoadForFrame(int parent_routing_id
,
198 void OnDidFailProvisionalLoadWithError(
199 const FrameHostMsg_DidFailProvisionalLoadWithError_Params
& params
);
200 void OnDidFailLoadWithError(
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(
210 const base::TimeTicks
& renderer_before_unload_start_time
,
211 const base::TimeTicks
& renderer_before_unload_end_time
);
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
,
223 IPC::Message
* reply_msg
);
224 void OnRequestDesktopNotificationPermission(const GURL
& origin
,
226 void OnShowDesktopNotification(
228 const ShowDesktopNotificationHostMsgParams
& params
);
229 void OnCancelDesktopNotification(int notification_id
);
230 void OnDidAccessInitialDocument();
231 void OnDidDisownOpener();
232 void OnUpdateTitle(int32 page_id
,
233 const base::string16
& title
,
234 blink::WebTextDirection title_direction
);
235 void OnUpdateEncoding(const std::string
& encoding
);
237 // Returns whether the given URL is allowed to commit in the current process.
238 // This is a more conservative check than RenderProcessHost::FilterURL, since
239 // it will be used to kill processes that commit unauthorized URLs.
240 bool CanCommitURL(const GURL
& url
);
242 void DesktopNotificationPermissionRequestDone(int callback_context
);
244 // For now, RenderFrameHosts indirectly keep RenderViewHosts alive via a
245 // refcount that calls Shutdown when it reaches zero. This allows each
246 // RenderFrameHostManager to just care about RenderFrameHosts, while ensuring
247 // we have a RenderViewHost for each RenderFrameHost.
248 // TODO(creis): RenderViewHost will eventually go away and be replaced with
249 // some form of page context.
250 RenderViewHostImpl
* render_view_host_
;
252 RenderFrameHostDelegate
* delegate_
;
254 // |cross_process_frame_connector_| passes messages from an out-of-process
255 // child frame to the parent process for compositing.
257 // This is only non-NULL when this is the swapped out RenderFrameHost in
258 // the same site instance as this frame's parent.
260 // See the class comment above CrossProcessFrameConnector for more
263 // This will move to RenderFrameProxyHost when that class is created.
264 CrossProcessFrameConnector
* cross_process_frame_connector_
;
266 // The proxy created for this RenderFrameHost. It is used to send and receive
267 // IPC messages while in swapped out state.
268 // TODO(nasko): This can be removed once we don't have a swapped out state on
269 // RenderFrameHosts. See https://crbug.com/357747.
270 RenderFrameProxyHost
* render_frame_proxy_host_
;
272 // Reference to the whole frame tree that this RenderFrameHost belongs to.
273 // Allows this RenderFrameHost to add and remove nodes in response to
274 // messages from the renderer requesting DOM manipulation.
275 FrameTree
* frame_tree_
;
277 // The FrameTreeNode which this RenderFrameHostImpl is hosted in.
278 FrameTreeNode
* frame_tree_node_
;
280 // The mapping of pending JavaScript calls created by
281 // ExecuteJavaScript and their corresponding callbacks.
282 std::map
<int, JavaScriptResultCallback
> javascript_callbacks_
;
284 // Map from notification_id to a callback to cancel them.
285 std::map
<int, base::Closure
> cancel_notification_callbacks_
;
288 bool is_swapped_out_
;
290 // When the last BeforeUnload message was sent.
291 base::TimeTicks send_before_unload_start_time_
;
293 base::WeakPtrFactory
<RenderFrameHostImpl
> weak_ptr_factory_
;
295 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostImpl
);
298 } // namespace content
300 #endif // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_IMPL_H_