cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / content / browser / renderer_host / render_view_host_impl.h
blobc6fdf055b0d0b95348057ca7466421f22af2b5da
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 FrameMsg_Navigate_Params;
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;
61 #if defined(COMPILER_MSVC)
62 // RenderViewHostImpl is the bottom of a diamond-shaped hierarchy,
63 // with RenderWidgetHost at the root. VS warns when methods from the
64 // root are overridden in only one of the base classes and not both
65 // (in this case, RenderWidgetHostImpl provides implementations of
66 // many of the methods). This is a silly warning when dealing with
67 // pure virtual methods that only have a single implementation in the
68 // hierarchy above this class, and is safe to ignore in this case.
69 #pragma warning(push)
70 #pragma warning(disable: 4250)
71 #endif
73 // This implements the RenderViewHost interface that is exposed to
74 // embedders of content, and adds things only visible to content.
76 // The exact API of this object needs to be more thoroughly designed. Right
77 // now it mimics what WebContentsImpl exposed, which is a fairly large API and
78 // may contain things that are not relevant to a common subset of views. See
79 // also the comment in render_view_host_delegate.h about the size and scope of
80 // the delegate API.
82 // Right now, the concept of page navigation (both top level and frame) exists
83 // in the WebContentsImpl still, so if you instantiate one of these elsewhere,
84 // you will not be able to traverse pages back and forward. We need to determine
85 // if we want to bring that and other functionality down into this object so it
86 // can be shared by others.
87 class CONTENT_EXPORT RenderViewHostImpl
88 : public RenderViewHost,
89 public RenderWidgetHostImpl {
90 public:
91 // Convenience function, just like RenderViewHost::FromID.
92 static RenderViewHostImpl* FromID(int render_process_id, int render_view_id);
94 // |routing_id| could be a valid route id, or it could be MSG_ROUTING_NONE, in
95 // which case RenderWidgetHost will create a new one. |swapped_out| indicates
96 // whether the view should initially be swapped out (e.g., for an opener
97 // frame being rendered by another process). |hidden| indicates whether the
98 // view is initially hidden or visible.
100 // The |session_storage_namespace| parameter allows multiple render views and
101 // WebContentses to share the same session storage (part of the WebStorage
102 // spec) space. This is useful when restoring contentses, but most callers
103 // should pass in NULL which will cause a new SessionStorageNamespace to be
104 // created.
105 RenderViewHostImpl(SiteInstance* instance,
106 RenderViewHostDelegate* delegate,
107 RenderWidgetHostDelegate* widget_delegate,
108 int routing_id,
109 int main_frame_routing_id,
110 bool swapped_out,
111 bool hidden,
112 bool has_initialized_audio_host);
113 ~RenderViewHostImpl() override;
115 // RenderViewHost implementation.
116 RenderFrameHost* GetMainFrame() override;
117 void AllowBindings(int binding_flags) override;
118 void ClearFocusedElement() override;
119 bool IsFocusedElementEditable() override;
120 void ClosePage() override;
121 void CopyImageAt(int x, int y) override;
122 void SaveImageAt(int x, int y) override;
123 void DirectoryEnumerationFinished(
124 int request_id,
125 const std::vector<base::FilePath>& files) override;
126 void DisableScrollbarsForThreshold(const gfx::Size& size) override;
127 void DragSourceEndedAt(int client_x,
128 int client_y,
129 int screen_x,
130 int screen_y,
131 blink::WebDragOperation operation) override;
132 void DragSourceSystemDragEnded() override;
133 void DragTargetDragEnter(const DropData& drop_data,
134 const gfx::Point& client_pt,
135 const gfx::Point& screen_pt,
136 blink::WebDragOperationsMask operations_allowed,
137 int key_modifiers) override;
138 void DragTargetDragOver(const gfx::Point& client_pt,
139 const gfx::Point& screen_pt,
140 blink::WebDragOperationsMask operations_allowed,
141 int key_modifiers) override;
142 void DragTargetDragLeave() override;
143 void DragTargetDrop(const gfx::Point& client_pt,
144 const gfx::Point& screen_pt,
145 int key_modifiers) override;
146 void EnableAutoResize(const gfx::Size& min_size,
147 const gfx::Size& max_size) override;
148 void DisableAutoResize(const gfx::Size& new_size) override;
149 void EnablePreferredSizeMode() override;
150 void ExecuteMediaPlayerActionAtLocation(
151 const gfx::Point& location,
152 const blink::WebMediaPlayerAction& action) override;
153 void ExecutePluginActionAtLocation(
154 const gfx::Point& location,
155 const blink::WebPluginAction& action) override;
156 void ExitFullscreen() override;
157 void FilesSelectedInChooser(
158 const std::vector<content::FileChooserFileInfo>& files,
159 FileChooserParams::Mode permissions) override;
160 RenderViewHostDelegate* GetDelegate() const override;
161 int GetEnabledBindings() const override;
162 SiteInstanceImpl* GetSiteInstance() const override;
163 bool IsRenderViewLive() const override;
164 void NotifyMoveOrResizeStarted() override;
165 void SetWebUIProperty(const std::string& name,
166 const std::string& value) override;
167 void Zoom(PageZoom zoom) override;
168 void SyncRendererPrefs() override;
169 WebPreferences GetWebkitPreferences() override;
170 void UpdateWebkitPreferences(const WebPreferences& prefs) override;
171 void OnWebkitPreferencesChanged() override;
172 void GetAudioOutputControllers(
173 const GetAudioOutputControllersCallback& callback) const override;
174 void SelectWordAroundCaret() override;
176 #if defined(OS_ANDROID)
177 virtual void ActivateNearestFindResult(int request_id,
178 float x,
179 float y) override;
180 virtual void RequestFindMatchRects(int current_version) override;
181 #endif
183 void set_delegate(RenderViewHostDelegate* d) {
184 CHECK(d); // http://crbug.com/82827
185 delegate_ = d;
188 // Set up the RenderView child process. Virtual because it is overridden by
189 // TestRenderViewHost. If the |frame_name| parameter is non-empty, it is used
190 // as the name of the new top-level frame.
191 // The |opener_route_id| parameter indicates which RenderView created this
192 // (MSG_ROUTING_NONE if none). If |max_page_id| is larger than -1, the
193 // RenderView is told to start issuing page IDs at |max_page_id| + 1.
194 // |window_was_created_with_opener| is true if this top-level frame was
195 // created with an opener. (The opener may have been closed since.)
196 // The |proxy_route_id| is only used when creating a RenderView in swapped out
197 // state.
198 virtual bool CreateRenderView(const base::string16& frame_name,
199 int opener_route_id,
200 int proxy_route_id,
201 int32 max_page_id,
202 bool window_was_created_with_opener);
204 base::TerminationStatus render_view_termination_status() const {
205 return render_view_termination_status_;
208 // Returns the content specific prefs for this RenderViewHost.
209 WebPreferences ComputeWebkitPrefs(const GURL& url);
211 // Tracks whether this RenderViewHost is in an active state (rather than
212 // pending swap out, pending deletion, or swapped out), according to its main
213 // frame RenderFrameHost.
214 bool is_active() const { return is_active_; }
215 void set_is_active(bool is_active) { is_active_ = is_active; }
217 // Tracks whether this RenderViewHost is swapped out, according to its main
218 // frame RenderFrameHost.
219 void set_is_swapped_out(bool is_swapped_out) {
220 is_swapped_out_ = is_swapped_out;
223 // TODO(creis): Remove as part of http://crbug.com/418265.
224 bool is_waiting_for_close_ack() const { return is_waiting_for_close_ack_; }
226 // Tells the renderer that this RenderView will soon be swapped out, and thus
227 // not to create any new modal dialogs until it happens. This must be done
228 // separately so that the PageGroupLoadDeferrers of any current dialogs are no
229 // longer on the stack when we attempt to swap it out.
230 void SuppressDialogsUntilSwapOut();
232 // Close the page ignoring whether it has unload events registers.
233 // This is called after the beforeunload and unload events have fired
234 // and the user has agreed to continue with closing the page.
235 void ClosePageIgnoringUnloadEvents();
237 // Tells the renderer view to focus the first (last if reverse is true) node.
238 void SetInitialFocus(bool reverse);
240 // Get html data by serializing all frames of current page with lists
241 // which contain all resource links that have local copy.
242 // The parameter links contain original URLs of all saved links.
243 // The parameter local_paths contain corresponding local file paths of
244 // all saved links, which matched with vector:links one by one.
245 // The parameter local_directory_name is relative path of directory which
246 // contain all saved auxiliary files included all sub frames and resouces.
247 void GetSerializedHtmlDataForCurrentPageWithLocalLinks(
248 const std::vector<GURL>& links,
249 const std::vector<base::FilePath>& local_paths,
250 const base::FilePath& local_directory_name);
252 // Notifies the RenderViewHost that its load state changed.
253 void LoadStateChanged(const GURL& url,
254 const net::LoadStateWithParam& load_state,
255 uint64 upload_position,
256 uint64 upload_size);
258 bool SuddenTerminationAllowed() const;
259 void set_sudden_termination_allowed(bool enabled) {
260 sudden_termination_allowed_ = enabled;
263 // RenderWidgetHost public overrides.
264 void Init() override;
265 void Shutdown() override;
266 void WasHidden() override;
267 void WasShown(const ui::LatencyInfo& latency_info) override;
268 bool IsRenderView() const override;
269 bool OnMessageReceived(const IPC::Message& msg) override;
270 void GotFocus() override;
271 void LostCapture() override;
272 void LostMouseLock() override;
273 void SetIsLoading(bool is_loading) override;
274 void ForwardMouseEvent(const blink::WebMouseEvent& mouse_event) override;
275 void OnPointerEventActivate() override;
276 void ForwardKeyboardEvent(const NativeWebKeyboardEvent& key_event) override;
277 gfx::Rect GetRootWindowResizerRect() const override;
279 // Creates a new RenderView with the given route id.
280 void CreateNewWindow(
281 int route_id,
282 int main_frame_route_id,
283 const ViewHostMsg_CreateWindow_Params& params,
284 SessionStorageNamespace* session_storage_namespace);
286 // Creates a new RenderWidget with the given route id. |popup_type| indicates
287 // if this widget is a popup and what kind of popup it is (select, autofill).
288 void CreateNewWidget(int route_id, blink::WebPopupType popup_type);
290 // Creates a full screen RenderWidget.
291 void CreateNewFullscreenWidget(int route_id);
293 #if defined(ENABLE_BROWSER_CDMS)
294 MediaWebContentsObserver* media_web_contents_observer() {
295 return media_web_contents_observer_.get();
297 #endif
299 int main_frame_routing_id() const {
300 return main_frame_routing_id_;
303 void OnTextSurroundingSelectionResponse(const base::string16& content,
304 size_t start_offset,
305 size_t end_offset);
307 // Update the FrameTree to use this RenderViewHost's main frame
308 // RenderFrameHost. Called when the RenderViewHost is committed.
310 // TODO(ajwong): Remove once RenderViewHost no longer owns the main frame
311 // RenderFrameHost.
312 void AttachToFrameTree();
314 // Increases the refcounting on this RVH. This is done by the FrameTree on
315 // creation of a RenderFrameHost.
316 void increment_ref_count() { ++frames_ref_count_; }
318 // Decreases the refcounting on this RVH. This is done by the FrameTree on
319 // destruction of a RenderFrameHost.
320 void decrement_ref_count() { --frames_ref_count_; }
322 // Returns the refcount on this RVH, that is the number of RenderFrameHosts
323 // currently using it.
324 int ref_count() { return frames_ref_count_; }
326 // NOTE: Do not add functions that just send an IPC message that are called in
327 // one or two places. Have the caller send the IPC message directly (unless
328 // the caller places are in different platforms, in which case it's better
329 // to keep them consistent).
331 protected:
332 // RenderWidgetHost protected overrides.
333 void OnUserGesture() override;
334 void NotifyRendererUnresponsive() override;
335 void NotifyRendererResponsive() override;
336 void OnRenderAutoResized(const gfx::Size& size) override;
337 void RequestToLockMouse(bool user_gesture,
338 bool last_unlocked_by_target) override;
339 bool IsFullscreen() const override;
340 void OnFocus() override;
341 void OnBlur() override;
343 // IPC message handlers.
344 void OnShowView(int route_id,
345 WindowOpenDisposition disposition,
346 const gfx::Rect& initial_pos,
347 bool user_gesture);
348 void OnShowWidget(int route_id, const gfx::Rect& initial_pos);
349 void OnShowFullscreenWidget(int route_id);
350 void OnRunModal(int opener_id, IPC::Message* reply_msg);
351 void OnRenderViewReady();
352 void OnRenderProcessGone(int status, int error_code);
353 void OnUpdateState(int32 page_id, const PageState& state);
354 void OnUpdateTargetURL(const GURL& url);
355 void OnClose();
356 void OnRequestMove(const gfx::Rect& pos);
357 void OnDocumentAvailableInMainFrame(bool uses_temporary_zoom_level);
358 void OnToggleFullscreen(bool enter_fullscreen);
359 void OnDidContentsPreferredSizeChange(const gfx::Size& new_size);
360 void OnPasteFromSelectionClipboard();
361 void OnRouteCloseEvent();
362 void OnRouteMessageEvent(const ViewMsg_PostMessage_Params& params);
363 void OnStartDragging(const DropData& drop_data,
364 blink::WebDragOperationsMask operations_allowed,
365 const SkBitmap& bitmap,
366 const gfx::Vector2d& bitmap_offset_in_dip,
367 const DragEventSourceInfo& event_info);
368 void OnUpdateDragCursor(blink::WebDragOperation drag_operation);
369 void OnTargetDropACK();
370 void OnTakeFocus(bool reverse);
371 void OnFocusedNodeChanged(bool is_editable_node);
372 void OnClosePageACK();
373 void OnDidZoomURL(double zoom_level, const GURL& url);
374 void OnRunFileChooser(const FileChooserParams& params);
375 void OnFocusedNodeTouched(bool editable);
377 private:
378 // TODO(nasko): Temporarily friend RenderFrameHostImpl, so we don't duplicate
379 // utility functions and state needed in both classes, while we move frame
380 // specific code away from this class.
381 friend class RenderFrameHostImpl;
382 friend class TestRenderViewHost;
383 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, BasicRenderFrameHost);
384 FRIEND_TEST_ALL_PREFIXES(RenderViewHostTest, RoutingIdSane);
386 // TODO(creis): Move to a private namespace on RenderFrameHostImpl.
387 // Delay to wait on closing the WebContents for a beforeunload/unload handler
388 // to fire.
389 static const int64 kUnloadTimeoutMS;
391 bool CanAccessFilesOfPageState(const PageState& state) const;
393 // The number of RenderFrameHosts which have a reference to this RVH.
394 int frames_ref_count_;
396 // Our delegate, which wants to know about changes in the RenderView.
397 RenderViewHostDelegate* delegate_;
399 // The SiteInstance associated with this RenderViewHost. All pages drawn
400 // in this RenderViewHost are part of this SiteInstance. Cannot change
401 // over time.
402 scoped_refptr<SiteInstanceImpl> instance_;
404 // true if we are currently waiting for a response for drag context
405 // information.
406 bool waiting_for_drag_context_response_;
408 // A bitwise OR of bindings types that have been enabled for this RenderView.
409 // See BindingsPolicy for details.
410 int enabled_bindings_;
412 // The most recent page ID we've heard from the renderer process. This is
413 // used as context when other session history related IPCs arrive.
414 // TODO(creis): Allocate this in WebContents/NavigationController instead.
415 int32 page_id_;
417 // Tracks whether this RenderViewHost is in an active state. False if the
418 // main frame is pending swap out, pending deletion, or swapped out, because
419 // it is not visible to the user in any of these cases.
420 bool is_active_;
422 // Tracks whether the main frame RenderFrameHost is swapped out. Unlike
423 // is_active_, this is false when the frame is pending swap out or deletion.
424 // TODO(creis): Remove this when we no longer use swappedout://.
425 // See http://crbug.com/357747.
426 bool is_swapped_out_;
428 // Routing ID for the main frame's RenderFrameHost.
429 int main_frame_routing_id_;
431 // If we were asked to RunModal, then this will hold the reply_msg that we
432 // must return to the renderer to unblock it.
433 IPC::Message* run_modal_reply_msg_;
434 // This will hold the routing id of the RenderView that opened us.
435 int run_modal_opener_id_;
437 // Set to true when waiting for a ViewHostMsg_ClosePageACK.
438 // TODO(creis): Move to RenderFrameHost and RenderWidgetHost.
439 // See http://crbug.com/418265.
440 bool is_waiting_for_close_ack_;
442 // True if the render view can be shut down suddenly.
443 bool sudden_termination_allowed_;
445 // The termination status of the last render view that terminated.
446 base::TerminationStatus render_view_termination_status_;
448 // Set to true if we requested the on screen keyboard to be displayed.
449 bool virtual_keyboard_requested_;
451 #if defined(ENABLE_BROWSER_CDMS)
452 // Manages all the media player and CDM managers and forwards IPCs to them.
453 scoped_ptr<MediaWebContentsObserver> media_web_contents_observer_;
454 #endif
456 // True if the current focused element is editable.
457 bool is_focused_element_editable_;
459 // This is updated every time UpdateWebkitPreferences is called. That method
460 // is in turn called when any of the settings change that the WebPreferences
461 // values depend on.
462 scoped_ptr<WebPreferences> web_preferences_;
464 bool updating_web_preferences_;
466 base::WeakPtrFactory<RenderViewHostImpl> weak_factory_;
468 DISALLOW_COPY_AND_ASSIGN(RenderViewHostImpl);
471 #if defined(COMPILER_MSVC)
472 #pragma warning(pop)
473 #endif
475 } // namespace content
477 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_VIEW_HOST_IMPL_H_