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_
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_view_host.h"
22 #include "content/public/common/window_container_type.h"
23 #include "net/base/load_states.h"
24 #include "third_party/WebKit/public/web/WebAXEnums.h"
25 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
26 #include "third_party/WebKit/public/web/WebPopupType.h"
27 #include "third_party/skia/include/core/SkColor.h"
28 #include "ui/base/window_open_disposition.h"
31 class FrameMsg_Navigate
;
32 struct MediaPlayerAction
;
33 struct ViewHostMsg_CreateWindow_Params
;
34 struct ViewMsg_PostMessage_Params
;
50 class MediaWebContentsObserver
;
51 class ChildProcessSecurityPolicyImpl
;
53 class RenderWidgetHostDelegate
;
54 class SessionStorageNamespace
;
55 class SessionStorageNamespaceImpl
;
56 class TestRenderViewHost
;
57 struct FileChooserFileInfo
;
58 struct FileChooserParams
;
60 #if defined(COMPILER_MSVC)
61 // RenderViewHostImpl is the bottom of a diamond-shaped hierarchy,
62 // with RenderWidgetHost at the root. VS warns when methods from the
63 // root are overridden in only one of the base classes and not both
64 // (in this case, RenderWidgetHostImpl provides implementations of
65 // many of the methods). This is a silly warning when dealing with
66 // pure virtual methods that only have a single implementation in the
67 // hierarchy above this class, and is safe to ignore in this case.
69 #pragma warning(disable: 4250)
72 // This implements the RenderViewHost interface that is exposed to
73 // embedders of content, and adds things only visible to content.
75 // The exact API of this object needs to be more thoroughly designed. Right
76 // now it mimics what WebContentsImpl exposed, which is a fairly large API and
77 // may contain things that are not relevant to a common subset of views. See
78 // also the comment in render_view_host_delegate.h about the size and scope of
81 // Right now, the concept of page navigation (both top level and frame) exists
82 // in the WebContentsImpl still, so if you instantiate one of these elsewhere,
83 // you will not be able to traverse pages back and forward. We need to determine
84 // if we want to bring that and other functionality down into this object so it
85 // can be shared by others.
87 // DEPRECATED: RenderViewHostImpl is being removed as part of the SiteIsolation
88 // project. New code should not be added here, but to either RenderFrameHostImpl
89 // (if frame specific) or WebContentsImpl (if page specific).
91 // For context, please see https://crbug.com/467770 and
92 // http://www.chromium.org/developers/design-documents/site-isolation.
93 class CONTENT_EXPORT RenderViewHostImpl
94 : public RenderViewHost
,
95 public RenderWidgetHostImpl
{
97 // Convenience function, just like RenderViewHost::FromID.
98 static RenderViewHostImpl
* FromID(int render_process_id
, int render_view_id
);
100 // |routing_id| could be a valid route id, or it could be MSG_ROUTING_NONE, in
101 // which case RenderWidgetHost will create a new one. |swapped_out| indicates
102 // whether the view should initially be swapped out (e.g., for an opener
103 // frame being rendered by another process). |hidden| indicates whether the
104 // view is initially hidden or visible.
106 // The |session_storage_namespace| parameter allows multiple render views and
107 // WebContentses to share the same session storage (part of the WebStorage
108 // spec) space. This is useful when restoring contentses, but most callers
109 // should pass in NULL which will cause a new SessionStorageNamespace to be
111 RenderViewHostImpl(SiteInstance
* instance
,
112 RenderViewHostDelegate
* delegate
,
113 RenderWidgetHostDelegate
* widget_delegate
,
115 int main_frame_routing_id
,
118 bool has_initialized_audio_host
);
119 ~RenderViewHostImpl() override
;
121 // RenderViewHost implementation.
122 RenderFrameHost
* GetMainFrame() override
;
123 void AllowBindings(int binding_flags
) override
;
124 void ClearFocusedElement() override
;
125 bool IsFocusedElementEditable() override
;
126 void ClosePage() override
;
127 void CopyImageAt(int x
, int y
) override
;
128 void SaveImageAt(int x
, int y
) override
;
129 void DirectoryEnumerationFinished(
131 const std::vector
<base::FilePath
>& files
) override
;
132 void DisableScrollbarsForThreshold(const gfx::Size
& size
) override
;
133 void DragSourceEndedAt(int client_x
,
137 blink::WebDragOperation operation
) override
;
138 void DragSourceSystemDragEnded() override
;
139 void DragTargetDragEnter(const DropData
& drop_data
,
140 const gfx::Point
& client_pt
,
141 const gfx::Point
& screen_pt
,
142 blink::WebDragOperationsMask operations_allowed
,
143 int key_modifiers
) override
;
144 void DragTargetDragOver(const gfx::Point
& client_pt
,
145 const gfx::Point
& screen_pt
,
146 blink::WebDragOperationsMask operations_allowed
,
147 int key_modifiers
) override
;
148 void DragTargetDragLeave() override
;
149 void DragTargetDrop(const gfx::Point
& client_pt
,
150 const gfx::Point
& screen_pt
,
151 int key_modifiers
) override
;
152 void EnableAutoResize(const gfx::Size
& min_size
,
153 const gfx::Size
& max_size
) override
;
154 void DisableAutoResize(const gfx::Size
& new_size
) override
;
155 void EnablePreferredSizeMode() override
;
156 void ExecuteMediaPlayerActionAtLocation(
157 const gfx::Point
& location
,
158 const blink::WebMediaPlayerAction
& action
) override
;
159 void ExecutePluginActionAtLocation(
160 const gfx::Point
& location
,
161 const blink::WebPluginAction
& action
) override
;
162 void FilesSelectedInChooser(
163 const std::vector
<content::FileChooserFileInfo
>& files
,
164 FileChooserParams::Mode permissions
) override
;
165 RenderViewHostDelegate
* GetDelegate() const override
;
166 int GetEnabledBindings() const override
;
167 SiteInstanceImpl
* GetSiteInstance() const override
;
168 bool IsRenderViewLive() const override
;
169 void NotifyMoveOrResizeStarted() override
;
170 void SetWebUIProperty(const std::string
& name
,
171 const std::string
& value
) override
;
172 void Zoom(PageZoom zoom
) override
;
173 void SyncRendererPrefs() override
;
174 WebPreferences
GetWebkitPreferences() override
;
175 void UpdateWebkitPreferences(const WebPreferences
& prefs
) override
;
176 void OnWebkitPreferencesChanged() override
;
177 void SelectWordAroundCaret() override
;
179 #if defined(OS_ANDROID)
180 void ActivateNearestFindResult(int request_id
, float x
, float y
) override
;
181 void RequestFindMatchRects(int current_version
) override
;
184 void set_delegate(RenderViewHostDelegate
* d
) {
185 CHECK(d
); // http://crbug.com/82827
189 // Set up the RenderView child process. Virtual because it is overridden by
190 // TestRenderViewHost. If the |frame_name| parameter is non-empty, it is used
191 // as the name of the new top-level frame.
192 // The |opener_route_id| parameter indicates which RenderView created this
193 // (MSG_ROUTING_NONE if none). If |max_page_id| is larger than -1, the
194 // RenderView is told to start issuing page IDs at |max_page_id| + 1.
195 // |window_was_created_with_opener| is true if this top-level frame was
196 // created with an opener. (The opener may have been closed since.)
197 // The |proxy_route_id| is only used when creating a RenderView in swapped out
199 virtual bool CreateRenderView(const base::string16
& frame_name
,
203 bool window_was_created_with_opener
);
205 base::TerminationStatus
render_view_termination_status() const {
206 return render_view_termination_status_
;
209 // Tracks whether this RenderViewHost is in an active state (rather than
210 // pending swap out, pending deletion, or swapped out), according to its main
211 // frame RenderFrameHost.
212 bool is_active() const { return is_active_
; }
213 void set_is_active(bool is_active
) { is_active_
= is_active
; }
215 // Tracks whether this RenderViewHost is swapped out, according to its main
216 // frame RenderFrameHost.
217 void set_is_swapped_out(bool is_swapped_out
) {
218 is_swapped_out_
= is_swapped_out
;
221 // TODO(creis): Remove as part of http://crbug.com/418265.
222 bool is_waiting_for_close_ack() const { return is_waiting_for_close_ack_
; }
224 // Tells the renderer that this RenderView will soon be swapped out, and thus
225 // not to create any new modal dialogs until it happens. This must be done
226 // separately so that the PageGroupLoadDeferrers of any current dialogs are no
227 // longer on the stack when we attempt to swap it out.
228 void SuppressDialogsUntilSwapOut();
230 // Close the page ignoring whether it has unload events registers.
231 // This is called after the beforeunload and unload events have fired
232 // and the user has agreed to continue with closing the page.
233 void ClosePageIgnoringUnloadEvents();
235 // Tells the renderer view to focus the first (last if reverse is true) node.
236 void SetInitialFocus(bool reverse
);
238 // Get html data by serializing all frames of current page with lists
239 // which contain all resource links that have local copy.
240 // The parameter links contain original URLs of all saved links.
241 // The parameter local_paths contain corresponding local file paths of
242 // all saved links, which matched with vector:links one by one.
243 // The parameter local_directory_name is relative path of directory which
244 // contain all saved auxiliary files included all sub frames and resouces.
245 void GetSerializedHtmlDataForCurrentPageWithLocalLinks(
246 const std::vector
<GURL
>& links
,
247 const std::vector
<base::FilePath
>& local_paths
,
248 const base::FilePath
& local_directory_name
);
250 // Notifies the RenderViewHost that its load state changed.
251 void LoadStateChanged(const GURL
& url
,
252 const net::LoadStateWithParam
& load_state
,
253 uint64 upload_position
,
256 bool SuddenTerminationAllowed() const;
257 void set_sudden_termination_allowed(bool enabled
) {
258 sudden_termination_allowed_
= enabled
;
261 // RenderWidgetHost public overrides.
262 void Init() override
;
263 void Shutdown() override
;
264 void WasHidden() override
;
265 void WasShown(const ui::LatencyInfo
& latency_info
) override
;
266 bool IsRenderView() const override
;
267 bool OnMessageReceived(const IPC::Message
& msg
) override
;
268 void GotFocus() override
;
269 void LostCapture() override
;
270 void LostMouseLock() override
;
271 void SetIsLoading(bool is_loading
) override
;
272 void ForwardMouseEvent(const blink::WebMouseEvent
& mouse_event
) override
;
273 void ForwardKeyboardEvent(const NativeWebKeyboardEvent
& key_event
) override
;
274 gfx::Rect
GetRootWindowResizerRect() const override
;
276 // Creates a new RenderView with the given route id.
277 void CreateNewWindow(
279 int main_frame_route_id
,
280 const ViewHostMsg_CreateWindow_Params
& params
,
281 SessionStorageNamespace
* session_storage_namespace
);
283 // Creates a new RenderWidget with the given route id. |popup_type| indicates
284 // if this widget is a popup and what kind of popup it is (select, autofill).
285 void CreateNewWidget(int route_id
, blink::WebPopupType popup_type
);
287 // Creates a full screen RenderWidget.
288 void CreateNewFullscreenWidget(int route_id
);
290 int main_frame_routing_id() const {
291 return main_frame_routing_id_
;
294 void OnTextSurroundingSelectionResponse(const base::string16
& content
,
298 // Update the FrameTree to use this RenderViewHost's main frame
299 // RenderFrameHost. Called when the RenderViewHost is committed.
301 // TODO(ajwong): Remove once RenderViewHost no longer owns the main frame
303 void AttachToFrameTree();
305 // Increases the refcounting on this RVH. This is done by the FrameTree on
306 // creation of a RenderFrameHost.
307 void increment_ref_count() { ++frames_ref_count_
; }
309 // Decreases the refcounting on this RVH. This is done by the FrameTree on
310 // destruction of a RenderFrameHost.
311 void decrement_ref_count() { --frames_ref_count_
; }
313 // Returns the refcount on this RVH, that is the number of RenderFrameHosts
314 // currently using it.
315 int ref_count() { return frames_ref_count_
; }
317 // NOTE: Do not add functions that just send an IPC message that are called in
318 // one or two places. Have the caller send the IPC message directly (unless
319 // the caller places are in different platforms, in which case it's better
320 // to keep them consistent).
323 // RenderWidgetHost protected overrides.
324 void OnUserGesture() override
;
325 void NotifyRendererUnresponsive() override
;
326 void NotifyRendererResponsive() override
;
327 void OnRenderAutoResized(const gfx::Size
& size
) override
;
328 void RequestToLockMouse(bool user_gesture
,
329 bool last_unlocked_by_target
) override
;
330 bool IsFullscreen() const override
;
331 void OnFocus() override
;
332 void OnBlur() override
;
334 // IPC message handlers.
335 void OnShowView(int route_id
,
336 WindowOpenDisposition disposition
,
337 const gfx::Rect
& initial_rect
,
339 void OnShowWidget(int route_id
, const gfx::Rect
& initial_rect
);
340 void OnShowFullscreenWidget(int route_id
);
341 void OnRenderViewReady();
342 void OnRenderProcessGone(int status
, int error_code
);
343 void OnUpdateState(int32 page_id
, const PageState
& state
);
344 void OnUpdateTargetURL(const GURL
& url
);
346 void OnRequestMove(const gfx::Rect
& pos
);
347 void OnDocumentAvailableInMainFrame(bool uses_temporary_zoom_level
);
348 void OnDidContentsPreferredSizeChange(const gfx::Size
& new_size
);
349 void OnPasteFromSelectionClipboard();
350 void OnRouteCloseEvent();
351 void OnRouteMessageEvent(const ViewMsg_PostMessage_Params
& params
);
352 void OnStartDragging(const DropData
& drop_data
,
353 blink::WebDragOperationsMask operations_allowed
,
354 const SkBitmap
& bitmap
,
355 const gfx::Vector2d
& bitmap_offset_in_dip
,
356 const DragEventSourceInfo
& event_info
);
357 void OnUpdateDragCursor(blink::WebDragOperation drag_operation
);
358 void OnTargetDropACK();
359 void OnTakeFocus(bool reverse
);
360 void OnFocusedNodeChanged(bool is_editable_node
,
361 const gfx::Rect
& node_bounds_in_viewport
);
362 void OnClosePageACK();
363 void OnDidZoomURL(double zoom_level
, const GURL
& url
);
364 void OnPageScaleFactorIsOneChanged(bool is_one
);
365 void OnRunFileChooser(const FileChooserParams
& params
);
366 void OnFocusedNodeTouched(bool editable
);
369 // TODO(nasko): Temporarily friend RenderFrameHostImpl, so we don't duplicate
370 // utility functions and state needed in both classes, while we move frame
371 // specific code away from this class.
372 friend class RenderFrameHostImpl
;
373 friend class TestRenderViewHost
;
374 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest
, BasicRenderFrameHost
);
375 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest
, RoutingIdSane
);
377 // TODO(creis): Move to a private namespace on RenderFrameHostImpl.
378 // Delay to wait on closing the WebContents for a beforeunload/unload handler
380 static const int64 kUnloadTimeoutMS
;
382 // Returns the content specific prefs for this RenderViewHost.
383 // TODO(creis): Move most of this method to RenderProcessHost, since it's
384 // mostly the same across all RVHs in a process. Move the rest to RFH.
385 // See https://crbug.com/304341.
386 WebPreferences
ComputeWebkitPrefs();
388 // Returns whether the current RenderProcessHost has read access to the files
389 // reported in |state|.
390 bool CanAccessFilesOfPageState(const PageState
& state
) const;
392 // Grants the current RenderProcessHost read access to any file listed in
393 // |validated_state|. It is important that the PageState has been validated
394 // upon receipt from the renderer process to prevent it from forging access to
395 // files without the user's consent.
396 void GrantFileAccessFromPageState(const PageState
& validated_state
);
398 // The number of RenderFrameHosts which have a reference to this RVH.
399 int frames_ref_count_
;
401 // Our delegate, which wants to know about changes in the RenderView.
402 RenderViewHostDelegate
* delegate_
;
404 // The SiteInstance associated with this RenderViewHost. All pages drawn
405 // in this RenderViewHost are part of this SiteInstance. Cannot change
407 scoped_refptr
<SiteInstanceImpl
> instance_
;
409 // true if we are currently waiting for a response for drag context
411 bool waiting_for_drag_context_response_
;
413 // A bitwise OR of bindings types that have been enabled for this RenderView.
414 // See BindingsPolicy for details.
415 int enabled_bindings_
;
417 // The most recent page ID we've heard from the renderer process. This is
418 // used as context when other session history related IPCs arrive.
419 // TODO(creis): Allocate this in WebContents/NavigationController instead.
422 // Tracks whether this RenderViewHost is in an active state. False if the
423 // main frame is pending swap out, pending deletion, or swapped out, because
424 // it is not visible to the user in any of these cases.
427 // Tracks whether the main frame RenderFrameHost is swapped out. Unlike
428 // is_active_, this is false when the frame is pending swap out or deletion.
429 // TODO(creis): Remove this when we no longer use swappedout://.
430 // See http://crbug.com/357747.
431 bool is_swapped_out_
;
433 // Routing ID for the main frame's RenderFrameHost.
434 int main_frame_routing_id_
;
436 // Set to true when waiting for a ViewHostMsg_ClosePageACK.
437 // TODO(creis): Move to RenderFrameHost and RenderWidgetHost.
438 // See http://crbug.com/418265.
439 bool is_waiting_for_close_ack_
;
441 // True if the render view can be shut down suddenly.
442 bool sudden_termination_allowed_
;
444 // The termination status of the last render view that terminated.
445 base::TerminationStatus render_view_termination_status_
;
447 // Set to true if we requested the on screen keyboard to be displayed.
448 bool virtual_keyboard_requested_
;
450 // True if the current focused element is editable.
451 bool is_focused_element_editable_
;
453 // This is updated every time UpdateWebkitPreferences is called. That method
454 // is in turn called when any of the settings change that the WebPreferences
456 scoped_ptr
<WebPreferences
> web_preferences_
;
458 bool updating_web_preferences_
;
460 base::WeakPtrFactory
<RenderViewHostImpl
> weak_factory_
;
462 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImpl
);
465 #if defined(COMPILER_MSVC)
469 } // namespace content
471 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_IMPL_H_