render_frame_host_impl.h revision 116680a4aac90f2aa7413d9095a592090648e557
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/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 "third_party/WebKit/public/web/WebTextDirection.h"
24#include "ui/accessibility/ax_node_data.h"
25
26class GURL;
27struct AccessibilityHostMsg_EventParams;
28struct AccessibilityHostMsg_LocationChangeParams;
29struct FrameHostMsg_DidFailProvisionalLoadWithError_Params;
30struct FrameHostMsg_OpenURL_Params;
31struct FrameHostMsg_BeginNavigation_Params;
32struct FrameMsg_Navigate_Params;
33
34namespace base {
35class FilePath;
36class ListValue;
37}
38
39namespace content {
40
41class CrossProcessFrameConnector;
42class CrossSiteTransferringRequest;
43class FrameTree;
44class FrameTreeNode;
45class RenderFrameHostDelegate;
46class RenderFrameProxyHost;
47class RenderProcessHost;
48class RenderViewHostImpl;
49class RenderWidgetHostImpl;
50struct ContextMenuParams;
51struct GlobalRequestID;
52struct Referrer;
53struct ShowDesktopNotificationHostMsgParams;
54
55class CONTENT_EXPORT RenderFrameHostImpl
56    : public RenderFrameHost,
57      public BrowserAccessibilityDelegate {
58 public:
59  static RenderFrameHostImpl* FromID(int process_id, int routing_id);
60
61  virtual ~RenderFrameHostImpl();
62
63  // RenderFrameHost
64  virtual int GetRoutingID() OVERRIDE;
65  virtual SiteInstance* GetSiteInstance() OVERRIDE;
66  virtual RenderProcessHost* GetProcess() OVERRIDE;
67  virtual RenderFrameHost* GetParent() OVERRIDE;
68  virtual const std::string& GetFrameName() OVERRIDE;
69  virtual bool IsCrossProcessSubframe() OVERRIDE;
70  virtual GURL GetLastCommittedURL() OVERRIDE;
71  virtual gfx::NativeView GetNativeView() OVERRIDE;
72  virtual void ExecuteJavaScript(
73      const base::string16& javascript) OVERRIDE;
74  virtual void ExecuteJavaScript(
75      const base::string16& javascript,
76      const JavaScriptResultCallback& callback) OVERRIDE;
77  virtual RenderViewHost* GetRenderViewHost() OVERRIDE;
78  virtual ServiceRegistry* GetServiceRegistry() OVERRIDE;
79
80  // IPC::Sender
81  virtual bool Send(IPC::Message* msg) OVERRIDE;
82
83  // IPC::Listener
84  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
85
86  // BrowserAccessibilityDelegate
87  virtual void AccessibilitySetFocus(int acc_obj_id) OVERRIDE;
88  virtual void AccessibilityDoDefaultAction(int acc_obj_id) OVERRIDE;
89  virtual void AccessibilityShowMenu(const gfx::Point& global_point) OVERRIDE;
90  virtual void AccessibilityScrollToMakeVisible(
91      int acc_obj_id, const gfx::Rect& subfocus) OVERRIDE;
92  virtual void AccessibilityScrollToPoint(
93      int acc_obj_id, const gfx::Point& point) OVERRIDE;
94  virtual void AccessibilitySetTextSelection(
95      int acc_obj_id, int start_offset, int end_offset) OVERRIDE;
96  virtual bool AccessibilityViewHasFocus() const OVERRIDE;
97  virtual gfx::Rect AccessibilityGetViewBounds() const OVERRIDE;
98  virtual gfx::Point AccessibilityOriginInScreen(const gfx::Rect& bounds)
99      const OVERRIDE;
100  virtual void AccessibilityHitTest(const gfx::Point& point) OVERRIDE;
101  virtual void AccessibilityFatalError() OVERRIDE;
102
103  void Init();
104  int routing_id() const { return routing_id_; }
105  void OnCreateChildFrame(int new_routing_id,
106                          const std::string& frame_name);
107
108  RenderViewHostImpl* render_view_host() { return render_view_host_; }
109  RenderFrameHostDelegate* delegate() { return delegate_; }
110  FrameTreeNode* frame_tree_node() { return frame_tree_node_; }
111  // TODO(nasko): The RenderWidgetHost will be owned by RenderFrameHost in
112  // the future, so update this accessor to return the right pointer.
113  RenderWidgetHostImpl* GetRenderWidgetHost();
114
115  // This function is called when this is a swapped out RenderFrameHost that
116  // lives in the same process as the parent frame. The
117  // |cross_process_frame_connector| allows the non-swapped-out
118  // RenderFrameHost for a frame to communicate with the parent process
119  // so that it may composite drawing data.
120  //
121  // Ownership is not transfered.
122  void set_cross_process_frame_connector(
123      CrossProcessFrameConnector* cross_process_frame_connector) {
124    cross_process_frame_connector_ = cross_process_frame_connector;
125  }
126
127  void set_render_frame_proxy_host(RenderFrameProxyHost* proxy) {
128    render_frame_proxy_host_ = proxy;
129  }
130
131  // Returns a bitwise OR of bindings types that have been enabled for this
132  // RenderFrameHostImpl's RenderView. See BindingsPolicy for details.
133  // TODO(creis): Make bindings frame-specific, to support cases like <webview>.
134  int GetEnabledBindings();
135
136  // Called on the pending RenderFrameHost when the network response is ready to
137  // commit.  We should ensure that the old RenderFrameHost runs its unload
138  // handler and determine whether a transfer to a different RenderFrameHost is
139  // needed.
140  void OnCrossSiteResponse(
141      const GlobalRequestID& global_request_id,
142      scoped_ptr<CrossSiteTransferringRequest> cross_site_transferring_request,
143      const std::vector<GURL>& transfer_url_chain,
144      const Referrer& referrer,
145      PageTransition page_transition,
146      bool should_replace_current_entry);
147
148  // Called on the current RenderFrameHost when the network response is first
149  // receieved.
150  void OnDeferredAfterResponseStarted(const GlobalRequestID& global_request_id);
151
152  // Tells the renderer that this RenderFrame is being swapped out for one in a
153  // different renderer process.  It should run its unload handler, move to
154  // a blank document and create a RenderFrameProxy to replace the RenderFrame.
155  // The renderer should preserve the Proxy object until it exits, in case we
156  // come back.  The renderer can exit if it has no other active RenderFrames,
157  // but not until WasSwappedOut is called (when it is no longer visible).
158  void SwapOut(RenderFrameProxyHost* proxy);
159
160  void OnSwappedOut(bool timed_out);
161  bool is_swapped_out() { return is_swapped_out_; }
162  void set_swapped_out(bool is_swapped_out) {
163    is_swapped_out_ = is_swapped_out;
164  }
165
166  // Sets the RVH for |this| as pending shutdown. |on_swap_out| will be called
167  // when the SwapOutACK is received.
168  void SetPendingShutdown(const base::Closure& on_swap_out);
169
170  // Sends the given navigation message. Use this rather than sending it
171  // yourself since this does the internal bookkeeping described below. This
172  // function takes ownership of the provided message pointer.
173  //
174  // If a cross-site request is in progress, we may be suspended while waiting
175  // for the onbeforeunload handler, so this function might buffer the message
176  // rather than sending it.
177  void Navigate(const FrameMsg_Navigate_Params& params);
178
179  // Load the specified URL; this is a shortcut for Navigate().
180  void NavigateToURL(const GURL& url);
181
182  // Runs the beforeunload handler for this frame. |for_cross_site_transition|
183  // indicates whether this call is for the current frame during a cross-process
184  // navigation. False means we're closing the entire tab.
185  void DispatchBeforeUnload(bool for_cross_site_transition);
186
187  // Deletes the current selection plus the specified number of characters
188  // before and after the selection or caret.
189  void ExtendSelectionAndDelete(size_t before, size_t after);
190
191  // Notifies the RenderFrame that the JavaScript message that was shown was
192  // closed by the user.
193  void JavaScriptDialogClosed(IPC::Message* reply_msg,
194                              bool success,
195                              const base::string16& user_input,
196                              bool dialog_was_suppressed);
197
198  // Called when an HTML5 notification is closed.
199  void NotificationClosed(int notification_id);
200
201  // Sets whether there is an outstanding transition request. This is called at
202  // the start of a provisional load for the main frame, and cleared when we
203  // hear the response or commit.
204  void SetHasPendingTransitionRequest(bool has_pending_request);
205
206  // Send a message to the renderer process to change the accessibility mode.
207  void SetAccessibilityMode(AccessibilityMode AccessibilityMode);
208
209  // Turn on accessibility testing. The given callback will be run
210  // every time an accessibility notification is received from the
211  // renderer process, and the accessibility tree it sent can be
212  // retrieved using GetAXTreeForTesting().
213  void SetAccessibilityCallbackForTesting(
214      const base::Callback<void(ui::AXEvent, int)>& callback);
215
216  // Returns a snapshot of the accessibility tree received from the
217  // renderer as of the last time an accessibility notification was
218  // received.
219  const ui::AXTree* GetAXTreeForTesting();
220
221  // Access the BrowserAccessibilityManager if it already exists.
222  BrowserAccessibilityManager* browser_accessibility_manager() const {
223    return browser_accessibility_manager_.get();
224  }
225
226  // If accessibility is enabled, get the BrowserAccessibilityManager for
227  // this frame, or create one if it doesn't exist yet, otherwise return
228  // NULL.
229  BrowserAccessibilityManager* GetOrCreateBrowserAccessibilityManager();
230
231#if defined(OS_WIN)
232  void SetParentNativeViewAccessible(
233      gfx::NativeViewAccessible accessible_parent);
234  gfx::NativeViewAccessible GetParentNativeViewAccessible() const;
235#endif
236
237 protected:
238  friend class RenderFrameHostFactory;
239
240  // TODO(nasko): Remove dependency on RenderViewHost here. RenderProcessHost
241  // should be the abstraction needed here, but we need RenderViewHost to pass
242  // into WebContentsObserver::FrameDetached for now.
243  RenderFrameHostImpl(RenderViewHostImpl* render_view_host,
244                      RenderFrameHostDelegate* delegate,
245                      FrameTree* frame_tree,
246                      FrameTreeNode* frame_tree_node,
247                      int routing_id,
248                      bool is_swapped_out);
249
250 private:
251  friend class TestRenderFrameHost;
252  friend class TestRenderViewHost;
253
254  // IPC Message handlers.
255  void OnAddMessageToConsole(int32 level,
256                             const base::string16& message,
257                             int32 line_no,
258                             const base::string16& source_id);
259  void OnDetach();
260  void OnFrameFocused();
261  void OnOpenURL(const FrameHostMsg_OpenURL_Params& params);
262  void OnDocumentOnLoadCompleted();
263  void OnDidStartProvisionalLoadForFrame(const GURL& url);
264  void OnDidFailProvisionalLoadWithError(
265      const FrameHostMsg_DidFailProvisionalLoadWithError_Params& params);
266  void OnDidFailLoadWithError(
267      const GURL& url,
268      int error_code,
269      const base::string16& error_description);
270  void OnDidRedirectProvisionalLoad(int32 page_id,
271                                    const GURL& source_url,
272                                    const GURL& target_url);
273  void OnNavigate(const IPC::Message& msg);
274  void OnBeforeUnloadACK(
275      bool proceed,
276      const base::TimeTicks& renderer_before_unload_start_time,
277      const base::TimeTicks& renderer_before_unload_end_time);
278  void OnSwapOutACK();
279  void OnContextMenu(const ContextMenuParams& params);
280  void OnJavaScriptExecuteResponse(int id, const base::ListValue& result);
281  void OnRunJavaScriptMessage(const base::string16& message,
282                              const base::string16& default_prompt,
283                              const GURL& frame_url,
284                              JavaScriptMessageType type,
285                              IPC::Message* reply_msg);
286  void OnRunBeforeUnloadConfirm(const GURL& frame_url,
287                                const base::string16& message,
288                                bool is_reload,
289                                IPC::Message* reply_msg);
290  void OnRequestDesktopNotificationPermission(const GURL& origin,
291                                              int callback_id);
292  void OnShowDesktopNotification(
293      int notification_id,
294      const ShowDesktopNotificationHostMsgParams& params);
295  void OnCancelDesktopNotification(int notification_id);
296  void OnTextSurroundingSelectionResponse(const base::string16& content,
297                                          size_t start_offset,
298                                          size_t end_offset);
299  void OnDidAccessInitialDocument();
300  void OnDidDisownOpener();
301  void OnUpdateTitle(int32 page_id,
302                     const base::string16& title,
303                     blink::WebTextDirection title_direction);
304  void OnUpdateEncoding(const std::string& encoding);
305  void OnBeginNavigation(
306      const FrameHostMsg_BeginNavigation_Params& params);
307  void OnAccessibilityEvents(
308      const std::vector<AccessibilityHostMsg_EventParams>& params);
309  void OnAccessibilityLocationChanges(
310      const std::vector<AccessibilityHostMsg_LocationChangeParams>& params);
311
312  // Returns whether the given URL is allowed to commit in the current process.
313  // This is a more conservative check than RenderProcessHost::FilterURL, since
314  // it will be used to kill processes that commit unauthorized URLs.
315  bool CanCommitURL(const GURL& url);
316
317  void DesktopNotificationPermissionRequestDone(int callback_context);
318
319  // For now, RenderFrameHosts indirectly keep RenderViewHosts alive via a
320  // refcount that calls Shutdown when it reaches zero.  This allows each
321  // RenderFrameHostManager to just care about RenderFrameHosts, while ensuring
322  // we have a RenderViewHost for each RenderFrameHost.
323  // TODO(creis): RenderViewHost will eventually go away and be replaced with
324  // some form of page context.
325  RenderViewHostImpl* render_view_host_;
326
327  RenderFrameHostDelegate* delegate_;
328
329  // |cross_process_frame_connector_| passes messages from an out-of-process
330  // child frame to the parent process for compositing.
331  //
332  // This is only non-NULL when this is the swapped out RenderFrameHost in
333  // the same site instance as this frame's parent.
334  //
335  // See the class comment above CrossProcessFrameConnector for more
336  // information.
337  //
338  // This will move to RenderFrameProxyHost when that class is created.
339  CrossProcessFrameConnector* cross_process_frame_connector_;
340
341  // The proxy created for this RenderFrameHost. It is used to send and receive
342  // IPC messages while in swapped out state.
343  // TODO(nasko): This can be removed once we don't have a swapped out state on
344  // RenderFrameHosts. See https://crbug.com/357747.
345  RenderFrameProxyHost* render_frame_proxy_host_;
346
347  // Reference to the whole frame tree that this RenderFrameHost belongs to.
348  // Allows this RenderFrameHost to add and remove nodes in response to
349  // messages from the renderer requesting DOM manipulation.
350  FrameTree* frame_tree_;
351
352  // The FrameTreeNode which this RenderFrameHostImpl is hosted in.
353  FrameTreeNode* frame_tree_node_;
354
355  // The mapping of pending JavaScript calls created by
356  // ExecuteJavaScript and their corresponding callbacks.
357  std::map<int, JavaScriptResultCallback> javascript_callbacks_;
358
359  // Map from notification_id to a callback to cancel them.
360  std::map<int, base::Closure> cancel_notification_callbacks_;
361
362  int routing_id_;
363  bool is_swapped_out_;
364
365  // When the last BeforeUnload message was sent.
366  base::TimeTicks send_before_unload_start_time_;
367
368  ServiceRegistryImpl service_registry_;
369
370  base::WeakPtrFactory<RenderFrameHostImpl> weak_ptr_factory_;
371
372  scoped_ptr<BrowserAccessibilityManager> browser_accessibility_manager_;
373
374  // Callback when an event is received, for testing.
375  base::Callback<void(ui::AXEvent, int)> accessibility_testing_callback_;
376  // The most recently received accessibility tree - for testing only.
377  scoped_ptr<ui::AXTree> ax_tree_for_testing_;
378
379  DISALLOW_COPY_AND_ASSIGN(RenderFrameHostImpl);
380};
381
382}  // namespace content
383
384#endif  // CONTENT_BROWSER_FRAME_HOST_RENDER_FRAME_HOST_IMPL_H_
385