update copyright date
[gnash.git] / plugin / npapi / callbacks.cpp
blob98a8373a93e82533df8e5ea1f59d1adc4978d20a
1 //
2 // Copyright (C) 2010, 2011 Free Software Foundation, Inc
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 #include "gnashconfig.h"
21 #endif
23 #include <iostream>
24 #include <fstream>
25 #include <sstream>
26 #include <map>
27 #include <string>
28 #include <cstring>
29 #include <cstdlib>
30 #include "npapi.h"
31 #include "external.h"
32 #include "npruntime.h"
33 #include "plugin.h"
34 #include "pluginScriptObject.h"
36 #if 0
38 // http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_04.html
39 // Tell Target Methods. Use TGetProperty, TGetPropertyAsNumber, TSetProperty
40 TCallLabel
41 TCurrentFrame
42 TCurrentLabel
43 TGetProperty
44 TGetPropertyAsNumber
45 TGotoFrame
46 TGotoLabel
47 TPlay
48 TSetProperty
49 TStopPlay
51 // Target Properties
52 X_POS
53 Y_POS
54 X_SCALE
55 Y_SCALE
56 CURRENT_FRAME // read-only
57 TOTAL_FRAMES // read-only
58 ALPHA
59 VISIBLE
60 WIDTH // read-only
61 HEIGHT // read-only
62 ROTATE
63 TARGET // read-only
64 FRAMES_LOADED // read-only
65 NAME
66 DROP_TARGET // read-only
67 URL // read-only
68 HIGH_QUALITY
69 FOCUS_RECT
70 SOUND_BUF_TIME
72 // Standard Events
73 OnProgress
74 OnReadyStateChange
75 FSCommand
76 #endif
78 namespace gnash {
80 // Callbacks for the default methods
82 // As these callbacks use a generalized typedef for the signature, often some
83 // of the parameters can be ignored. These are commented out to elimnate the
84 // volumnes of bogus warnings about not using them in the method.
86 // SetVariable( Name, Value )
88 // Sends something like this:
89 // <invoke name="SetVariable" returntype="xml">
90 // <arguments>
91 // <string>var1</string>
92 // <string>value1</string>
93 // </arguments>
94 // </invoke>
96 // Receives:
97 // nothing
98 bool
99 SetVariableCallback (NPObject *npobj, NPIdentifier /* name */, const NPVariant *args,
100 uint32_t argCount, NPVariant *result)
102 log_debug(__PRETTY_FUNCTION__);
104 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
106 std::string varname;
107 if (argCount == 2) {
108 varname = NPStringToString(NPVARIANT_TO_STRING(args[0]));
109 const NPVariant& value = args[1];
110 // log_debug("Setting Variable \"%s\"", varname);
111 gpso->SetVariable(varname, value);
112 BOOLEAN_TO_NPVARIANT(true, *result);
113 return true;
116 BOOLEAN_TO_NPVARIANT(false, *result);
117 return false;
120 // GetVariable( Name )
122 // Sends something like this:
123 // <invoke name="GetVariable" returntype="xml">
124 // <arguments>
125 // <string>var1</string>
126 // </arguments>
127 // </invoke>
129 // Receives something like this:
130 // <number>123</number>
131 bool
132 GetVariableCallback (NPObject *npobj, NPIdentifier /* name */,
133 const NPVariant *args,
134 uint32_t argCount, NPVariant *result)
136 log_debug(__PRETTY_FUNCTION__);
138 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
139 std::string varname;
140 // This method only takes one argument
141 if (argCount == 1) {
142 varname = NPStringToString(NPVARIANT_TO_STRING(args[0]));
144 GnashNPVariant value = gpso->GetVariable(varname);
145 value.copy(*result);
147 return true;
150 NULL_TO_NPVARIANT(*result);
151 return false;
154 // GotoFrame( frameNumber )
156 // Sends something like this:
157 // <invoke name="GotoFrame" returntype="xml">
158 // <arguments>
159 // <number>123</number>
160 // </arguments>
161 // </invoke>
163 // Receives:
164 // nothing
165 bool
166 GotoFrame (NPObject *npobj, NPIdentifier /* name */, const NPVariant *args,
167 uint32_t argCount, NPVariant *result)
169 log_debug(__PRETTY_FUNCTION__);
171 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
173 std::string varname;
174 if (argCount == 1) {
175 std::string str = plugin::ExternalInterface::convertNPVariant(&args[0]);
176 std::vector<std::string> iargs;
177 iargs.push_back(str);
178 str = plugin::ExternalInterface::makeInvoke("GotoFrame", iargs);
180 // Write the message to the Control FD.
181 size_t ret = gpso->writePlayer(str);
182 // Unless we wrote the same amount of data as the message contained,
183 // something went wrong.
184 if (ret != str.size()) {
185 log_error("Couldn't goto the specified frame, network problems.");
186 return false;
188 // gpso->GotoFrame(value);
189 BOOLEAN_TO_NPVARIANT(true, *result);
190 return true;
193 BOOLEAN_TO_NPVARIANT(false, *result);
194 return false;
197 // IsPlaying()
199 // Sends this:
200 // <invoke name="IsPlaying" returntype="xml">
201 // <arguments></arguments>
202 // </invoke>
204 // Receives something like this:
205 // </true/>
206 bool
207 IsPlaying (NPObject *npobj, NPIdentifier /* name */, const NPVariant */*args */,
208 uint32_t argCount, NPVariant *result)
210 log_debug(__PRETTY_FUNCTION__);
212 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
214 if (argCount == 0) {
215 std::vector<std::string> iargs;
216 std::string str = plugin::ExternalInterface::makeInvoke("IsPlaying", iargs);
218 // Write the message to the Control FD.
219 size_t ret = gpso->writePlayer(str);
220 // Unless we wrote the same amount of data as the message contained,
221 // something went wrong.
222 if (ret != str.size()) {
223 log_error("Couldn't check if the movie is playing, network problems.");
224 BOOLEAN_TO_NPVARIANT(false, *result);
225 return false;
227 std::string data = gpso->readPlayer();
228 if (data.empty()) {
229 BOOLEAN_TO_NPVARIANT(false, *result);
230 return false;
233 GnashNPVariant value = plugin::ExternalInterface::parseXML(data);
234 if (NPVARIANT_TO_BOOLEAN(value.get()) == true) {
235 BOOLEAN_TO_NPVARIANT(true, *result);
236 } else {
237 BOOLEAN_TO_NPVARIANT(false, *result);
240 return true;
243 BOOLEAN_TO_NPVARIANT(false, *result);
244 return false;
247 // LoadMovie( Layer, Url )
249 // Sends something like this:
250 // <invoke name="LoadMovie" returntype="xml">
251 // <arguments>
252 // <number>2</number>
253 // <string>bogus</string>
254 // </arguments>
255 // </invoke>
257 // Receives:
258 // nothing
259 bool
260 LoadMovie (NPObject *npobj, NPIdentifier /* name */, const NPVariant *args,
261 uint32_t argCount, NPVariant *result)
263 log_debug(__PRETTY_FUNCTION__);
265 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
267 if (argCount == 2) {
268 // int layer = NPVARIANT_TO_INT32(args[0]);
269 // std::string url = NPStringToString(NPVARIANT_TO_STRING(args[1]));
270 std::string str = plugin::ExternalInterface::convertNPVariant(&args[0]);
271 std::vector<std::string> iargs;
272 iargs.push_back(str);
273 str = plugin::ExternalInterface::convertNPVariant(&args[1]);
274 iargs.push_back(str);
275 str = plugin::ExternalInterface::makeInvoke("LoadMovie", iargs);
277 // Write the message to the Control FD.
278 size_t ret = gpso->writePlayer(str);
279 // Unless we wrote the same amount of data as the message contained,
280 // something went wrong.
281 if (ret != str.size()) {
282 log_error("Couldn't load the movie, network problems.");
283 return false;
285 // gpso->LoadMovie();
286 BOOLEAN_TO_NPVARIANT(true, *result);
287 return true;
290 BOOLEAN_TO_NPVARIANT(false, *result);
291 return false;
294 // Pan ( x, y, mode )
296 // Sends something like this:
297 // <invoke name="Pan" returntype="xml">
298 // <arguments>
299 // <number>1</number>
300 // <number>2</number>
301 // <number>0</number>
302 // </arguments>
303 // </invoke>
305 // Receives:
306 // nothing
307 bool
308 Pan (NPObject *npobj, NPIdentifier /* name */, const NPVariant *args,
309 uint32_t argCount, NPVariant *result)
311 log_debug(__PRETTY_FUNCTION__);
313 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
315 if (argCount == 3) {
316 std::string str = plugin::ExternalInterface::convertNPVariant(&args[0]);
317 std::vector<std::string> iargs;
318 iargs.push_back(str);
319 str = plugin::ExternalInterface::convertNPVariant(&args[1]);
320 iargs.push_back(str);
321 str = plugin::ExternalInterface::convertNPVariant(&args[2]);
322 iargs.push_back(str);
323 str = plugin::ExternalInterface::makeInvoke("Pan", iargs);
325 // Write the message to the Control FD.
326 size_t ret = gpso->writePlayer(str);
327 // Unless we wrote the same amount of data as the message contained,
328 // something went wrong.
329 if (ret != str.size()) {
330 log_error("Couldn't pan the movie, network problems.");
331 return false;
333 BOOLEAN_TO_NPVARIANT(true, *result);
334 return true;
337 BOOLEAN_TO_NPVARIANT(false, *result);
338 return false;
341 // PercentLoaded()
343 // Sends this:
344 // <invoke name="PercentLoaded" returntype="xml">
345 // <arguments></arguments>
346 // </invoke>
348 // Receives something like this:
349 // <number>33</number>
350 bool
351 PercentLoaded (NPObject *npobj, NPIdentifier /* name */, const NPVariant */*args */,
352 uint32_t argCount, NPVariant *result)
354 // log_debug(__PRETTY_FUNCTION__);
356 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
358 if (argCount == 0) {
359 std::vector<std::string> iargs;
360 std::string str = plugin::ExternalInterface::makeInvoke("PercentLoaded", iargs);
362 // Write the message to the Control FD.
363 size_t ret = gpso->writePlayer(str);
364 // Unless we wrote the same amount of data as the message contained,
365 // something went wrong.
366 if (ret != str.size()) {
367 log_error("Couldn't check percent loaded, network problems.");
368 BOOLEAN_TO_NPVARIANT(false, *result);
369 return false;
371 std::string data = gpso->readPlayer();
372 if (data.empty()) {
373 BOOLEAN_TO_NPVARIANT(false, *result);
374 return false;
377 GnashNPVariant value = plugin::ExternalInterface::parseXML(data);
378 if (NPVARIANT_IS_INT32(value.get())) {
379 INT32_TO_NPVARIANT(NPVARIANT_TO_INT32(value.get()), *result);
380 } else {
381 INT32_TO_NPVARIANT(0, *result);
384 return true;
387 BOOLEAN_TO_NPVARIANT(false, *result);
388 return false;
391 // Play();
393 // Sends this:
394 // <invoke name="Play" returntype="xml">
395 // <arguments></arguments>
396 // </invoke>
398 // Receives:
399 // nothing
400 bool
401 Play (NPObject *npobj, NPIdentifier /* name */, const NPVariant */*args */,
402 uint32_t argCount, NPVariant *result)
404 log_debug(__PRETTY_FUNCTION__);
406 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
408 if (argCount == 0) {
409 std::vector<std::string> iargs;
410 std::string str = plugin::ExternalInterface::makeInvoke("Play", iargs);
412 // Write the message to the Control FD.
413 size_t ret = gpso->writePlayer(str);
414 // Unless we wrote the same amount of data as the message contained,
415 // something went wrong.
416 if (ret != str.size()) {
417 log_error("Couldn't play movie, network problems.");
418 return false;
420 // gpso->IsPlaying(value);
421 BOOLEAN_TO_NPVARIANT(true, *result);
422 return true;
425 BOOLEAN_TO_NPVARIANT(false, *result);
426 return false;
429 // Rewind();
431 // Sends this:
432 // <invoke name="Rewind" returntype="xml">
433 // <arguments></arguments>
434 // </invoke>
436 // Receives:
437 // nothing
438 bool
439 Rewind (NPObject *npobj, NPIdentifier /* name */, const NPVariant */*args */,
440 uint32_t argCount, NPVariant *result)
442 log_debug(__PRETTY_FUNCTION__);
444 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
446 if (argCount == 0) {
447 std::vector<std::string> iargs;
448 std::string str = plugin::ExternalInterface::makeInvoke("Rewind", iargs);
450 // Write the message to the Control FD.
451 size_t ret = gpso->writePlayer(str);
452 // Unless we wrote the same amount of data as the message contained,
453 // something went wrong.
454 if (ret != str.size()) {
455 log_error("Couldn't rewind movie, network problems.");
456 return false;
458 // gpso->Rewind(value);
459 BOOLEAN_TO_NPVARIANT(true, *result);
460 return true;
463 BOOLEAN_TO_NPVARIANT(false, *result);
464 return false;
467 // SetZoomRect ( left, top, right, bottom )
468 // Sends something like this:
469 // <invoke name="SetZoomRect" returntype="xml">
470 // <arguments>
471 // <number>1</number>
472 // <number>2</number>
473 // <number>3</number>
474 // <number>4</number>
475 // </arguments>
476 // </invoke>
478 // Receives:
479 // nothing
480 bool
481 SetZoomRect (NPObject *npobj, NPIdentifier /* name */, const NPVariant *args,
482 uint32_t argCount, NPVariant *result)
484 log_debug(__PRETTY_FUNCTION__);
486 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
488 if (argCount == 4) {
489 std::string str = plugin::ExternalInterface::convertNPVariant(&args[0]);
490 std::vector<std::string> iargs;
491 iargs.push_back(str);
492 str = plugin::ExternalInterface::convertNPVariant(&args[1]);
493 iargs.push_back(str);
494 str = plugin::ExternalInterface::convertNPVariant(&args[2]);
495 iargs.push_back(str);
496 str = plugin::ExternalInterface::convertNPVariant(&args[3]);
497 iargs.push_back(str);
498 str = plugin::ExternalInterface::makeInvoke("SetZoomRect", iargs);
500 // Write the message to the Control FD.
501 size_t ret = gpso->writePlayer(str);
502 // Unless we wrote the same amount of data as the message contained,
503 // something went wrong.
504 if (ret != str.size()) {
505 log_error("Couldn't Set the Zoom Rect the movie, network problems.");
506 return false;
508 BOOLEAN_TO_NPVARIANT(true, *result);
509 return true;
512 BOOLEAN_TO_NPVARIANT(false, *result);
513 return false;
516 // StopPlay()
518 // Sends this:
519 // <invoke name="StopPlay" returntype="xml">
520 // <arguments></arguments>
521 // </invoke>
523 // Receives:
524 // nothing
525 bool
526 StopPlay (NPObject *npobj, NPIdentifier /* name */, const NPVariant */*args */,
527 uint32_t argCount, NPVariant *result)
529 log_debug(__PRETTY_FUNCTION__);
531 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
533 if (argCount == 0) {
534 std::vector<std::string> iargs;
535 std::string str = plugin::ExternalInterface::makeInvoke("StopPlay", iargs);
537 // Write the message to the Control FD.
538 size_t ret = gpso->writePlayer(str);
539 // Unless we wrote the same amount of data as the message contained,
540 // something went wrong.
541 if (ret != str.size()) {
542 log_error("Couldn't stop-play movie, network problems.");
543 return false;
545 // gpso->IsPlaying(value);
546 BOOLEAN_TO_NPVARIANT(true, *result);
547 return true;
550 BOOLEAN_TO_NPVARIANT(false, *result);
551 return false;
554 // Zoom( percent )
556 // Sends something like this:
557 // <invoke name="Zoom" returntype="xml">
558 // <arguments>
559 // <number>12</number>
560 // </arguments>
561 // </invoke>
563 // Receives:
564 // nothing
565 bool
566 Zoom (NPObject *npobj, NPIdentifier /* name */, const NPVariant *args,
567 uint32_t argCount, NPVariant *result)
569 log_debug(__PRETTY_FUNCTION__);
571 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
573 if (argCount == 1) {
574 std::string str = plugin::ExternalInterface::convertNPVariant(&args[0]);
575 std::vector<std::string> iargs;
576 iargs.push_back(str);
577 str = plugin::ExternalInterface::makeInvoke("Zoom", iargs);
579 // Write the message to the Control FD.
580 size_t ret = gpso->writePlayer(str);
581 // Unless we wrote the same amount of data as the message contained,
582 // something went wrong.
583 if (ret != str.size()) {
584 log_error("Couldn't zoom movie, network problems.");
585 return false;
587 BOOLEAN_TO_NPVARIANT(true, *result);
588 return true;
591 BOOLEAN_TO_NPVARIANT(false, *result);
592 return false;
595 // TotalFrames()
597 // Sends something like this:
598 // <invoke name="TotalFrames" returntype="xml">
599 // <arguments></arguments>
600 // </invoke>
602 // Receives:
603 // <number>66</number>
604 bool
605 TotalFrames (NPObject *npobj, NPIdentifier /* name */, const NPVariant */*args */,
606 uint32_t argCount, NPVariant *result)
608 log_debug(__PRETTY_FUNCTION__);
610 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
612 if (argCount == 0) {
613 std::vector<std::string> iargs;
614 std::string str = plugin::ExternalInterface::makeInvoke("TotalFrames", iargs);
616 // Write the message to the Control FD.
617 size_t ret = gpso->writePlayer(str);
618 // Unless we wrote the same amount of data as the message contained,
619 // something went wrong.
620 if (ret != str.size()) {
621 log_error("Couldn't check percent loaded, network problems.");
622 BOOLEAN_TO_NPVARIANT(false, *result);
623 return false;
625 std::string data = gpso->readPlayer();
626 if (data.empty()) {
627 BOOLEAN_TO_NPVARIANT(false, *result);
628 return false;
631 GnashNPVariant value = plugin::ExternalInterface::parseXML(data);
632 if (NPVARIANT_IS_INT32(value.get())) {
633 value.copy(*result);
634 } else {
635 INT32_TO_NPVARIANT(0, *result);
638 return true;
641 BOOLEAN_TO_NPVARIANT(false, *result);
642 return false;
645 // Sends something like this:
646 // <invoke name="TestASMethod" returntype="xml">
647 // <arguments>
648 // <number>123</number>
649 // </arguments>
650 // </invoke>
652 // Receives:
653 // An XML response of one of the standard types like Number or String.
654 bool
655 remoteCallback (NPObject *npobj, NPIdentifier name, const NPVariant *args,
656 uint32_t argCount, NPVariant *result)
658 // log_debug(__PRETTY_FUNCTION__);
660 GnashPluginScriptObject *gpso = (GnashPluginScriptObject *)npobj;
662 std::string method;
664 #if 1
665 if (NPN_IdentifierIsString(name)) {
666 log_debug("Invoking remote Method \"%s\"...",
667 NPN_UTF8FromIdentifier(name));
668 method = NPN_UTF8FromIdentifier(name);
669 } else {
670 log_debug("Invoking remote Method: \"%d\"...",
671 NPN_IntFromIdentifier(name));
673 #endif
675 // Build the argument array
676 std::vector<std::string> fnargs;
677 for (uint32_t i=0; i<argCount; ++i) {
678 std::string xml = plugin::ExternalInterface::convertNPVariant(&args[i]);
679 fnargs.push_back(xml);
683 std::string str = plugin::ExternalInterface::makeInvoke(method, fnargs);
685 // Write the message to the Control FD.
686 size_t ret = gpso->writePlayer(str);
687 // Unless we wrote the same amount of data as the message contained,
688 // something went wrong.
689 if (ret != str.size()) {
690 log_error("Couldn't invoke %s, network problems.", method);
691 return false;
694 // Have the read function allocate the memory
695 std::string data = gpso->readPlayer();
696 if (data.empty()) {
697 log_error("Couldn't read a response for invoke, network problems.");
698 NULL_TO_NPVARIANT(*result);
699 return false;
702 std::string answer;
703 GnashNPVariant parsed = plugin::ExternalInterface::parseXML(data);
704 if (!NPVARIANT_IS_NULL(parsed.get())) {
705 answer = NPStringToString(NPVARIANT_TO_STRING(parsed.get()));
707 if (answer == "Error") {
708 NULL_TO_NPVARIANT(*result);
709 } else if (answer == "SecurityError") {
710 NULL_TO_NPVARIANT(*result);
711 } else {
712 parsed.copy(*result);
715 // printNPVariant(&parsed.get());
717 // Returning false makes Javascript stop executing the script.
718 return true;
721 } // end of gnash namespace
723 // local Variables:
724 // mode: C++
725 // indent-tabs-mode: nil
726 // End: