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 #include "content/browser/renderer_host/render_widget_helper.h"
8 #include "base/bind_helpers.h"
9 #include "base/lazy_instance.h"
10 #include "base/posix/eintr_wrapper.h"
11 #include "base/threading/thread.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "content/browser/gpu/gpu_surface_tracker.h"
14 #include "content/browser/loader/resource_dispatcher_host_impl.h"
15 #include "content/browser/renderer_host/render_process_host_impl.h"
16 #include "content/browser/renderer_host/render_view_host_impl.h"
17 #include "content/browser/dom_storage/session_storage_namespace_impl.h"
18 #include "content/common/view_messages.h"
23 typedef std::map
<int, RenderWidgetHelper
*> WidgetHelperMap
;
24 base::LazyInstance
<WidgetHelperMap
> g_widget_helpers
=
25 LAZY_INSTANCE_INITIALIZER
;
27 void AddWidgetHelper(int render_process_id
,
28 const scoped_refptr
<RenderWidgetHelper
>& widget_helper
) {
29 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
30 // We don't care if RenderWidgetHelpers overwrite an existing process_id. Just
31 // want this to be up to date.
32 g_widget_helpers
.Get()[render_process_id
] = widget_helper
.get();
37 // A helper used with DidReceiveBackingStoreMsg that we hold a pointer to in
39 class RenderWidgetHelper::BackingStoreMsgProxy
{
41 BackingStoreMsgProxy(RenderWidgetHelper
* h
, const IPC::Message
& m
);
42 ~BackingStoreMsgProxy();
44 void Cancel() { cancelled_
= true; }
46 const IPC::Message
& message() const { return message_
; }
49 scoped_refptr
<RenderWidgetHelper
> helper_
;
50 IPC::Message message_
;
51 bool cancelled_
; // If true, then the message will not be dispatched.
53 DISALLOW_COPY_AND_ASSIGN(BackingStoreMsgProxy
);
56 RenderWidgetHelper::BackingStoreMsgProxy::BackingStoreMsgProxy(
57 RenderWidgetHelper
* h
, const IPC::Message
& m
)
63 RenderWidgetHelper::BackingStoreMsgProxy::~BackingStoreMsgProxy() {
64 // If the paint message was never dispatched, then we need to let the
65 // helper know that we are going away.
66 if (!cancelled_
&& helper_
.get())
67 helper_
->OnDiscardBackingStoreMsg(this);
70 void RenderWidgetHelper::BackingStoreMsgProxy::Run() {
72 helper_
->OnDispatchBackingStoreMsg(this);
77 RenderWidgetHelper::RenderWidgetHelper()
78 : render_process_id_(-1),
80 event_(CreateEvent(NULL
, FALSE
/* auto-reset */, FALSE
, NULL
)),
81 #elif defined(OS_POSIX)
82 event_(false /* auto-reset */, false),
84 resource_dispatcher_host_(NULL
) {
87 RenderWidgetHelper::~RenderWidgetHelper() {
88 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
90 // Delete this RWH from the map if it is found.
91 WidgetHelperMap
& widget_map
= g_widget_helpers
.Get();
92 WidgetHelperMap::iterator it
= widget_map
.find(render_process_id_
);
93 if (it
!= widget_map
.end() && it
->second
== this)
96 // The elements of pending_paints_ each hold an owning reference back to this
97 // object, so we should not be destroyed unless pending_paints_ is empty!
98 DCHECK(pending_paints_
.empty());
100 #if defined(OS_POSIX) && !defined(OS_ANDROID)
101 ClearAllocatedDIBs();
105 void RenderWidgetHelper::Init(
106 int render_process_id
,
107 ResourceDispatcherHostImpl
* resource_dispatcher_host
) {
108 render_process_id_
= render_process_id
;
109 resource_dispatcher_host_
= resource_dispatcher_host
;
111 BrowserThread::PostTask(
112 BrowserThread::IO
, FROM_HERE
,
113 base::Bind(&AddWidgetHelper
,
114 render_process_id_
, make_scoped_refptr(this)));
117 int RenderWidgetHelper::GetNextRoutingID() {
118 return next_routing_id_
.GetNext() + 1;
122 RenderWidgetHelper
* RenderWidgetHelper::FromProcessHostID(
123 int render_process_host_id
) {
124 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
125 WidgetHelperMap::const_iterator ci
= g_widget_helpers
.Get().find(
126 render_process_host_id
);
127 return (ci
== g_widget_helpers
.Get().end())? NULL
: ci
->second
;
130 void RenderWidgetHelper::ResumeDeferredNavigation(
131 const GlobalRequestID
& request_id
) {
132 BrowserThread::PostTask(
133 BrowserThread::IO
, FROM_HERE
,
134 base::Bind(&RenderWidgetHelper::OnResumeDeferredNavigation
,
139 void RenderWidgetHelper::ResumeResponseDeferredAtStart(
140 const GlobalRequestID
& request_id
) {
141 BrowserThread::PostTask(
144 base::Bind(&RenderWidgetHelper::OnResumeResponseDeferredAtStart
,
149 bool RenderWidgetHelper::WaitForBackingStoreMsg(
150 int render_widget_id
, const base::TimeDelta
& max_delay
, IPC::Message
* msg
) {
151 base::TimeTicks time_start
= base::TimeTicks::Now();
154 BackingStoreMsgProxy
* proxy
= NULL
;
156 base::AutoLock
lock(pending_paints_lock_
);
158 BackingStoreMsgProxyMap::iterator it
=
159 pending_paints_
.find(render_widget_id
);
160 if (it
!= pending_paints_
.end()) {
161 BackingStoreMsgProxyQueue
&queue
= it
->second
;
162 DCHECK(!queue
.empty());
163 proxy
= queue
.front();
165 // Flag the proxy as cancelled so that when it is run as a task it will
171 pending_paints_
.erase(it
);
176 *msg
= proxy
->message();
177 DCHECK(msg
->routing_id() == render_widget_id
);
181 // Calculate the maximum amount of time that we are willing to sleep.
182 base::TimeDelta max_sleep_time
=
183 max_delay
- (base::TimeTicks::Now() - time_start
);
184 if (max_sleep_time
<= base::TimeDelta::FromMilliseconds(0))
187 base::ThreadRestrictions::ScopedAllowWait allow_wait
;
188 event_
.TimedWait(max_sleep_time
);
194 void RenderWidgetHelper::ResumeRequestsForView(int route_id
) {
195 // We only need to resume blocked requests if we used a valid route_id.
196 // See CreateNewWindow.
197 if (route_id
!= MSG_ROUTING_NONE
) {
198 BrowserThread::PostTask(
199 BrowserThread::IO
, FROM_HERE
,
200 base::Bind(&RenderWidgetHelper::OnResumeRequestsForView
,
205 void RenderWidgetHelper::DidReceiveBackingStoreMsg(const IPC::Message
& msg
) {
206 int render_widget_id
= msg
.routing_id();
208 BackingStoreMsgProxy
* proxy
= new BackingStoreMsgProxy(this, msg
);
210 base::AutoLock
lock(pending_paints_lock_
);
212 pending_paints_
[render_widget_id
].push_back(proxy
);
215 // Notify anyone waiting on the UI thread that there is a new entry in the
216 // proxy map. If they don't find the entry they are looking for, then they
217 // will just continue waiting.
220 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
221 base::Bind(&BackingStoreMsgProxy::Run
, base::Owned(proxy
)));
224 void RenderWidgetHelper::OnDiscardBackingStoreMsg(BackingStoreMsgProxy
* proxy
) {
225 const IPC::Message
& msg
= proxy
->message();
227 // Remove the proxy from the map now that we are going to handle it normally.
229 base::AutoLock
lock(pending_paints_lock_
);
231 BackingStoreMsgProxyMap::iterator it
=
232 pending_paints_
.find(msg
.routing_id());
233 DCHECK(it
!= pending_paints_
.end());
234 BackingStoreMsgProxyQueue
&queue
= it
->second
;
235 DCHECK(queue
.front() == proxy
);
239 pending_paints_
.erase(it
);
243 void RenderWidgetHelper::OnDispatchBackingStoreMsg(
244 BackingStoreMsgProxy
* proxy
) {
245 OnDiscardBackingStoreMsg(proxy
);
247 // It is reasonable for the host to no longer exist.
248 RenderProcessHost
* host
= RenderProcessHost::FromID(render_process_id_
);
250 host
->OnMessageReceived(proxy
->message());
253 void RenderWidgetHelper::OnResumeDeferredNavigation(
254 const GlobalRequestID
& request_id
) {
255 resource_dispatcher_host_
->ResumeDeferredNavigation(request_id
);
258 void RenderWidgetHelper::OnResumeResponseDeferredAtStart(
259 const GlobalRequestID
& request_id
) {
260 resource_dispatcher_host_
->ResumeResponseDeferredAtStart(request_id
);
263 void RenderWidgetHelper::CreateNewWindow(
264 const ViewHostMsg_CreateWindow_Params
& params
,
265 bool no_javascript_access
,
266 base::ProcessHandle render_process
,
268 int* main_frame_route_id
,
270 SessionStorageNamespace
* session_storage_namespace
) {
271 if (params
.opener_suppressed
|| no_javascript_access
) {
272 // If the opener is supppressed or script access is disallowed, we should
273 // open the window in a new BrowsingInstance, and thus a new process. That
274 // means the current renderer process will not be able to route messages to
275 // it. Because of this, we will immediately show and navigate the window
276 // in OnCreateWindowOnUI, using the params provided here.
277 *route_id
= MSG_ROUTING_NONE
;
278 *main_frame_route_id
= MSG_ROUTING_NONE
;
281 *route_id
= GetNextRoutingID();
282 *main_frame_route_id
= GetNextRoutingID();
283 *surface_id
= GpuSurfaceTracker::Get()->AddSurfaceForRenderer(
284 render_process_id_
, *route_id
);
285 // Block resource requests until the view is created, since the HWND might
286 // be needed if a response ends up creating a plugin.
287 resource_dispatcher_host_
->BlockRequestsForRoute(
288 render_process_id_
, *route_id
);
289 resource_dispatcher_host_
->BlockRequestsForRoute(
290 render_process_id_
, *main_frame_route_id
);
293 BrowserThread::PostTask(
294 BrowserThread::UI
, FROM_HERE
,
295 base::Bind(&RenderWidgetHelper::OnCreateWindowOnUI
,
296 this, params
, *route_id
, *main_frame_route_id
,
297 make_scoped_refptr(session_storage_namespace
)));
300 void RenderWidgetHelper::OnCreateWindowOnUI(
301 const ViewHostMsg_CreateWindow_Params
& params
,
303 int main_frame_route_id
,
304 SessionStorageNamespace
* session_storage_namespace
) {
305 RenderViewHostImpl
* host
=
306 RenderViewHostImpl::FromID(render_process_id_
, params
.opener_id
);
308 host
->CreateNewWindow(route_id
, main_frame_route_id
, params
,
309 session_storage_namespace
);
312 void RenderWidgetHelper::OnResumeRequestsForView(int route_id
) {
313 resource_dispatcher_host_
->ResumeBlockedRequestsForRoute(
314 render_process_id_
, route_id
);
317 void RenderWidgetHelper::CreateNewWidget(int opener_id
,
318 blink::WebPopupType popup_type
,
321 *route_id
= GetNextRoutingID();
322 *surface_id
= GpuSurfaceTracker::Get()->AddSurfaceForRenderer(
323 render_process_id_
, *route_id
);
324 BrowserThread::PostTask(
325 BrowserThread::UI
, FROM_HERE
,
327 &RenderWidgetHelper::OnCreateWidgetOnUI
, this, opener_id
, *route_id
,
331 void RenderWidgetHelper::CreateNewFullscreenWidget(int opener_id
,
334 *route_id
= GetNextRoutingID();
335 *surface_id
= GpuSurfaceTracker::Get()->AddSurfaceForRenderer(
336 render_process_id_
, *route_id
);
337 BrowserThread::PostTask(
338 BrowserThread::UI
, FROM_HERE
,
340 &RenderWidgetHelper::OnCreateFullscreenWidgetOnUI
, this,
341 opener_id
, *route_id
));
344 void RenderWidgetHelper::OnCreateWidgetOnUI(
345 int opener_id
, int route_id
, blink::WebPopupType popup_type
) {
346 RenderViewHostImpl
* host
= RenderViewHostImpl::FromID(
347 render_process_id_
, opener_id
);
349 host
->CreateNewWidget(route_id
, popup_type
);
352 void RenderWidgetHelper::OnCreateFullscreenWidgetOnUI(int opener_id
,
354 RenderViewHostImpl
* host
= RenderViewHostImpl::FromID(
355 render_process_id_
, opener_id
);
357 host
->CreateNewFullscreenWidget(route_id
);
360 #if defined(OS_POSIX) && !defined(OS_ANDROID)
361 void RenderWidgetHelper::AllocTransportDIB(uint32 size
,
362 bool cache_in_browser
,
363 TransportDIB::Handle
* result
) {
364 scoped_ptr
<base::SharedMemory
> shared_memory(new base::SharedMemory());
365 if (!shared_memory
->CreateAnonymous(size
)) {
367 result
->auto_close
= false;
371 shared_memory
->GiveToProcess(0 /* pid, not needed */, result
);
373 if (cache_in_browser
) {
374 // Keep a copy of the file descriptor around
375 base::AutoLock
locked(allocated_dibs_lock_
);
376 allocated_dibs_
[shared_memory
->id()] = dup(result
->fd
);
380 void RenderWidgetHelper::FreeTransportDIB(TransportDIB::Id dib_id
) {
381 base::AutoLock
locked(allocated_dibs_lock_
);
383 const std::map
<TransportDIB::Id
, int>::iterator
384 i
= allocated_dibs_
.find(dib_id
);
386 if (i
!= allocated_dibs_
.end()) {
387 if (IGNORE_EINTR(close(i
->second
)) < 0)
388 PLOG(ERROR
) << "close";
389 allocated_dibs_
.erase(i
);
391 DLOG(WARNING
) << "Renderer asked us to free unknown transport DIB";
395 void RenderWidgetHelper::ClearAllocatedDIBs() {
396 for (std::map
<TransportDIB::Id
, int>::iterator
397 i
= allocated_dibs_
.begin(); i
!= allocated_dibs_
.end(); ++i
) {
398 if (IGNORE_EINTR(close(i
->second
)) < 0)
399 PLOG(ERROR
) << "close: " << i
->first
;
402 allocated_dibs_
.clear();
406 } // namespace content