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.
4
5 #include "content/child/npapi/plugin_instance.h"
6
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/files/file_util.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "build/build_config.h"
14 #include "content/child/npapi/plugin_host.h"
15 #include "content/child/npapi/plugin_lib.h"
16 #include "content/child/npapi/plugin_stream_url.h"
17 #include "content/child/npapi/plugin_string_stream.h"
18 #include "content/child/npapi/webplugin.h"
19 #include "content/child/npapi/webplugin_delegate.h"
20 #include "content/child/npapi/webplugin_resource_client.h"
21 #include "content/public/common/content_constants.h"
22 #include "content/public/common/content_switches.h"
23 #include "net/base/escape.h"
24
25 #if defined(OS_MACOSX)
26 #include <ApplicationServices/ApplicationServices.h>
27 #endif
28
29 namespace content {
30
PluginInstance(PluginLib * plugin,const std::string & mime_type)31 PluginInstance::PluginInstance(PluginLib* plugin, const std::string& mime_type)
32 : plugin_(plugin),
33 npp_(0),
34 host_(PluginHost::Singleton()),
35 npp_functions_(plugin->functions()),
36 window_handle_(0),
37 windowless_(false),
38 transparent_(true),
39 webplugin_(0),
40 mime_type_(mime_type),
41 get_notify_data_(0),
42 use_mozilla_user_agent_(false),
43 #if defined (OS_MACOSX)
44 #ifdef NP_NO_QUICKDRAW
45 drawing_model_(NPDrawingModelCoreGraphics),
46 #else
47 drawing_model_(NPDrawingModelQuickDraw),
48 #endif
49 #ifdef NP_NO_CARBON
50 event_model_(NPEventModelCocoa),
51 #else
52 event_model_(NPEventModelCarbon),
53 #endif
54 currently_handled_event_(NULL),
55 #endif
56 message_loop_(base::MessageLoop::current()),
57 load_manually_(false),
58 in_close_streams_(false),
59 next_timer_id_(1),
60 next_notify_id_(0),
61 next_range_request_id_(0),
62 handles_url_redirects_(false) {
63 npp_ = new NPP_t();
64 npp_->ndata = 0;
65 npp_->pdata = 0;
66
67 if (mime_type_ == kFlashPluginSwfMimeType)
68 transparent_ = false;
69
70 memset(&zero_padding_, 0, sizeof(zero_padding_));
71 DCHECK(message_loop_);
72 }
73
~PluginInstance()74 PluginInstance::~PluginInstance() {
75 CloseStreams();
76
77 if (npp_ != 0) {
78 delete npp_;
79 npp_ = 0;
80 }
81
82 if (plugin_.get())
83 plugin_->CloseInstance();
84 }
85
CreateStream(unsigned long resource_id,const GURL & url,const std::string & mime_type,int notify_id)86 PluginStreamUrl* PluginInstance::CreateStream(unsigned long resource_id,
87 const GURL& url,
88 const std::string& mime_type,
89 int notify_id) {
90
91 bool notify;
92 void* notify_data;
93 GetNotifyData(notify_id, ¬ify, ¬ify_data);
94 PluginStreamUrl* stream = new PluginStreamUrl(
95 resource_id, url, this, notify, notify_data);
96
97 AddStream(stream);
98 return stream;
99 }
100
AddStream(PluginStream * stream)101 void PluginInstance::AddStream(PluginStream* stream) {
102 open_streams_.push_back(make_scoped_refptr(stream));
103 }
104
RemoveStream(PluginStream * stream)105 void PluginInstance::RemoveStream(PluginStream* stream) {
106 if (in_close_streams_)
107 return;
108
109 std::vector<scoped_refptr<PluginStream> >::iterator stream_index;
110 for (stream_index = open_streams_.begin();
111 stream_index != open_streams_.end(); ++stream_index) {
112 if (stream_index->get() == stream) {
113 open_streams_.erase(stream_index);
114 break;
115 }
116 }
117 }
118
IsValidStream(const NPStream * stream)119 bool PluginInstance::IsValidStream(const NPStream* stream) {
120 std::vector<scoped_refptr<PluginStream> >::iterator stream_index;
121 for (stream_index = open_streams_.begin();
122 stream_index != open_streams_.end(); ++stream_index) {
123 if ((*stream_index)->stream() == stream)
124 return true;
125 }
126
127 return false;
128 }
129
CloseStreams()130 void PluginInstance::CloseStreams() {
131 in_close_streams_ = true;
132 for (unsigned int index = 0; index < open_streams_.size(); ++index) {
133 // Close all streams on the way down.
134 open_streams_[index]->Close(NPRES_USER_BREAK);
135 }
136 open_streams_.clear();
137 in_close_streams_ = false;
138 }
139
GetRangeRequest(int id)140 WebPluginResourceClient* PluginInstance::GetRangeRequest(
141 int id) {
142 PendingRangeRequestMap::iterator iter = pending_range_requests_.find(id);
143 if (iter == pending_range_requests_.end()) {
144 NOTREACHED();
145 return NULL;
146 }
147
148 WebPluginResourceClient* rv = iter->second->AsResourceClient();
149 pending_range_requests_.erase(iter);
150 return rv;
151 }
152
Start(const GURL & url,char ** const param_names,char ** const param_values,int param_count,bool load_manually)153 bool PluginInstance::Start(const GURL& url,
154 char** const param_names,
155 char** const param_values,
156 int param_count,
157 bool load_manually) {
158 load_manually_ = load_manually;
159 unsigned short mode = load_manually_ ? NP_FULL : NP_EMBED;
160 npp_->ndata = this;
161
162 NPError err = NPP_New(mode, param_count,
163 const_cast<char **>(param_names), const_cast<char **>(param_values));
164
165 if (err == NPERR_NO_ERROR) {
166 handles_url_redirects_ =
167 ((npp_functions_->version >= NPVERS_HAS_URL_REDIRECT_HANDLING) &&
168 (npp_functions_->urlredirectnotify));
169 }
170 return err == NPERR_NO_ERROR;
171 }
172
GetPluginScriptableObject()173 NPObject *PluginInstance::GetPluginScriptableObject() {
174 NPObject *value = NULL;
175 NPError error = NPP_GetValue(NPPVpluginScriptableNPObject, &value);
176 if (error != NPERR_NO_ERROR || value == NULL)
177 return NULL;
178 return value;
179 }
180
GetFormValue(base::string16 * value)181 bool PluginInstance::GetFormValue(base::string16* value) {
182 // Plugins will allocate memory for the return value by using NPN_MemAlloc().
183 char *plugin_value = NULL;
184 NPError error = NPP_GetValue(NPPVformValue, &plugin_value);
185 if (error != NPERR_NO_ERROR || !plugin_value) {
186 return false;
187 }
188 // Assumes the result is UTF8 text, as Firefox does.
189 *value = base::UTF8ToUTF16(plugin_value);
190 host_->host_functions()->memfree(plugin_value);
191 return true;
192 }
193
194 // WebPluginLoadDelegate methods
DidFinishLoadWithReason(const GURL & url,NPReason reason,int notify_id)195 void PluginInstance::DidFinishLoadWithReason(const GURL& url,
196 NPReason reason,
197 int notify_id) {
198 bool notify;
199 void* notify_data;
200 GetNotifyData(notify_id, ¬ify, ¬ify_data);
201 if (!notify) {
202 NOTREACHED();
203 return;
204 }
205
206 NPP_URLNotify(url.spec().c_str(), reason, notify_data);
207 }
208
GetBackingTextureId()209 unsigned PluginInstance::GetBackingTextureId() {
210 // By default the plugin instance is not backed by an OpenGL texture.
211 return 0;
212 }
213
214 // NPAPI methods
NPP_New(unsigned short mode,short argc,char * argn[],char * argv[])215 NPError PluginInstance::NPP_New(unsigned short mode,
216 short argc,
217 char* argn[],
218 char* argv[]) {
219 DCHECK(npp_functions_ != 0);
220 DCHECK(npp_functions_->newp != 0);
221 DCHECK(argc >= 0);
222
223 if (npp_functions_->newp != 0) {
224 return npp_functions_->newp(
225 (NPMIMEType)mime_type_.c_str(), npp_, mode, argc, argn, argv, NULL);
226 }
227 return NPERR_INVALID_FUNCTABLE_ERROR;
228 }
229
NPP_Destroy()230 void PluginInstance::NPP_Destroy() {
231 DCHECK(npp_functions_ != 0);
232 DCHECK(npp_functions_->destroy != 0);
233
234 if (npp_functions_->destroy != 0) {
235 NPSavedData *savedData = 0;
236 npp_functions_->destroy(npp_, &savedData);
237
238 // TODO: Support savedData. Technically, these need to be
239 // saved on a per-URL basis, and then only passed
240 // to new instances of the plugin at the same URL.
241 // Sounds like a huge security risk. When we do support
242 // these, we should pass them back to the PluginLib
243 // to be stored there.
244 DCHECK(savedData == 0);
245 }
246
247 for (unsigned int file_index = 0; file_index < files_created_.size();
248 file_index++) {
249 base::DeleteFile(files_created_[file_index], false);
250 }
251
252 // Ensure that no timer callbacks are invoked after NPP_Destroy.
253 timers_.clear();
254 }
255
NPP_SetWindow(NPWindow * window)256 NPError PluginInstance::NPP_SetWindow(NPWindow* window) {
257 DCHECK(npp_functions_ != 0);
258 DCHECK(npp_functions_->setwindow != 0);
259
260 if (npp_functions_->setwindow != 0) {
261 return npp_functions_->setwindow(npp_, window);
262 }
263 return NPERR_INVALID_FUNCTABLE_ERROR;
264 }
265
NPP_NewStream(NPMIMEType type,NPStream * stream,NPBool seekable,unsigned short * stype)266 NPError PluginInstance::NPP_NewStream(NPMIMEType type,
267 NPStream* stream,
268 NPBool seekable,
269 unsigned short* stype) {
270 DCHECK(npp_functions_ != 0);
271 DCHECK(npp_functions_->newstream != 0);
272 if (npp_functions_->newstream != 0) {
273 return npp_functions_->newstream(npp_, type, stream, seekable, stype);
274 }
275 return NPERR_INVALID_FUNCTABLE_ERROR;
276 }
277
NPP_DestroyStream(NPStream * stream,NPReason reason)278 NPError PluginInstance::NPP_DestroyStream(NPStream* stream, NPReason reason) {
279 DCHECK(npp_functions_ != 0);
280 DCHECK(npp_functions_->destroystream != 0);
281
282 if (stream == NULL || !IsValidStream(stream) || (stream->ndata == NULL))
283 return NPERR_INVALID_INSTANCE_ERROR;
284
285 if (npp_functions_->destroystream != 0) {
286 NPError result = npp_functions_->destroystream(npp_, stream, reason);
287 stream->ndata = NULL;
288 return result;
289 }
290 return NPERR_INVALID_FUNCTABLE_ERROR;
291 }
292
NPP_WriteReady(NPStream * stream)293 int PluginInstance::NPP_WriteReady(NPStream* stream) {
294 DCHECK(npp_functions_ != 0);
295 DCHECK(npp_functions_->writeready != 0);
296 if (npp_functions_->writeready != 0) {
297 return npp_functions_->writeready(npp_, stream);
298 }
299 return 0;
300 }
301
NPP_Write(NPStream * stream,int offset,int len,void * buffer)302 int PluginInstance::NPP_Write(NPStream* stream,
303 int offset,
304 int len,
305 void* buffer) {
306 DCHECK(npp_functions_ != 0);
307 DCHECK(npp_functions_->write != 0);
308 if (npp_functions_->write != 0) {
309 return npp_functions_->write(npp_, stream, offset, len, buffer);
310 }
311 return 0;
312 }
313
NPP_StreamAsFile(NPStream * stream,const char * fname)314 void PluginInstance::NPP_StreamAsFile(NPStream* stream, const char* fname) {
315 DCHECK(npp_functions_ != 0);
316 DCHECK(npp_functions_->asfile != 0);
317 if (npp_functions_->asfile != 0) {
318 npp_functions_->asfile(npp_, stream, fname);
319 }
320
321 // Creating a temporary FilePath instance on the stack as the explicit
322 // FilePath constructor with StringType as an argument causes a compiler
323 // error when invoked via vector push back.
324 base::FilePath file_name = base::FilePath::FromUTF8Unsafe(fname);
325 files_created_.push_back(file_name);
326 }
327
NPP_URLNotify(const char * url,NPReason reason,void * notifyData)328 void PluginInstance::NPP_URLNotify(const char* url,
329 NPReason reason,
330 void* notifyData) {
331 DCHECK(npp_functions_ != 0);
332 DCHECK(npp_functions_->urlnotify != 0);
333 if (npp_functions_->urlnotify != 0) {
334 npp_functions_->urlnotify(npp_, url, reason, notifyData);
335 }
336 }
337
NPP_GetValue(NPPVariable variable,void * value)338 NPError PluginInstance::NPP_GetValue(NPPVariable variable, void* value) {
339 DCHECK(npp_functions_ != 0);
340 // getvalue is NULL for Shockwave
341 if (npp_functions_->getvalue != 0) {
342 return npp_functions_->getvalue(npp_, variable, value);
343 }
344 return NPERR_INVALID_FUNCTABLE_ERROR;
345 }
346
NPP_SetValue(NPNVariable variable,void * value)347 NPError PluginInstance::NPP_SetValue(NPNVariable variable, void* value) {
348 DCHECK(npp_functions_ != 0);
349 if (npp_functions_->setvalue != 0) {
350 return npp_functions_->setvalue(npp_, variable, value);
351 }
352 return NPERR_INVALID_FUNCTABLE_ERROR;
353 }
354
NPP_HandleEvent(void * event)355 short PluginInstance::NPP_HandleEvent(void* event) {
356 DCHECK(npp_functions_ != 0);
357 DCHECK(npp_functions_->event != 0);
358 if (npp_functions_->event != 0) {
359 return npp_functions_->event(npp_, (void*)event);
360 }
361 return false;
362 }
363
NPP_Print(NPPrint * platform_print)364 bool PluginInstance::NPP_Print(NPPrint* platform_print) {
365 DCHECK(npp_functions_ != 0);
366 if (npp_functions_->print != 0) {
367 npp_functions_->print(npp_, platform_print);
368 return true;
369 }
370 return false;
371 }
372
NPP_URLRedirectNotify(const char * url,int32_t status,void * notify_data)373 void PluginInstance::NPP_URLRedirectNotify(const char* url, int32_t status,
374 void* notify_data) {
375 DCHECK(npp_functions_ != 0);
376 if (npp_functions_->urlredirectnotify != 0) {
377 npp_functions_->urlredirectnotify(npp_, url, status, notify_data);
378 }
379 }
380
SendJavaScriptStream(const GURL & url,const std::string & result,bool success,int notify_id)381 void PluginInstance::SendJavaScriptStream(const GURL& url,
382 const std::string& result,
383 bool success,
384 int notify_id) {
385 bool notify;
386 void* notify_data;
387 GetNotifyData(notify_id, ¬ify, ¬ify_data);
388
389 if (success) {
390 PluginStringStream *stream =
391 new PluginStringStream(this, url, notify, notify_data);
392 AddStream(stream);
393 stream->SendToPlugin(result, "text/html");
394 } else {
395 // NOTE: Sending an empty stream here will crash MacroMedia
396 // Flash 9. Just send the URL Notify.
397 if (notify)
398 NPP_URLNotify(url.spec().c_str(), NPRES_DONE, notify_data);
399 }
400 }
401
DidReceiveManualResponse(const GURL & url,const std::string & mime_type,const std::string & headers,uint32 expected_length,uint32 last_modified)402 void PluginInstance::DidReceiveManualResponse(const GURL& url,
403 const std::string& mime_type,
404 const std::string& headers,
405 uint32 expected_length,
406 uint32 last_modified) {
407 DCHECK(load_manually_);
408
409 plugin_data_stream_ =
410 CreateStream(static_cast<unsigned long>(-1), url, mime_type, 0);
411 plugin_data_stream_->DidReceiveResponse(mime_type, headers, expected_length,
412 last_modified, true);
413 }
414
DidReceiveManualData(const char * buffer,int length)415 void PluginInstance::DidReceiveManualData(const char* buffer, int length) {
416 DCHECK(load_manually_);
417 if (plugin_data_stream_.get() != NULL) {
418 plugin_data_stream_->DidReceiveData(buffer, length, 0);
419 }
420 }
421
DidFinishManualLoading()422 void PluginInstance::DidFinishManualLoading() {
423 DCHECK(load_manually_);
424 if (plugin_data_stream_.get() != NULL) {
425 plugin_data_stream_->DidFinishLoading(plugin_data_stream_->ResourceId());
426 plugin_data_stream_->Close(NPRES_DONE);
427 plugin_data_stream_ = NULL;
428 }
429 }
430
DidManualLoadFail()431 void PluginInstance::DidManualLoadFail() {
432 DCHECK(load_manually_);
433 if (plugin_data_stream_.get() != NULL) {
434 plugin_data_stream_->DidFail(plugin_data_stream_->ResourceId());
435 plugin_data_stream_ = NULL;
436 }
437 }
438
PluginThreadAsyncCall(void (* func)(void *),void * user_data)439 void PluginInstance::PluginThreadAsyncCall(void (*func)(void*),
440 void* user_data) {
441 message_loop_->PostTask(
442 FROM_HERE, base::Bind(&PluginInstance::OnPluginThreadAsyncCall, this,
443 func, user_data));
444 }
445
OnPluginThreadAsyncCall(void (* func)(void *),void * user_data)446 void PluginInstance::OnPluginThreadAsyncCall(void (*func)(void*),
447 void* user_data) {
448 // Do not invoke the callback if NPP_Destroy has already been invoked.
449 if (webplugin_)
450 func(user_data);
451 }
452
ScheduleTimer(uint32 interval,NPBool repeat,void (* func)(NPP id,uint32 timer_id))453 uint32 PluginInstance::ScheduleTimer(uint32 interval,
454 NPBool repeat,
455 void (*func)(NPP id, uint32 timer_id)) {
456 // Use next timer id.
457 uint32 timer_id;
458 timer_id = next_timer_id_;
459 ++next_timer_id_;
460 DCHECK(next_timer_id_ != 0);
461
462 // Record timer interval and repeat.
463 TimerInfo info;
464 info.interval = interval;
465 info.repeat = repeat ? true : false;
466 timers_[timer_id] = info;
467
468 // Schedule the callback.
469 base::MessageLoop::current()->PostDelayedTask(
470 FROM_HERE,
471 base::Bind(&PluginInstance::OnTimerCall, this, func, npp_, timer_id),
472 base::TimeDelta::FromMilliseconds(interval));
473 return timer_id;
474 }
475
UnscheduleTimer(uint32 timer_id)476 void PluginInstance::UnscheduleTimer(uint32 timer_id) {
477 // Remove info about the timer.
478 TimerMap::iterator it = timers_.find(timer_id);
479 if (it != timers_.end())
480 timers_.erase(it);
481 }
482
483 #if !defined(OS_MACOSX)
PopUpContextMenu(NPMenu * menu)484 NPError PluginInstance::PopUpContextMenu(NPMenu* menu) {
485 NOTIMPLEMENTED();
486 return NPERR_GENERIC_ERROR;
487 }
488 #endif
489
OnTimerCall(void (* func)(NPP id,uint32 timer_id),NPP id,uint32 timer_id)490 void PluginInstance::OnTimerCall(void (*func)(NPP id, uint32 timer_id),
491 NPP id,
492 uint32 timer_id) {
493 // Do not invoke callback if the timer has been unscheduled.
494 TimerMap::iterator it = timers_.find(timer_id);
495 if (it == timers_.end())
496 return;
497
498 // Get all information about the timer before invoking the callback. The
499 // callback might unschedule the timer.
500 TimerInfo info = it->second;
501
502 func(id, timer_id);
503
504 // If the timer was unscheduled by the callback, just free up the timer id.
505 if (timers_.find(timer_id) == timers_.end())
506 return;
507
508 // Reschedule repeating timers after invoking the callback so callback is not
509 // re-entered if it pumps the message loop.
510 if (info.repeat) {
511 base::MessageLoop::current()->PostDelayedTask(
512 FROM_HERE,
513 base::Bind(&PluginInstance::OnTimerCall, this, func, npp_, timer_id),
514 base::TimeDelta::FromMilliseconds(info.interval));
515 } else {
516 timers_.erase(it);
517 }
518 }
519
PushPopupsEnabledState(bool enabled)520 void PluginInstance::PushPopupsEnabledState(bool enabled) {
521 popups_enabled_stack_.push(enabled);
522 }
523
PopPopupsEnabledState()524 void PluginInstance::PopPopupsEnabledState() {
525 popups_enabled_stack_.pop();
526 }
527
RequestRead(NPStream * stream,NPByteRange * range_list)528 void PluginInstance::RequestRead(NPStream* stream, NPByteRange* range_list) {
529 std::string range_info = "bytes=";
530
531 while (range_list) {
532 range_info += base::IntToString(range_list->offset);
533 range_info.push_back('-');
534 range_info +=
535 base::IntToString(range_list->offset + range_list->length - 1);
536 range_list = range_list->next;
537 if (range_list)
538 range_info.push_back(',');
539 }
540
541 if (plugin_data_stream_.get()) {
542 if (plugin_data_stream_->stream() == stream) {
543 webplugin_->CancelDocumentLoad();
544 plugin_data_stream_ = NULL;
545 }
546 }
547
548 // The lifetime of a NPStream instance depends on the PluginStream instance
549 // which owns it. When a plugin invokes NPN_RequestRead on a seekable stream,
550 // we don't want to create a new stream when the corresponding response is
551 // received. We send over a cookie which represents the PluginStream
552 // instance which is sent back from the renderer when the response is
553 // received.
554 std::vector<scoped_refptr<PluginStream> >::iterator stream_index;
555 for (stream_index = open_streams_.begin();
556 stream_index != open_streams_.end(); ++stream_index) {
557 PluginStream* plugin_stream = stream_index->get();
558 if (plugin_stream->stream() == stream) {
559 // A stream becomes seekable the first time NPN_RequestRead
560 // is called on it.
561 plugin_stream->set_seekable(true);
562
563 if (CommandLine::ForCurrentProcess()->HasSwitch(
564 switches::kDisableDirectNPAPIRequests)) {
565 pending_range_requests_[++next_range_request_id_] = plugin_stream;
566 webplugin_->InitiateHTTPRangeRequest(
567 stream->url, range_info.c_str(), next_range_request_id_);
568 return;
569 } else {
570 PluginStreamUrl* plugin_stream_url =
571 static_cast<PluginStreamUrl*>(plugin_stream);
572 plugin_stream_url->FetchRange(range_info);
573 return;
574 }
575 }
576 }
577 NOTREACHED();
578 }
579
RequestURL(const char * url,const char * method,const char * target,const char * buf,unsigned int len,bool notify,void * notify_data)580 void PluginInstance::RequestURL(const char* url,
581 const char* method,
582 const char* target,
583 const char* buf,
584 unsigned int len,
585 bool notify,
586 void* notify_data) {
587 int notify_id = 0;
588 if (notify) {
589 notify_id = ++next_notify_id_;
590 pending_requests_[notify_id] = notify_data;
591 }
592
593 webplugin_->HandleURLRequest(
594 url, method, target, buf, len, notify_id, popups_allowed(),
595 notify ? handles_url_redirects_ : false);
596 }
597
ConvertPoint(double source_x,double source_y,NPCoordinateSpace source_space,double * dest_x,double * dest_y,NPCoordinateSpace dest_space)598 bool PluginInstance::ConvertPoint(double source_x, double source_y,
599 NPCoordinateSpace source_space,
600 double* dest_x, double* dest_y,
601 NPCoordinateSpace dest_space) {
602 #if defined(OS_MACOSX)
603 CGRect main_display_bounds = CGDisplayBounds(CGMainDisplayID());
604
605 double flipped_screen_x = source_x;
606 double flipped_screen_y = source_y;
607 switch(source_space) {
608 case NPCoordinateSpacePlugin:
609 flipped_screen_x += plugin_origin_.x();
610 flipped_screen_y += plugin_origin_.y();
611 break;
612 case NPCoordinateSpaceWindow:
613 flipped_screen_x += containing_window_frame_.x();
614 flipped_screen_y = containing_window_frame_.height() - source_y +
615 containing_window_frame_.y();
616 break;
617 case NPCoordinateSpaceFlippedWindow:
618 flipped_screen_x += containing_window_frame_.x();
619 flipped_screen_y += containing_window_frame_.y();
620 break;
621 case NPCoordinateSpaceScreen:
622 flipped_screen_y = main_display_bounds.size.height - flipped_screen_y;
623 break;
624 case NPCoordinateSpaceFlippedScreen:
625 break;
626 default:
627 NOTREACHED();
628 return false;
629 }
630
631 double target_x = flipped_screen_x;
632 double target_y = flipped_screen_y;
633 switch(dest_space) {
634 case NPCoordinateSpacePlugin:
635 target_x -= plugin_origin_.x();
636 target_y -= plugin_origin_.y();
637 break;
638 case NPCoordinateSpaceWindow:
639 target_x -= containing_window_frame_.x();
640 target_y -= containing_window_frame_.y();
641 target_y = containing_window_frame_.height() - target_y;
642 break;
643 case NPCoordinateSpaceFlippedWindow:
644 target_x -= containing_window_frame_.x();
645 target_y -= containing_window_frame_.y();
646 break;
647 case NPCoordinateSpaceScreen:
648 target_y = main_display_bounds.size.height - flipped_screen_y;
649 break;
650 case NPCoordinateSpaceFlippedScreen:
651 break;
652 default:
653 NOTREACHED();
654 return false;
655 }
656
657 if (dest_x)
658 *dest_x = target_x;
659 if (dest_y)
660 *dest_y = target_y;
661 return true;
662 #else
663 NOTIMPLEMENTED();
664 return false;
665 #endif
666 }
667
GetNotifyData(int notify_id,bool * notify,void ** notify_data)668 void PluginInstance::GetNotifyData(int notify_id,
669 bool* notify,
670 void** notify_data) {
671 PendingRequestMap::iterator iter = pending_requests_.find(notify_id);
672 if (iter != pending_requests_.end()) {
673 *notify = true;
674 *notify_data = iter->second;
675 pending_requests_.erase(iter);
676 } else {
677 *notify = false;
678 *notify_data = NULL;
679 }
680 }
681
URLRedirectResponse(bool allow,void * notify_data)682 void PluginInstance::URLRedirectResponse(bool allow, void* notify_data) {
683 // The notify_data passed in allows us to identify the matching stream.
684 std::vector<scoped_refptr<PluginStream> >::iterator stream_index;
685 for (stream_index = open_streams_.begin();
686 stream_index != open_streams_.end(); ++stream_index) {
687 PluginStream* plugin_stream = stream_index->get();
688 if (plugin_stream->notify_data() == notify_data) {
689 PluginStreamUrl* plugin_stream_url =
690 static_cast<PluginStreamUrl*>(plugin_stream);
691 plugin_stream_url->URLRedirectResponse(allow);
692 break;
693 }
694 }
695 }
696
697 } // namespace content
698