Simplify ChildProcessLauncher
[chromium-blink-merge.git] / content / browser / renderer_host / render_view_host_impl.h
blobb15019b1e7e5c5a534cd8b45576c4adb48207f84
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_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"
30 class SkBitmap;
31 class FrameMsg_Navigate;
32 struct MediaPlayerAction;
33 struct ViewHostMsg_CreateWindow_Params;
34 struct ViewMsg_PostMessage_Params;
36 namespace base {
37 class ListValue;
40 namespace gfx {
41 class Range;
44 namespace ui {
45 class AXTree;
48 namespace content {
50 class MediaWebContentsObserver;
51 class ChildProcessSecurityPolicyImpl;
52 class PageState;
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.
68 #pragma warning(push)
69 #pragma warning(disable: 4250)
70 #endif
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
79 // the delegate API.
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.
86 class CONTENT_EXPORT RenderViewHostImpl
87 : public RenderViewHost,
88 public RenderWidgetHostImpl {
89 public:
90 // Convenience function, just like RenderViewHost::FromID.
91 static RenderViewHostImpl* FromID(int render_process_id, int render_view_id);
93 // |routing_id| could be a valid route id, or it could be MSG_ROUTING_NONE, in
94 // which case RenderWidgetHost will create a new one. |swapped_out| indicates
95 // whether the view should initially be swapped out (e.g., for an opener
96 // frame being rendered by another process). |hidden| indicates whether the
97 // view is initially hidden or visible.
99 // The |session_storage_namespace| parameter allows multiple render views and
100 // WebContentses to share the same session storage (part of the WebStorage
101 // spec) space. This is useful when restoring contentses, but most callers
102 // should pass in NULL which will cause a new SessionStorageNamespace to be
103 // created.
104 RenderViewHostImpl(SiteInstance* instance,
105 RenderViewHostDelegate* delegate,
106 RenderWidgetHostDelegate* widget_delegate,
107 int routing_id,
108 int main_frame_routing_id,
109 bool swapped_out,
110 bool hidden,
111 bool has_initialized_audio_host);
112 ~RenderViewHostImpl() override;
114 // RenderViewHost implementation.
115 RenderFrameHost* GetMainFrame() override;
116 void AllowBindings(int binding_flags) override;
117 void ClearFocusedElement() override;
118 bool IsFocusedElementEditable() override;
119 void ClosePage() override;
120 void CopyImageAt(int x, int y) override;
121 void SaveImageAt(int x, int y) override;
122 void DirectoryEnumerationFinished(
123 int request_id,
124 const std::vector<base::FilePath>& files) override;
125 void DisableScrollbarsForThreshold(const gfx::Size& size) override;
126 void DragSourceEndedAt(int client_x,
127 int client_y,
128 int screen_x,
129 int screen_y,
130 blink::WebDragOperation operation) override;
131 void DragSourceSystemDragEnded() override;
132 void DragTargetDragEnter(const DropData& drop_data,
133 const gfx::Point& client_pt,
134 const gfx::Point& screen_pt,
135 blink::WebDragOperationsMask operations_allowed,
136 int key_modifiers) override;
137 void DragTargetDragOver(const gfx::Point& client_pt,
138 const gfx::Point& screen_pt,
139 blink::WebDragOperationsMask operations_allowed,
140 int key_modifiers) override;
141 void DragTargetDragLeave() override;
142 void DragTargetDrop(const gfx::Point& client_pt,
143 const gfx::Point& screen_pt,
144 int key_modifiers) override;
145 void EnableAutoResize(const gfx::Size& min_size,
146 const gfx::Size& max_size) override;
147 void DisableAutoResize(const gfx::Size& new_size) override;
148 void EnablePreferredSizeMode() override;
149 void ExecuteMediaPlayerActionAtLocation(
150 const gfx::Point& location,
151 const blink::WebMediaPlayerAction& action) override;
152 void ExecutePluginActionAtLocation(
153 const gfx::Point& location,
154 const blink::WebPluginAction& action) override;
155 void FilesSelectedInChooser(
156 const std::vector<content::FileChooserFileInfo>& files,
157 FileChooserParams::Mode permissions) override;
158 RenderViewHostDelegate* GetDelegate() const override;
159 int GetEnabledBindings() const override;
160 SiteInstanceImpl* GetSiteInstance() const override;
161 bool IsRenderViewLive() const override;
162 void NotifyMoveOrResizeStarted() override;
163 void SetWebUIProperty(const std::string& name,
164 const std::string& value) override;
165 void Zoom(PageZoom zoom) override;
166 void SyncRendererPrefs() override;
167 WebPreferences GetWebkitPreferences() override;
168 void UpdateWebkitPreferences(const WebPreferences& prefs) override;
169 void OnWebkitPreferencesChanged() override;
170 void SelectWordAroundCaret() override;
172 #if defined(OS_ANDROID)
173 void ActivateNearestFindResult(int request_id, float x, float y) override;
174 void RequestFindMatchRects(int current_version) override;
175 #endif
177 void set_delegate(RenderViewHostDelegate* d) {
178 CHECK(d); // http://crbug.com/82827
179 delegate_ = d;
182 // Set up the RenderView child process. Virtual because it is overridden by
183 // TestRenderViewHost. If the |frame_name| parameter is non-empty, it is used
184 // as the name of the new top-level frame.
185 // The |opener_route_id| parameter indicates which RenderView created this
186 // (MSG_ROUTING_NONE if none). If |max_page_id| is larger than -1, the
187 // RenderView is told to start issuing page IDs at |max_page_id| + 1.
188 // |window_was_created_with_opener| is true if this top-level frame was
189 // created with an opener. (The opener may have been closed since.)
190 // The |proxy_route_id| is only used when creating a RenderView in swapped out
191 // state.
192 virtual bool CreateRenderView(const base::string16& frame_name,
193 int opener_route_id,
194 int proxy_route_id,
195 int32 max_page_id,
196 bool window_was_created_with_opener);
198 base::TerminationStatus render_view_termination_status() const {
199 return render_view_termination_status_;
202 // Tracks whether this RenderViewHost is in an active state (rather than
203 // pending swap out, pending deletion, or swapped out), according to its main
204 // frame RenderFrameHost.
205 bool is_active() const { return is_active_; }
206 void set_is_active(bool is_active) { is_active_ = is_active; }
208 // Tracks whether this RenderViewHost is swapped out, according to its main
209 // frame RenderFrameHost.
210 void set_is_swapped_out(bool is_swapped_out) {
211 is_swapped_out_ = is_swapped_out;
214 // TODO(creis): Remove as part of http://crbug.com/418265.
215 bool is_waiting_for_close_ack() const { return is_waiting_for_close_ack_; }
217 // Tells the renderer that this RenderView will soon be swapped out, and thus
218 // not to create any new modal dialogs until it happens. This must be done
219 // separately so that the PageGroupLoadDeferrers of any current dialogs are no
220 // longer on the stack when we attempt to swap it out.
221 void SuppressDialogsUntilSwapOut();
223 // Close the page ignoring whether it has unload events registers.
224 // This is called after the beforeunload and unload events have fired
225 // and the user has agreed to continue with closing the page.
226 void ClosePageIgnoringUnloadEvents();
228 // Tells the renderer view to focus the first (last if reverse is true) node.
229 void SetInitialFocus(bool reverse);
231 // Get html data by serializing all frames of current page with lists
232 // which contain all resource links that have local copy.
233 // The parameter links contain original URLs of all saved links.
234 // The parameter local_paths contain corresponding local file paths of
235 // all saved links, which matched with vector:links one by one.
236 // The parameter local_directory_name is relative path of directory which
237 // contain all saved auxiliary files included all sub frames and resouces.
238 void GetSerializedHtmlDataForCurrentPageWithLocalLinks(
239 const std::vector<GURL>& links,
240 const std::vector<base::FilePath>& local_paths,
241 const base::FilePath& local_directory_name);
243 // Notifies the RenderViewHost that its load state changed.
244 void LoadStateChanged(const GURL& url,
245 const net::LoadStateWithParam& load_state,
246 uint64 upload_position,
247 uint64 upload_size);
249 bool SuddenTerminationAllowed() const;
250 void set_sudden_termination_allowed(bool enabled) {
251 sudden_termination_allowed_ = enabled;
254 // RenderWidgetHost public overrides.
255 void Init() override;
256 void Shutdown() override;
257 void WasHidden() override;
258 void WasShown(const ui::LatencyInfo& latency_info) override;
259 bool IsRenderView() const override;
260 bool OnMessageReceived(const IPC::Message& msg) override;
261 void GotFocus() override;
262 void LostCapture() override;
263 void LostMouseLock() override;
264 void SetIsLoading(bool is_loading) override;
265 void ForwardMouseEvent(const blink::WebMouseEvent& mouse_event) override;
266 void OnPointerEventActivate() override;
267 void ForwardKeyboardEvent(const NativeWebKeyboardEvent& key_event) override;
268 gfx::Rect GetRootWindowResizerRect() const override;
270 // Creates a new RenderView with the given route id.
271 void CreateNewWindow(
272 int route_id,
273 int main_frame_route_id,
274 const ViewHostMsg_CreateWindow_Params& params,
275 SessionStorageNamespace* session_storage_namespace);
277 // Creates a new RenderWidget with the given route id. |popup_type| indicates
278 // if this widget is a popup and what kind of popup it is (select, autofill).
279 void CreateNewWidget(int route_id, blink::WebPopupType popup_type);
281 // Creates a full screen RenderWidget.
282 void CreateNewFullscreenWidget(int route_id);
284 int main_frame_routing_id() const {
285 return main_frame_routing_id_;
288 void OnTextSurroundingSelectionResponse(const base::string16& content,
289 size_t start_offset,
290 size_t end_offset);
292 // Update the FrameTree to use this RenderViewHost's main frame
293 // RenderFrameHost. Called when the RenderViewHost is committed.
295 // TODO(ajwong): Remove once RenderViewHost no longer owns the main frame
296 // RenderFrameHost.
297 void AttachToFrameTree();
299 // Increases the refcounting on this RVH. This is done by the FrameTree on
300 // creation of a RenderFrameHost.
301 void increment_ref_count() { ++frames_ref_count_; }
303 // Decreases the refcounting on this RVH. This is done by the FrameTree on
304 // destruction of a RenderFrameHost.
305 void decrement_ref_count() { --frames_ref_count_; }
307 // Returns the refcount on this RVH, that is the number of RenderFrameHosts
308 // currently using it.
309 int ref_count() { return frames_ref_count_; }
311 // NOTE: Do not add functions that just send an IPC message that are called in
312 // one or two places. Have the caller send the IPC message directly (unless
313 // the caller places are in different platforms, in which case it's better
314 // to keep them consistent).
316 protected:
317 // RenderWidgetHost protected overrides.
318 void OnUserGesture() override;
319 void NotifyRendererUnresponsive() override;
320 void NotifyRendererResponsive() override;
321 void OnRenderAutoResized(const gfx::Size& size) override;
322 void RequestToLockMouse(bool user_gesture,
323 bool last_unlocked_by_target) override;
324 bool IsFullscreen() const override;
325 void OnFocus() override;
326 void OnBlur() override;
328 // IPC message handlers.
329 void OnShowView(int route_id,
330 WindowOpenDisposition disposition,
331 const gfx::Rect& initial_rect,
332 bool user_gesture);
333 void OnShowWidget(int route_id, const gfx::Rect& initial_rect);
334 void OnShowFullscreenWidget(int route_id);
335 void OnRenderViewReady();
336 void OnRenderProcessGone(int status, int error_code);
337 void OnUpdateState(int32 page_id, const PageState& state);
338 void OnUpdateTargetURL(const GURL& url);
339 void OnClose();
340 void OnRequestMove(const gfx::Rect& pos);
341 void OnDocumentAvailableInMainFrame(bool uses_temporary_zoom_level);
342 void OnDidContentsPreferredSizeChange(const gfx::Size& new_size);
343 void OnPasteFromSelectionClipboard();
344 void OnRouteCloseEvent();
345 void OnRouteMessageEvent(const ViewMsg_PostMessage_Params& params);
346 void OnStartDragging(const DropData& drop_data,
347 blink::WebDragOperationsMask operations_allowed,
348 const SkBitmap& bitmap,
349 const gfx::Vector2d& bitmap_offset_in_dip,
350 const DragEventSourceInfo& event_info);
351 void OnUpdateDragCursor(blink::WebDragOperation drag_operation);
352 void OnTargetDropACK();
353 void OnTakeFocus(bool reverse);
354 void OnFocusedNodeChanged(bool is_editable_node,
355 const gfx::Rect& node_bounds_in_viewport);
356 void OnClosePageACK();
357 void OnDidZoomURL(double zoom_level, const GURL& url);
358 void OnPageScaleFactorIsOneChanged(bool is_one);
359 void OnRunFileChooser(const FileChooserParams& params);
360 void OnFocusedNodeTouched(bool editable);
362 private:
363 // TODO(nasko): Temporarily friend RenderFrameHostImpl, so we don't duplicate
364 // utility functions and state needed in both classes, while we move frame
365 // specific code away from this class.
366 friend class RenderFrameHostImpl;
367 friend class TestRenderViewHost;
368 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, BasicRenderFrameHost);
369 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, RoutingIdSane);
371 // TODO(creis): Move to a private namespace on RenderFrameHostImpl.
372 // Delay to wait on closing the WebContents for a beforeunload/unload handler
373 // to fire.
374 static const int64 kUnloadTimeoutMS;
376 // Returns the content specific prefs for this RenderViewHost.
377 // TODO(creis): Move most of this method to RenderProcessHost, since it's
378 // mostly the same across all RVHs in a process. Move the rest to RFH.
379 // See https://crbug.com/304341.
380 WebPreferences ComputeWebkitPrefs();
382 // Returns whether the current RenderProcessHost has read access to the files
383 // reported in |state|.
384 bool CanAccessFilesOfPageState(const PageState& state) const;
386 // Grants the current RenderProcessHost read access to any file listed in
387 // |validated_state|. It is important that the PageState has been validated
388 // upon receipt from the renderer process to prevent it from forging access to
389 // files without the user's consent.
390 void GrantFileAccessFromPageState(const PageState& validated_state);
392 // The number of RenderFrameHosts which have a reference to this RVH.
393 int frames_ref_count_;
395 // Our delegate, which wants to know about changes in the RenderView.
396 RenderViewHostDelegate* delegate_;
398 // The SiteInstance associated with this RenderViewHost. All pages drawn
399 // in this RenderViewHost are part of this SiteInstance. Cannot change
400 // over time.
401 scoped_refptr<SiteInstanceImpl> instance_;
403 // true if we are currently waiting for a response for drag context
404 // information.
405 bool waiting_for_drag_context_response_;
407 // A bitwise OR of bindings types that have been enabled for this RenderView.
408 // See BindingsPolicy for details.
409 int enabled_bindings_;
411 // The most recent page ID we've heard from the renderer process. This is
412 // used as context when other session history related IPCs arrive.
413 // TODO(creis): Allocate this in WebContents/NavigationController instead.
414 int32 page_id_;
416 // Tracks whether this RenderViewHost is in an active state. False if the
417 // main frame is pending swap out, pending deletion, or swapped out, because
418 // it is not visible to the user in any of these cases.
419 bool is_active_;
421 // Tracks whether the main frame RenderFrameHost is swapped out. Unlike
422 // is_active_, this is false when the frame is pending swap out or deletion.
423 // TODO(creis): Remove this when we no longer use swappedout://.
424 // See http://crbug.com/357747.
425 bool is_swapped_out_;
427 // Routing ID for the main frame's RenderFrameHost.
428 int main_frame_routing_id_;
430 // Set to true when waiting for a ViewHostMsg_ClosePageACK.
431 // TODO(creis): Move to RenderFrameHost and RenderWidgetHost.
432 // See http://crbug.com/418265.
433 bool is_waiting_for_close_ack_;
435 // True if the render view can be shut down suddenly.
436 bool sudden_termination_allowed_;
438 // The termination status of the last render view that terminated.
439 base::TerminationStatus render_view_termination_status_;
441 // Set to true if we requested the on screen keyboard to be displayed.
442 bool virtual_keyboard_requested_;
444 // True if the current focused element is editable.
445 bool is_focused_element_editable_;
447 // This is updated every time UpdateWebkitPreferences is called. That method
448 // is in turn called when any of the settings change that the WebPreferences
449 // values depend on.
450 scoped_ptr<WebPreferences> web_preferences_;
452 bool updating_web_preferences_;
454 base::WeakPtrFactory<RenderViewHostImpl> weak_factory_;
456 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImpl);
459 #if defined(COMPILER_MSVC)
460 #pragma warning(pop)
461 #endif
463 } // namespace content
465 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_IMPL_H_