blob: c5e80a1a29688decb69df050eea7750f13c63c6c [file] [log] [blame]
[email protected]9f4f3322012-01-18 22:29:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1d8a3d1f2011-02-19 07:11:522// 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_RENDERER_HOST_RENDER_WIDGET_HELPER_H_
6#define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_
[email protected]1d8a3d1f2011-02-19 07:11:527
[email protected]aa4117f2011-12-09 22:19:218#include <deque>
[email protected]1d8a3d1f2011-02-19 07:11:529#include <map>
10
[email protected]1d8a3d1f2011-02-19 07:11:5211#include "base/atomic_sequence_num.h"
[email protected]14c1c232013-06-11 17:52:4412#include "base/containers/hash_tables.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]fa20e002013-07-23 21:20:5414#include "base/process/process.h"
[email protected]1d8a3d1f2011-02-19 07:11:5215#include "base/synchronization/lock.h"
16#include "base/synchronization/waitable_event.h"
[email protected]c63b4d42012-04-26 01:01:0717#include "content/public/browser/browser_thread.h"
[email protected]03b6d552012-03-29 04:03:0118#include "content/public/browser/content_browser_client.h"
[email protected]e1986832013-06-14 07:27:2819#include "content/public/browser/global_request_id.h"
[email protected]87f3c082011-10-19 18:07:4420#include "content/public/common/window_container_type.h"
[email protected]2255a9332013-06-17 05:12:3121#include "third_party/WebKit/public/web/WebPopupType.h"
[email protected]2d7c8552011-06-27 19:21:5522#include "ui/gfx/native_widget_types.h"
[email protected]d353541f2012-05-03 22:45:4123#include "ui/surface/transport_dib.h"
[email protected]1d8a3d1f2011-02-19 07:11:5224
25namespace IPC {
26class Message;
27}
28
29namespace base {
30class TimeDelta;
31}
32
[email protected]1d8a3d1f2011-02-19 07:11:5233struct ViewHostMsg_CreateWindow_Params;
[email protected]992db4c2011-05-12 15:37:1534struct ViewMsg_SwapOut_Params;
[email protected]1d8a3d1f2011-02-19 07:11:5235
[email protected]fc4616f2012-07-21 01:29:5836namespace content {
37class ResourceDispatcherHostImpl;
38class SessionStorageNamespace;
[email protected]1d8a3d1f2011-02-19 07:11:5239
40// Instantiated per RenderProcessHost to provide various optimizations on
41// behalf of a RenderWidgetHost. This class bridges between the IO thread
42// where the RenderProcessHost's MessageFilter lives and the UI thread where
43// the RenderWidgetHost lives.
44//
45//
46// OPTIMIZED RESIZE
47//
48// RenderWidgetHelper is used to implement optimized resize. When the
49// RenderWidgetHost is resized, it sends a Resize message to its RenderWidget
[email protected]c63b4d42012-04-26 01:01:0750// counterpart in the renderer process. In response to the Resize message,
51// the RenderWidget generates a new BackingStore and sends an UpdateRect
52// message (or BuffersSwapped via the GPU process in the case of accelerated
53// compositing), and it sets the IS_RESIZE_ACK flag in the UpdateRect message
54// to true. In the accelerated case, an UpdateRect is still sent from the
55// renderer to the browser with acks and plugin moves even though the GPU
56// BackingStore was sent earlier in the BuffersSwapped message. "BackingStore
57// message" is used throughout this code and documentation to mean either a
58// software UpdateRect or GPU BuffersSwapped message.
[email protected]1d8a3d1f2011-02-19 07:11:5259//
60// Back in the browser process, when the RenderProcessHost's MessageFilter
[email protected]c63b4d42012-04-26 01:01:0761// sees an UpdateRect message (or when the GpuProcessHost sees a
62// BuffersSwapped message), it directs it to the RenderWidgetHelper by calling
63// the DidReceiveBackingStoreMsg method. That method stores the data for the
64// message in a map, where it can be directly accessed by the RenderWidgetHost
65// on the UI thread during a call to RenderWidgetHost's GetBackingStore
66// method.
[email protected]1d8a3d1f2011-02-19 07:11:5267//
68// When the RenderWidgetHost's GetBackingStore method is called, it first
69// checks to see if it is waiting for a resize ack. If it is, then it calls
[email protected]c63b4d42012-04-26 01:01:0770// the RenderWidgetHelper's WaitForBackingStoreMsg to check if there is
71// already a resulting BackingStore message (or to wait a short amount of time
72// for one to arrive). The main goal of this mechanism is to short-cut the
73// usual way in which IPC messages are proxied over to the UI thread via
74// InvokeLater. This approach is necessary since window resize is followed up
75// immediately by a request to repaint the window.
[email protected]1d8a3d1f2011-02-19 07:11:5276//
77//
78// OPTIMIZED TAB SWITCHING
79//
80// When a RenderWidgetHost is in a background tab, it is flagged as hidden.
[email protected]c63b4d42012-04-26 01:01:0781// This causes the corresponding RenderWidget to stop sending BackingStore
[email protected]1d8a3d1f2011-02-19 07:11:5282// messages. The RenderWidgetHost also discards its backingstore when it is
83// hidden, which helps free up memory. As a result, when a RenderWidgetHost
[email protected]9e2e4632012-07-27 16:38:4184// is restored, it can be momentarily be without a backingstore. (Restoring
85// a RenderWidgetHost results in a WasShown message being sent to the
[email protected]c63b4d42012-04-26 01:01:0786// RenderWidget, which triggers a full BackingStore message.) This can lead
87// to an observed rendering glitch as the WebContentsImpl will just have to
[email protected]9e2e4632012-07-27 16:38:4188// fill white overtop the RenderWidgetHost until the RenderWidgetHost
89// receives a BackingStore message to refresh its backingstore.
[email protected]1d8a3d1f2011-02-19 07:11:5290//
91// To avoid this 'white flash', the RenderWidgetHost again makes use of the
[email protected]c63b4d42012-04-26 01:01:0792// RenderWidgetHelper's WaitForBackingStoreMsg method. When the
93// RenderWidgetHost's GetBackingStore method is called, it will call
94// WaitForBackingStoreMsg if it has no backingstore.
[email protected]1d8a3d1f2011-02-19 07:11:5295//
96// TRANSPORT DIB CREATION
97//
98// On some platforms (currently the Mac) the renderer cannot create transport
99// DIBs because of sandbox limitations. Thus, it has to make synchronous IPCs
100// to the browser for them. Since these requests are synchronous, they cannot
101// terminate on the UI thread. Thus, in this case, this object performs the
102// allocation and maintains the set of allocated transport DIBs which the
103// renderers can refer to.
104//
105class RenderWidgetHelper
[email protected]fc4616f2012-07-21 01:29:58106 : public base::RefCountedThreadSafe<RenderWidgetHelper,
107 BrowserThread::DeleteOnIOThread> {
[email protected]1d8a3d1f2011-02-19 07:11:52108 public:
109 RenderWidgetHelper();
110
111 void Init(int render_process_id,
[email protected]fc4616f2012-07-21 01:29:58112 ResourceDispatcherHostImpl* resource_dispatcher_host);
[email protected]1d8a3d1f2011-02-19 07:11:52113
114 // Gets the next available routing id. This is thread safe.
115 int GetNextRoutingID();
116
[email protected]c63b4d42012-04-26 01:01:07117 // IO THREAD ONLY -----------------------------------------------------------
118
119 // Lookup the RenderWidgetHelper from the render_process_host_id. Returns NULL
120 // if not found. NOTE: The raw pointer is for temporary use only. To retain,
121 // store in a scoped_refptr.
122 static RenderWidgetHelper* FromProcessHostID(int render_process_host_id);
[email protected]1d8a3d1f2011-02-19 07:11:52123
124 // UI THREAD ONLY -----------------------------------------------------------
125
126 // These three functions provide the backend implementation of the
127 // corresponding functions in RenderProcessHost. See those declarations
128 // for documentation.
[email protected]e1986832013-06-14 07:27:28129 void ResumeDeferredNavigation(const GlobalRequestID& request_id);
[email protected]c63b4d42012-04-26 01:01:07130 bool WaitForBackingStoreMsg(int render_widget_id,
131 const base::TimeDelta& max_delay,
132 IPC::Message* msg);
[email protected]aacd1d72012-07-23 17:35:09133 // Called to resume the requests for a view after it's ready. The view was
134 // created by CreateNewWindow which initially blocked the requests.
135 void ResumeRequestsForView(int route_id);
[email protected]1d8a3d1f2011-02-19 07:11:52136
[email protected]1dfb8bef2013-05-17 07:31:44137#if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
[email protected]1d8a3d1f2011-02-19 07:11:52138 // Given the id of a transport DIB, return a mapping to it or NULL on error.
139 TransportDIB* MapTransportDIB(TransportDIB::Id dib_id);
140#endif
141
[email protected]1d8a3d1f2011-02-19 07:11:52142 // IO THREAD ONLY -----------------------------------------------------------
143
[email protected]c63b4d42012-04-26 01:01:07144 // Called on the IO thread when a BackingStore message is received.
145 void DidReceiveBackingStoreMsg(const IPC::Message& msg);
[email protected]1d8a3d1f2011-02-19 07:11:52146
[email protected]97714c82012-06-06 10:15:13147 void CreateNewWindow(
148 const ViewHostMsg_CreateWindow_Params& params,
149 bool no_javascript_access,
150 base::ProcessHandle render_process,
151 int* route_id,
[email protected]227692c52013-05-31 22:43:04152 int* main_frame_route_id,
[email protected]97714c82012-06-06 10:15:13153 int* surface_id,
[email protected]fc4616f2012-07-21 01:29:58154 SessionStorageNamespace* session_storage_namespace);
[email protected]1d8a3d1f2011-02-19 07:11:52155 void CreateNewWidget(int opener_id,
156 WebKit::WebPopupType popup_type,
[email protected]9f4f3322012-01-18 22:29:56157 int* route_id,
158 int* surface_id);
159 void CreateNewFullscreenWidget(int opener_id, int* route_id, int* surface_id);
[email protected]1d8a3d1f2011-02-19 07:11:52160
[email protected]1dfb8bef2013-05-17 07:31:44161#if defined(OS_POSIX)
[email protected]1d8a3d1f2011-02-19 07:11:52162 // Called on the IO thread to handle the allocation of a TransportDIB. If
163 // |cache_in_browser| is |true|, then a copy of the shmem is kept by the
164 // browser, and it is the caller's repsonsibility to call
165 // FreeTransportDIB(). In all cases, the caller is responsible for deleting
166 // the resulting TransportDIB.
[email protected]324f4b12013-06-04 06:15:22167 void AllocTransportDIB(uint32 size,
[email protected]1d8a3d1f2011-02-19 07:11:52168 bool cache_in_browser,
169 TransportDIB::Handle* result);
170
171 // Called on the IO thread to handle the freeing of a transport DIB
172 void FreeTransportDIB(TransportDIB::Id dib_id);
173#endif
174
175 private:
176 // A class used to proxy a paint message. PaintMsgProxy objects are created
177 // on the IO thread and destroyed on the UI thread.
[email protected]c63b4d42012-04-26 01:01:07178 class BackingStoreMsgProxy;
179 friend class BackingStoreMsgProxy;
[email protected]1d8a3d1f2011-02-19 07:11:52180 friend class base::RefCountedThreadSafe<RenderWidgetHelper>;
[email protected]fc4616f2012-07-21 01:29:58181 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>;
[email protected]c63b4d42012-04-26 01:01:07182 friend class base::DeleteHelper<RenderWidgetHelper>;
[email protected]1d8a3d1f2011-02-19 07:11:52183
[email protected]c63b4d42012-04-26 01:01:07184 typedef std::deque<BackingStoreMsgProxy*> BackingStoreMsgProxyQueue;
[email protected]aa4117f2011-12-09 22:19:21185 // Map from render_widget_id to a queue of live PaintMsgProxy instances.
[email protected]c63b4d42012-04-26 01:01:07186 typedef base::hash_map<int, BackingStoreMsgProxyQueue >
187 BackingStoreMsgProxyMap;
[email protected]1d8a3d1f2011-02-19 07:11:52188
189 ~RenderWidgetHelper();
190
191 // Called on the UI thread to discard a paint message.
[email protected]c63b4d42012-04-26 01:01:07192 void OnDiscardBackingStoreMsg(BackingStoreMsgProxy* proxy);
[email protected]1d8a3d1f2011-02-19 07:11:52193
194 // Called on the UI thread to dispatch a paint message if necessary.
[email protected]c63b4d42012-04-26 01:01:07195 void OnDispatchBackingStoreMsg(BackingStoreMsgProxy* proxy);
[email protected]1d8a3d1f2011-02-19 07:11:52196
197 // Called on the UI thread to finish creating a window.
[email protected]97714c82012-06-06 10:15:13198 void OnCreateWindowOnUI(
199 const ViewHostMsg_CreateWindow_Params& params,
200 int route_id,
[email protected]227692c52013-05-31 22:43:04201 int main_frame_route_id,
[email protected]fc4616f2012-07-21 01:29:58202 SessionStorageNamespace* session_storage_namespace);
[email protected]1d8a3d1f2011-02-19 07:11:52203
204 // Called on the IO thread after a window was created on the UI thread.
[email protected]aacd1d72012-07-23 17:35:09205 void OnResumeRequestsForView(int route_id);
[email protected]1d8a3d1f2011-02-19 07:11:52206
207 // Called on the UI thread to finish creating a widget.
208 void OnCreateWidgetOnUI(int opener_id,
209 int route_id,
210 WebKit::WebPopupType popup_type);
211
212 // Called on the UI thread to create a fullscreen widget.
213 void OnCreateFullscreenWidgetOnUI(int opener_id, int route_id);
214
[email protected]e1986832013-06-14 07:27:28215 // Called on the IO thread to resume a paused navigation in the network
216 // stack without transferring it to a new renderer process.
217 void OnResumeDeferredNavigation(const GlobalRequestID& request_id);
[email protected]1d8a3d1f2011-02-19 07:11:52218
[email protected]1dfb8bef2013-05-17 07:31:44219#if defined(OS_POSIX)
[email protected]1d8a3d1f2011-02-19 07:11:52220 // Called on destruction to release all allocated transport DIBs
221 void ClearAllocatedDIBs();
222
[email protected]1dfb8bef2013-05-17 07:31:44223 // On POSIX we keep file descriptors to all the allocated DIBs around until
[email protected]1d8a3d1f2011-02-19 07:11:52224 // the renderer frees them.
225 base::Lock allocated_dibs_lock_;
226 std::map<TransportDIB::Id, int> allocated_dibs_;
227#endif
228
229 // A map of live paint messages. Must hold pending_paints_lock_ to access.
[email protected]c63b4d42012-04-26 01:01:07230 // The BackingStoreMsgProxy objects are not owned by this map. (See
231 // BackingStoreMsgProxy for details about how the lifetime of instances are
232 // managed.)
233 BackingStoreMsgProxyMap pending_paints_;
[email protected]1d8a3d1f2011-02-19 07:11:52234 base::Lock pending_paints_lock_;
235
[email protected]1d8a3d1f2011-02-19 07:11:52236 int render_process_id_;
237
[email protected]c63b4d42012-04-26 01:01:07238 // Event used to implement WaitForBackingStoreMsg.
[email protected]1d8a3d1f2011-02-19 07:11:52239 base::WaitableEvent event_;
240
241 // The next routing id to use.
242 base::AtomicSequenceNumber next_routing_id_;
243
[email protected]fc4616f2012-07-21 01:29:58244 ResourceDispatcherHostImpl* resource_dispatcher_host_;
[email protected]1d8a3d1f2011-02-19 07:11:52245
246 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHelper);
247};
248
[email protected]fc4616f2012-07-21 01:29:58249} // namespace content
250
[email protected]1d8a3d1f2011-02-19 07:11:52251#endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HELPER_H_