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