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/browser/accessibility/browser_accessibility_manager.h"
17 #include "content/common/accessibility_mode_enums.h"
18 #include "content/common/content_export.h"
19 #include "content/common/mojo/service_registry_impl.h"
20 #include "content/public/browser/render_frame_host.h"
21 #include "content/public/common/javascript_message_type.h"
22 #include "content/public/common/page_transition_types.h"
23 #include "net/http/http_response_headers.h"
24 #include "third_party/WebKit/public/platform/WebNotificationPermission.h"
25 #include "third_party/WebKit/public/web/WebTextDirection.h"
26 #include "ui/accessibility/ax_node_data.h"
29 struct AccessibilityHostMsg_EventParams
;
30 struct AccessibilityHostMsg_LocationChangeParams
;
31 struct FrameHostMsg_DidFailProvisionalLoadWithError_Params
;
32 struct FrameHostMsg_OpenURL_Params
;
33 struct FrameHostMsg_BeginNavigation_Params
;
34 struct FrameMsg_Navigate_Params
;
35 #if defined(OS_MACOSX) || defined(OS_ANDROID)
36 struct FrameHostMsg_ShowPopup_Params
;
46 class CrossProcessFrameConnector
;
47 class CrossSiteTransferringRequest
;
50 class RenderFrameHostDelegate
;
51 class RenderFrameProxyHost
;
52 class RenderProcessHost
;
53 class RenderViewHostImpl
;
54 class RenderWidgetHostImpl
;
55 struct ContextMenuParams
;
56 struct GlobalRequestID
;
58 struct ShowDesktopNotificationHostMsgParams
;
59 struct TransitionLayerData
;
61 class CONTENT_EXPORT RenderFrameHostImpl
62 : public RenderFrameHost
,
63 public BrowserAccessibilityDelegate
{
65 static RenderFrameHostImpl
* FromID(int process_id
, int routing_id
);
67 virtual ~RenderFrameHostImpl();
70 virtual int GetRoutingID() OVERRIDE
;
71 virtual SiteInstance
* GetSiteInstance() OVERRIDE
;
72 virtual RenderProcessHost
* GetProcess() OVERRIDE
;
73 virtual RenderFrameHost
* GetParent() OVERRIDE
;
74 virtual const std::string
& GetFrameName() OVERRIDE
;
75 virtual bool IsCrossProcessSubframe() OVERRIDE
;
76 virtual GURL
GetLastCommittedURL() OVERRIDE
;
77 virtual gfx::NativeView
GetNativeView() OVERRIDE
;
78 virtual void ExecuteJavaScript(
79 const base::string16
& javascript
) OVERRIDE
;
80 virtual void ExecuteJavaScript(
81 const base::string16
& javascript
,
82 const JavaScriptResultCallback
& callback
) OVERRIDE
;
83 virtual RenderViewHost
* GetRenderViewHost() OVERRIDE
;
84 virtual ServiceRegistry
* GetServiceRegistry() OVERRIDE
;
87 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
90 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
92 // BrowserAccessibilityDelegate
93 virtual void AccessibilitySetFocus(int acc_obj_id
) OVERRIDE
;
94 virtual void AccessibilityDoDefaultAction(int acc_obj_id
) OVERRIDE
;
95 virtual void AccessibilityShowMenu(const gfx::Point
& global_point
) OVERRIDE
;
96 virtual void AccessibilityScrollToMakeVisible(
97 int acc_obj_id
, const gfx::Rect
& subfocus
) OVERRIDE
;
98 virtual void AccessibilityScrollToPoint(
99 int acc_obj_id
, const gfx::Point
& point
) OVERRIDE
;
100 virtual void AccessibilitySetTextSelection(
101 int acc_obj_id
, int start_offset
, int end_offset
) OVERRIDE
;
102 virtual bool AccessibilityViewHasFocus() const OVERRIDE
;
103 virtual gfx::Rect
AccessibilityGetViewBounds() const OVERRIDE
;
104 virtual gfx::Point
AccessibilityOriginInScreen(const gfx::Rect
& bounds
)
106 virtual void AccessibilityHitTest(const gfx::Point
& point
) OVERRIDE
;
107 virtual void AccessibilityFatalError() OVERRIDE
;
108 virtual gfx::AcceleratedWidget
AccessibilityGetAcceleratedWidget() OVERRIDE
;
109 virtual gfx::NativeViewAccessible
AccessibilityGetNativeViewAccessible()
112 bool CreateRenderFrame(int parent_routing_id
);
113 bool IsRenderFrameLive();
115 int routing_id() const { return routing_id_
; }
116 void OnCreateChildFrame(int new_routing_id
,
117 const std::string
& frame_name
);
119 RenderViewHostImpl
* render_view_host() { return render_view_host_
; }
120 RenderFrameHostDelegate
* delegate() { return delegate_
; }
121 FrameTreeNode
* frame_tree_node() { return frame_tree_node_
; }
122 // TODO(nasko): The RenderWidgetHost will be owned by RenderFrameHost in
123 // the future, so update this accessor to return the right pointer.
124 RenderWidgetHostImpl
* GetRenderWidgetHost();
126 // This function is called when this is a swapped out RenderFrameHost that
127 // lives in the same process as the parent frame. The
128 // |cross_process_frame_connector| allows the non-swapped-out
129 // RenderFrameHost for a frame to communicate with the parent process
130 // so that it may composite drawing data.
132 // Ownership is not transfered.
133 void set_cross_process_frame_connector(
134 CrossProcessFrameConnector
* cross_process_frame_connector
) {
135 cross_process_frame_connector_
= cross_process_frame_connector
;
138 void set_render_frame_proxy_host(RenderFrameProxyHost
* proxy
) {
139 render_frame_proxy_host_
= proxy
;
142 // Returns a bitwise OR of bindings types that have been enabled for this
143 // RenderFrameHostImpl's RenderView. See BindingsPolicy for details.
144 // TODO(creis): Make bindings frame-specific, to support cases like <webview>.
145 int GetEnabledBindings();
147 // Called on the pending RenderFrameHost when the network response is ready to
148 // commit. We should ensure that the old RenderFrameHost runs its unload
149 // handler and determine whether a transfer to a different RenderFrameHost is
151 void OnCrossSiteResponse(
152 const GlobalRequestID
& global_request_id
,
153 scoped_ptr
<CrossSiteTransferringRequest
> cross_site_transferring_request
,
154 const std::vector
<GURL
>& transfer_url_chain
,
155 const Referrer
& referrer
,
156 PageTransition page_transition
,
157 bool should_replace_current_entry
);
159 // Called on the current RenderFrameHost when the network response is first
161 void OnDeferredAfterResponseStarted(
162 const GlobalRequestID
& global_request_id
,
163 const TransitionLayerData
& transition_data
);
165 // Tells the renderer that this RenderFrame is being swapped out for one in a
166 // different renderer process. It should run its unload handler, move to
167 // a blank document and create a RenderFrameProxy to replace the RenderFrame.
168 // The renderer should preserve the Proxy object until it exits, in case we
169 // come back. The renderer can exit if it has no other active RenderFrames,
170 // but not until WasSwappedOut is called (when it is no longer visible).
171 void SwapOut(RenderFrameProxyHost
* proxy
);
173 void OnSwappedOut(bool timed_out
);
174 bool is_swapped_out() { return is_swapped_out_
; }
175 void set_swapped_out(bool is_swapped_out
) {
176 is_swapped_out_
= is_swapped_out
;
179 // Sets the RVH for |this| as pending shutdown. |on_swap_out| will be called
180 // when the SwapOutACK is received.
181 void SetPendingShutdown(const base::Closure
& on_swap_out
);
183 // Sends the given navigation message. Use this rather than sending it
184 // yourself since this does the internal bookkeeping described below. This
185 // function takes ownership of the provided message pointer.
187 // If a cross-site request is in progress, we may be suspended while waiting
188 // for the onbeforeunload handler, so this function might buffer the message
189 // rather than sending it.
190 void Navigate(const FrameMsg_Navigate_Params
& params
);
192 // Load the specified URL; this is a shortcut for Navigate().
193 void NavigateToURL(const GURL
& url
);
195 // Returns whether navigation messages are currently suspended for this
196 // RenderFrameHost. Only true during a cross-site navigation, while waiting
197 // for the onbeforeunload handler.
198 bool are_navigations_suspended() const { return navigations_suspended_
; }
200 // Suspends (or unsuspends) any navigation messages from being sent from this
201 // RenderFrameHost. This is called when a pending RenderFrameHost is created
202 // for a cross-site navigation, because we must suspend any navigations until
203 // we hear back from the old renderer's onbeforeunload handler. Note that it
204 // is important that only one navigation event happen after calling this
205 // method with |suspend| equal to true. If |suspend| is false and there is a
206 // suspended_nav_message_, this will send the message. This function should
207 // only be called to toggle the state; callers should check
208 // are_navigations_suspended() first. If |suspend| is false, the time that the
209 // user decided the navigation should proceed should be passed as
211 void SetNavigationsSuspended(bool suspend
,
212 const base::TimeTicks
& proceed_time
);
214 // Clears any suspended navigation state after a cross-site navigation is
215 // canceled or suspended. This is important if we later return to this
217 void CancelSuspendedNavigations();
219 // Runs the beforeunload handler for this frame. |for_cross_site_transition|
220 // indicates whether this call is for the current frame during a cross-process
221 // navigation. False means we're closing the entire tab.
222 void DispatchBeforeUnload(bool for_cross_site_transition
);
224 // Set the frame's opener to null in the renderer process in response to an
225 // action in another renderer process.
228 // Deletes the current selection plus the specified number of characters
229 // before and after the selection or caret.
230 void ExtendSelectionAndDelete(size_t before
, size_t after
);
232 // Notifies the RenderFrame that the JavaScript message that was shown was
233 // closed by the user.
234 void JavaScriptDialogClosed(IPC::Message
* reply_msg
,
236 const base::string16
& user_input
,
237 bool dialog_was_suppressed
);
239 // Called when an HTML5 notification is closed.
240 void NotificationClosed(int notification_id
);
242 // Clears any outstanding transition request. This is called when we hear the
243 // response or commit.
244 void ClearPendingTransitionRequestData();
246 // Send a message to the renderer process to change the accessibility mode.
247 void SetAccessibilityMode(AccessibilityMode AccessibilityMode
);
249 // Turn on accessibility testing. The given callback will be run
250 // every time an accessibility notification is received from the
251 // renderer process, and the accessibility tree it sent can be
252 // retrieved using GetAXTreeForTesting().
253 void SetAccessibilityCallbackForTesting(
254 const base::Callback
<void(ui::AXEvent
, int)>& callback
);
256 // Returns a snapshot of the accessibility tree received from the
257 // renderer as of the last time an accessibility notification was
259 const ui::AXTree
* GetAXTreeForTesting();
261 // Access the BrowserAccessibilityManager if it already exists.
262 BrowserAccessibilityManager
* browser_accessibility_manager() const {
263 return browser_accessibility_manager_
.get();
266 // If accessibility is enabled, get the BrowserAccessibilityManager for
267 // this frame, or create one if it doesn't exist yet, otherwise return
269 BrowserAccessibilityManager
* GetOrCreateBrowserAccessibilityManager();
272 void SetParentNativeViewAccessible(
273 gfx::NativeViewAccessible accessible_parent
);
274 gfx::NativeViewAccessible
GetParentNativeViewAccessible() const;
275 #elif defined(OS_MACOSX)
276 // Select popup menu related methods (for external popup menus).
277 void DidSelectPopupMenuItem(int selected_index
);
278 void DidCancelPopupMenu();
279 #elif defined(OS_ANDROID)
280 void DidSelectPopupMenuItems(const std::vector
<int>& selected_indices
);
281 void DidCancelPopupMenu();
285 friend class RenderFrameHostFactory
;
287 // TODO(nasko): Remove dependency on RenderViewHost here. RenderProcessHost
288 // should be the abstraction needed here, but we need RenderViewHost to pass
289 // into WebContentsObserver::FrameDetached for now.
290 RenderFrameHostImpl(RenderViewHostImpl
* render_view_host
,
291 RenderFrameHostDelegate
* delegate
,
292 FrameTree
* frame_tree
,
293 FrameTreeNode
* frame_tree_node
,
295 bool is_swapped_out
);
298 friend class TestRenderFrameHost
;
299 friend class TestRenderViewHost
;
301 // IPC Message handlers.
302 void OnAddMessageToConsole(int32 level
,
303 const base::string16
& message
,
305 const base::string16
& source_id
);
307 void OnFrameFocused();
308 void OnOpenURL(const FrameHostMsg_OpenURL_Params
& params
);
309 void OnDocumentOnLoadCompleted();
310 void OnDidStartProvisionalLoadForFrame(const GURL
& url
,
311 bool is_transition_navigation
);
312 void OnDidFailProvisionalLoadWithError(
313 const FrameHostMsg_DidFailProvisionalLoadWithError_Params
& params
);
314 void OnDidFailLoadWithError(
317 const base::string16
& error_description
);
318 void OnDidRedirectProvisionalLoad(int32 page_id
,
319 const GURL
& source_url
,
320 const GURL
& target_url
);
321 void OnNavigate(const IPC::Message
& msg
);
322 void OnBeforeUnloadACK(
324 const base::TimeTicks
& renderer_before_unload_start_time
,
325 const base::TimeTicks
& renderer_before_unload_end_time
);
327 void OnContextMenu(const ContextMenuParams
& params
);
328 void OnJavaScriptExecuteResponse(int id
, const base::ListValue
& result
);
329 void OnRunJavaScriptMessage(const base::string16
& message
,
330 const base::string16
& default_prompt
,
331 const GURL
& frame_url
,
332 JavaScriptMessageType type
,
333 IPC::Message
* reply_msg
);
334 void OnRunBeforeUnloadConfirm(const GURL
& frame_url
,
335 const base::string16
& message
,
337 IPC::Message
* reply_msg
);
338 void OnRequestPlatformNotificationPermission(const GURL
& origin
,
340 void OnShowDesktopNotification(
342 const ShowDesktopNotificationHostMsgParams
& params
);
343 void OnCancelDesktopNotification(int notification_id
);
344 void OnTextSurroundingSelectionResponse(const base::string16
& content
,
347 void OnDidAccessInitialDocument();
348 void OnDidDisownOpener();
349 void OnUpdateTitle(int32 page_id
,
350 const base::string16
& title
,
351 blink::WebTextDirection title_direction
);
352 void OnUpdateEncoding(const std::string
& encoding
);
353 void OnBeginNavigation(
354 const FrameHostMsg_BeginNavigation_Params
& params
);
355 void OnAccessibilityEvents(
356 const std::vector
<AccessibilityHostMsg_EventParams
>& params
);
357 void OnAccessibilityLocationChanges(
358 const std::vector
<AccessibilityHostMsg_LocationChangeParams
>& params
);
360 #if defined(OS_MACOSX) || defined(OS_ANDROID)
361 void OnShowPopup(const FrameHostMsg_ShowPopup_Params
& params
);
365 // Returns whether the given URL is allowed to commit in the current process.
366 // This is a more conservative check than RenderProcessHost::FilterURL, since
367 // it will be used to kill processes that commit unauthorized URLs.
368 bool CanCommitURL(const GURL
& url
);
370 void PlatformNotificationPermissionRequestDone(
371 int request_id
, blink::WebNotificationPermission permission
);
373 // For now, RenderFrameHosts indirectly keep RenderViewHosts alive via a
374 // refcount that calls Shutdown when it reaches zero. This allows each
375 // RenderFrameHostManager to just care about RenderFrameHosts, while ensuring
376 // we have a RenderViewHost for each RenderFrameHost.
377 // TODO(creis): RenderViewHost will eventually go away and be replaced with
378 // some form of page context.
379 RenderViewHostImpl
* render_view_host_
;
381 RenderFrameHostDelegate
* delegate_
;
383 // |cross_process_frame_connector_| passes messages from an out-of-process
384 // child frame to the parent process for compositing.
386 // This is only non-NULL when this is the swapped out RenderFrameHost in
387 // the same site instance as this frame's parent.
389 // See the class comment above CrossProcessFrameConnector for more
392 // This will move to RenderFrameProxyHost when that class is created.
393 CrossProcessFrameConnector
* cross_process_frame_connector_
;
395 // The proxy created for this RenderFrameHost. It is used to send and receive
396 // IPC messages while in swapped out state.
397 // TODO(nasko): This can be removed once we don't have a swapped out state on
398 // RenderFrameHosts. See https://crbug.com/357747.
399 RenderFrameProxyHost
* render_frame_proxy_host_
;
401 // Reference to the whole frame tree that this RenderFrameHost belongs to.
402 // Allows this RenderFrameHost to add and remove nodes in response to
403 // messages from the renderer requesting DOM manipulation.
404 FrameTree
* frame_tree_
;
406 // The FrameTreeNode which this RenderFrameHostImpl is hosted in.
407 FrameTreeNode
* frame_tree_node_
;
409 // The mapping of pending JavaScript calls created by
410 // ExecuteJavaScript and their corresponding callbacks.
411 std::map
<int, JavaScriptResultCallback
> javascript_callbacks_
;
413 // Map from notification_id to a callback to cancel them.
414 std::map
<int, base::Closure
> cancel_notification_callbacks_
;
417 bool is_swapped_out_
;
418 bool renderer_initialized_
;
420 // Whether we should buffer outgoing Navigate messages rather than sending
421 // them. This will be true when a RenderFrameHost is created for a cross-site
422 // request, until we hear back from the onbeforeunload handler of the old
424 bool navigations_suspended_
;
426 // We only buffer the params for a suspended navigation while this RFH is the
427 // pending RenderFrameHost of a RenderFrameHostManager. There will only ever
428 // be one suspended navigation, because RenderFrameHostManager will destroy
429 // the pending RenderFrameHost and create a new one if a second navigation
431 scoped_ptr
<FrameMsg_Navigate_Params
> suspended_nav_params_
;
433 // When the last BeforeUnload message was sent.
434 base::TimeTicks send_before_unload_start_time_
;
436 ServiceRegistryImpl service_registry_
;
438 base::WeakPtrFactory
<RenderFrameHostImpl
> weak_ptr_factory_
;
440 scoped_ptr
<BrowserAccessibilityManager
> browser_accessibility_manager_
;
442 // Callback when an event is received, for testing.
443 base::Callback
<void(ui::AXEvent
, int)> accessibility_testing_callback_
;
444 // The most recently received accessibility tree - for testing only.
445 scoped_ptr
<ui::AXTree
> ax_tree_for_testing_
;
447 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostImpl
);
450 } // namespace content
452 #endif // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_IMPL_H_