Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / renderer_host / render_view_host_impl.h
blob08c12eb2f10c40beea33798b41a8fcdb57252210
1 // Copyright (c) 2012 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_RENDERER_HOST_RENDER_VIEW_HOST_IMPL_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_IMPL_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/process/kill.h"
17 #include "content/browser/renderer_host/render_widget_host_impl.h"
18 #include "content/browser/site_instance_impl.h"
19 #include "content/common/drag_event_source_info.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/render_process_host_observer.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/common/window_container_type.h"
24 #include "net/base/load_states.h"
25 #include "third_party/WebKit/public/web/WebAXEnums.h"
26 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
27 #include "third_party/WebKit/public/web/WebPopupType.h"
28 #include "third_party/skia/include/core/SkColor.h"
29 #include "ui/base/window_open_disposition.h"
31 class SkBitmap;
32 class FrameMsg_Navigate;
33 struct MediaPlayerAction;
34 struct ViewHostMsg_CreateWindow_Params;
35 struct ViewMsg_PostMessage_Params;
37 namespace base {
38 class ListValue;
41 namespace gfx {
42 class Range;
45 namespace ui {
46 class AXTree;
49 namespace content {
51 class MediaWebContentsObserver;
52 class ChildProcessSecurityPolicyImpl;
53 class PageState;
54 class RenderWidgetHostDelegate;
55 class SessionStorageNamespace;
56 class SessionStorageNamespaceImpl;
57 class TestRenderViewHost;
58 struct FileChooserFileInfo;
59 struct FileChooserParams;
60 struct FrameReplicationState;
62 #if defined(COMPILER_MSVC)
63 // RenderViewHostImpl is the bottom of a diamond-shaped hierarchy,
64 // with RenderWidgetHost at the root. VS warns when methods from the
65 // root are overridden in only one of the base classes and not both
66 // (in this case, RenderWidgetHostImpl provides implementations of
67 // many of the methods). This is a silly warning when dealing with
68 // pure virtual methods that only have a single implementation in the
69 // hierarchy above this class, and is safe to ignore in this case.
70 #pragma warning(push)
71 #pragma warning(disable: 4250)
72 #endif
74 // This implements the RenderViewHost interface that is exposed to
75 // embedders of content, and adds things only visible to content.
77 // The exact API of this object needs to be more thoroughly designed. Right
78 // now it mimics what WebContentsImpl exposed, which is a fairly large API and
79 // may contain things that are not relevant to a common subset of views. See
80 // also the comment in render_view_host_delegate.h about the size and scope of
81 // the delegate API.
83 // Right now, the concept of page navigation (both top level and frame) exists
84 // in the WebContentsImpl still, so if you instantiate one of these elsewhere,
85 // you will not be able to traverse pages back and forward. We need to determine
86 // if we want to bring that and other functionality down into this object so it
87 // can be shared by others.
89 // DEPRECATED: RenderViewHostImpl is being removed as part of the SiteIsolation
90 // project. New code should not be added here, but to either RenderFrameHostImpl
91 // (if frame specific) or WebContentsImpl (if page specific).
93 // For context, please see https://crbug.com/467770 and
94 // http://www.chromium.org/developers/design-documents/site-isolation.
95 class CONTENT_EXPORT RenderViewHostImpl
96 : public RenderViewHost,
97 public RenderWidgetHostImpl,
98 public RenderProcessHostObserver {
99 public:
100 // Convenience function, just like RenderViewHost::FromID.
101 static RenderViewHostImpl* FromID(int render_process_id, int render_view_id);
103 // |routing_id| could be a valid route id, or it could be MSG_ROUTING_NONE, in
104 // which case RenderWidgetHost will create a new one. |swapped_out| indicates
105 // whether the view should initially be swapped out (e.g., for an opener
106 // frame being rendered by another process). |hidden| indicates whether the
107 // view is initially hidden or visible.
109 // The |session_storage_namespace| parameter allows multiple render views and
110 // WebContentses to share the same session storage (part of the WebStorage
111 // spec) space. This is useful when restoring contentses, but most callers
112 // should pass in NULL which will cause a new SessionStorageNamespace to be
113 // created.
114 RenderViewHostImpl(SiteInstance* instance,
115 RenderViewHostDelegate* delegate,
116 RenderWidgetHostDelegate* widget_delegate,
117 int32 routing_id,
118 int32 surface_id,
119 int32 main_frame_routing_id,
120 bool swapped_out,
121 bool hidden,
122 bool has_initialized_audio_host);
123 ~RenderViewHostImpl() override;
125 // RenderViewHost implementation.
126 RenderFrameHost* GetMainFrame() override;
127 void AllowBindings(int binding_flags) override;
128 void ClearFocusedElement() override;
129 bool IsFocusedElementEditable() override;
130 void CopyImageAt(int x, int y) override;
131 void SaveImageAt(int x, int y) override;
132 void DirectoryEnumerationFinished(
133 int request_id,
134 const std::vector<base::FilePath>& files) override;
135 void DisableScrollbarsForThreshold(const gfx::Size& size) override;
136 void DragSourceEndedAt(int client_x,
137 int client_y,
138 int screen_x,
139 int screen_y,
140 blink::WebDragOperation operation) override;
141 void DragSourceSystemDragEnded() override;
142 void DragTargetDragEnter(const DropData& drop_data,
143 const gfx::Point& client_pt,
144 const gfx::Point& screen_pt,
145 blink::WebDragOperationsMask operations_allowed,
146 int key_modifiers) override;
147 void DragTargetDragOver(const gfx::Point& client_pt,
148 const gfx::Point& screen_pt,
149 blink::WebDragOperationsMask operations_allowed,
150 int key_modifiers) override;
151 void DragTargetDragLeave() override;
152 void DragTargetDrop(const gfx::Point& client_pt,
153 const gfx::Point& screen_pt,
154 int key_modifiers) override;
155 void EnableAutoResize(const gfx::Size& min_size,
156 const gfx::Size& max_size) override;
157 void DisableAutoResize(const gfx::Size& new_size) override;
158 void EnablePreferredSizeMode() override;
159 void ExecuteMediaPlayerActionAtLocation(
160 const gfx::Point& location,
161 const blink::WebMediaPlayerAction& action) override;
162 void ExecutePluginActionAtLocation(
163 const gfx::Point& location,
164 const blink::WebPluginAction& action) override;
165 void FilesSelectedInChooser(
166 const std::vector<content::FileChooserFileInfo>& files,
167 FileChooserParams::Mode permissions) override;
168 RenderViewHostDelegate* GetDelegate() const override;
169 int GetEnabledBindings() const override;
170 SiteInstanceImpl* GetSiteInstance() const override;
171 bool IsRenderViewLive() const override;
172 void NotifyMoveOrResizeStarted() override;
173 void SetWebUIProperty(const std::string& name,
174 const std::string& value) override;
175 void Zoom(PageZoom zoom) override;
176 void SyncRendererPrefs() override;
177 WebPreferences GetWebkitPreferences() override;
178 void UpdateWebkitPreferences(const WebPreferences& prefs) override;
179 void OnWebkitPreferencesChanged() override;
180 void SelectWordAroundCaret() override;
182 #if defined(OS_ANDROID)
183 void ActivateNearestFindResult(int request_id, float x, float y) override;
184 void RequestFindMatchRects(int current_version) override;
185 #endif
187 // RenderProcessHostObserver implementation
188 void RenderProcessExited(RenderProcessHost* host,
189 base::TerminationStatus status,
190 int exit_code) override;
192 void set_delegate(RenderViewHostDelegate* d) {
193 CHECK(d); // http://crbug.com/82827
194 delegate_ = d;
197 // Set up the RenderView child process. Virtual because it is overridden by
198 // TestRenderViewHost.
199 // The |opener_route_id| parameter indicates which RenderView created this
200 // (MSG_ROUTING_NONE if none). If |max_page_id| is larger than -1, the
201 // RenderView is told to start issuing page IDs at |max_page_id| + 1.
202 // |window_was_created_with_opener| is true if this top-level frame was
203 // created with an opener. (The opener may have been closed since.)
204 // The |proxy_route_id| is only used when creating a RenderView in swapped out
205 // state. |replicated_frame_state| contains replicated data for the
206 // top-level frame, such as its name and sandbox flags.
207 virtual bool CreateRenderView(
208 int opener_frame_route_id,
209 int proxy_route_id,
210 int32 max_page_id,
211 const FrameReplicationState& replicated_frame_state,
212 bool window_was_created_with_opener);
214 base::TerminationStatus render_view_termination_status() const {
215 return render_view_termination_status_;
218 // Tracks whether this RenderViewHost is in an active state (rather than
219 // pending swap out, pending deletion, or swapped out), according to its main
220 // frame RenderFrameHost.
221 bool is_active() const { return is_active_; }
222 void set_is_active(bool is_active) { is_active_ = is_active; }
224 // Tracks whether this RenderViewHost is swapped out, according to its main
225 // frame RenderFrameHost.
226 void set_is_swapped_out(bool is_swapped_out) {
227 is_swapped_out_ = is_swapped_out;
230 // TODO(creis): Remove as part of http://crbug.com/418265.
231 bool is_waiting_for_close_ack() const { return is_waiting_for_close_ack_; }
233 // Tells the renderer that this RenderView will soon be swapped out, and thus
234 // not to create any new modal dialogs until it happens. This must be done
235 // separately so that the PageGroupLoadDeferrers of any current dialogs are no
236 // longer on the stack when we attempt to swap it out.
237 void SuppressDialogsUntilSwapOut();
239 // Tells the renderer process to run the page's unload handler.
240 // A ClosePage_ACK ack is sent back when the handler execution completes.
241 void ClosePage();
243 // Close the page ignoring whether it has unload events registers.
244 // This is called after the beforeunload and unload events have fired
245 // and the user has agreed to continue with closing the page.
246 void ClosePageIgnoringUnloadEvents();
248 // Tells the renderer view to focus the first (last if reverse is true) node.
249 void SetInitialFocus(bool reverse);
251 // Get html data by serializing all frames of current page with lists
252 // which contain all resource links that have local copy.
253 // The parameter links contain original URLs of all saved links.
254 // The parameter local_paths contain corresponding local file paths of
255 // all saved links, which matched with vector:links one by one.
256 // The parameter local_directory_name is relative path of directory which
257 // contain all saved auxiliary files included all sub frames and resouces.
258 void GetSerializedHtmlDataForCurrentPageWithLocalLinks(
259 const std::vector<GURL>& links,
260 const std::vector<base::FilePath>& local_paths,
261 const base::FilePath& local_directory_name);
263 // Notifies the RenderViewHost that its load state changed.
264 void LoadStateChanged(const GURL& url,
265 const net::LoadStateWithParam& load_state,
266 uint64 upload_position,
267 uint64 upload_size);
269 bool SuddenTerminationAllowed() const;
270 void set_sudden_termination_allowed(bool enabled) {
271 sudden_termination_allowed_ = enabled;
274 // RenderWidgetHost public overrides.
275 void Init() override;
276 void Shutdown() override;
277 void WasHidden() override;
278 void WasShown(const ui::LatencyInfo& latency_info) override;
279 bool IsRenderView() const override;
280 bool OnMessageReceived(const IPC::Message& msg) override;
281 void GotFocus() override;
282 void LostCapture() override;
283 void LostMouseLock() override;
284 void SetIsLoading(bool is_loading) override;
285 void ForwardMouseEvent(const blink::WebMouseEvent& mouse_event) override;
286 void ForwardKeyboardEvent(const NativeWebKeyboardEvent& key_event) override;
287 gfx::Rect GetRootWindowResizerRect() const override;
289 // Creates a new RenderView with the given route id.
290 void CreateNewWindow(
291 int route_id,
292 int main_frame_route_id,
293 const ViewHostMsg_CreateWindow_Params& params,
294 SessionStorageNamespace* session_storage_namespace);
296 // Creates a new RenderWidget with the given route id. |popup_type| indicates
297 // if this widget is a popup and what kind of popup it is (select, autofill).
298 void CreateNewWidget(int32 route_id,
299 int32 surface_id,
300 blink::WebPopupType popup_type);
302 // Creates a full screen RenderWidget.
303 void CreateNewFullscreenWidget(int32 route_id, int32 surface_id);
305 void set_main_frame_routing_id(int routing_id) {
306 main_frame_routing_id_ = routing_id;
309 void OnTextSurroundingSelectionResponse(const base::string16& content,
310 size_t start_offset,
311 size_t end_offset);
313 // Increases the refcounting on this RVH. This is done by the FrameTree on
314 // creation of a RenderFrameHost.
315 void increment_ref_count() { ++frames_ref_count_; }
317 // Decreases the refcounting on this RVH. This is done by the FrameTree on
318 // destruction of a RenderFrameHost.
319 void decrement_ref_count() { --frames_ref_count_; }
321 // Returns the refcount on this RVH, that is the number of RenderFrameHosts
322 // currently using it.
323 int ref_count() { return frames_ref_count_; }
325 // TODO(avi): Move to RenderFrameHost once PageState is broken up into
326 // FrameStates.
327 int nav_entry_id() const { return nav_entry_id_; }
328 void set_nav_entry_id(int nav_entry_id) { nav_entry_id_ = nav_entry_id; }
330 // NOTE: Do not add functions that just send an IPC message that are called in
331 // one or two places. Have the caller send the IPC message directly (unless
332 // the caller places are in different platforms, in which case it's better
333 // to keep them consistent).
335 protected:
336 // RenderWidgetHost protected overrides.
337 void OnUserGesture() override;
338 void NotifyRendererUnresponsive() override;
339 void NotifyRendererResponsive() override;
340 void OnRenderAutoResized(const gfx::Size& size) override;
341 void RequestToLockMouse(bool user_gesture,
342 bool last_unlocked_by_target) override;
343 bool IsFullscreenGranted() const override;
344 blink::WebDisplayMode GetDisplayMode() const override;
345 void OnFocus() override;
346 void OnBlur() override;
348 // IPC message handlers.
349 void OnShowView(int route_id,
350 WindowOpenDisposition disposition,
351 const gfx::Rect& initial_rect,
352 bool user_gesture);
353 void OnShowWidget(int route_id, const gfx::Rect& initial_rect);
354 void OnShowFullscreenWidget(int route_id);
355 void OnRenderViewReady();
356 void OnRenderProcessGone(int status, int error_code);
357 void OnUpdateState(int32 page_id, const PageState& state);
358 void OnUpdateTargetURL(const GURL& url);
359 void OnClose();
360 void OnRequestMove(const gfx::Rect& pos);
361 void OnDocumentAvailableInMainFrame(bool uses_temporary_zoom_level);
362 void OnDidContentsPreferredSizeChange(const gfx::Size& new_size);
363 void OnPasteFromSelectionClipboard();
364 void OnRouteCloseEvent();
365 void OnStartDragging(const DropData& drop_data,
366 blink::WebDragOperationsMask operations_allowed,
367 const SkBitmap& bitmap,
368 const gfx::Vector2d& bitmap_offset_in_dip,
369 const DragEventSourceInfo& event_info);
370 void OnUpdateDragCursor(blink::WebDragOperation drag_operation);
371 void OnTakeFocus(bool reverse);
372 void OnFocusedNodeChanged(bool is_editable_node,
373 const gfx::Rect& node_bounds_in_viewport);
374 void OnClosePageACK();
375 void OnDidZoomURL(double zoom_level, const GURL& url);
376 void OnPageScaleFactorIsOneChanged(bool is_one);
377 void OnRunFileChooser(const FileChooserParams& params);
378 void OnFocusedNodeTouched(bool editable);
380 private:
381 // TODO(nasko): Temporarily friend RenderFrameHostImpl, so we don't duplicate
382 // utility functions and state needed in both classes, while we move frame
383 // specific code away from this class.
384 friend class RenderFrameHostImpl;
385 friend class TestRenderViewHost;
386 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, BasicRenderFrameHost);
387 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, RoutingIdSane);
388 FRIEND_TEST_ALL_PREFIXES(RenderFrameHostManagerTest,
389 CleanUpSwappedOutRVHOnProcessCrash);
391 // TODO(creis): Move to a private namespace on RenderFrameHostImpl.
392 // Delay to wait on closing the WebContents for a beforeunload/unload handler
393 // to fire.
394 static const int64 kUnloadTimeoutMS;
396 // Returns the content specific prefs for this RenderViewHost.
397 // TODO(creis): Move most of this method to RenderProcessHost, since it's
398 // mostly the same across all RVHs in a process. Move the rest to RFH.
399 // See https://crbug.com/304341.
400 WebPreferences ComputeWebkitPrefs();
402 // Returns whether the current RenderProcessHost has read access to the files
403 // reported in |state|.
404 bool CanAccessFilesOfPageState(const PageState& state) const;
406 // Grants the current RenderProcessHost read access to any file listed in
407 // |validated_state|. It is important that the PageState has been validated
408 // upon receipt from the renderer process to prevent it from forging access to
409 // files without the user's consent.
410 void GrantFileAccessFromPageState(const PageState& validated_state);
412 // The number of RenderFrameHosts which have a reference to this RVH.
413 int frames_ref_count_;
415 // Our delegate, which wants to know about changes in the RenderView.
416 RenderViewHostDelegate* delegate_;
418 // The SiteInstance associated with this RenderViewHost. All pages drawn
419 // in this RenderViewHost are part of this SiteInstance. Cannot change
420 // over time.
421 scoped_refptr<SiteInstanceImpl> instance_;
423 // true if we are currently waiting for a response for drag context
424 // information.
425 bool waiting_for_drag_context_response_;
427 // A bitwise OR of bindings types that have been enabled for this RenderView.
428 // See BindingsPolicy for details.
429 int enabled_bindings_;
431 // The most recent page ID we've heard from the renderer process. This is
432 // used as context when other session history related IPCs arrive.
433 // TODO(creis): Allocate this in WebContents/NavigationController instead.
434 int32 page_id_;
436 // The unique ID of the latest NavigationEntry that this RenderViewHost is
437 // showing. TODO(avi): Move to RenderFrameHost once PageState is broken up
438 // into FrameStates.
439 int nav_entry_id_;
441 // Tracks whether this RenderViewHost is in an active state. False if the
442 // main frame is pending swap out, pending deletion, or swapped out, because
443 // it is not visible to the user in any of these cases.
444 bool is_active_;
446 // Tracks whether the main frame RenderFrameHost is swapped out. Unlike
447 // is_active_, this is false when the frame is pending swap out or deletion.
448 // TODO(creis): Remove this when we no longer use swappedout://.
449 // See http://crbug.com/357747.
450 bool is_swapped_out_;
452 // Routing ID for the main frame's RenderFrameHost.
453 int main_frame_routing_id_;
455 // Set to true when waiting for a ViewHostMsg_ClosePageACK.
456 // TODO(creis): Move to RenderFrameHost and RenderWidgetHost.
457 // See http://crbug.com/418265.
458 bool is_waiting_for_close_ack_;
460 // True if the render view can be shut down suddenly.
461 bool sudden_termination_allowed_;
463 // The termination status of the last render view that terminated.
464 base::TerminationStatus render_view_termination_status_;
466 // Set to true if we requested the on screen keyboard to be displayed.
467 bool virtual_keyboard_requested_;
469 // True if the current focused element is editable.
470 bool is_focused_element_editable_;
472 // This is updated every time UpdateWebkitPreferences is called. That method
473 // is in turn called when any of the settings change that the WebPreferences
474 // values depend on.
475 scoped_ptr<WebPreferences> web_preferences_;
477 bool updating_web_preferences_;
479 base::WeakPtrFactory<RenderViewHostImpl> weak_factory_;
481 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImpl);
484 #if defined(COMPILER_MSVC)
485 #pragma warning(pop)
486 #endif
488 } // namespace content
490 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_IMPL_H_