blob: f1da1d8002f097469e103b3cc27f8302b1b6a2d8 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
Ben Murdochbb1529c2013-08-08 10:24:53 +01005#include "content/renderer/npapi/webplugin_delegate_proxy.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00006
7#if defined(TOOLKIT_GTK)
8#include <gtk/gtk.h>
9#elif defined(USE_X11)
10#include <cairo/cairo.h>
11#endif
12
13#include <algorithm>
14
15#include "base/auto_reset.h"
16#include "base/basictypes.h"
17#include "base/command_line.h"
18#include "base/file_util.h"
19#include "base/logging.h"
20#include "base/memory/ref_counted.h"
21#include "base/memory/scoped_ptr.h"
Ben Murdochbbcdd452013-07-25 10:06:34 +010022#include "base/process/process.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000023#include "base/strings/string_split.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010024#include "base/strings/string_util.h"
25#include "base/strings/utf_string_conversions.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000026#include "base/version.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010027#include "content/child/child_process.h"
Ben Murdochca12bfa2013-07-23 11:17:05 +010028#include "content/child/npapi/npobject_proxy.h"
29#include "content/child/npapi/npobject_stub.h"
30#include "content/child/npapi/npobject_util.h"
31#include "content/child/npapi/webplugin.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010032#include "content/child/plugin_messages.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000033#include "content/common/view_messages.h"
34#include "content/public/renderer/content_renderer_client.h"
Ben Murdochbb1529c2013-08-08 10:24:53 +010035#include "content/renderer/npapi/plugin_channel_host.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000036#include "content/renderer/render_thread_impl.h"
37#include "content/renderer/render_view_impl.h"
38#include "ipc/ipc_channel_handle.h"
39#include "net/base/mime_util.h"
40#include "skia/ext/platform_canvas.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010041#include "third_party/WebKit/public/web/WebBindings.h"
42#include "third_party/WebKit/public/web/WebDocument.h"
43#include "third_party/WebKit/public/web/WebFrame.h"
44#include "third_party/WebKit/public/web/WebView.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010045#include "third_party/WebKit/public/platform/WebDragData.h"
46#include "third_party/WebKit/public/platform/WebString.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000047#include "ui/gfx/blit.h"
48#include "ui/gfx/canvas.h"
49#include "ui/gfx/native_widget_types.h"
50#include "ui/gfx/size.h"
51#include "ui/gfx/skia_util.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010052#include "webkit/common/cursors/webcursor.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000053#include "webkit/glue/webkit_glue.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000054#include "webkit/plugins/plugin_constants.h"
55#include "webkit/plugins/sad_plugin.h"
56
57#if defined(OS_POSIX)
58#include "ipc/ipc_channel_posix.h"
59#endif
60
61#if defined(OS_MACOSX)
62#include "base/mac/mac_util.h"
63#endif
64
65#if defined(OS_WIN)
66#include "content/public/common/sandbox_init.h"
67#endif
68
69using WebKit::WebBindings;
70using WebKit::WebCursorInfo;
71using WebKit::WebDragData;
72using WebKit::WebInputEvent;
73using WebKit::WebString;
74using WebKit::WebView;
75
76namespace content {
77
78namespace {
79
80class ScopedLogLevel {
81 public:
82 explicit ScopedLogLevel(int level);
83 ~ScopedLogLevel();
84
85 private:
86 int old_level_;
87
88 DISALLOW_COPY_AND_ASSIGN(ScopedLogLevel);
89};
90
91ScopedLogLevel::ScopedLogLevel(int level)
92 : old_level_(logging::GetMinLogLevel()) {
93 logging::SetMinLogLevel(level);
94}
95
96ScopedLogLevel::~ScopedLogLevel() {
97 logging::SetMinLogLevel(old_level_);
98}
99
100// Proxy for WebPluginResourceClient. The object owns itself after creation,
101// deleting itself after its callback has been called.
Ben Murdochca12bfa2013-07-23 11:17:05 +0100102class ResourceClientProxy : public WebPluginResourceClient {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000103 public:
104 ResourceClientProxy(PluginChannelHost* channel, int instance_id)
105 : channel_(channel), instance_id_(instance_id), resource_id_(0),
106 multibyte_response_expected_(false) {
107 }
108
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000109 virtual ~ResourceClientProxy() {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000110 }
111
112 void Initialize(unsigned long resource_id, const GURL& url, int notify_id) {
113 resource_id_ = resource_id;
114 channel_->Send(new PluginMsg_HandleURLRequestReply(
115 instance_id_, resource_id, url, notify_id));
116 }
117
118 void InitializeForSeekableStream(unsigned long resource_id,
119 int range_request_id) {
120 resource_id_ = resource_id;
121 multibyte_response_expected_ = true;
122 channel_->Send(new PluginMsg_HTTPRangeRequestReply(
123 instance_id_, resource_id, range_request_id));
124 }
125
126 // PluginResourceClient implementation:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000127 virtual void WillSendRequest(const GURL& url, int http_status_code) OVERRIDE {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100128 DCHECK(channel_.get() != NULL);
129 channel_->Send(new PluginMsg_WillSendRequest(
130 instance_id_, resource_id_, url, http_status_code));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000131 }
132
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000133 virtual void DidReceiveResponse(const std::string& mime_type,
134 const std::string& headers,
135 uint32 expected_length,
136 uint32 last_modified,
137 bool request_is_seekable) OVERRIDE {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100138 DCHECK(channel_.get() != NULL);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000139 PluginMsg_DidReceiveResponseParams params;
140 params.id = resource_id_;
141 params.mime_type = mime_type;
142 params.headers = headers;
143 params.expected_length = expected_length;
144 params.last_modified = last_modified;
145 params.request_is_seekable = request_is_seekable;
146 // Grab a reference on the underlying channel so it does not get
147 // deleted from under us.
148 scoped_refptr<PluginChannelHost> channel_ref(channel_);
149 channel_->Send(new PluginMsg_DidReceiveResponse(instance_id_, params));
150 }
151
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000152 virtual void DidReceiveData(const char* buffer,
153 int length,
154 int data_offset) OVERRIDE {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100155 DCHECK(channel_.get() != NULL);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000156 DCHECK_GT(length, 0);
157 std::vector<char> data;
158 data.resize(static_cast<size_t>(length));
159 memcpy(&data.front(), buffer, length);
160 // Grab a reference on the underlying channel so it does not get
161 // deleted from under us.
162 scoped_refptr<PluginChannelHost> channel_ref(channel_);
163 channel_->Send(new PluginMsg_DidReceiveData(instance_id_, resource_id_,
164 data, data_offset));
165 }
166
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100167 virtual void DidFinishLoading(unsigned long resource_id) OVERRIDE {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100168 DCHECK(channel_.get() != NULL);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100169 DCHECK_EQ(resource_id, resource_id_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000170 channel_->Send(new PluginMsg_DidFinishLoading(instance_id_, resource_id_));
171 channel_ = NULL;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100172 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000173 }
174
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100175 virtual void DidFail(unsigned long resource_id) OVERRIDE {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100176 DCHECK(channel_.get() != NULL);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100177 DCHECK_EQ(resource_id, resource_id_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000178 channel_->Send(new PluginMsg_DidFail(instance_id_, resource_id_));
179 channel_ = NULL;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100180 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000181 }
182
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000183 virtual bool IsMultiByteResponseExpected() OVERRIDE {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000184 return multibyte_response_expected_;
185 }
186
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000187 virtual int ResourceId() OVERRIDE {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000188 return resource_id_;
189 }
190
191 private:
192 scoped_refptr<PluginChannelHost> channel_;
193 int instance_id_;
194 unsigned long resource_id_;
195 // Set to true if the response expected is a multibyte response.
196 // For e.g. response for a HTTP byte range request.
197 bool multibyte_response_expected_;
198};
199
200} // namespace
201
202WebPluginDelegateProxy::WebPluginDelegateProxy(
203 const std::string& mime_type,
204 const base::WeakPtr<RenderViewImpl>& render_view)
205 : render_view_(render_view),
206 plugin_(NULL),
207 uses_shared_bitmaps_(false),
208#if defined(OS_MACOSX)
209 uses_compositor_(false),
210#elif defined(OS_WIN)
211 dummy_activation_window_(NULL),
212#endif
213 window_(gfx::kNullPluginWindow),
214 mime_type_(mime_type),
215 instance_id_(MSG_ROUTING_NONE),
216 npobject_(NULL),
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100217 npp_(new NPP_t),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000218 sad_plugin_(NULL),
219 invalidate_pending_(false),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000220 transparent_(false),
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000221 front_buffer_index_(0),
222 page_url_(render_view_->webview()->mainFrame()->document().url()) {
223}
224
225WebPluginDelegateProxy::~WebPluginDelegateProxy() {
226 if (npobject_)
227 WebBindings::releaseObject(npobject_);
228}
229
230WebPluginDelegateProxy::SharedBitmap::SharedBitmap() {}
231
232WebPluginDelegateProxy::SharedBitmap::~SharedBitmap() {}
233
234void WebPluginDelegateProxy::PluginDestroyed() {
235#if defined(OS_MACOSX) || defined(OS_WIN)
236 // Ensure that the renderer doesn't think the plugin still has focus.
237 if (render_view_)
238 render_view_->PluginFocusChanged(false, instance_id_);
239#endif
240
241#if defined(OS_WIN)
242 if (dummy_activation_window_ && render_view_) {
243 render_view_->Send(new ViewHostMsg_WindowlessPluginDummyWindowDestroyed(
244 render_view_->routing_id(), dummy_activation_window_));
245 }
246 dummy_activation_window_ = NULL;
247#endif
248
249 if (window_)
250 WillDestroyWindow();
251
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100252 if (render_view_.get())
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000253 render_view_->UnregisterPluginDelegate(this);
254
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100255 if (channel_host_.get()) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000256 Send(new PluginMsg_DestroyInstance(instance_id_));
257
Ben Murdochca12bfa2013-07-23 11:17:05 +0100258 // Must remove the route after sending the destroy message, rather than
259 // before, since RemoveRoute can lead to all the outstanding NPObjects
260 // being told the channel went away if this was the last instance.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000261 channel_host_->RemoveRoute(instance_id_);
262
Ben Murdocheb525c52013-07-10 11:40:50 +0100263 // Remove the mapping between our instance-Id and NPP identifiers, used by
264 // the channel to track object ownership, before releasing it.
265 channel_host_->RemoveMappingForNPObjectOwner(instance_id_);
266
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000267 // Release the channel host now. If we are is the last reference to the
268 // channel, this avoids a race where this renderer asks a new connection to
269 // the same plugin between now and the time 'this' is actually deleted.
270 // Destroying the channel host is what releases the channel name -> FD
271 // association on POSIX, and if we ask for a new connection before it is
272 // released, the plugin will give us a new FD, and we'll assert when trying
273 // to associate it with the channel name.
274 channel_host_ = NULL;
275 }
276
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000277 plugin_ = NULL;
278
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100279 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000280}
281
282bool WebPluginDelegateProxy::Initialize(
283 const GURL& url,
284 const std::vector<std::string>& arg_names,
285 const std::vector<std::string>& arg_values,
Ben Murdochca12bfa2013-07-23 11:17:05 +0100286 WebPlugin* plugin,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000287 bool load_manually) {
288 // TODO(shess): Attempt to work around http://crbug.com/97285 and
289 // http://crbug.com/141055 by retrying the connection. Reports seem
290 // to indicate that the plugin hasn't crashed, and that the problem
291 // is not 100% persistent.
292 const size_t kAttempts = 2;
293
294 bool result = false;
295 scoped_refptr<PluginChannelHost> channel_host;
296 int instance_id = 0;
297
298 for (size_t attempt = 0; !result && attempt < kAttempts; attempt++) {
299#if defined(OS_MACOSX)
300 // TODO(shess): Debugging for http://crbug.com/97285 . See comment
301 // in plugin_channel_host.cc.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000302 scoped_ptr<base::AutoReset<bool> > track_nested_removes(
303 new base::AutoReset<bool>(PluginChannelHost::GetRemoveTrackingFlag(),
304 true));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000305#endif
306
307 IPC::ChannelHandle channel_handle;
308 if (!RenderThreadImpl::current()->Send(new ViewHostMsg_OpenChannelToPlugin(
309 render_view_->routing_id(), url, page_url_, mime_type_,
310 &channel_handle, &info_))) {
311 continue;
312 }
313
314 if (channel_handle.name.empty()) {
315 // We got an invalid handle. Either the plugin couldn't be found (which
316 // shouldn't happen, since if we got here the plugin should exist) or the
317 // plugin crashed on initialization.
318 if (!info_.path.empty()) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000319 render_view_->PluginCrashed(info_.path, base::kNullProcessId);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000320 LOG(ERROR) << "Plug-in crashed on start";
321
322 // Return true so that the plugin widget is created and we can paint the
323 // crashed plugin there.
324 return true;
325 }
326 LOG(ERROR) << "Plug-in couldn't be found";
327 return false;
328 }
329
330 channel_host =
331 PluginChannelHost::GetPluginChannelHost(
332 channel_handle, ChildProcess::current()->io_message_loop_proxy());
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100333 if (!channel_host.get()) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000334 LOG(ERROR) << "Couldn't get PluginChannelHost";
335 continue;
336 }
337#if defined(OS_MACOSX)
338 track_nested_removes.reset();
339#endif
340
341 {
342 // TODO(bauerb): Debugging for http://crbug.com/141055.
343 ScopedLogLevel log_level(-2); // Equivalent to --v=2
344 result = channel_host->Send(new PluginMsg_CreateInstance(
345 mime_type_, &instance_id));
346 if (!result) {
347 LOG(ERROR) << "Couldn't send PluginMsg_CreateInstance";
348 continue;
349 }
350 }
351 }
352
353 // Failed too often, give up.
354 if (!result)
355 return false;
356
357 channel_host_ = channel_host;
358 instance_id_ = instance_id;
359
360 channel_host_->AddRoute(instance_id_, this, NULL);
361
Ben Murdocheb525c52013-07-10 11:40:50 +0100362 // Inform the channel of the mapping between our instance-Id and dummy NPP
363 // identifier, for use in object ownership tracking.
364 channel_host_->AddMappingForNPObjectOwner(instance_id_, GetPluginNPP());
365
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000366 // Now tell the PluginInstance in the plugin process to initialize.
367 PluginMsg_Init_Params params;
368 params.url = url;
369 params.page_url = page_url_;
370 params.arg_names = arg_names;
371 params.arg_values = arg_values;
372 params.host_render_view_routing_id = render_view_->routing_id();
373 params.load_manually = load_manually;
374
375 plugin_ = plugin;
376
377 result = false;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000378 Send(new PluginMsg_Init(instance_id_, params, &transparent_, &result));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000379
380 if (!result)
381 LOG(ERROR) << "PluginMsg_Init returned false";
382
383 render_view_->RegisterPluginDelegate(this);
384
385 return result;
386}
387
388bool WebPluginDelegateProxy::Send(IPC::Message* msg) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100389 if (!channel_host_.get()) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000390 DLOG(WARNING) << "dropping message because channel host is null";
391 delete msg;
392 return false;
393 }
394
395 return channel_host_->Send(msg);
396}
397
398void WebPluginDelegateProxy::SendJavaScriptStream(const GURL& url,
399 const std::string& result,
400 bool success,
401 int notify_id) {
402 Send(new PluginMsg_SendJavaScriptStream(
403 instance_id_, url, result, success, notify_id));
404}
405
406void WebPluginDelegateProxy::DidReceiveManualResponse(
407 const GURL& url, const std::string& mime_type,
408 const std::string& headers, uint32 expected_length,
409 uint32 last_modified) {
410 PluginMsg_DidReceiveResponseParams params;
411 params.id = 0;
412 params.mime_type = mime_type;
413 params.headers = headers;
414 params.expected_length = expected_length;
415 params.last_modified = last_modified;
416 Send(new PluginMsg_DidReceiveManualResponse(instance_id_, url, params));
417}
418
419void WebPluginDelegateProxy::DidReceiveManualData(const char* buffer,
420 int length) {
421 DCHECK_GT(length, 0);
422 std::vector<char> data;
423 data.resize(static_cast<size_t>(length));
424 memcpy(&data.front(), buffer, length);
425 Send(new PluginMsg_DidReceiveManualData(instance_id_, data));
426}
427
428void WebPluginDelegateProxy::DidFinishManualLoading() {
429 Send(new PluginMsg_DidFinishManualLoading(instance_id_));
430}
431
432void WebPluginDelegateProxy::DidManualLoadFail() {
433 Send(new PluginMsg_DidManualLoadFail(instance_id_));
434}
435
436bool WebPluginDelegateProxy::OnMessageReceived(const IPC::Message& msg) {
437 GetContentClient()->SetActiveURL(page_url_);
438
439 bool handled = true;
440 IPC_BEGIN_MESSAGE_MAP(WebPluginDelegateProxy, msg)
441 IPC_MESSAGE_HANDLER(PluginHostMsg_SetWindow, OnSetWindow)
442#if defined(OS_WIN)
443 IPC_MESSAGE_HANDLER(PluginHostMsg_SetWindowlessData, OnSetWindowlessData)
444 IPC_MESSAGE_HANDLER(PluginHostMsg_NotifyIMEStatus, OnNotifyIMEStatus)
445#endif
446 IPC_MESSAGE_HANDLER(PluginHostMsg_CancelResource, OnCancelResource)
447 IPC_MESSAGE_HANDLER(PluginHostMsg_InvalidateRect, OnInvalidateRect)
448 IPC_MESSAGE_HANDLER(PluginHostMsg_GetWindowScriptNPObject,
449 OnGetWindowScriptNPObject)
450 IPC_MESSAGE_HANDLER(PluginHostMsg_GetPluginElement, OnGetPluginElement)
451 IPC_MESSAGE_HANDLER(PluginHostMsg_ResolveProxy, OnResolveProxy)
452 IPC_MESSAGE_HANDLER(PluginHostMsg_SetCookie, OnSetCookie)
453 IPC_MESSAGE_HANDLER(PluginHostMsg_GetCookies, OnGetCookies)
454 IPC_MESSAGE_HANDLER(PluginHostMsg_URLRequest, OnHandleURLRequest)
455 IPC_MESSAGE_HANDLER(PluginHostMsg_CancelDocumentLoad, OnCancelDocumentLoad)
456 IPC_MESSAGE_HANDLER(PluginHostMsg_InitiateHTTPRangeRequest,
457 OnInitiateHTTPRangeRequest)
458 IPC_MESSAGE_HANDLER(PluginHostMsg_DeferResourceLoading,
459 OnDeferResourceLoading)
460
461#if defined(OS_MACOSX)
462 IPC_MESSAGE_HANDLER(PluginHostMsg_FocusChanged,
463 OnFocusChanged);
464 IPC_MESSAGE_HANDLER(PluginHostMsg_StartIme,
465 OnStartIme);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000466 IPC_MESSAGE_HANDLER(PluginHostMsg_AcceleratedPluginEnabledRendering,
467 OnAcceleratedPluginEnabledRendering)
468 IPC_MESSAGE_HANDLER(PluginHostMsg_AcceleratedPluginAllocatedIOSurface,
469 OnAcceleratedPluginAllocatedIOSurface)
470 IPC_MESSAGE_HANDLER(PluginHostMsg_AcceleratedPluginSwappedIOSurface,
471 OnAcceleratedPluginSwappedIOSurface)
472#endif
473 IPC_MESSAGE_HANDLER(PluginHostMsg_URLRedirectResponse,
474 OnURLRedirectResponse)
475 IPC_MESSAGE_UNHANDLED(handled = false)
476 IPC_END_MESSAGE_MAP()
477 DCHECK(handled);
478 return handled;
479}
480
481void WebPluginDelegateProxy::OnChannelError() {
482 if (plugin_) {
483 if (window_) {
484 // The actual WebPluginDelegate never got a chance to tell the WebPlugin
485 // its window was going away. Do it on its behalf.
486 WillDestroyWindow();
487 }
488 plugin_->Invalidate();
489 }
490 if (!channel_host_->expecting_shutdown())
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000491 render_view_->PluginCrashed(info_.path, channel_host_->peer_pid());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000492
493#if defined(OS_MACOSX) || defined(OS_WIN)
494 // Ensure that the renderer doesn't think the plugin still has focus.
495 if (render_view_)
496 render_view_->PluginFocusChanged(false, instance_id_);
497#endif
498}
499
500static void CopyTransportDIBHandleForMessage(
501 const TransportDIB::Handle& handle_in,
502 TransportDIB::Handle* handle_out,
503 base::ProcessId peer_pid) {
504#if defined(OS_MACOSX)
505 // On Mac, TransportDIB::Handle is typedef'ed to FileDescriptor, and
506 // FileDescriptor message fields needs to remain valid until the message is
507 // sent or else the sendmsg() call will fail.
508 if ((handle_out->fd = HANDLE_EINTR(dup(handle_in.fd))) < 0) {
509 PLOG(ERROR) << "dup()";
510 return;
511 }
512 handle_out->auto_close = true;
513#elif defined(OS_WIN)
514 // On Windows we need to duplicate the handle for the plugin process.
515 *handle_out = NULL;
516 BrokerDuplicateHandle(handle_in, peer_pid, handle_out,
517 FILE_MAP_READ | FILE_MAP_WRITE, 0);
518 DCHECK(*handle_out != NULL);
519#else
520 // Don't need to do anything special for other platforms.
521 *handle_out = handle_in;
522#endif
523}
524
525void WebPluginDelegateProxy::SendUpdateGeometry(
526 bool bitmaps_changed) {
527 PluginMsg_UpdateGeometry_Param param;
528 param.window_rect = plugin_rect_;
529 param.clip_rect = clip_rect_;
530 param.windowless_buffer0 = TransportDIB::DefaultHandleValue();
531 param.windowless_buffer1 = TransportDIB::DefaultHandleValue();
532 param.windowless_buffer_index = back_buffer_index();
533
534#if defined(OS_POSIX)
535 // If we're using POSIX mmap'd TransportDIBs, sending the handle across
536 // IPC establishes a new mapping rather than just sending a window ID,
537 // so only do so if we've actually changed the shared memory bitmaps.
538 if (bitmaps_changed)
539#endif
540 {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100541 if (transport_stores_[0].dib)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000542 CopyTransportDIBHandleForMessage(transport_stores_[0].dib->handle(),
543 &param.windowless_buffer0,
544 channel_host_->peer_pid());
545
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100546 if (transport_stores_[1].dib)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000547 CopyTransportDIBHandleForMessage(transport_stores_[1].dib->handle(),
548 &param.windowless_buffer1,
549 channel_host_->peer_pid());
550 }
551
552 IPC::Message* msg;
553#if defined(OS_WIN)
554 if (UseSynchronousGeometryUpdates()) {
555 msg = new PluginMsg_UpdateGeometrySync(instance_id_, param);
556 } else // NOLINT
557#endif
558 {
559 msg = new PluginMsg_UpdateGeometry(instance_id_, param);
560 msg->set_unblock(true);
561 }
562
563 Send(msg);
564}
565
566void WebPluginDelegateProxy::UpdateGeometry(const gfx::Rect& window_rect,
567 const gfx::Rect& clip_rect) {
568 // window_rect becomes either a window in native windowing system
569 // coords, or a backing buffer. In either case things will go bad
570 // if the rectangle is very large.
571 if (window_rect.width() < 0 || window_rect.width() > kMaxPluginSideLength ||
572 window_rect.height() < 0 || window_rect.height() > kMaxPluginSideLength ||
573 // We know this won't overflow due to above checks.
574 static_cast<uint32>(window_rect.width()) *
575 static_cast<uint32>(window_rect.height()) > kMaxPluginSize) {
576 return;
577 }
578
579 plugin_rect_ = window_rect;
580 clip_rect_ = clip_rect;
581
582 bool bitmaps_changed = false;
583
584 if (uses_shared_bitmaps_) {
585 if (!front_buffer_canvas() ||
586 (window_rect.width() != front_buffer_canvas()->getDevice()->width() ||
587 window_rect.height() != front_buffer_canvas()->getDevice()->height()))
588 {
589 bitmaps_changed = true;
590
591 // Create a shared memory section that the plugin paints into
592 // asynchronously.
593 ResetWindowlessBitmaps();
594 if (!window_rect.IsEmpty()) {
595 if (!CreateSharedBitmap(&transport_stores_[0].dib,
596 &transport_stores_[0].canvas) ||
597 !CreateSharedBitmap(&transport_stores_[1].dib,
598 &transport_stores_[1].canvas)) {
599 DCHECK(false);
600 ResetWindowlessBitmaps();
601 return;
602 }
603 }
604 }
605 }
606
607 SendUpdateGeometry(bitmaps_changed);
608}
609
610void WebPluginDelegateProxy::ResetWindowlessBitmaps() {
611 transport_stores_[0].dib.reset();
612 transport_stores_[1].dib.reset();
613
614 transport_stores_[0].canvas.reset();
615 transport_stores_[1].canvas.reset();
616 transport_store_painted_ = gfx::Rect();
617 front_buffer_diff_ = gfx::Rect();
618}
619
620static size_t BitmapSizeForPluginRect(const gfx::Rect& plugin_rect) {
621 const size_t stride =
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000622 skia::PlatformCanvasStrideForWidth(plugin_rect.width());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000623 return stride * plugin_rect.height();
624}
625
626#if !defined(OS_WIN)
627bool WebPluginDelegateProxy::CreateLocalBitmap(
628 std::vector<uint8>* memory,
629 scoped_ptr<skia::PlatformCanvas>* canvas) {
630 const size_t size = BitmapSizeForPluginRect(plugin_rect_);
631 memory->resize(size);
632 if (memory->size() != size)
633 return false;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000634 canvas->reset(skia::CreatePlatformCanvas(
635 plugin_rect_.width(), plugin_rect_.height(), true, &((*memory)[0]),
636 skia::CRASH_ON_FAILURE));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000637 return true;
638}
639#endif
640
641bool WebPluginDelegateProxy::CreateSharedBitmap(
642 scoped_ptr<TransportDIB>* memory,
643 scoped_ptr<skia::PlatformCanvas>* canvas) {
644 const size_t size = BitmapSizeForPluginRect(plugin_rect_);
645#if defined(OS_POSIX) && !defined(OS_MACOSX)
646 memory->reset(TransportDIB::Create(size, 0));
647 if (!memory->get())
648 return false;
649#endif
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100650#if defined(OS_POSIX) && !defined(TOOLKIT_GTK) && !defined(OS_ANDROID)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000651 TransportDIB::Handle handle;
652 IPC::Message* msg = new ViewHostMsg_AllocTransportDIB(size, false, &handle);
653 if (!RenderThreadImpl::current()->Send(msg))
654 return false;
655 if (handle.fd < 0)
656 return false;
657 memory->reset(TransportDIB::Map(handle));
658#else
659 static uint32 sequence_number = 0;
660 memory->reset(TransportDIB::Create(size, sequence_number++));
661#endif
662 canvas->reset((*memory)->GetPlatformCanvas(plugin_rect_.width(),
663 plugin_rect_.height()));
664 return !!canvas->get();
665}
666
667#if defined(OS_MACOSX)
668// Flips |rect| vertically within an enclosing rect with height |height|.
669// Intended for converting rects between flipped and non-flipped contexts.
670static void FlipRectVerticallyWithHeight(gfx::Rect* rect, int height) {
671 rect->set_y(height - rect->bottom());
672}
673#endif
674
675void WebPluginDelegateProxy::Paint(WebKit::WebCanvas* canvas,
676 const gfx::Rect& damaged_rect) {
677 // Limit the damaged rectangle to whatever is contained inside the plugin
678 // rectangle, as that's the rectangle that we'll actually draw.
679 gfx::Rect rect = gfx::IntersectRects(damaged_rect, plugin_rect_);
680
681 // If the plugin is no longer connected (channel crashed) draw a crashed
682 // plugin bitmap
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100683 if (!channel_host_.get() || !channel_host_->channel_valid()) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000684 PaintSadPlugin(canvas, rect);
685 return;
686 }
687
688 if (!uses_shared_bitmaps_)
689 return;
690
691 // We got a paint before the plugin's coordinates, so there's no buffer to
692 // copy from.
693 if (!front_buffer_canvas())
694 return;
695
696 gfx::Rect offset_rect = rect;
697 offset_rect.Offset(-plugin_rect_.x(), -plugin_rect_.y());
698
699 // transport_store_painted_ is really a bounding box, so in principle this
700 // check could falsely indicate that we don't need to paint offset_rect, but
701 // in practice it works fine.
702 if (!transport_store_painted_.Contains(offset_rect)) {
703 Send(new PluginMsg_Paint(instance_id_, offset_rect));
704 // Since the plugin is not blocked on the renderer in this context, there is
705 // a chance that it will begin repainting the back-buffer before we complete
706 // capturing the data. Buffer flipping would increase that risk because
707 // geometry update is asynchronous, so we don't want to use buffer flipping
708 // here.
709 UpdateFrontBuffer(offset_rect, false);
710 }
711
712 const SkBitmap& bitmap =
713 front_buffer_canvas()->getDevice()->accessBitmap(false);
714 SkPaint paint;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000715 paint.setXfermodeMode(
716 transparent_ ? SkXfermode::kSrcATop_Mode : SkXfermode::kSrc_Mode);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000717 SkIRect src_rect = gfx::RectToSkIRect(offset_rect);
718 canvas->drawBitmapRect(bitmap,
719 &src_rect,
720 gfx::RectToSkRect(rect),
721 &paint);
722
723 if (invalidate_pending_) {
724 // Only send the PaintAck message if this paint is in response to an
725 // invalidate from the plugin, since this message acts as an access token
726 // to ensure only one process is using the transport dib at a time.
727 invalidate_pending_ = false;
728 Send(new PluginMsg_DidPaint(instance_id_));
729 }
730}
731
732NPObject* WebPluginDelegateProxy::GetPluginScriptableObject() {
733 if (npobject_)
734 return WebBindings::retainObject(npobject_);
735
736 int route_id = MSG_ROUTING_NONE;
737 Send(new PluginMsg_GetPluginScriptableObject(instance_id_, &route_id));
738 if (route_id == MSG_ROUTING_NONE)
739 return NULL;
740
741 npobject_ = NPObjectProxy::Create(
Ben Murdocheb525c52013-07-10 11:40:50 +0100742 channel_host_.get(), route_id, 0, page_url_, GetPluginNPP());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000743
744 return WebBindings::retainObject(npobject_);
745}
746
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100747NPP WebPluginDelegateProxy::GetPluginNPP() {
748 // Return a dummy NPP for WebKit to use to identify this plugin.
749 return npp_.get();
750}
751
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000752bool WebPluginDelegateProxy::GetFormValue(string16* value) {
753 bool success = false;
754 Send(new PluginMsg_GetFormValue(instance_id_, value, &success));
755 return success;
756}
757
758void WebPluginDelegateProxy::DidFinishLoadWithReason(
759 const GURL& url, NPReason reason, int notify_id) {
760 Send(new PluginMsg_DidFinishLoadWithReason(
761 instance_id_, url, reason, notify_id));
762}
763
764void WebPluginDelegateProxy::SetFocus(bool focused) {
765 Send(new PluginMsg_SetFocus(instance_id_, focused));
766#if defined(OS_WIN)
767 if (render_view_)
768 render_view_->PluginFocusChanged(focused, instance_id_);
769#endif
770}
771
772bool WebPluginDelegateProxy::HandleInputEvent(
773 const WebInputEvent& event,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100774 WebCursor::CursorInfo* cursor_info) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000775 bool handled;
776 WebCursor cursor;
777 // A windowless plugin can enter a modal loop in the context of a
778 // NPP_HandleEvent call, in which case we need to pump messages to
779 // the plugin. We pass of the corresponding event handle to the
780 // plugin process, which is set if the plugin does enter a modal loop.
781 IPC::SyncMessage* message = new PluginMsg_HandleInputEvent(
782 instance_id_, &event, &handled, &cursor);
783 message->set_pump_messages_event(modal_loop_pump_messages_event_.get());
784 Send(message);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000785 return handled;
786}
787
788int WebPluginDelegateProxy::GetProcessId() {
789 return channel_host_->peer_pid();
790}
791
792void WebPluginDelegateProxy::SetContentAreaFocus(bool has_focus) {
793 IPC::Message* msg = new PluginMsg_SetContentAreaFocus(instance_id_,
794 has_focus);
795 // Make sure focus events are delivered in the right order relative to
796 // sync messages they might interact with (Paint, HandleEvent, etc.).
797 msg->set_unblock(true);
798 Send(msg);
799}
800
801#if defined(OS_WIN)
802void WebPluginDelegateProxy::ImeCompositionUpdated(
803 const string16& text,
804 const std::vector<int>& clauses,
805 const std::vector<int>& target,
806 int cursor_position,
807 int plugin_id) {
808 // Dispatch the raw IME data if this plug-in is the focused one.
809 if (instance_id_ != plugin_id)
810 return;
811
812 IPC::Message* msg = new PluginMsg_ImeCompositionUpdated(instance_id_,
813 text, clauses, target, cursor_position);
814 msg->set_unblock(true);
815 Send(msg);
816}
817
818void WebPluginDelegateProxy::ImeCompositionCompleted(const string16& text,
819 int plugin_id) {
820 // Dispatch the IME text if this plug-in is the focused one.
821 if (instance_id_ != plugin_id)
822 return;
823
824 IPC::Message* msg = new PluginMsg_ImeCompositionCompleted(instance_id_, text);
825 msg->set_unblock(true);
826 Send(msg);
827}
828#endif
829
830#if defined(OS_MACOSX)
831void WebPluginDelegateProxy::SetWindowFocus(bool window_has_focus) {
832 IPC::Message* msg = new PluginMsg_SetWindowFocus(instance_id_,
833 window_has_focus);
834 // Make sure focus events are delivered in the right order relative to
835 // sync messages they might interact with (Paint, HandleEvent, etc.).
836 msg->set_unblock(true);
837 Send(msg);
838}
839
840void WebPluginDelegateProxy::SetContainerVisibility(bool is_visible) {
841 IPC::Message* msg;
842 if (is_visible) {
843 gfx::Rect window_frame = render_view_->rootWindowRect();
844 gfx::Rect view_frame = render_view_->windowRect();
845 WebKit::WebView* webview = render_view_->webview();
846 msg = new PluginMsg_ContainerShown(instance_id_, window_frame, view_frame,
847 webview && webview->isActive());
848 } else {
849 msg = new PluginMsg_ContainerHidden(instance_id_);
850 }
851 // Make sure visibility events are delivered in the right order relative to
852 // sync messages they might interact with (Paint, HandleEvent, etc.).
853 msg->set_unblock(true);
854 Send(msg);
855}
856
857void WebPluginDelegateProxy::WindowFrameChanged(gfx::Rect window_frame,
858 gfx::Rect view_frame) {
859 IPC::Message* msg = new PluginMsg_WindowFrameChanged(instance_id_,
860 window_frame,
861 view_frame);
862 // Make sure frame events are delivered in the right order relative to
863 // sync messages they might interact with (e.g., HandleEvent).
864 msg->set_unblock(true);
865 Send(msg);
866}
867void WebPluginDelegateProxy::ImeCompositionCompleted(const string16& text,
868 int plugin_id) {
869 // If the message isn't intended for this plugin, there's nothing to do.
870 if (instance_id_ != plugin_id)
871 return;
872
873 IPC::Message* msg = new PluginMsg_ImeCompositionCompleted(instance_id_,
874 text);
875 // Order relative to other key events is important.
876 msg->set_unblock(true);
877 Send(msg);
878}
879#endif // OS_MACOSX
880
881void WebPluginDelegateProxy::OnSetWindow(gfx::PluginWindowHandle window) {
882#if defined(OS_MACOSX)
883 uses_shared_bitmaps_ = !window && !uses_compositor_;
884#else
885 uses_shared_bitmaps_ = !window;
886#endif
887 window_ = window;
888 if (plugin_)
889 plugin_->SetWindow(window);
890}
891
892void WebPluginDelegateProxy::WillDestroyWindow() {
893 DCHECK(window_);
894 plugin_->WillDestroyWindow(window_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000895 window_ = gfx::kNullPluginWindow;
896}
897
898#if defined(OS_WIN)
899void WebPluginDelegateProxy::OnSetWindowlessData(
900 HANDLE modal_loop_pump_messages_event,
901 gfx::NativeViewId dummy_activation_window) {
902 DCHECK(modal_loop_pump_messages_event_ == NULL);
903 DCHECK(dummy_activation_window_ == NULL);
904
905 dummy_activation_window_ = dummy_activation_window;
906 render_view_->Send(new ViewHostMsg_WindowlessPluginDummyWindowCreated(
907 render_view_->routing_id(), dummy_activation_window_));
908
909 // Bug 25583: this can be null because some "virus scanners" block the
910 // DuplicateHandle call in the plugin process.
911 if (!modal_loop_pump_messages_event)
912 return;
913
914 modal_loop_pump_messages_event_.reset(
915 new base::WaitableEvent(modal_loop_pump_messages_event));
916}
917
918void WebPluginDelegateProxy::OnNotifyIMEStatus(int input_type,
919 const gfx::Rect& caret_rect) {
920 if (!render_view_)
921 return;
922
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100923 render_view_->Send(new ViewHostMsg_TextInputTypeChanged(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000924 render_view_->routing_id(),
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100925 static_cast<ui::TextInputType>(input_type),
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100926 true,
927 ui::TEXT_INPUT_MODE_DEFAULT));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000928
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000929 ViewHostMsg_SelectionBounds_Params bounds_params;
930 bounds_params.anchor_rect = bounds_params.focus_rect = caret_rect;
931 bounds_params.anchor_dir = bounds_params.focus_dir =
932 WebKit::WebTextDirectionLeftToRight;
933 bounds_params.is_anchor_first = true;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000934 render_view_->Send(new ViewHostMsg_SelectionBoundsChanged(
935 render_view_->routing_id(),
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000936 bounds_params));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000937}
938#endif
939
940void WebPluginDelegateProxy::OnCancelResource(int id) {
941 if (plugin_)
942 plugin_->CancelResource(id);
943}
944
945void WebPluginDelegateProxy::OnInvalidateRect(const gfx::Rect& rect) {
946 if (!plugin_)
947 return;
948
949 // Clip the invalidation rect to the plugin bounds; the plugin may have been
950 // resized since the invalidate message was sent.
951 gfx::Rect clipped_rect =
952 gfx::IntersectRects(rect, gfx::Rect(plugin_rect_.size()));
953
954 invalidate_pending_ = true;
955 // The plugin is blocked on the renderer because the invalidate message it has
956 // sent us is synchronous, so we can use buffer flipping here if the caller
957 // allows it.
958 UpdateFrontBuffer(clipped_rect, true);
959 plugin_->InvalidateRect(clipped_rect);
960}
961
962void WebPluginDelegateProxy::OnGetWindowScriptNPObject(
963 int route_id, bool* success) {
964 *success = false;
965 NPObject* npobject = NULL;
966 if (plugin_)
967 npobject = plugin_->GetWindowScriptNPObject();
968
969 if (!npobject)
970 return;
971
972 // The stub will delete itself when the proxy tells it that it's released, or
973 // otherwise when the channel is closed.
Ben Murdochca12bfa2013-07-23 11:17:05 +0100974 new NPObjectStub(npobject, channel_host_.get(), route_id, 0, page_url_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000975 *success = true;
976}
977
978void WebPluginDelegateProxy::OnResolveProxy(const GURL& url,
979 bool* result,
980 std::string* proxy_list) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000981 *result = RenderThreadImpl::current()->ResolveProxy(url, proxy_list);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000982}
983
984void WebPluginDelegateProxy::OnGetPluginElement(int route_id, bool* success) {
985 *success = false;
986 NPObject* npobject = NULL;
987 if (plugin_)
988 npobject = plugin_->GetPluginElement();
989 if (!npobject)
990 return;
991
992 // The stub will delete itself when the proxy tells it that it's released, or
993 // otherwise when the channel is closed.
994 new NPObjectStub(
995 npobject, channel_host_.get(), route_id, 0, page_url_);
996 *success = true;
997}
998
999void WebPluginDelegateProxy::OnSetCookie(const GURL& url,
1000 const GURL& first_party_for_cookies,
1001 const std::string& cookie) {
1002 if (plugin_)
1003 plugin_->SetCookie(url, first_party_for_cookies, cookie);
1004}
1005
1006void WebPluginDelegateProxy::OnGetCookies(const GURL& url,
1007 const GURL& first_party_for_cookies,
1008 std::string* cookies) {
1009 DCHECK(cookies);
1010 if (plugin_)
1011 *cookies = plugin_->GetCookies(url, first_party_for_cookies);
1012}
1013
1014void WebPluginDelegateProxy::PaintSadPlugin(WebKit::WebCanvas* native_context,
1015 const gfx::Rect& rect) {
1016 // Lazily load the sad plugin image.
1017 if (!sad_plugin_)
1018 sad_plugin_ = GetContentClient()->renderer()->GetSadPluginBitmap();
1019 if (sad_plugin_)
1020 webkit::PaintSadPlugin(native_context, plugin_rect_, *sad_plugin_);
1021}
1022
1023void WebPluginDelegateProxy::CopyFromBackBufferToFrontBuffer(
1024 const gfx::Rect& rect) {
1025#if defined(OS_MACOSX)
1026 // Blitting the bits directly is much faster than going through CG, and since
1027 // the goal is just to move the raw pixels between two bitmaps with the same
1028 // pixel format (no compositing, color correction, etc.), it's safe.
1029 const size_t stride =
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001030 skia::PlatformCanvasStrideForWidth(plugin_rect_.width());
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001031 const size_t chunk_size = 4 * rect.width();
1032 DCHECK(back_buffer_dib() != NULL);
1033 uint8* source_data = static_cast<uint8*>(back_buffer_dib()->memory()) +
1034 rect.y() * stride + 4 * rect.x();
1035 DCHECK(front_buffer_dib() != NULL);
1036 uint8* target_data = static_cast<uint8*>(front_buffer_dib()->memory()) +
1037 rect.y() * stride + 4 * rect.x();
1038 for (int row = 0; row < rect.height(); ++row) {
1039 memcpy(target_data, source_data, chunk_size);
1040 source_data += stride;
1041 target_data += stride;
1042 }
1043#else
1044 BlitCanvasToCanvas(front_buffer_canvas(),
1045 rect,
1046 back_buffer_canvas(),
1047 rect.origin());
1048#endif
1049}
1050
1051void WebPluginDelegateProxy::UpdateFrontBuffer(
1052 const gfx::Rect& rect,
1053 bool allow_buffer_flipping) {
1054 if (!front_buffer_canvas()) {
1055 return;
1056 }
1057
1058#if defined(OS_WIN)
1059 // If SendUpdateGeometry() would block on the plugin process then we don't
1060 // want to use buffer flipping at all since it would add extra locking.
1061 // (Alternatively we could probably safely use async updates for buffer
1062 // flipping all the time since the size is not changing.)
1063 if (UseSynchronousGeometryUpdates()) {
1064 allow_buffer_flipping = false;
1065 }
1066#endif
1067
1068 // Plugin has just painted "rect" into the back-buffer, so the front-buffer
1069 // no longer holds the latest content for that rectangle.
1070 front_buffer_diff_.Subtract(rect);
1071 if (allow_buffer_flipping && front_buffer_diff_.IsEmpty()) {
1072 // Back-buffer contains the latest content for all areas; simply flip
1073 // the buffers.
1074 front_buffer_index_ = back_buffer_index();
1075 SendUpdateGeometry(false);
1076 // The front-buffer now holds newer content for this region than the
1077 // back-buffer.
1078 front_buffer_diff_ = rect;
1079 } else {
1080 // Back-buffer contains the latest content for "rect" but the front-buffer
1081 // contains the latest content for some other areas (or buffer flipping not
1082 // allowed); fall back to copying the data.
1083 CopyFromBackBufferToFrontBuffer(rect);
1084 }
1085 transport_store_painted_.Union(rect);
1086}
1087
1088void WebPluginDelegateProxy::OnHandleURLRequest(
1089 const PluginHostMsg_URLRequest_Params& params) {
1090 const char* data = NULL;
1091 if (params.buffer.size())
1092 data = &params.buffer[0];
1093
1094 const char* target = NULL;
1095 if (params.target.length())
1096 target = params.target.c_str();
1097
1098 plugin_->HandleURLRequest(
1099 params.url.c_str(), params.method.c_str(), target, data,
1100 static_cast<unsigned int>(params.buffer.size()), params.notify_id,
1101 params.popups_allowed, params.notify_redirects);
1102}
1103
Ben Murdochca12bfa2013-07-23 11:17:05 +01001104WebPluginResourceClient* WebPluginDelegateProxy::CreateResourceClient(
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001105 unsigned long resource_id, const GURL& url, int notify_id) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001106 if (!channel_host_.get())
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001107 return NULL;
1108
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001109 ResourceClientProxy* proxy =
1110 new ResourceClientProxy(channel_host_.get(), instance_id_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001111 proxy->Initialize(resource_id, url, notify_id);
1112 return proxy;
1113}
1114
Ben Murdochca12bfa2013-07-23 11:17:05 +01001115WebPluginResourceClient* WebPluginDelegateProxy::CreateSeekableResourceClient(
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001116 unsigned long resource_id, int range_request_id) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001117 if (!channel_host_.get())
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001118 return NULL;
1119
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01001120 ResourceClientProxy* proxy =
1121 new ResourceClientProxy(channel_host_.get(), instance_id_);
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001122 proxy->InitializeForSeekableStream(resource_id, range_request_id);
1123 return proxy;
1124}
1125
1126#if defined(OS_MACOSX)
1127void WebPluginDelegateProxy::OnFocusChanged(bool focused) {
1128 if (render_view_)
1129 render_view_->PluginFocusChanged(focused, instance_id_);
1130}
1131
1132void WebPluginDelegateProxy::OnStartIme() {
1133 if (render_view_)
1134 render_view_->StartPluginIme();
1135}
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001136#endif
1137
1138gfx::PluginWindowHandle WebPluginDelegateProxy::GetPluginWindowHandle() {
1139 return window_;
1140}
1141
1142void WebPluginDelegateProxy::OnCancelDocumentLoad() {
1143 plugin_->CancelDocumentLoad();
1144}
1145
1146void WebPluginDelegateProxy::OnInitiateHTTPRangeRequest(
1147 const std::string& url,
1148 const std::string& range_info,
1149 int range_request_id) {
1150 plugin_->InitiateHTTPRangeRequest(
1151 url.c_str(), range_info.c_str(), range_request_id);
1152}
1153
1154void WebPluginDelegateProxy::OnDeferResourceLoading(unsigned long resource_id,
1155 bool defer) {
1156 plugin_->SetDeferResourceLoading(resource_id, defer);
1157}
1158
1159#if defined(OS_MACOSX)
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001160void WebPluginDelegateProxy::OnAcceleratedPluginEnabledRendering() {
1161 uses_compositor_ = true;
1162 OnSetWindow(gfx::kNullPluginWindow);
1163}
1164
1165void WebPluginDelegateProxy::OnAcceleratedPluginAllocatedIOSurface(
1166 int32 width,
1167 int32 height,
1168 uint32 surface_id) {
1169 if (plugin_)
1170 plugin_->AcceleratedPluginAllocatedIOSurface(width, height, surface_id);
1171}
1172
1173void WebPluginDelegateProxy::OnAcceleratedPluginSwappedIOSurface() {
1174 if (plugin_)
1175 plugin_->AcceleratedPluginSwappedIOSurface();
1176}
1177#endif
1178
1179#if defined(OS_WIN)
1180bool WebPluginDelegateProxy::UseSynchronousGeometryUpdates() {
1181 // Need to update geometry synchronously with WMP, otherwise if a site
1182 // scripts the plugin to start playing while it's in the middle of handling
1183 // an update geometry message, videos don't play. See urls in bug 20260.
1184 if (info_.name.find(ASCIIToUTF16("Windows Media Player")) != string16::npos)
1185 return true;
1186
1187 // The move networks plugin needs to be informed of geometry updates
1188 // synchronously.
Ben Murdochca12bfa2013-07-23 11:17:05 +01001189 std::vector<WebPluginMimeType>::iterator index;
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001190 for (index = info_.mime_types.begin(); index != info_.mime_types.end();
1191 index++) {
1192 if (index->mime_type == "application/x-vnd.moveplayer.qm" ||
1193 index->mime_type == "application/x-vnd.moveplay2.qm" ||
1194 index->mime_type == "application/x-vnd.movenetworks.qm" ||
1195 index->mime_type == "application/x-vnd.mnplayer.qm") {
1196 return true;
1197 }
1198 }
1199 return false;
1200}
1201#endif
1202
1203void WebPluginDelegateProxy::OnURLRedirectResponse(bool allow,
1204 int resource_id) {
1205 if (!plugin_)
1206 return;
1207
1208 plugin_->URLRedirectResponse(allow, resource_id);
1209}
1210
1211} // namespace content