2 // Copyright (C) 2010, 2011 Free Software Foundation, Inc
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.
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
20 #include "gnashconfig.h"
32 #include "npruntime.h"
34 #include "pluginScriptObject.h"
38 // http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_04.html
39 // Tell Target Methods. Use TGetProperty, TGetPropertyAsNumber, TSetProperty
56 CURRENT_FRAME
// read-only
57 TOTAL_FRAMES
// read-only
64 FRAMES_LOADED
// read-only
66 DROP_TARGET
// read-only
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">
91 // <string>var1</string>
92 // <string>value1</string>
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
;
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
);
116 BOOLEAN_TO_NPVARIANT(false, *result
);
120 // GetVariable( Name )
122 // Sends something like this:
123 // <invoke name="GetVariable" returntype="xml">
125 // <string>var1</string>
129 // Receives something like this:
130 // <number>123</number>
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
;
140 // This method only takes one argument
142 varname
= NPStringToString(NPVARIANT_TO_STRING(args
[0]));
144 GnashNPVariant value
= gpso
->GetVariable(varname
);
150 NULL_TO_NPVARIANT(*result
);
154 // GotoFrame( frameNumber )
156 // Sends something like this:
157 // <invoke name="GotoFrame" returntype="xml">
159 // <number>123</number>
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
;
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.");
188 // gpso->GotoFrame(value);
189 BOOLEAN_TO_NPVARIANT(true, *result
);
193 BOOLEAN_TO_NPVARIANT(false, *result
);
200 // <invoke name="IsPlaying" returntype="xml">
201 // <arguments></arguments>
204 // Receives something like this:
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
;
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
);
227 std::string data
= gpso
->readPlayer();
229 BOOLEAN_TO_NPVARIANT(false, *result
);
233 GnashNPVariant value
= plugin::ExternalInterface::parseXML(data
);
234 if (NPVARIANT_TO_BOOLEAN(value
.get()) == true) {
235 BOOLEAN_TO_NPVARIANT(true, *result
);
237 BOOLEAN_TO_NPVARIANT(false, *result
);
243 BOOLEAN_TO_NPVARIANT(false, *result
);
247 // LoadMovie( Layer, Url )
249 // Sends something like this:
250 // <invoke name="LoadMovie" returntype="xml">
252 // <number>2</number>
253 // <string>bogus</string>
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
;
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.");
285 // gpso->LoadMovie();
286 BOOLEAN_TO_NPVARIANT(true, *result
);
290 BOOLEAN_TO_NPVARIANT(false, *result
);
294 // Pan ( x, y, mode )
296 // Sends something like this:
297 // <invoke name="Pan" returntype="xml">
299 // <number>1</number>
300 // <number>2</number>
301 // <number>0</number>
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
;
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.");
333 BOOLEAN_TO_NPVARIANT(true, *result
);
337 BOOLEAN_TO_NPVARIANT(false, *result
);
344 // <invoke name="PercentLoaded" returntype="xml">
345 // <arguments></arguments>
348 // Receives something like this:
349 // <number>33</number>
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
;
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
);
371 std::string data
= gpso
->readPlayer();
373 BOOLEAN_TO_NPVARIANT(false, *result
);
377 GnashNPVariant value
= plugin::ExternalInterface::parseXML(data
);
378 if (NPVARIANT_IS_INT32(value
.get())) {
379 INT32_TO_NPVARIANT(NPVARIANT_TO_INT32(value
.get()), *result
);
381 INT32_TO_NPVARIANT(0, *result
);
387 BOOLEAN_TO_NPVARIANT(false, *result
);
394 // <invoke name="Play" returntype="xml">
395 // <arguments></arguments>
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
;
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.");
420 // gpso->IsPlaying(value);
421 BOOLEAN_TO_NPVARIANT(true, *result
);
425 BOOLEAN_TO_NPVARIANT(false, *result
);
432 // <invoke name="Rewind" returntype="xml">
433 // <arguments></arguments>
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
;
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.");
458 // gpso->Rewind(value);
459 BOOLEAN_TO_NPVARIANT(true, *result
);
463 BOOLEAN_TO_NPVARIANT(false, *result
);
467 // SetZoomRect ( left, top, right, bottom )
468 // Sends something like this:
469 // <invoke name="SetZoomRect" returntype="xml">
471 // <number>1</number>
472 // <number>2</number>
473 // <number>3</number>
474 // <number>4</number>
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
;
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.");
508 BOOLEAN_TO_NPVARIANT(true, *result
);
512 BOOLEAN_TO_NPVARIANT(false, *result
);
519 // <invoke name="StopPlay" returntype="xml">
520 // <arguments></arguments>
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
;
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.");
545 // gpso->IsPlaying(value);
546 BOOLEAN_TO_NPVARIANT(true, *result
);
550 BOOLEAN_TO_NPVARIANT(false, *result
);
556 // Sends something like this:
557 // <invoke name="Zoom" returntype="xml">
559 // <number>12</number>
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
;
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.");
587 BOOLEAN_TO_NPVARIANT(true, *result
);
591 BOOLEAN_TO_NPVARIANT(false, *result
);
597 // Sends something like this:
598 // <invoke name="TotalFrames" returntype="xml">
599 // <arguments></arguments>
603 // <number>66</number>
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
;
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
);
625 std::string data
= gpso
->readPlayer();
627 BOOLEAN_TO_NPVARIANT(false, *result
);
631 GnashNPVariant value
= plugin::ExternalInterface::parseXML(data
);
632 if (NPVARIANT_IS_INT32(value
.get())) {
635 INT32_TO_NPVARIANT(0, *result
);
641 BOOLEAN_TO_NPVARIANT(false, *result
);
645 // Sends something like this:
646 // <invoke name="TestASMethod" returntype="xml">
648 // <number>123</number>
653 // An XML response of one of the standard types like Number or String.
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
;
665 if (NPN_IdentifierIsString(name
)) {
666 log_debug("Invoking remote Method \"%s\"...",
667 NPN_UTF8FromIdentifier(name
));
668 method
= NPN_UTF8FromIdentifier(name
);
670 log_debug("Invoking remote Method: \"%d\"...",
671 NPN_IntFromIdentifier(name
));
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
);
694 // Have the read function allocate the memory
695 std::string data
= gpso
->readPlayer();
697 log_error("Couldn't read a response for invoke, network problems.");
698 NULL_TO_NPVARIANT(*result
);
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
);
712 parsed
.copy(*result
);
715 // printNPVariant(&parsed.get());
717 // Returning false makes Javascript stop executing the script.
721 } // end of gnash namespace
725 // indent-tabs-mode: nil