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/plugin/webplugin_proxy.h"
7 #include "build/build_config.h"
10 #include "base/lazy_instance.h"
11 #include "base/memory/shared_memory.h"
12 #include "build/build_config.h"
13 #include "content/child/npapi/npobject_proxy.h"
14 #include "content/child/npapi/npobject_util.h"
15 #include "content/child/npapi/webplugin_delegate_impl.h"
16 #include "content/child/npapi/webplugin_resource_client.h"
17 #include "content/child/plugin_messages.h"
18 #include "content/plugin/plugin_channel.h"
19 #include "content/plugin/plugin_thread.h"
20 #include "content/public/common/content_client.h"
21 #include "skia/ext/platform_canvas.h"
22 #include "skia/ext/platform_device.h"
23 #include "third_party/WebKit/public/web/WebBindings.h"
24 #include "ui/gfx/blit.h"
25 #include "ui/gfx/canvas.h"
26 #include "url/url_constants.h"
28 #if defined(OS_MACOSX)
29 #include "base/mac/mac_util.h"
30 #include "base/mac/scoped_cftyperef.h"
31 #include "content/plugin/webplugin_accelerated_surface_proxy_mac.h"
35 #include "content/common/plugin_process_messages.h"
36 #include "content/public/common/sandbox_init.h"
39 using blink::WebBindings
;
43 WebPluginProxy::SharedTransportDIB::SharedTransportDIB(TransportDIB
* dib
)
47 WebPluginProxy::SharedTransportDIB::~SharedTransportDIB() {
50 WebPluginProxy::WebPluginProxy(
51 PluginChannel
* channel
,
54 int host_render_view_routing_id
)
57 window_npobject_(NULL
),
58 plugin_element_(NULL
),
60 waiting_for_paint_(false),
62 windowless_buffer_index_(0),
63 host_render_view_routing_id_(host_render_view_routing_id
),
67 WebPluginProxy::~WebPluginProxy() {
68 #if defined(OS_MACOSX)
69 // Destroy the surface early, since it may send messages during cleanup.
70 if (accelerated_surface_
)
71 accelerated_surface_
.reset();
75 WebBindings::releaseObject(plugin_element_
);
77 WebBindings::releaseObject(window_npobject_
);
80 bool WebPluginProxy::Send(IPC::Message
* msg
) {
81 return channel_
->Send(msg
);
84 void WebPluginProxy::SetWindow(gfx::PluginWindowHandle window
) {
85 Send(new PluginHostMsg_SetWindow(route_id_
, window
));
88 void WebPluginProxy::SetAcceptsInputEvents(bool accepts
) {
92 void WebPluginProxy::WillDestroyWindow(gfx::PluginWindowHandle window
) {
94 PluginThread::current()->Send(
95 new PluginProcessHostMsg_PluginWindowDestroyed(
96 window
, ::GetParent(window
)));
103 void WebPluginProxy::SetWindowlessData(
104 HANDLE pump_messages_event
, gfx::NativeViewId dummy_activation_window
) {
105 HANDLE pump_messages_event_for_renderer
= NULL
;
106 BrokerDuplicateHandle(pump_messages_event
, channel_
->peer_pid(),
107 &pump_messages_event_for_renderer
,
108 SYNCHRONIZE
| EVENT_MODIFY_STATE
, 0);
109 DCHECK(pump_messages_event_for_renderer
);
110 Send(new PluginHostMsg_SetWindowlessData(
111 route_id_
, pump_messages_event_for_renderer
, dummy_activation_window
));
115 void WebPluginProxy::CancelResource(unsigned long id
) {
116 Send(new PluginHostMsg_CancelResource(route_id_
, id
));
117 resource_clients_
.erase(id
);
120 void WebPluginProxy::Invalidate() {
122 delegate_
->GetRect().width(),
123 delegate_
->GetRect().height());
124 InvalidateRect(rect
);
127 void WebPluginProxy::InvalidateRect(const gfx::Rect
& rect
) {
128 #if defined(OS_MACOSX)
129 // If this is a Core Animation plugin, all we need to do is inform the
131 if (!windowless_context()) {
132 delegate_
->PluginDidInvalidate();
136 // Some plugins will send invalidates larger than their own rect when
137 // offscreen, so constrain invalidates to the plugin rect.
138 gfx::Rect plugin_rect
= delegate_
->GetRect();
139 plugin_rect
.set_origin(gfx::Point(0, 0));
140 plugin_rect
.Intersect(rect
);
141 const gfx::Rect
invalidate_rect(plugin_rect
);
143 const gfx::Rect
invalidate_rect(rect
);
145 damaged_rect_
.Union(invalidate_rect
);
146 // Ignore NPN_InvalidateRect calls with empty rects. Also don't send an
147 // invalidate if it's outside the clipping region, since if we did it won't
148 // lead to a paint and we'll be stuck waiting forever for a DidPaint response.
150 // TODO(piman): There is a race condition here, because this test assumes
151 // that when the paint actually occurs, the clip rect will not have changed.
152 // This is not true because scrolling (or window resize) could occur and be
153 // handled by the renderer before it receives the InvalidateRect message,
154 // changing the clip rect and then not painting.
155 if (damaged_rect_
.IsEmpty() ||
156 !delegate_
->GetClipRect().Intersects(damaged_rect_
))
159 // Only send a single InvalidateRect message at a time. From DidPaint we
160 // will dispatch an additional InvalidateRect message if necessary.
161 if (!waiting_for_paint_
) {
162 waiting_for_paint_
= true;
163 // Invalidates caused by calls to NPN_InvalidateRect/NPN_InvalidateRgn
164 // need to be painted asynchronously as per the NPAPI spec.
165 base::MessageLoop::current()->PostTask(
167 base::Bind(&WebPluginProxy::OnPaint
,
168 weak_factory_
.GetWeakPtr(),
170 damaged_rect_
= gfx::Rect();
174 NPObject
* WebPluginProxy::GetWindowScriptNPObject() {
175 if (window_npobject_
)
176 return window_npobject_
;
178 int npobject_route_id
= channel_
->GenerateRouteID();
179 bool success
= false;
180 Send(new PluginHostMsg_GetWindowScriptNPObject(
181 route_id_
, npobject_route_id
, &success
));
185 // PluginChannel creates a dummy owner identifier for unknown owners, so
187 NPP owner
= channel_
->GetExistingNPObjectOwner(MSG_ROUTING_NONE
);
189 window_npobject_
= NPObjectProxy::Create(channel_
.get(),
191 host_render_view_routing_id_
,
195 return window_npobject_
;
198 NPObject
* WebPluginProxy::GetPluginElement() {
200 return plugin_element_
;
202 int npobject_route_id
= channel_
->GenerateRouteID();
203 bool success
= false;
204 Send(new PluginHostMsg_GetPluginElement(route_id_
, npobject_route_id
,
209 // PluginChannel creates a dummy owner identifier for unknown owners, so
211 NPP owner
= channel_
->GetExistingNPObjectOwner(MSG_ROUTING_NONE
);
213 plugin_element_
= NPObjectProxy::Create(channel_
.get(),
215 host_render_view_routing_id_
,
219 return plugin_element_
;
222 bool WebPluginProxy::FindProxyForUrl(const GURL
& url
, std::string
* proxy_list
) {
224 Send(new PluginHostMsg_ResolveProxy(route_id_
, url
, &result
, proxy_list
));
228 void WebPluginProxy::SetCookie(const GURL
& url
,
229 const GURL
& first_party_for_cookies
,
230 const std::string
& cookie
) {
231 Send(new PluginHostMsg_SetCookie(route_id_
, url
,
232 first_party_for_cookies
, cookie
));
235 std::string
WebPluginProxy::GetCookies(const GURL
& url
,
236 const GURL
& first_party_for_cookies
) {
238 Send(new PluginHostMsg_GetCookies(route_id_
, url
,
239 first_party_for_cookies
, &cookies
));
244 WebPluginResourceClient
* WebPluginProxy::GetResourceClient(int id
) {
245 ResourceClientMap::iterator iterator
= resource_clients_
.find(id
);
246 // The IPC messages which deal with streams are now asynchronous. It is
247 // now possible to receive stream messages from the renderer for streams
248 // which may have been cancelled by the plugin.
249 if (iterator
== resource_clients_
.end()) {
253 return iterator
->second
;
256 int WebPluginProxy::GetRendererId() {
258 return channel_
->renderer_id();
262 void WebPluginProxy::DidPaint() {
263 // If we have an accumulated damaged rect, then check to see if we need to
264 // send out another InvalidateRect message.
265 waiting_for_paint_
= false;
266 if (!damaged_rect_
.IsEmpty())
267 InvalidateRect(damaged_rect_
);
270 void WebPluginProxy::OnResourceCreated(int resource_id
,
271 WebPluginResourceClient
* client
) {
272 DCHECK(resource_clients_
.find(resource_id
) == resource_clients_
.end());
273 resource_clients_
[resource_id
] = client
;
276 void WebPluginProxy::HandleURLRequest(const char* url
,
283 bool notify_redirects
) {
284 if (!target
&& (0 == base::strcasecmp(method
, "GET"))) {
285 // Please refer to https://bugzilla.mozilla.org/show_bug.cgi?id=366082
286 // for more details on this.
287 if (delegate_
->GetQuirks() &
288 WebPluginDelegateImpl::PLUGIN_QUIRK_BLOCK_NONSTANDARD_GETURL_REQUESTS
) {
289 GURL
request_url(url
);
290 if (!request_url
.SchemeIs(url::kHttpScheme
) &&
291 !request_url
.SchemeIs(url::kHttpsScheme
) &&
292 !request_url
.SchemeIs(url::kFtpScheme
)) {
298 PluginHostMsg_URLRequest_Params params
;
300 params
.method
= method
;
302 params
.target
= std::string(target
);
305 params
.buffer
.resize(len
);
306 memcpy(¶ms
.buffer
.front(), buf
, len
);
309 params
.notify_id
= notify_id
;
310 params
.popups_allowed
= popups_allowed
;
311 params
.notify_redirects
= notify_redirects
;
313 Send(new PluginHostMsg_URLRequest(route_id_
, params
));
316 void WebPluginProxy::Paint(const gfx::Rect
& rect
) {
317 #if defined(OS_MACOSX)
318 if (!windowless_context())
321 if (!windowless_canvas() || !windowless_canvas()->getDevice())
325 // Clear the damaged area so that if the plugin doesn't paint there we won't
326 // end up with the old values.
327 gfx::Rect offset_rect
= rect
;
328 offset_rect
.Offset(delegate_
->GetRect().OffsetFromOrigin());
329 #if defined(OS_MACOSX)
330 CGContextSaveGState(windowless_context());
331 // It is possible for windowless_contexts_ to change during plugin painting
332 // (since the plugin can make a synchronous call during paint event handling),
333 // in which case we don't want to try to restore later. Not an owning ref
334 // since owning the ref without owning the shared backing memory doesn't make
335 // sense, so this should only be used for pointer comparisons.
336 CGContextRef saved_context_weak
= windowless_context();
337 // We also save the buffer index for the comparison because if we flip buffers
338 // but haven't reallocated them then we do need to restore the context because
339 // it is going to continue to be used.
340 int saved_index
= windowless_buffer_index_
;
342 CGContextClipToRect(windowless_context(), rect
.ToCGRect());
343 // TODO(caryclark): This is a temporary workaround to allow the Darwin / Skia
344 // port to share code with the Darwin / CG port. All ports will eventually use
345 // the common code below.
346 delegate_
->CGPaint(windowless_context(), rect
);
347 if (windowless_contexts_
[saved_index
].get() == saved_context_weak
)
348 CGContextRestoreGState(windowless_contexts_
[saved_index
]);
350 // See above comment about windowless_context_ changing.
351 // http::/crbug.com/139462
352 skia::RefPtr
<skia::PlatformCanvas
> saved_canvas
= windowless_canvas();
354 saved_canvas
->save();
356 // The given clip rect is relative to the plugin coordinate system.
357 SkRect sk_rect
= { SkIntToScalar(rect
.x()),
358 SkIntToScalar(rect
.y()),
359 SkIntToScalar(rect
.right()),
360 SkIntToScalar(rect
.bottom()) };
361 saved_canvas
->clipRect(sk_rect
);
363 // Fill a transparent value so that if the plugin supports transparency that
365 saved_canvas
->drawColor(SkColorSetARGB(0, 0, 0, 0), SkXfermode::kSrc_Mode
);
367 // Bring the windowless canvas into the window coordinate system, which is
368 // how the plugin expects to draw (since the windowless API was originally
369 // designed just for scribbling over the web page).
370 saved_canvas
->translate(SkIntToScalar(-delegate_
->GetRect().x()),
371 SkIntToScalar(-delegate_
->GetRect().y()));
373 // Before we send the invalidate, paint so that renderer uses the updated
375 delegate_
->Paint(saved_canvas
.get(), offset_rect
);
377 saved_canvas
->restore();
381 void WebPluginProxy::UpdateGeometry(
382 const gfx::Rect
& window_rect
,
383 const gfx::Rect
& clip_rect
,
384 const TransportDIB::Handle
& windowless_buffer0
,
385 const TransportDIB::Handle
& windowless_buffer1
,
386 int windowless_buffer_index
) {
387 gfx::Rect old
= delegate_
->GetRect();
388 gfx::Rect old_clip_rect
= delegate_
->GetClipRect();
390 // Update the buffers before doing anything that could call into plugin code,
391 // so that we don't process buffer changes out of order if plugins make
392 // synchronous calls that lead to nested UpdateGeometry calls.
393 if (TransportDIB::is_valid_handle(windowless_buffer0
)) {
394 // The plugin's rect changed, so now we have new buffers to draw into.
395 SetWindowlessBuffers(windowless_buffer0
,
400 DCHECK(0 <= windowless_buffer_index
&& windowless_buffer_index
<= 1);
401 windowless_buffer_index_
= windowless_buffer_index
;
403 #if defined(OS_MACOSX)
404 delegate_
->UpdateGeometryAndContext(
405 window_rect
, clip_rect
, windowless_context());
407 delegate_
->UpdateGeometry(window_rect
, clip_rect
);
410 // Send over any pending invalidates which occured when the plugin was
412 if (delegate_
->IsWindowless() && !clip_rect
.IsEmpty() &&
413 !damaged_rect_
.IsEmpty()) {
414 InvalidateRect(damaged_rect_
);
420 void WebPluginProxy::CreateCanvasFromHandle(
421 const TransportDIB::Handle
& dib_handle
,
422 const gfx::Rect
& window_rect
,
423 skia::RefPtr
<skia::PlatformCanvas
>* canvas
) {
424 *canvas
= skia::AdoptRef(
425 skia::CreatePlatformCanvas(window_rect
.width(),
426 window_rect
.height(),
429 skia::RETURN_NULL_ON_FAILURE
));
430 // The canvas does not own the section so we need to close it now.
431 CloseHandle(dib_handle
);
434 void WebPluginProxy::SetWindowlessBuffers(
435 const TransportDIB::Handle
& windowless_buffer0
,
436 const TransportDIB::Handle
& windowless_buffer1
,
437 const gfx::Rect
& window_rect
) {
438 CreateCanvasFromHandle(windowless_buffer0
,
440 &windowless_canvases_
[0]);
441 if (!windowless_canvases_
[0]) {
442 windowless_canvases_
[1].clear();
445 CreateCanvasFromHandle(windowless_buffer1
,
447 &windowless_canvases_
[1]);
448 if (!windowless_canvases_
[1]) {
449 windowless_canvases_
[0].clear();
454 #elif defined(OS_MACOSX)
456 void WebPluginProxy::CreateDIBAndCGContextFromHandle(
457 const TransportDIB::Handle
& dib_handle
,
458 const gfx::Rect
& window_rect
,
459 scoped_ptr
<TransportDIB
>* dib_out
,
460 base::ScopedCFTypeRef
<CGContextRef
>* cg_context_out
) {
461 // Convert the shared memory handle to a handle that works in our process,
462 // and then use that to create a CGContextRef.
463 TransportDIB
* dib
= TransportDIB::Map(dib_handle
);
464 CGContextRef cg_context
= NULL
;
466 cg_context
= CGBitmapContextCreate(
469 window_rect
.height(),
471 4 * window_rect
.width(),
472 base::mac::GetSystemColorSpace(),
473 kCGImageAlphaPremultipliedFirst
| kCGBitmapByteOrder32Host
);
474 CGContextTranslateCTM(cg_context
, 0, window_rect
.height());
475 CGContextScaleCTM(cg_context
, 1, -1);
478 cg_context_out
->reset(cg_context
);
481 void WebPluginProxy::SetWindowlessBuffers(
482 const TransportDIB::Handle
& windowless_buffer0
,
483 const TransportDIB::Handle
& windowless_buffer1
,
484 const gfx::Rect
& window_rect
) {
485 CreateDIBAndCGContextFromHandle(windowless_buffer0
,
487 &windowless_dibs_
[0],
488 &windowless_contexts_
[0]);
489 CreateDIBAndCGContextFromHandle(windowless_buffer1
,
491 &windowless_dibs_
[1],
492 &windowless_contexts_
[1]);
497 void WebPluginProxy::SetWindowlessBuffers(
498 const TransportDIB::Handle
& windowless_buffer0
,
499 const TransportDIB::Handle
& windowless_buffer1
,
500 const gfx::Rect
& window_rect
) {
506 void WebPluginProxy::CancelDocumentLoad() {
507 Send(new PluginHostMsg_CancelDocumentLoad(route_id_
));
510 void WebPluginProxy::InitiateHTTPRangeRequest(
511 const char* url
, const char* range_info
, int range_request_id
) {
512 Send(new PluginHostMsg_InitiateHTTPRangeRequest(
513 route_id_
, url
, range_info
, range_request_id
));
516 void WebPluginProxy::DidStartLoading() {
517 Send(new PluginHostMsg_DidStartLoading(route_id_
));
520 void WebPluginProxy::DidStopLoading() {
521 Send(new PluginHostMsg_DidStopLoading(route_id_
));
524 void WebPluginProxy::SetDeferResourceLoading(unsigned long resource_id
,
526 Send(new PluginHostMsg_DeferResourceLoading(route_id_
, resource_id
, defer
));
529 #if defined(OS_MACOSX)
530 void WebPluginProxy::FocusChanged(bool focused
) {
531 IPC::Message
* msg
= new PluginHostMsg_FocusChanged(route_id_
, focused
);
535 void WebPluginProxy::StartIme() {
536 IPC::Message
* msg
= new PluginHostMsg_StartIme(route_id_
);
537 // This message can be sent during event-handling, and needs to be delivered
538 // within that context.
539 msg
->set_unblock(true);
543 WebPluginAcceleratedSurface
* WebPluginProxy::GetAcceleratedSurface(
544 gfx::GpuPreference gpu_preference
) {
545 if (!accelerated_surface_
)
546 accelerated_surface_
.reset(
547 WebPluginAcceleratedSurfaceProxy::Create(this, gpu_preference
));
548 return accelerated_surface_
.get();
551 void WebPluginProxy::AcceleratedPluginEnabledRendering() {
552 Send(new PluginHostMsg_AcceleratedPluginEnabledRendering(route_id_
));
555 void WebPluginProxy::AcceleratedPluginAllocatedIOSurface(int32 width
,
558 Send(new PluginHostMsg_AcceleratedPluginAllocatedIOSurface(
559 route_id_
, width
, height
, surface_id
));
562 void WebPluginProxy::AcceleratedPluginSwappedIOSurface() {
563 Send(new PluginHostMsg_AcceleratedPluginSwappedIOSurface(
568 void WebPluginProxy::OnPaint(const gfx::Rect
& damaged_rect
) {
569 GetContentClient()->SetActiveURL(page_url_
);
572 Send(new PluginHostMsg_InvalidateRect(route_id_
, damaged_rect
));
575 bool WebPluginProxy::IsOffTheRecord() {
576 return channel_
->incognito();
579 void WebPluginProxy::ResourceClientDeleted(
580 WebPluginResourceClient
* resource_client
) {
581 // resource_client->ResourceId() is 0 at this point, so can't use it as an
582 // index into the map.
583 ResourceClientMap::iterator index
= resource_clients_
.begin();
584 while (index
!= resource_clients_
.end()) {
585 WebPluginResourceClient
* client
= (*index
).second
;
586 if (client
== resource_client
) {
587 resource_clients_
.erase(index
);
595 void WebPluginProxy::URLRedirectResponse(bool allow
, int resource_id
) {
596 Send(new PluginHostMsg_URLRedirectResponse(route_id_
, allow
, resource_id
));
599 bool WebPluginProxy::CheckIfRunInsecureContent(const GURL
& url
) {
601 Send(new PluginHostMsg_CheckIfRunInsecureContent(
602 route_id_
, url
, &result
));
606 #if defined(OS_WIN) && !defined(USE_AURA)
607 void WebPluginProxy::UpdateIMEStatus() {
608 // Retrieve the IME status from a plug-in and send it to a renderer process
609 // when the plug-in has updated it.
611 gfx::Rect caret_rect
;
612 if (!delegate_
->GetIMEStatus(&input_type
, &caret_rect
))
615 Send(new PluginHostMsg_NotifyIMEStatus(route_id_
, input_type
, caret_rect
));
619 } // namespace content