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/scoped_handle.h"
12 #include "base/memory/shared_memory.h"
13 #include "build/build_config.h"
14 #include "content/child/npapi/npobject_proxy.h"
15 #include "content/child/npapi/npobject_util.h"
16 #include "content/child/npapi/webplugin_delegate_impl.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 "content/public/common/url_constants.h"
22 #include "skia/ext/platform_canvas.h"
23 #include "skia/ext/platform_device.h"
24 #include "third_party/WebKit/public/web/WebBindings.h"
25 #include "ui/gfx/blit.h"
26 #include "ui/gfx/canvas.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 "ui/base/x/x11_util_internal.h"
39 #include "content/common/plugin_process_messages.h"
40 #include "content/public/common/sandbox_init.h"
43 using WebKit::WebBindings
;
47 WebPluginProxy::SharedTransportDIB::SharedTransportDIB(TransportDIB
* dib
)
51 WebPluginProxy::SharedTransportDIB::~SharedTransportDIB() {
54 WebPluginProxy::WebPluginProxy(
55 PluginChannel
* channel
,
58 int host_render_view_routing_id
)
61 window_npobject_(NULL
),
62 plugin_element_(NULL
),
64 waiting_for_paint_(false),
66 windowless_buffer_index_(0),
67 host_render_view_routing_id_(host_render_view_routing_id
),
70 windowless_shm_pixmaps_
[0] = None
;
71 windowless_shm_pixmaps_
[1] = None
;
72 use_shm_pixmap_
= false;
74 // If the X server supports SHM pixmaps
75 // and the color depth and masks match,
76 // then consider using SHM pixmaps for windowless plugin painting.
77 Display
* display
= ui::GetXDisplay();
78 if (ui::QuerySharedMemorySupport(display
) == ui::SHARED_MEMORY_PIXMAP
&&
79 ui::BitsPerPixelForPixmapDepth(
80 display
, DefaultDepth(display
, DefaultScreen(display
))) == 32) {
81 Visual
* vis
= DefaultVisual(display
, DefaultScreen(display
));
83 if (vis
->red_mask
== 0xff0000 &&
84 vis
->green_mask
== 0xff00 &&
85 vis
->blue_mask
== 0xff)
86 use_shm_pixmap_
= true;
91 WebPluginProxy::~WebPluginProxy() {
93 if (windowless_shm_pixmaps_
[0] != None
)
94 XFreePixmap(ui::GetXDisplay(), windowless_shm_pixmaps_
[0]);
95 if (windowless_shm_pixmaps_
[1] != None
)
96 XFreePixmap(ui::GetXDisplay(), windowless_shm_pixmaps_
[1]);
99 #if defined(OS_MACOSX)
100 // Destroy the surface early, since it may send messages during cleanup.
101 if (accelerated_surface_
)
102 accelerated_surface_
.reset();
106 WebBindings::releaseObject(plugin_element_
);
107 if (window_npobject_
)
108 WebBindings::releaseObject(window_npobject_
);
111 bool WebPluginProxy::Send(IPC::Message
* msg
) {
112 return channel_
->Send(msg
);
115 void WebPluginProxy::SetWindow(gfx::PluginWindowHandle window
) {
116 Send(new PluginHostMsg_SetWindow(route_id_
, window
));
119 void WebPluginProxy::SetAcceptsInputEvents(bool accepts
) {
123 void WebPluginProxy::WillDestroyWindow(gfx::PluginWindowHandle window
) {
125 PluginThread::current()->Send(
126 new PluginProcessHostMsg_PluginWindowDestroyed(
127 window
, ::GetParent(window
)));
128 #elif defined(USE_X11)
136 void WebPluginProxy::SetWindowlessData(
137 HANDLE pump_messages_event
, gfx::NativeViewId dummy_activation_window
) {
138 HANDLE pump_messages_event_for_renderer
= NULL
;
139 BrokerDuplicateHandle(pump_messages_event
, channel_
->peer_pid(),
140 &pump_messages_event_for_renderer
,
141 SYNCHRONIZE
| EVENT_MODIFY_STATE
, 0);
142 DCHECK(pump_messages_event_for_renderer
);
143 Send(new PluginHostMsg_SetWindowlessData(
144 route_id_
, pump_messages_event_for_renderer
, dummy_activation_window
));
148 void WebPluginProxy::CancelResource(unsigned long id
) {
149 Send(new PluginHostMsg_CancelResource(route_id_
, id
));
150 resource_clients_
.erase(id
);
153 void WebPluginProxy::Invalidate() {
155 delegate_
->GetRect().width(),
156 delegate_
->GetRect().height());
157 InvalidateRect(rect
);
160 void WebPluginProxy::InvalidateRect(const gfx::Rect
& rect
) {
161 #if defined(OS_MACOSX)
162 // If this is a Core Animation plugin, all we need to do is inform the
164 if (!windowless_context()) {
165 delegate_
->PluginDidInvalidate();
169 // Some plugins will send invalidates larger than their own rect when
170 // offscreen, so constrain invalidates to the plugin rect.
171 gfx::Rect plugin_rect
= delegate_
->GetRect();
172 plugin_rect
.set_origin(gfx::Point(0, 0));
173 plugin_rect
.Intersect(rect
);
174 const gfx::Rect
invalidate_rect(plugin_rect
);
176 const gfx::Rect
invalidate_rect(rect
);
178 damaged_rect_
.Union(invalidate_rect
);
179 // Ignore NPN_InvalidateRect calls with empty rects. Also don't send an
180 // invalidate if it's outside the clipping region, since if we did it won't
181 // lead to a paint and we'll be stuck waiting forever for a DidPaint response.
183 // TODO(piman): There is a race condition here, because this test assumes
184 // that when the paint actually occurs, the clip rect will not have changed.
185 // This is not true because scrolling (or window resize) could occur and be
186 // handled by the renderer before it receives the InvalidateRect message,
187 // changing the clip rect and then not painting.
188 if (damaged_rect_
.IsEmpty() ||
189 !delegate_
->GetClipRect().Intersects(damaged_rect_
))
192 // Only send a single InvalidateRect message at a time. From DidPaint we
193 // will dispatch an additional InvalidateRect message if necessary.
194 if (!waiting_for_paint_
) {
195 waiting_for_paint_
= true;
196 // Invalidates caused by calls to NPN_InvalidateRect/NPN_InvalidateRgn
197 // need to be painted asynchronously as per the NPAPI spec.
198 base::MessageLoop::current()->PostTask(
200 base::Bind(&WebPluginProxy::OnPaint
,
201 weak_factory_
.GetWeakPtr(),
203 damaged_rect_
= gfx::Rect();
207 NPObject
* WebPluginProxy::GetWindowScriptNPObject() {
208 if (window_npobject_
)
209 return window_npobject_
;
211 int npobject_route_id
= channel_
->GenerateRouteID();
212 bool success
= false;
213 Send(new PluginHostMsg_GetWindowScriptNPObject(
214 route_id_
, npobject_route_id
, &success
));
218 // PluginChannel creates a dummy owner identifier for unknown owners, so
220 NPP owner
= channel_
->GetExistingNPObjectOwner(MSG_ROUTING_NONE
);
222 window_npobject_
= NPObjectProxy::Create(channel_
.get(),
224 host_render_view_routing_id_
,
228 return window_npobject_
;
231 NPObject
* WebPluginProxy::GetPluginElement() {
233 return plugin_element_
;
235 int npobject_route_id
= channel_
->GenerateRouteID();
236 bool success
= false;
237 Send(new PluginHostMsg_GetPluginElement(route_id_
, npobject_route_id
,
242 // PluginChannel creates a dummy owner identifier for unknown owners, so
244 NPP owner
= channel_
->GetExistingNPObjectOwner(MSG_ROUTING_NONE
);
246 plugin_element_
= NPObjectProxy::Create(channel_
.get(),
248 host_render_view_routing_id_
,
252 return plugin_element_
;
255 bool WebPluginProxy::FindProxyForUrl(const GURL
& url
, std::string
* proxy_list
) {
257 Send(new PluginHostMsg_ResolveProxy(route_id_
, url
, &result
, proxy_list
));
261 void WebPluginProxy::SetCookie(const GURL
& url
,
262 const GURL
& first_party_for_cookies
,
263 const std::string
& cookie
) {
264 Send(new PluginHostMsg_SetCookie(route_id_
, url
,
265 first_party_for_cookies
, cookie
));
268 std::string
WebPluginProxy::GetCookies(const GURL
& url
,
269 const GURL
& first_party_for_cookies
) {
271 Send(new PluginHostMsg_GetCookies(route_id_
, url
,
272 first_party_for_cookies
, &cookies
));
277 WebPluginResourceClient
* WebPluginProxy::GetResourceClient(int id
) {
278 ResourceClientMap::iterator iterator
= resource_clients_
.find(id
);
279 // The IPC messages which deal with streams are now asynchronous. It is
280 // now possible to receive stream messages from the renderer for streams
281 // which may have been cancelled by the plugin.
282 if (iterator
== resource_clients_
.end()) {
286 return iterator
->second
;
289 int WebPluginProxy::GetRendererId() {
291 return channel_
->renderer_id();
295 void WebPluginProxy::DidPaint() {
296 // If we have an accumulated damaged rect, then check to see if we need to
297 // send out another InvalidateRect message.
298 waiting_for_paint_
= false;
299 if (!damaged_rect_
.IsEmpty())
300 InvalidateRect(damaged_rect_
);
303 void WebPluginProxy::OnResourceCreated(int resource_id
,
304 WebPluginResourceClient
* client
) {
305 DCHECK(resource_clients_
.find(resource_id
) == resource_clients_
.end());
306 resource_clients_
[resource_id
] = client
;
309 void WebPluginProxy::HandleURLRequest(const char* url
,
316 bool notify_redirects
) {
317 if (!target
&& (0 == base::strcasecmp(method
, "GET"))) {
318 // Please refer to https://bugzilla.mozilla.org/show_bug.cgi?id=366082
319 // for more details on this.
320 if (delegate_
->GetQuirks() &
321 WebPluginDelegateImpl::PLUGIN_QUIRK_BLOCK_NONSTANDARD_GETURL_REQUESTS
) {
322 GURL
request_url(url
);
323 if (!request_url
.SchemeIs(chrome::kHttpScheme
) &&
324 !request_url
.SchemeIs(chrome::kHttpsScheme
) &&
325 !request_url
.SchemeIs(chrome::kFtpScheme
)) {
331 PluginHostMsg_URLRequest_Params params
;
333 params
.method
= method
;
335 params
.target
= std::string(target
);
338 params
.buffer
.resize(len
);
339 memcpy(¶ms
.buffer
.front(), buf
, len
);
342 params
.notify_id
= notify_id
;
343 params
.popups_allowed
= popups_allowed
;
344 params
.notify_redirects
= notify_redirects
;
346 Send(new PluginHostMsg_URLRequest(route_id_
, params
));
349 void WebPluginProxy::Paint(const gfx::Rect
& rect
) {
350 #if defined(OS_MACOSX)
351 if (!windowless_context())
354 if (!windowless_canvas() || !windowless_canvas()->getDevice())
358 // Clear the damaged area so that if the plugin doesn't paint there we won't
359 // end up with the old values.
360 gfx::Rect offset_rect
= rect
;
361 offset_rect
.Offset(delegate_
->GetRect().OffsetFromOrigin());
362 #if defined(OS_MACOSX)
363 CGContextSaveGState(windowless_context());
364 // It is possible for windowless_contexts_ to change during plugin painting
365 // (since the plugin can make a synchronous call during paint event handling),
366 // in which case we don't want to try to restore later. Not an owning ref
367 // since owning the ref without owning the shared backing memory doesn't make
368 // sense, so this should only be used for pointer comparisons.
369 CGContextRef saved_context_weak
= windowless_context();
370 // We also save the buffer index for the comparison because if we flip buffers
371 // but haven't reallocated them then we do need to restore the context because
372 // it is going to continue to be used.
373 int saved_index
= windowless_buffer_index_
;
375 CGContextClipToRect(windowless_context(), rect
.ToCGRect());
376 // TODO(caryclark): This is a temporary workaround to allow the Darwin / Skia
377 // port to share code with the Darwin / CG port. All ports will eventually use
378 // the common code below.
379 delegate_
->CGPaint(windowless_context(), rect
);
380 if (windowless_contexts_
[saved_index
].get() == saved_context_weak
)
381 CGContextRestoreGState(windowless_contexts_
[saved_index
]);
383 // See above comment about windowless_context_ changing.
384 // http::/crbug.com/139462
385 skia::RefPtr
<skia::PlatformCanvas
> saved_canvas
= windowless_canvas();
387 scoped_refptr
<SharedTransportDIB
> local_dib_ref(
388 windowless_dibs_
[windowless_buffer_index_
]);
391 saved_canvas
->save();
393 // The given clip rect is relative to the plugin coordinate system.
394 SkRect sk_rect
= { SkIntToScalar(rect
.x()),
395 SkIntToScalar(rect
.y()),
396 SkIntToScalar(rect
.right()),
397 SkIntToScalar(rect
.bottom()) };
398 saved_canvas
->clipRect(sk_rect
);
400 // Fill a transparent value so that if the plugin supports transparency that
402 saved_canvas
->drawColor(SkColorSetARGB(0, 0, 0, 0), SkXfermode::kSrc_Mode
);
404 // Bring the windowless canvas into the window coordinate system, which is
405 // how the plugin expects to draw (since the windowless API was originally
406 // designed just for scribbling over the web page).
407 saved_canvas
->translate(SkIntToScalar(-delegate_
->GetRect().x()),
408 SkIntToScalar(-delegate_
->GetRect().y()));
410 // Before we send the invalidate, paint so that renderer uses the updated
412 delegate_
->Paint(saved_canvas
.get(), offset_rect
);
414 saved_canvas
->restore();
418 void WebPluginProxy::UpdateGeometry(
419 const gfx::Rect
& window_rect
,
420 const gfx::Rect
& clip_rect
,
421 const TransportDIB::Handle
& windowless_buffer0
,
422 const TransportDIB::Handle
& windowless_buffer1
,
423 int windowless_buffer_index
) {
424 gfx::Rect old
= delegate_
->GetRect();
425 gfx::Rect old_clip_rect
= delegate_
->GetClipRect();
427 // Update the buffers before doing anything that could call into plugin code,
428 // so that we don't process buffer changes out of order if plugins make
429 // synchronous calls that lead to nested UpdateGeometry calls.
430 if (TransportDIB::is_valid_handle(windowless_buffer0
)) {
431 // The plugin's rect changed, so now we have new buffers to draw into.
432 SetWindowlessBuffers(windowless_buffer0
,
437 DCHECK(0 <= windowless_buffer_index
&& windowless_buffer_index
<= 1);
438 windowless_buffer_index_
= windowless_buffer_index
;
440 delegate_
->SetWindowlessShmPixmap(windowless_shm_pixmap());
443 #if defined(OS_MACOSX)
444 delegate_
->UpdateGeometryAndContext(
445 window_rect
, clip_rect
, windowless_context());
447 delegate_
->UpdateGeometry(window_rect
, clip_rect
);
450 // Send over any pending invalidates which occured when the plugin was
452 if (delegate_
->IsWindowless() && !clip_rect
.IsEmpty() &&
453 !damaged_rect_
.IsEmpty()) {
454 InvalidateRect(damaged_rect_
);
460 void WebPluginProxy::CreateCanvasFromHandle(
461 const TransportDIB::Handle
& dib_handle
,
462 const gfx::Rect
& window_rect
,
463 skia::RefPtr
<skia::PlatformCanvas
>* canvas
) {
464 *canvas
= skia::AdoptRef(
465 skia::CreatePlatformCanvas(window_rect
.width(),
466 window_rect
.height(),
469 skia::RETURN_NULL_ON_FAILURE
));
470 // The canvas does not own the section so we need to close it now.
471 CloseHandle(dib_handle
);
474 void WebPluginProxy::SetWindowlessBuffers(
475 const TransportDIB::Handle
& windowless_buffer0
,
476 const TransportDIB::Handle
& windowless_buffer1
,
477 const gfx::Rect
& window_rect
) {
478 CreateCanvasFromHandle(windowless_buffer0
,
480 &windowless_canvases_
[0]);
481 if (!windowless_canvases_
[0]) {
482 windowless_canvases_
[1].clear();
485 CreateCanvasFromHandle(windowless_buffer1
,
487 &windowless_canvases_
[1]);
488 if (!windowless_canvases_
[1]) {
489 windowless_canvases_
[0].clear();
494 #elif defined(OS_MACOSX)
496 void WebPluginProxy::CreateDIBAndCGContextFromHandle(
497 const TransportDIB::Handle
& dib_handle
,
498 const gfx::Rect
& window_rect
,
499 scoped_ptr
<TransportDIB
>* dib_out
,
500 base::ScopedCFTypeRef
<CGContextRef
>* cg_context_out
) {
501 // Convert the shared memory handle to a handle that works in our process,
502 // and then use that to create a CGContextRef.
503 TransportDIB
* dib
= TransportDIB::Map(dib_handle
);
504 CGContextRef cg_context
= NULL
;
506 cg_context
= CGBitmapContextCreate(
509 window_rect
.height(),
511 4 * window_rect
.width(),
512 base::mac::GetSystemColorSpace(),
513 kCGImageAlphaPremultipliedFirst
| kCGBitmapByteOrder32Host
);
514 CGContextTranslateCTM(cg_context
, 0, window_rect
.height());
515 CGContextScaleCTM(cg_context
, 1, -1);
518 cg_context_out
->reset(cg_context
);
521 void WebPluginProxy::SetWindowlessBuffers(
522 const TransportDIB::Handle
& windowless_buffer0
,
523 const TransportDIB::Handle
& windowless_buffer1
,
524 const gfx::Rect
& window_rect
) {
525 CreateDIBAndCGContextFromHandle(windowless_buffer0
,
527 &windowless_dibs_
[0],
528 &windowless_contexts_
[0]);
529 CreateDIBAndCGContextFromHandle(windowless_buffer1
,
531 &windowless_dibs_
[1],
532 &windowless_contexts_
[1]);
535 #elif defined(TOOLKIT_GTK)
537 void WebPluginProxy::CreateDIBAndCanvasFromHandle(
538 const TransportDIB::Handle
& dib_handle
,
539 const gfx::Rect
& window_rect
,
540 scoped_refptr
<SharedTransportDIB
>* dib_out
,
541 skia::RefPtr
<skia::PlatformCanvas
>* canvas
) {
542 TransportDIB
* dib
= TransportDIB::Map(dib_handle
);
543 // dib may be NULL if the renderer has already destroyed the TransportDIB by
544 // the time we receive the handle, e.g. in case of multiple resizes.
546 *canvas
= skia::AdoptRef(
547 dib
->GetPlatformCanvas(window_rect
.width(), window_rect
.height()));
551 *dib_out
= new SharedTransportDIB(dib
);
554 void WebPluginProxy::CreateShmPixmapFromDIB(
556 const gfx::Rect
& window_rect
,
559 Display
* display
= ui::GetXDisplay();
560 XID root_window
= ui::GetX11RootWindow();
561 XShmSegmentInfo shminfo
= {0};
563 if (*pixmap_out
!= None
)
564 XFreePixmap(display
, *pixmap_out
);
566 shminfo
.shmseg
= dib
->MapToX(display
);
567 // Create a shared memory pixmap based on the image buffer.
568 *pixmap_out
= XShmCreatePixmap(display
, root_window
,
570 window_rect
.width(), window_rect
.height(),
571 DefaultDepth(display
,
572 DefaultScreen(display
)));
576 void WebPluginProxy::SetWindowlessBuffers(
577 const TransportDIB::Handle
& windowless_buffer0
,
578 const TransportDIB::Handle
& windowless_buffer1
,
579 const gfx::Rect
& window_rect
) {
580 CreateDIBAndCanvasFromHandle(windowless_buffer0
,
582 &windowless_dibs_
[0],
583 &windowless_canvases_
[0]);
584 CreateDIBAndCanvasFromHandle(windowless_buffer1
,
586 &windowless_dibs_
[1],
587 &windowless_canvases_
[1]);
589 // If SHM pixmaps support is available, create SHM pixmaps to pass to the
590 // delegate for windowless plugin painting.
591 if (delegate_
->IsWindowless() && use_shm_pixmap_
) {
592 CreateShmPixmapFromDIB(windowless_dibs_
[0]->dib(),
594 &windowless_shm_pixmaps_
[0]);
595 CreateShmPixmapFromDIB(windowless_dibs_
[1]->dib(),
597 &windowless_shm_pixmaps_
[1]);
603 void WebPluginProxy::SetWindowlessBuffers(
604 const TransportDIB::Handle
& windowless_buffer0
,
605 const TransportDIB::Handle
& windowless_buffer1
,
606 const gfx::Rect
& window_rect
) {
612 void WebPluginProxy::CancelDocumentLoad() {
613 Send(new PluginHostMsg_CancelDocumentLoad(route_id_
));
616 void WebPluginProxy::InitiateHTTPRangeRequest(
617 const char* url
, const char* range_info
, int range_request_id
) {
618 Send(new PluginHostMsg_InitiateHTTPRangeRequest(
619 route_id_
, url
, range_info
, range_request_id
));
622 void WebPluginProxy::SetDeferResourceLoading(unsigned long resource_id
,
624 Send(new PluginHostMsg_DeferResourceLoading(route_id_
, resource_id
, defer
));
627 #if defined(OS_MACOSX)
628 void WebPluginProxy::FocusChanged(bool focused
) {
629 IPC::Message
* msg
= new PluginHostMsg_FocusChanged(route_id_
, focused
);
633 void WebPluginProxy::StartIme() {
634 IPC::Message
* msg
= new PluginHostMsg_StartIme(route_id_
);
635 // This message can be sent during event-handling, and needs to be delivered
636 // within that context.
637 msg
->set_unblock(true);
641 WebPluginAcceleratedSurface
* WebPluginProxy::GetAcceleratedSurface(
642 gfx::GpuPreference gpu_preference
) {
643 if (!accelerated_surface_
)
644 accelerated_surface_
.reset(
645 WebPluginAcceleratedSurfaceProxy::Create(this, gpu_preference
));
646 return accelerated_surface_
.get();
649 void WebPluginProxy::AcceleratedPluginEnabledRendering() {
650 Send(new PluginHostMsg_AcceleratedPluginEnabledRendering(route_id_
));
653 void WebPluginProxy::AcceleratedPluginAllocatedIOSurface(int32 width
,
656 Send(new PluginHostMsg_AcceleratedPluginAllocatedIOSurface(
657 route_id_
, width
, height
, surface_id
));
660 void WebPluginProxy::AcceleratedPluginSwappedIOSurface() {
661 Send(new PluginHostMsg_AcceleratedPluginSwappedIOSurface(
666 void WebPluginProxy::OnPaint(const gfx::Rect
& damaged_rect
) {
667 GetContentClient()->SetActiveURL(page_url_
);
670 Send(new PluginHostMsg_InvalidateRect(route_id_
, damaged_rect
));
673 bool WebPluginProxy::IsOffTheRecord() {
674 return channel_
->incognito();
677 void WebPluginProxy::ResourceClientDeleted(
678 WebPluginResourceClient
* resource_client
) {
679 ResourceClientMap::iterator index
= resource_clients_
.begin();
680 while (index
!= resource_clients_
.end()) {
681 WebPluginResourceClient
* client
= (*index
).second
;
683 if (client
== resource_client
) {
684 resource_clients_
.erase(index
++);
691 void WebPluginProxy::URLRedirectResponse(bool allow
, int resource_id
) {
692 Send(new PluginHostMsg_URLRedirectResponse(route_id_
, allow
, resource_id
));
695 #if defined(OS_WIN) && !defined(USE_AURA)
696 void WebPluginProxy::UpdateIMEStatus() {
697 // Retrieve the IME status from a plug-in and send it to a renderer process
698 // when the plug-in has updated it.
700 gfx::Rect caret_rect
;
701 if (!delegate_
->GetIMEStatus(&input_type
, &caret_rect
))
704 Send(new PluginHostMsg_NotifyIMEStatus(route_id_
, input_type
, caret_rect
));
708 } // namespace content