Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / plugin / webplugin_proxy.cc
blob6b6d696ca5da8f1458a3c712744b3238e605d1f2
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"
9 #include "base/bind.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/canvas.h"
25 #include "url/url_constants.h"
27 #if defined(OS_MACOSX)
28 #include "base/mac/mac_util.h"
29 #include "base/mac/scoped_cftyperef.h"
30 #include "content/plugin/webplugin_accelerated_surface_proxy_mac.h"
31 #endif
33 #if defined(OS_WIN)
34 #include "content/common/plugin_process_messages.h"
35 #include "content/public/common/sandbox_init.h"
36 #endif
38 using blink::WebBindings;
40 namespace content {
42 WebPluginProxy::SharedTransportDIB::SharedTransportDIB(TransportDIB* dib)
43 : dib_(dib) {
46 WebPluginProxy::SharedTransportDIB::~SharedTransportDIB() {
49 WebPluginProxy::WebPluginProxy(
50 PluginChannel* channel,
51 int route_id,
52 const GURL& page_url,
53 int host_render_view_routing_id)
54 : channel_(channel),
55 route_id_(route_id),
56 window_npobject_(NULL),
57 plugin_element_(NULL),
58 delegate_(NULL),
59 waiting_for_paint_(false),
60 page_url_(page_url),
61 windowless_buffer_index_(0),
62 host_render_view_routing_id_(host_render_view_routing_id),
63 weak_factory_(this) {
66 WebPluginProxy::~WebPluginProxy() {
67 #if defined(OS_MACOSX)
68 // Destroy the surface early, since it may send messages during cleanup.
69 if (accelerated_surface_)
70 accelerated_surface_.reset();
71 #endif
73 if (plugin_element_)
74 WebBindings::releaseObject(plugin_element_);
75 if (window_npobject_)
76 WebBindings::releaseObject(window_npobject_);
79 bool WebPluginProxy::Send(IPC::Message* msg) {
80 return channel_->Send(msg);
83 void WebPluginProxy::SetWindow(gfx::PluginWindowHandle window) {
84 Send(new PluginHostMsg_SetWindow(route_id_, window));
87 void WebPluginProxy::SetAcceptsInputEvents(bool accepts) {
88 NOTREACHED();
91 void WebPluginProxy::WillDestroyWindow(gfx::PluginWindowHandle window) {
92 #if defined(OS_WIN)
93 PluginThread::current()->Send(
94 new PluginProcessHostMsg_PluginWindowDestroyed(
95 window, ::GetParent(window)));
96 #else
97 NOTIMPLEMENTED();
98 #endif
101 #if defined(OS_WIN)
102 void WebPluginProxy::SetWindowlessData(
103 HANDLE pump_messages_event, gfx::NativeViewId dummy_activation_window) {
104 HANDLE pump_messages_event_for_renderer = NULL;
105 BrokerDuplicateHandle(pump_messages_event, channel_->peer_pid(),
106 &pump_messages_event_for_renderer,
107 SYNCHRONIZE | EVENT_MODIFY_STATE, 0);
108 DCHECK(pump_messages_event_for_renderer);
109 Send(new PluginHostMsg_SetWindowlessData(
110 route_id_, pump_messages_event_for_renderer, dummy_activation_window));
112 #endif
114 void WebPluginProxy::CancelResource(unsigned long id) {
115 Send(new PluginHostMsg_CancelResource(route_id_, id));
116 resource_clients_.erase(id);
119 void WebPluginProxy::Invalidate() {
120 gfx::Rect rect(0, 0,
121 delegate_->GetRect().width(),
122 delegate_->GetRect().height());
123 InvalidateRect(rect);
126 void WebPluginProxy::InvalidateRect(const gfx::Rect& rect) {
127 #if defined(OS_MACOSX)
128 // If this is a Core Animation plugin, all we need to do is inform the
129 // delegate.
130 if (!windowless_context()) {
131 delegate_->PluginDidInvalidate();
132 return;
135 // Some plugins will send invalidates larger than their own rect when
136 // offscreen, so constrain invalidates to the plugin rect.
137 gfx::Rect plugin_rect = delegate_->GetRect();
138 plugin_rect.set_origin(gfx::Point(0, 0));
139 plugin_rect.Intersect(rect);
140 const gfx::Rect invalidate_rect(plugin_rect);
141 #else
142 const gfx::Rect invalidate_rect(rect);
143 #endif
144 damaged_rect_.Union(invalidate_rect);
145 // Ignore NPN_InvalidateRect calls with empty rects. Also don't send an
146 // invalidate if it's outside the clipping region, since if we did it won't
147 // lead to a paint and we'll be stuck waiting forever for a DidPaint response.
149 // TODO(piman): There is a race condition here, because this test assumes
150 // that when the paint actually occurs, the clip rect will not have changed.
151 // This is not true because scrolling (or window resize) could occur and be
152 // handled by the renderer before it receives the InvalidateRect message,
153 // changing the clip rect and then not painting.
154 if (damaged_rect_.IsEmpty() ||
155 !delegate_->GetClipRect().Intersects(damaged_rect_))
156 return;
158 // Only send a single InvalidateRect message at a time. From DidPaint we
159 // will dispatch an additional InvalidateRect message if necessary.
160 if (!waiting_for_paint_) {
161 waiting_for_paint_ = true;
162 // Invalidates caused by calls to NPN_InvalidateRect/NPN_InvalidateRgn
163 // need to be painted asynchronously as per the NPAPI spec.
164 base::MessageLoop::current()->PostTask(
165 FROM_HERE,
166 base::Bind(&WebPluginProxy::OnPaint,
167 weak_factory_.GetWeakPtr(),
168 damaged_rect_));
169 damaged_rect_ = gfx::Rect();
173 NPObject* WebPluginProxy::GetWindowScriptNPObject() {
174 if (window_npobject_)
175 return window_npobject_;
177 int npobject_route_id = channel_->GenerateRouteID();
178 bool success = false;
179 Send(new PluginHostMsg_GetWindowScriptNPObject(
180 route_id_, npobject_route_id, &success));
181 if (!success)
182 return NULL;
184 // PluginChannel creates a dummy owner identifier for unknown owners, so
185 // use that.
186 NPP owner = channel_->GetExistingNPObjectOwner(MSG_ROUTING_NONE);
188 window_npobject_ = NPObjectProxy::Create(channel_.get(),
189 npobject_route_id,
190 host_render_view_routing_id_,
191 page_url_,
192 owner);
194 return window_npobject_;
197 NPObject* WebPluginProxy::GetPluginElement() {
198 if (plugin_element_)
199 return plugin_element_;
201 int npobject_route_id = channel_->GenerateRouteID();
202 bool success = false;
203 Send(new PluginHostMsg_GetPluginElement(route_id_, npobject_route_id,
204 &success));
205 if (!success)
206 return NULL;
208 // PluginChannel creates a dummy owner identifier for unknown owners, so
209 // use that.
210 NPP owner = channel_->GetExistingNPObjectOwner(MSG_ROUTING_NONE);
212 plugin_element_ = NPObjectProxy::Create(channel_.get(),
213 npobject_route_id,
214 host_render_view_routing_id_,
215 page_url_,
216 owner);
218 return plugin_element_;
221 bool WebPluginProxy::FindProxyForUrl(const GURL& url, std::string* proxy_list) {
222 bool result = false;
223 Send(new PluginHostMsg_ResolveProxy(route_id_, url, &result, proxy_list));
224 return result;
227 void WebPluginProxy::SetCookie(const GURL& url,
228 const GURL& first_party_for_cookies,
229 const std::string& cookie) {
230 Send(new PluginHostMsg_SetCookie(route_id_, url,
231 first_party_for_cookies, cookie));
234 std::string WebPluginProxy::GetCookies(const GURL& url,
235 const GURL& first_party_for_cookies) {
236 std::string cookies;
237 Send(new PluginHostMsg_GetCookies(route_id_, url,
238 first_party_for_cookies, &cookies));
240 return cookies;
243 WebPluginResourceClient* WebPluginProxy::GetResourceClient(int id) {
244 ResourceClientMap::iterator iterator = resource_clients_.find(id);
245 // The IPC messages which deal with streams are now asynchronous. It is
246 // now possible to receive stream messages from the renderer for streams
247 // which may have been cancelled by the plugin.
248 if (iterator == resource_clients_.end()) {
249 return NULL;
252 return iterator->second;
255 int WebPluginProxy::GetRendererId() {
256 if (channel_.get())
257 return channel_->renderer_id();
258 return -1;
261 void WebPluginProxy::DidPaint() {
262 // If we have an accumulated damaged rect, then check to see if we need to
263 // send out another InvalidateRect message.
264 waiting_for_paint_ = false;
265 if (!damaged_rect_.IsEmpty())
266 InvalidateRect(damaged_rect_);
269 void WebPluginProxy::OnResourceCreated(int resource_id,
270 WebPluginResourceClient* client) {
271 DCHECK(resource_clients_.find(resource_id) == resource_clients_.end());
272 resource_clients_[resource_id] = client;
275 void WebPluginProxy::HandleURLRequest(const char* url,
276 const char* method,
277 const char* target,
278 const char* buf,
279 unsigned int len,
280 int notify_id,
281 bool popups_allowed,
282 bool notify_redirects) {
283 if (!target && base::EqualsCaseInsensitiveASCII(method, "GET")) {
284 // Please refer to https://bugzilla.mozilla.org/show_bug.cgi?id=366082
285 // for more details on this.
286 if (delegate_->GetQuirks() &
287 WebPluginDelegateImpl::PLUGIN_QUIRK_BLOCK_NONSTANDARD_GETURL_REQUESTS) {
288 GURL request_url(url);
289 if (!request_url.SchemeIs(url::kHttpScheme) &&
290 !request_url.SchemeIs(url::kHttpsScheme) &&
291 !request_url.SchemeIs(url::kFtpScheme)) {
292 return;
297 PluginHostMsg_URLRequest_Params params;
298 params.url = url;
299 params.method = method;
300 if (target)
301 params.target = std::string(target);
303 if (len) {
304 params.buffer.resize(len);
305 memcpy(&params.buffer.front(), buf, len);
308 params.notify_id = notify_id;
309 params.popups_allowed = popups_allowed;
310 params.notify_redirects = notify_redirects;
312 Send(new PluginHostMsg_URLRequest(route_id_, params));
315 void WebPluginProxy::Paint(const gfx::Rect& rect) {
316 #if defined(OS_MACOSX)
317 if (!windowless_context())
318 return;
319 #else
320 if (!windowless_canvas() || !windowless_canvas()->getDevice())
321 return;
322 #endif
324 // Clear the damaged area so that if the plugin doesn't paint there we won't
325 // end up with the old values.
326 gfx::Rect offset_rect = rect;
327 offset_rect.Offset(delegate_->GetRect().OffsetFromOrigin());
328 #if defined(OS_MACOSX)
329 CGContextSaveGState(windowless_context());
330 // It is possible for windowless_contexts_ to change during plugin painting
331 // (since the plugin can make a synchronous call during paint event handling),
332 // in which case we don't want to try to restore later. Not an owning ref
333 // since owning the ref without owning the shared backing memory doesn't make
334 // sense, so this should only be used for pointer comparisons.
335 CGContextRef saved_context_weak = windowless_context();
336 // We also save the buffer index for the comparison because if we flip buffers
337 // but haven't reallocated them then we do need to restore the context because
338 // it is going to continue to be used.
339 int saved_index = windowless_buffer_index_;
341 CGContextClipToRect(windowless_context(), rect.ToCGRect());
342 // TODO(caryclark): This is a temporary workaround to allow the Darwin / Skia
343 // port to share code with the Darwin / CG port. All ports will eventually use
344 // the common code below.
345 delegate_->CGPaint(windowless_context(), rect);
346 if (windowless_contexts_[saved_index].get() == saved_context_weak)
347 CGContextRestoreGState(windowless_contexts_[saved_index]);
348 #else
349 // See above comment about windowless_context_ changing.
350 // http::/crbug.com/139462
351 skia::RefPtr<skia::PlatformCanvas> saved_canvas = windowless_canvas();
353 saved_canvas->save();
355 // The given clip rect is relative to the plugin coordinate system.
356 SkRect sk_rect = { SkIntToScalar(rect.x()),
357 SkIntToScalar(rect.y()),
358 SkIntToScalar(rect.right()),
359 SkIntToScalar(rect.bottom()) };
360 saved_canvas->clipRect(sk_rect);
362 // Fill a transparent value so that if the plugin supports transparency that
363 // will work.
364 saved_canvas->drawColor(SkColorSetARGB(0, 0, 0, 0), SkXfermode::kSrc_Mode);
366 // Bring the windowless canvas into the window coordinate system, which is
367 // how the plugin expects to draw (since the windowless API was originally
368 // designed just for scribbling over the web page).
369 saved_canvas->translate(SkIntToScalar(-delegate_->GetRect().x()),
370 SkIntToScalar(-delegate_->GetRect().y()));
372 // Before we send the invalidate, paint so that renderer uses the updated
373 // bitmap.
374 delegate_->Paint(saved_canvas.get(), offset_rect);
376 saved_canvas->restore();
377 #endif
380 void WebPluginProxy::UpdateGeometry(
381 const gfx::Rect& window_rect,
382 const gfx::Rect& clip_rect,
383 const TransportDIB::Handle& windowless_buffer0,
384 const TransportDIB::Handle& windowless_buffer1,
385 int windowless_buffer_index) {
386 gfx::Rect old = delegate_->GetRect();
387 gfx::Rect old_clip_rect = delegate_->GetClipRect();
389 // Update the buffers before doing anything that could call into plugin code,
390 // so that we don't process buffer changes out of order if plugins make
391 // synchronous calls that lead to nested UpdateGeometry calls.
392 if (TransportDIB::is_valid_handle(windowless_buffer0)) {
393 // The plugin's rect changed, so now we have new buffers to draw into.
394 SetWindowlessBuffers(windowless_buffer0,
395 windowless_buffer1,
396 window_rect);
399 DCHECK(0 <= windowless_buffer_index && windowless_buffer_index <= 1);
400 windowless_buffer_index_ = windowless_buffer_index;
402 #if defined(OS_MACOSX)
403 delegate_->UpdateGeometryAndContext(
404 window_rect, clip_rect, windowless_context());
405 #else
406 delegate_->UpdateGeometry(window_rect, clip_rect);
407 #endif
409 // Send over any pending invalidates which occured when the plugin was
410 // off screen.
411 if (delegate_->IsWindowless() && !clip_rect.IsEmpty() &&
412 !damaged_rect_.IsEmpty()) {
413 InvalidateRect(damaged_rect_);
417 #if defined(OS_WIN)
419 void WebPluginProxy::CreateCanvasFromHandle(
420 const TransportDIB::Handle& dib_handle,
421 const gfx::Rect& window_rect,
422 skia::RefPtr<skia::PlatformCanvas>* canvas) {
423 *canvas = skia::AdoptRef(
424 skia::CreatePlatformCanvas(window_rect.width(),
425 window_rect.height(),
426 true,
427 dib_handle,
428 skia::RETURN_NULL_ON_FAILURE));
429 // The canvas does not own the section so we need to close it now.
430 CloseHandle(dib_handle);
433 void WebPluginProxy::SetWindowlessBuffers(
434 const TransportDIB::Handle& windowless_buffer0,
435 const TransportDIB::Handle& windowless_buffer1,
436 const gfx::Rect& window_rect) {
437 CreateCanvasFromHandle(windowless_buffer0,
438 window_rect,
439 &windowless_canvases_[0]);
440 if (!windowless_canvases_[0]) {
441 windowless_canvases_[1].clear();
442 return;
444 CreateCanvasFromHandle(windowless_buffer1,
445 window_rect,
446 &windowless_canvases_[1]);
447 if (!windowless_canvases_[1]) {
448 windowless_canvases_[0].clear();
449 return;
453 #elif defined(OS_MACOSX)
455 void WebPluginProxy::CreateDIBAndCGContextFromHandle(
456 const TransportDIB::Handle& dib_handle,
457 const gfx::Rect& window_rect,
458 scoped_ptr<TransportDIB>* dib_out,
459 base::ScopedCFTypeRef<CGContextRef>* cg_context_out) {
460 // Convert the shared memory handle to a handle that works in our process,
461 // and then use that to create a CGContextRef.
462 TransportDIB* dib = TransportDIB::Map(dib_handle);
463 CGContextRef cg_context = NULL;
464 if (dib) {
465 cg_context = CGBitmapContextCreate(
466 dib->memory(),
467 window_rect.width(),
468 window_rect.height(),
470 4 * window_rect.width(),
471 base::mac::GetSystemColorSpace(),
472 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
473 CGContextTranslateCTM(cg_context, 0, window_rect.height());
474 CGContextScaleCTM(cg_context, 1, -1);
476 dib_out->reset(dib);
477 cg_context_out->reset(cg_context);
480 void WebPluginProxy::SetWindowlessBuffers(
481 const TransportDIB::Handle& windowless_buffer0,
482 const TransportDIB::Handle& windowless_buffer1,
483 const gfx::Rect& window_rect) {
484 CreateDIBAndCGContextFromHandle(windowless_buffer0,
485 window_rect,
486 &windowless_dibs_[0],
487 &windowless_contexts_[0]);
488 CreateDIBAndCGContextFromHandle(windowless_buffer1,
489 window_rect,
490 &windowless_dibs_[1],
491 &windowless_contexts_[1]);
494 #else
496 void WebPluginProxy::SetWindowlessBuffers(
497 const TransportDIB::Handle& windowless_buffer0,
498 const TransportDIB::Handle& windowless_buffer1,
499 const gfx::Rect& window_rect) {
500 NOTIMPLEMENTED();
503 #endif
505 void WebPluginProxy::CancelDocumentLoad() {
506 Send(new PluginHostMsg_CancelDocumentLoad(route_id_));
509 void WebPluginProxy::InitiateHTTPRangeRequest(
510 const char* url, const char* range_info, int range_request_id) {
511 Send(new PluginHostMsg_InitiateHTTPRangeRequest(
512 route_id_, url, range_info, range_request_id));
515 void WebPluginProxy::DidStartLoading() {
516 Send(new PluginHostMsg_DidStartLoading(route_id_));
519 void WebPluginProxy::DidStopLoading() {
520 Send(new PluginHostMsg_DidStopLoading(route_id_));
523 void WebPluginProxy::SetDeferResourceLoading(unsigned long resource_id,
524 bool defer) {
525 Send(new PluginHostMsg_DeferResourceLoading(route_id_, resource_id, defer));
528 #if defined(OS_MACOSX)
529 void WebPluginProxy::FocusChanged(bool focused) {
530 IPC::Message* msg = new PluginHostMsg_FocusChanged(route_id_, focused);
531 Send(msg);
534 void WebPluginProxy::StartIme() {
535 IPC::Message* msg = new PluginHostMsg_StartIme(route_id_);
536 // This message can be sent during event-handling, and needs to be delivered
537 // within that context.
538 msg->set_unblock(true);
539 Send(msg);
542 WebPluginAcceleratedSurface* WebPluginProxy::GetAcceleratedSurface(
543 gfx::GpuPreference gpu_preference) {
544 if (!accelerated_surface_)
545 accelerated_surface_.reset(
546 WebPluginAcceleratedSurfaceProxy::Create(this, gpu_preference));
547 return accelerated_surface_.get();
550 void WebPluginProxy::AcceleratedPluginEnabledRendering() {
551 Send(new PluginHostMsg_AcceleratedPluginEnabledRendering(route_id_));
554 void WebPluginProxy::AcceleratedPluginAllocatedIOSurface(int32 width,
555 int32 height,
556 uint32 surface_id) {
557 Send(new PluginHostMsg_AcceleratedPluginAllocatedIOSurface(
558 route_id_, width, height, surface_id));
561 void WebPluginProxy::AcceleratedPluginSwappedIOSurface() {
562 Send(new PluginHostMsg_AcceleratedPluginSwappedIOSurface(
563 route_id_));
565 #endif
567 void WebPluginProxy::OnPaint(const gfx::Rect& damaged_rect) {
568 GetContentClient()->SetActiveURL(page_url_);
570 Paint(damaged_rect);
571 Send(new PluginHostMsg_InvalidateRect(route_id_, damaged_rect));
574 bool WebPluginProxy::IsOffTheRecord() {
575 return channel_->incognito();
578 void WebPluginProxy::ResourceClientDeleted(
579 WebPluginResourceClient* resource_client) {
580 // resource_client->ResourceId() is 0 at this point, so can't use it as an
581 // index into the map.
582 ResourceClientMap::iterator index = resource_clients_.begin();
583 while (index != resource_clients_.end()) {
584 WebPluginResourceClient* client = (*index).second;
585 if (client == resource_client) {
586 resource_clients_.erase(index);
587 return;
588 } else {
589 index++;
594 void WebPluginProxy::URLRedirectResponse(bool allow, int resource_id) {
595 Send(new PluginHostMsg_URLRedirectResponse(route_id_, allow, resource_id));
598 bool WebPluginProxy::CheckIfRunInsecureContent(const GURL& url) {
599 bool result = true;
600 Send(new PluginHostMsg_CheckIfRunInsecureContent(
601 route_id_, url, &result));
602 return result;
605 #if defined(OS_WIN) && !defined(USE_AURA)
606 void WebPluginProxy::UpdateIMEStatus() {
607 // Retrieve the IME status from a plugin and send it to a renderer process
608 // when the plugin has updated it.
609 int input_type;
610 gfx::Rect caret_rect;
611 if (!delegate_->GetIMEStatus(&input_type, &caret_rect))
612 return;
614 Send(new PluginHostMsg_NotifyIMEStatus(route_id_, input_type, caret_rect));
616 #endif
618 } // namespace content