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/gtest_prod_util.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/strings/string16.h"
16 #include "base/time/time.h"
17 #include "content/browser/accessibility/browser_accessibility_manager.h"
18 #include "content/common/accessibility_mode_enums.h"
19 #include "content/common/content_export.h"
20 #include "content/common/mojo/service_registry_impl.h"
21 #include "content/public/browser/render_frame_host.h"
22 #include "content/public/common/javascript_message_type.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"
27 #include "ui/base/page_transition_types.h"
30 struct AccessibilityHostMsg_EventParams;
31 struct AccessibilityHostMsg_LocationChangeParams;
32 struct FrameHostMsg_DidFailProvisionalLoadWithError_Params;
33 struct FrameHostMsg_OpenURL_Params;
34 struct FrameHostMsg_BeginNavigation_Params;
35 struct FrameMsg_Navigate_Params;
36 #if defined(OS_MACOSX) || defined(OS_ANDROID)
37 struct FrameHostMsg_ShowPopup_Params;
47 class CrossProcessFrameConnector;
48 class CrossSiteTransferringRequest;
51 class RenderFrameHostDelegate;
52 class RenderFrameProxyHost;
53 class RenderProcessHost;
54 class RenderViewHostImpl;
55 class RenderWidgetHostImpl;
56 struct ContextMenuParams;
57 struct GlobalRequestID;
59 struct ShowDesktopNotificationHostMsgParams;
60 struct TransitionLayerData;
62 class CONTENT_EXPORT RenderFrameHostImpl
63 : public RenderFrameHost,
64 public BrowserAccessibilityDelegate {
66 static RenderFrameHostImpl* FromID(int process_id, int routing_id);
68 virtual ~RenderFrameHostImpl();
71 virtual int GetRoutingID() OVERRIDE;
72 virtual SiteInstance* GetSiteInstance() OVERRIDE;
73 virtual RenderProcessHost* GetProcess() OVERRIDE;
74 virtual RenderFrameHost* GetParent() OVERRIDE;
75 virtual const std::string& GetFrameName() OVERRIDE;
76 virtual bool IsCrossProcessSubframe() OVERRIDE;
77 virtual GURL GetLastCommittedURL() OVERRIDE;
78 virtual gfx::NativeView GetNativeView() OVERRIDE;
79 virtual void ExecuteJavaScript(
80 const base::string16& javascript) OVERRIDE;
81 virtual void ExecuteJavaScript(
82 const base::string16& javascript,
83 const JavaScriptResultCallback& callback) OVERRIDE;
84 virtual void ExecuteJavaScriptForTests(
85 const base::string16& javascript) OVERRIDE;
86 virtual RenderViewHost* GetRenderViewHost() OVERRIDE;
87 virtual ServiceRegistry* GetServiceRegistry() OVERRIDE;
90 virtual bool Send(IPC::Message* msg) OVERRIDE;
93 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
95 // BrowserAccessibilityDelegate
96 virtual void AccessibilitySetFocus(int acc_obj_id) OVERRIDE;
97 virtual void AccessibilityDoDefaultAction(int acc_obj_id) OVERRIDE;
98 virtual void AccessibilityShowMenu(const gfx::Point& global_point) OVERRIDE;
99 virtual void AccessibilityScrollToMakeVisible(
100 int acc_obj_id, const gfx::Rect& subfocus) OVERRIDE;
101 virtual void AccessibilityScrollToPoint(
102 int acc_obj_id, const gfx::Point& point) OVERRIDE;
103 virtual void AccessibilitySetTextSelection(
104 int acc_obj_id, int start_offset, int end_offset) OVERRIDE;
105 virtual bool AccessibilityViewHasFocus() const OVERRIDE;
106 virtual gfx::Rect AccessibilityGetViewBounds() const OVERRIDE;
107 virtual gfx::Point AccessibilityOriginInScreen(const gfx::Rect& bounds)
109 virtual void AccessibilityHitTest(const gfx::Point& point) OVERRIDE;
110 virtual void AccessibilityFatalError() OVERRIDE;
111 virtual gfx::AcceleratedWidget AccessibilityGetAcceleratedWidget() OVERRIDE;
112 virtual gfx::NativeViewAccessible AccessibilityGetNativeViewAccessible()
114 virtual BrowserAccessibilityManager* AccessibilityGetChildFrame(
115 int accessibility_node_id) OVERRIDE;
116 virtual BrowserAccessibility* AccessibilityGetParentFrame() OVERRIDE;
118 // Creates a RenderFrame in the renderer process. Only called for
119 // cross-process subframe navigations in --site-per-process.
120 bool CreateRenderFrame(int parent_routing_id);
122 // Returns whether the RenderFrame in the renderer process has been created
123 // and still has a connection. This is valid for all frames.
124 bool IsRenderFrameLive();
126 // Tracks whether the RenderFrame for this RenderFrameHost has been created in
127 // the renderer process. This is currently only used for subframes.
128 // TODO(creis): Use this for main frames as well when RVH goes away.
129 void set_render_frame_created(bool created) {
130 render_frame_created_ = created;
133 // Called for renderer-created windows to resume requests from this frame,
134 // after they are blocked in RenderWidgetHelper::CreateNewWindow.
137 int routing_id() const { return routing_id_; }
138 void OnCreateChildFrame(int new_routing_id,
139 const std::string& frame_name);
141 RenderViewHostImpl* render_view_host() { return render_view_host_; }
142 RenderFrameHostDelegate* delegate() { return delegate_; }
143 FrameTreeNode* frame_tree_node() { return frame_tree_node_; }
144 // TODO(nasko): The RenderWidgetHost will be owned by RenderFrameHost in
145 // the future, so update this accessor to return the right pointer.
146 RenderWidgetHostImpl* GetRenderWidgetHost();
148 // This function is called when this is a swapped out RenderFrameHost that
149 // lives in the same process as the parent frame. The
150 // |cross_process_frame_connector| allows the non-swapped-out
151 // RenderFrameHost for a frame to communicate with the parent process
152 // so that it may composite drawing data.
154 // Ownership is not transfered.
155 void set_cross_process_frame_connector(
156 CrossProcessFrameConnector* cross_process_frame_connector) {
157 cross_process_frame_connector_ = cross_process_frame_connector;
160 void set_render_frame_proxy_host(RenderFrameProxyHost* proxy) {
161 render_frame_proxy_host_ = proxy;
164 // Returns a bitwise OR of bindings types that have been enabled for this
165 // RenderFrameHostImpl's RenderView. See BindingsPolicy for details.
166 // TODO(creis): Make bindings frame-specific, to support cases like <webview>.
167 int GetEnabledBindings();
169 // Called on the pending RenderFrameHost when the network response is ready to
170 // commit. We should ensure that the old RenderFrameHost runs its unload
171 // handler and determine whether a transfer to a different RenderFrameHost is
173 void OnCrossSiteResponse(
174 const GlobalRequestID& global_request_id,
175 scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request,
176 const std::vector<GURL>& transfer_url_chain,
177 const Referrer& referrer,
178 ui::PageTransition page_transition,
179 bool should_replace_current_entry);
181 // Called on the current RenderFrameHost when the network response is first
183 void OnDeferredAfterResponseStarted(
184 const GlobalRequestID& global_request_id,
185 const TransitionLayerData& transition_data);
187 // Tells the renderer that this RenderFrame is being swapped out for one in a
188 // different renderer process. It should run its unload handler, move to
189 // a blank document and create a RenderFrameProxy to replace the RenderFrame.
190 // The renderer should preserve the Proxy object until it exits, in case we
191 // come back. The renderer can exit if it has no other active RenderFrames,
192 // but not until WasSwappedOut is called (when it is no longer visible).
193 void SwapOut(RenderFrameProxyHost* proxy);
195 void OnSwappedOut(bool timed_out);
196 bool is_swapped_out() { return is_swapped_out_; }
197 void set_swapped_out(bool is_swapped_out) {
198 is_swapped_out_ = is_swapped_out;
201 // Sets the RVH for |this| as pending shutdown. |on_swap_out| will be called
202 // when the SwapOutACK is received.
203 void SetPendingShutdown(const base::Closure& on_swap_out);
205 // Sends the given navigation message. Use this rather than sending it
206 // yourself since this does the internal bookkeeping described below. This
207 // function takes ownership of the provided message pointer.
209 // If a cross-site request is in progress, we may be suspended while waiting
210 // for the onbeforeunload handler, so this function might buffer the message
211 // rather than sending it.
212 void Navigate(const FrameMsg_Navigate_Params& params);
214 // Load the specified URL; this is a shortcut for Navigate().
215 void NavigateToURL(const GURL& url);
217 // Stop the load in progress.
220 // Returns whether navigation messages are currently suspended for this
221 // RenderFrameHost. Only true during a cross-site navigation, while waiting
222 // for the onbeforeunload handler.
223 bool are_navigations_suspended() const { return navigations_suspended_; }
225 // Suspends (or unsuspends) any navigation messages from being sent from this
226 // RenderFrameHost. This is called when a pending RenderFrameHost is created
227 // for a cross-site navigation, because we must suspend any navigations until
228 // we hear back from the old renderer's onbeforeunload handler. Note that it
229 // is important that only one navigation event happen after calling this
230 // method with |suspend| equal to true. If |suspend| is false and there is a
231 // suspended_nav_message_, this will send the message. This function should
232 // only be called to toggle the state; callers should check
233 // are_navigations_suspended() first. If |suspend| is false, the time that the
234 // user decided the navigation should proceed should be passed as
236 void SetNavigationsSuspended(bool suspend,
237 const base::TimeTicks& proceed_time);
239 // Clears any suspended navigation state after a cross-site navigation is
240 // canceled or suspended. This is important if we later return to this
242 void CancelSuspendedNavigations();
244 // Runs the beforeunload handler for this frame. |for_cross_site_transition|
245 // indicates whether this call is for the current frame during a cross-process
246 // navigation. False means we're closing the entire tab.
247 void DispatchBeforeUnload(bool for_cross_site_transition);
249 // Set the frame's opener to null in the renderer process in response to an
250 // action in another renderer process.
253 // Deletes the current selection plus the specified number of characters
254 // before and after the selection or caret.
255 void ExtendSelectionAndDelete(size_t before, size_t after);
257 // Notifies the RenderFrame that the JavaScript message that was shown was
258 // closed by the user.
259 void JavaScriptDialogClosed(IPC::Message* reply_msg,
261 const base::string16& user_input,
262 bool dialog_was_suppressed);
264 // Called when an HTML5 notification is closed.
265 void NotificationClosed(int notification_id);
267 // Clears any outstanding transition request. This is called when we hear the
268 // response or commit.
269 void ClearPendingTransitionRequestData();
271 // Send a message to the renderer process to change the accessibility mode.
272 void SetAccessibilityMode(AccessibilityMode AccessibilityMode);
274 // Turn on accessibility testing. The given callback will be run
275 // every time an accessibility notification is received from the
276 // renderer process, and the accessibility tree it sent can be
277 // retrieved using GetAXTreeForTesting().
278 void SetAccessibilityCallbackForTesting(
279 const base::Callback<void(ui::AXEvent, int)>& callback);
281 // Returns a snapshot of the accessibility tree received from the
282 // renderer as of the last time an accessibility notification was
284 const ui::AXTree* GetAXTreeForTesting();
286 // Access the BrowserAccessibilityManager if it already exists.
287 BrowserAccessibilityManager* browser_accessibility_manager() const {
288 return browser_accessibility_manager_.get();
291 // If accessibility is enabled, get the BrowserAccessibilityManager for
292 // this frame, or create one if it doesn't exist yet, otherwise return
294 BrowserAccessibilityManager* GetOrCreateBrowserAccessibilityManager();
297 void SetParentNativeViewAccessible(
298 gfx::NativeViewAccessible accessible_parent);
299 gfx::NativeViewAccessible GetParentNativeViewAccessible() const;
300 #elif defined(OS_MACOSX)
301 // Select popup menu related methods (for external popup menus).
302 void DidSelectPopupMenuItem(int selected_index);
303 void DidCancelPopupMenu();
304 #elif defined(OS_ANDROID)
305 void DidSelectPopupMenuItems(const std::vector<int>& selected_indices);
306 void DidCancelPopupMenu();
310 friend class RenderFrameHostFactory;
312 // TODO(nasko): Remove dependency on RenderViewHost here. RenderProcessHost
313 // should be the abstraction needed here, but we need RenderViewHost to pass
314 // into WebContentsObserver::FrameDetached for now.
315 RenderFrameHostImpl(RenderViewHostImpl* render_view_host,
316 RenderFrameHostDelegate* delegate,
317 FrameTree* frame_tree,
318 FrameTreeNode* frame_tree_node,
320 bool is_swapped_out);
323 friend class TestRenderFrameHost;
324 friend class TestRenderViewHost;
326 FRIEND_TEST_ALL_PREFIXES(SitePerProcessBrowserTest, CrashSubframe);
328 // IPC Message handlers.
329 void OnAddMessageToConsole(int32 level,
330 const base::string16& message,
332 const base::string16& source_id);
334 void OnFrameFocused();
335 void OnOpenURL(const FrameHostMsg_OpenURL_Params& params);
336 void OnDocumentOnLoadCompleted();
337 void OnDidStartProvisionalLoadForFrame(const GURL& url,
338 bool is_transition_navigation);
339 void OnDidFailProvisionalLoadWithError(
340 const FrameHostMsg_DidFailProvisionalLoadWithError_Params& params);
341 void OnDidFailLoadWithError(
344 const base::string16& error_description);
345 void OnDidCommitProvisionalLoad(const IPC::Message& msg);
346 void OnBeforeUnloadACK(
348 const base::TimeTicks& renderer_before_unload_start_time,
349 const base::TimeTicks& renderer_before_unload_end_time);
351 void OnContextMenu(const ContextMenuParams& params);
352 void OnJavaScriptExecuteResponse(int id, const base::ListValue& result);
353 void OnRunJavaScriptMessage(const base::string16& message,
354 const base::string16& default_prompt,
355 const GURL& frame_url,
356 JavaScriptMessageType type,
357 IPC::Message* reply_msg);
358 void OnRunBeforeUnloadConfirm(const GURL& frame_url,
359 const base::string16& message,
361 IPC::Message* reply_msg);
362 void OnRequestPlatformNotificationPermission(const GURL& origin,
364 void OnShowDesktopNotification(
366 const ShowDesktopNotificationHostMsgParams& params);
367 void OnCancelDesktopNotification(int notification_id);
368 void OnTextSurroundingSelectionResponse(const base::string16& content,
371 void OnDidAccessInitialDocument();
372 void OnDidDisownOpener();
373 void OnDidAssignPageId(int32 page_id);
374 void OnUpdateTitle(int32 page_id,
375 const base::string16& title,
376 blink::WebTextDirection title_direction);
377 void OnUpdateEncoding(const std::string& encoding);
378 void OnBeginNavigation(
379 const FrameHostMsg_BeginNavigation_Params& params);
380 void OnAccessibilityEvents(
381 const std::vector<AccessibilityHostMsg_EventParams>& params);
382 void OnAccessibilityLocationChanges(
383 const std::vector<AccessibilityHostMsg_LocationChangeParams>& params);
385 #if defined(OS_MACOSX) || defined(OS_ANDROID)
386 void OnShowPopup(const FrameHostMsg_ShowPopup_Params& params);
390 // Returns whether the given URL is allowed to commit in the current process.
391 // This is a more conservative check than RenderProcessHost::FilterURL, since
392 // it will be used to kill processes that commit unauthorized URLs.
393 bool CanCommitURL(const GURL& url);
395 void PlatformNotificationPermissionRequestDone(
396 int request_id, blink::WebNotificationPermission permission);
398 // Update the the singleton FrameAccessibility instance with a map
399 // from accessibility node id to the frame routing id of a cross-process
401 void UpdateCrossProcessIframeAccessibility(
402 const std::map<int32, int> node_to_frame_routing_id_map);
404 // Update the the singleton FrameAccessibility instance with a map
405 // from accessibility node id to the browser plugin instance id of a
406 // guest WebContents.
407 void UpdateGuestFrameAccessibility(
408 const std::map<int32, int> node_to_browser_plugin_instance_id_map);
410 // For now, RenderFrameHosts indirectly keep RenderViewHosts alive via a
411 // refcount that calls Shutdown when it reaches zero. This allows each
412 // RenderFrameHostManager to just care about RenderFrameHosts, while ensuring
413 // we have a RenderViewHost for each RenderFrameHost.
414 // TODO(creis): RenderViewHost will eventually go away and be replaced with
415 // some form of page context.
416 RenderViewHostImpl* render_view_host_;
418 RenderFrameHostDelegate* delegate_;
420 // |cross_process_frame_connector_| passes messages from an out-of-process
421 // child frame to the parent process for compositing.
423 // This is only non-NULL when this is the swapped out RenderFrameHost in
424 // the same site instance as this frame's parent.
426 // See the class comment above CrossProcessFrameConnector for more
429 // This will move to RenderFrameProxyHost when that class is created.
430 CrossProcessFrameConnector* cross_process_frame_connector_;
432 // The proxy created for this RenderFrameHost. It is used to send and receive
433 // IPC messages while in swapped out state.
434 // TODO(nasko): This can be removed once we don't have a swapped out state on
435 // RenderFrameHosts. See https://crbug.com/357747.
436 RenderFrameProxyHost* render_frame_proxy_host_;
438 // Reference to the whole frame tree that this RenderFrameHost belongs to.
439 // Allows this RenderFrameHost to add and remove nodes in response to
440 // messages from the renderer requesting DOM manipulation.
441 FrameTree* frame_tree_;
443 // The FrameTreeNode which this RenderFrameHostImpl is hosted in.
444 FrameTreeNode* frame_tree_node_;
446 // The mapping of pending JavaScript calls created by
447 // ExecuteJavaScript and their corresponding callbacks.
448 std::map<int, JavaScriptResultCallback> javascript_callbacks_;
450 // Map from notification_id to a callback to cancel them.
451 std::map<int, base::Closure> cancel_notification_callbacks_;
454 bool is_swapped_out_;
456 // Tracks whether the RenderFrame for this RenderFrameHost has been created in
457 // the renderer process. Currently only used for subframes.
458 // TODO(creis): Use this for main frames as well when RVH goes away.
459 bool render_frame_created_;
461 // Whether we should buffer outgoing Navigate messages rather than sending
462 // them. This will be true when a RenderFrameHost is created for a cross-site
463 // request, until we hear back from the onbeforeunload handler of the old
465 bool navigations_suspended_;
467 // We only buffer the params for a suspended navigation while this RFH is the
468 // pending RenderFrameHost of a RenderFrameHostManager. There will only ever
469 // be one suspended navigation, because RenderFrameHostManager will destroy
470 // the pending RenderFrameHost and create a new one if a second navigation
472 scoped_ptr<FrameMsg_Navigate_Params> suspended_nav_params_;
474 // When the last BeforeUnload message was sent.
475 base::TimeTicks send_before_unload_start_time_;
477 ServiceRegistryImpl service_registry_;
479 scoped_ptr<BrowserAccessibilityManager> browser_accessibility_manager_;
481 // Callback when an event is received, for testing.
482 base::Callback<void(ui::AXEvent, int)> accessibility_testing_callback_;
483 // The most recently received accessibility tree - for testing only.
484 scoped_ptr<ui::AXTree> ax_tree_for_testing_;
486 // NOTE: This must be the last member.
487 base::WeakPtrFactory<RenderFrameHostImpl> weak_ptr_factory_;
489 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostImpl);
492 } // namespace content
494 #endif // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_IMPL_H_