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