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 void ExecuteJavaScriptForTests(
84 const base::string16
& javascript
) OVERRIDE
;
85 virtual RenderViewHost
* GetRenderViewHost() OVERRIDE
;
86 virtual ServiceRegistry
* GetServiceRegistry() OVERRIDE
;
89 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
92 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
94 // BrowserAccessibilityDelegate
95 virtual void AccessibilitySetFocus(int acc_obj_id
) OVERRIDE
;
96 virtual void AccessibilityDoDefaultAction(int acc_obj_id
) OVERRIDE
;
97 virtual void AccessibilityShowMenu(const gfx::Point
& global_point
) OVERRIDE
;
98 virtual void AccessibilityScrollToMakeVisible(
99 int acc_obj_id
, const gfx::Rect
& subfocus
) OVERRIDE
;
100 virtual void AccessibilityScrollToPoint(
101 int acc_obj_id
, const gfx::Point
& point
) OVERRIDE
;
102 virtual void AccessibilitySetTextSelection(
103 int acc_obj_id
, int start_offset
, int end_offset
) OVERRIDE
;
104 virtual bool AccessibilityViewHasFocus() const OVERRIDE
;
105 virtual gfx::Rect
AccessibilityGetViewBounds() const OVERRIDE
;
106 virtual gfx::Point
AccessibilityOriginInScreen(const gfx::Rect
& bounds
)
108 virtual void AccessibilityHitTest(const gfx::Point
& point
) OVERRIDE
;
109 virtual void AccessibilityFatalError() OVERRIDE
;
110 virtual gfx::AcceleratedWidget
AccessibilityGetAcceleratedWidget() OVERRIDE
;
111 virtual gfx::NativeViewAccessible
AccessibilityGetNativeViewAccessible()
113 virtual BrowserAccessibilityManager
* AccessibilityGetChildFrame(
114 int64 frame_tree_node_id
) OVERRIDE
;
115 virtual BrowserAccessibilityManager
* AccessibilityGetParentFrame() OVERRIDE
;
117 bool CreateRenderFrame(int parent_routing_id
);
118 bool IsRenderFrameLive();
120 int routing_id() const { return routing_id_
; }
121 void OnCreateChildFrame(int new_routing_id
,
122 const std::string
& frame_name
);
124 RenderViewHostImpl
* render_view_host() { return render_view_host_
; }
125 RenderFrameHostDelegate
* delegate() { return delegate_
; }
126 FrameTreeNode
* frame_tree_node() { return frame_tree_node_
; }
127 // TODO(nasko): The RenderWidgetHost will be owned by RenderFrameHost in
128 // the future, so update this accessor to return the right pointer.
129 RenderWidgetHostImpl
* GetRenderWidgetHost();
131 // This function is called when this is a swapped out RenderFrameHost that
132 // lives in the same process as the parent frame. The
133 // |cross_process_frame_connector| allows the non-swapped-out
134 // RenderFrameHost for a frame to communicate with the parent process
135 // so that it may composite drawing data.
137 // Ownership is not transfered.
138 void set_cross_process_frame_connector(
139 CrossProcessFrameConnector
* cross_process_frame_connector
) {
140 cross_process_frame_connector_
= cross_process_frame_connector
;
143 void set_render_frame_proxy_host(RenderFrameProxyHost
* proxy
) {
144 render_frame_proxy_host_
= proxy
;
147 // Returns a bitwise OR of bindings types that have been enabled for this
148 // RenderFrameHostImpl's RenderView. See BindingsPolicy for details.
149 // TODO(creis): Make bindings frame-specific, to support cases like <webview>.
150 int GetEnabledBindings();
152 // Called on the pending RenderFrameHost when the network response is ready to
153 // commit. We should ensure that the old RenderFrameHost runs its unload
154 // handler and determine whether a transfer to a different RenderFrameHost is
156 void OnCrossSiteResponse(
157 const GlobalRequestID
& global_request_id
,
158 scoped_ptr
<CrossSiteTransferringRequest
> cross_site_transferring_request
,
159 const std::vector
<GURL
>& transfer_url_chain
,
160 const Referrer
& referrer
,
161 PageTransition page_transition
,
162 bool should_replace_current_entry
);
164 // Called on the current RenderFrameHost when the network response is first
166 void OnDeferredAfterResponseStarted(
167 const GlobalRequestID
& global_request_id
,
168 const TransitionLayerData
& transition_data
);
170 // Tells the renderer that this RenderFrame is being swapped out for one in a
171 // different renderer process. It should run its unload handler, move to
172 // a blank document and create a RenderFrameProxy to replace the RenderFrame.
173 // The renderer should preserve the Proxy object until it exits, in case we
174 // come back. The renderer can exit if it has no other active RenderFrames,
175 // but not until WasSwappedOut is called (when it is no longer visible).
176 void SwapOut(RenderFrameProxyHost
* proxy
);
178 void OnSwappedOut(bool timed_out
);
179 bool is_swapped_out() { return is_swapped_out_
; }
180 void set_swapped_out(bool is_swapped_out
) {
181 is_swapped_out_
= is_swapped_out
;
184 // Sets the RVH for |this| as pending shutdown. |on_swap_out| will be called
185 // when the SwapOutACK is received.
186 void SetPendingShutdown(const base::Closure
& on_swap_out
);
188 // Sends the given navigation message. Use this rather than sending it
189 // yourself since this does the internal bookkeeping described below. This
190 // function takes ownership of the provided message pointer.
192 // If a cross-site request is in progress, we may be suspended while waiting
193 // for the onbeforeunload handler, so this function might buffer the message
194 // rather than sending it.
195 void Navigate(const FrameMsg_Navigate_Params
& params
);
197 // Load the specified URL; this is a shortcut for Navigate().
198 void NavigateToURL(const GURL
& url
);
200 // Stop the load in progress.
203 // Returns whether navigation messages are currently suspended for this
204 // RenderFrameHost. Only true during a cross-site navigation, while waiting
205 // for the onbeforeunload handler.
206 bool are_navigations_suspended() const { return navigations_suspended_
; }
208 // Suspends (or unsuspends) any navigation messages from being sent from this
209 // RenderFrameHost. This is called when a pending RenderFrameHost is created
210 // for a cross-site navigation, because we must suspend any navigations until
211 // we hear back from the old renderer's onbeforeunload handler. Note that it
212 // is important that only one navigation event happen after calling this
213 // method with |suspend| equal to true. If |suspend| is false and there is a
214 // suspended_nav_message_, this will send the message. This function should
215 // only be called to toggle the state; callers should check
216 // are_navigations_suspended() first. If |suspend| is false, the time that the
217 // user decided the navigation should proceed should be passed as
219 void SetNavigationsSuspended(bool suspend
,
220 const base::TimeTicks
& proceed_time
);
222 // Clears any suspended navigation state after a cross-site navigation is
223 // canceled or suspended. This is important if we later return to this
225 void CancelSuspendedNavigations();
227 // Runs the beforeunload handler for this frame. |for_cross_site_transition|
228 // indicates whether this call is for the current frame during a cross-process
229 // navigation. False means we're closing the entire tab.
230 void DispatchBeforeUnload(bool for_cross_site_transition
);
232 // Set the frame's opener to null in the renderer process in response to an
233 // action in another renderer process.
236 // Deletes the current selection plus the specified number of characters
237 // before and after the selection or caret.
238 void ExtendSelectionAndDelete(size_t before
, size_t after
);
240 // Notifies the RenderFrame that the JavaScript message that was shown was
241 // closed by the user.
242 void JavaScriptDialogClosed(IPC::Message
* reply_msg
,
244 const base::string16
& user_input
,
245 bool dialog_was_suppressed
);
247 // Called when an HTML5 notification is closed.
248 void NotificationClosed(int notification_id
);
250 // Clears any outstanding transition request. This is called when we hear the
251 // response or commit.
252 void ClearPendingTransitionRequestData();
254 // Send a message to the renderer process to change the accessibility mode.
255 void SetAccessibilityMode(AccessibilityMode AccessibilityMode
);
257 // Turn on accessibility testing. The given callback will be run
258 // every time an accessibility notification is received from the
259 // renderer process, and the accessibility tree it sent can be
260 // retrieved using GetAXTreeForTesting().
261 void SetAccessibilityCallbackForTesting(
262 const base::Callback
<void(ui::AXEvent
, int)>& callback
);
264 // Returns a snapshot of the accessibility tree received from the
265 // renderer as of the last time an accessibility notification was
267 const ui::AXTree
* GetAXTreeForTesting();
269 // Access the BrowserAccessibilityManager if it already exists.
270 BrowserAccessibilityManager
* browser_accessibility_manager() const {
271 return browser_accessibility_manager_
.get();
274 // If accessibility is enabled, get the BrowserAccessibilityManager for
275 // this frame, or create one if it doesn't exist yet, otherwise return
277 BrowserAccessibilityManager
* GetOrCreateBrowserAccessibilityManager();
280 void SetParentNativeViewAccessible(
281 gfx::NativeViewAccessible accessible_parent
);
282 gfx::NativeViewAccessible
GetParentNativeViewAccessible() const;
283 #elif defined(OS_MACOSX)
284 // Select popup menu related methods (for external popup menus).
285 void DidSelectPopupMenuItem(int selected_index
);
286 void DidCancelPopupMenu();
287 #elif defined(OS_ANDROID)
288 void DidSelectPopupMenuItems(const std::vector
<int>& selected_indices
);
289 void DidCancelPopupMenu();
293 friend class RenderFrameHostFactory
;
295 // TODO(nasko): Remove dependency on RenderViewHost here. RenderProcessHost
296 // should be the abstraction needed here, but we need RenderViewHost to pass
297 // into WebContentsObserver::FrameDetached for now.
298 RenderFrameHostImpl(RenderViewHostImpl
* render_view_host
,
299 RenderFrameHostDelegate
* delegate
,
300 FrameTree
* frame_tree
,
301 FrameTreeNode
* frame_tree_node
,
303 bool is_swapped_out
);
306 friend class TestRenderFrameHost
;
307 friend class TestRenderViewHost
;
309 // IPC Message handlers.
310 void OnAddMessageToConsole(int32 level
,
311 const base::string16
& message
,
313 const base::string16
& source_id
);
315 void OnFrameFocused();
316 void OnOpenURL(const FrameHostMsg_OpenURL_Params
& params
);
317 void OnDocumentOnLoadCompleted();
318 void OnDidStartProvisionalLoadForFrame(const GURL
& url
,
319 bool is_transition_navigation
);
320 void OnDidFailProvisionalLoadWithError(
321 const FrameHostMsg_DidFailProvisionalLoadWithError_Params
& params
);
322 void OnDidFailLoadWithError(
325 const base::string16
& error_description
);
326 void OnDidRedirectProvisionalLoad(int32 page_id
,
327 const GURL
& source_url
,
328 const GURL
& target_url
);
329 void OnDidCommitProvisionalLoad(const IPC::Message
& msg
);
330 void OnBeforeUnloadACK(
332 const base::TimeTicks
& renderer_before_unload_start_time
,
333 const base::TimeTicks
& renderer_before_unload_end_time
);
335 void OnContextMenu(const ContextMenuParams
& params
);
336 void OnJavaScriptExecuteResponse(int id
, const base::ListValue
& result
);
337 void OnRunJavaScriptMessage(const base::string16
& message
,
338 const base::string16
& default_prompt
,
339 const GURL
& frame_url
,
340 JavaScriptMessageType type
,
341 IPC::Message
* reply_msg
);
342 void OnRunBeforeUnloadConfirm(const GURL
& frame_url
,
343 const base::string16
& message
,
345 IPC::Message
* reply_msg
);
346 void OnRequestPlatformNotificationPermission(const GURL
& origin
,
348 void OnShowDesktopNotification(
350 const ShowDesktopNotificationHostMsgParams
& params
);
351 void OnCancelDesktopNotification(int notification_id
);
352 void OnTextSurroundingSelectionResponse(const base::string16
& content
,
355 void OnDidAccessInitialDocument();
356 void OnDidDisownOpener();
357 void OnDidAssignPageId(int32 page_id
);
358 void OnUpdateTitle(int32 page_id
,
359 const base::string16
& title
,
360 blink::WebTextDirection title_direction
);
361 void OnUpdateEncoding(const std::string
& encoding
);
362 void OnBeginNavigation(
363 const FrameHostMsg_BeginNavigation_Params
& params
);
364 void OnAccessibilityEvents(
365 const std::vector
<AccessibilityHostMsg_EventParams
>& params
);
366 void OnAccessibilityLocationChanges(
367 const std::vector
<AccessibilityHostMsg_LocationChangeParams
>& params
);
369 #if defined(OS_MACOSX) || defined(OS_ANDROID)
370 void OnShowPopup(const FrameHostMsg_ShowPopup_Params
& params
);
374 // Returns whether the given URL is allowed to commit in the current process.
375 // This is a more conservative check than RenderProcessHost::FilterURL, since
376 // it will be used to kill processes that commit unauthorized URLs.
377 bool CanCommitURL(const GURL
& url
);
379 void PlatformNotificationPermissionRequestDone(
380 int request_id
, blink::WebNotificationPermission permission
);
382 // For now, RenderFrameHosts indirectly keep RenderViewHosts alive via a
383 // refcount that calls Shutdown when it reaches zero. This allows each
384 // RenderFrameHostManager to just care about RenderFrameHosts, while ensuring
385 // we have a RenderViewHost for each RenderFrameHost.
386 // TODO(creis): RenderViewHost will eventually go away and be replaced with
387 // some form of page context.
388 RenderViewHostImpl
* render_view_host_
;
390 RenderFrameHostDelegate
* delegate_
;
392 // |cross_process_frame_connector_| passes messages from an out-of-process
393 // child frame to the parent process for compositing.
395 // This is only non-NULL when this is the swapped out RenderFrameHost in
396 // the same site instance as this frame's parent.
398 // See the class comment above CrossProcessFrameConnector for more
401 // This will move to RenderFrameProxyHost when that class is created.
402 CrossProcessFrameConnector
* cross_process_frame_connector_
;
404 // The proxy created for this RenderFrameHost. It is used to send and receive
405 // IPC messages while in swapped out state.
406 // TODO(nasko): This can be removed once we don't have a swapped out state on
407 // RenderFrameHosts. See https://crbug.com/357747.
408 RenderFrameProxyHost
* render_frame_proxy_host_
;
410 // Reference to the whole frame tree that this RenderFrameHost belongs to.
411 // Allows this RenderFrameHost to add and remove nodes in response to
412 // messages from the renderer requesting DOM manipulation.
413 FrameTree
* frame_tree_
;
415 // The FrameTreeNode which this RenderFrameHostImpl is hosted in.
416 FrameTreeNode
* frame_tree_node_
;
418 // The mapping of pending JavaScript calls created by
419 // ExecuteJavaScript and their corresponding callbacks.
420 std::map
<int, JavaScriptResultCallback
> javascript_callbacks_
;
422 // Map from notification_id to a callback to cancel them.
423 std::map
<int, base::Closure
> cancel_notification_callbacks_
;
426 bool is_swapped_out_
;
427 bool renderer_initialized_
;
429 // Whether we should buffer outgoing Navigate messages rather than sending
430 // them. This will be true when a RenderFrameHost is created for a cross-site
431 // request, until we hear back from the onbeforeunload handler of the old
433 bool navigations_suspended_
;
435 // We only buffer the params for a suspended navigation while this RFH is the
436 // pending RenderFrameHost of a RenderFrameHostManager. There will only ever
437 // be one suspended navigation, because RenderFrameHostManager will destroy
438 // the pending RenderFrameHost and create a new one if a second navigation
440 scoped_ptr
<FrameMsg_Navigate_Params
> suspended_nav_params_
;
442 // When the last BeforeUnload message was sent.
443 base::TimeTicks send_before_unload_start_time_
;
445 ServiceRegistryImpl service_registry_
;
447 base::WeakPtrFactory
<RenderFrameHostImpl
> weak_ptr_factory_
;
449 scoped_ptr
<BrowserAccessibilityManager
> browser_accessibility_manager_
;
451 // Callback when an event is received, for testing.
452 base::Callback
<void(ui::AXEvent
, int)> accessibility_testing_callback_
;
453 // The most recently received accessibility tree - for testing only.
454 scoped_ptr
<ui::AXTree
> ax_tree_for_testing_
;
456 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostImpl
);
459 } // namespace content
461 #endif // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_IMPL_H_