• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

pluginScriptObject.h

Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2010 Free Software Foundation, Inc
00003 // 
00004 // This program is free software; you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation; either version 3 of the License, or
00007 // (at your option) any later version.
00008 // 
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017 //
00018 
00019 
00020 // Test:
00021 //     Use browser to open plugin/scriptable-test.html.
00022 //
00023 // Notes:
00024 // 1. In order not to rewrite the whole Plugin, just be _scriptObject 
00025 //    of nsPluginInstance.
00026 // 2. GnashPluginScriptObject is the marshal object to response 
00027 //    JavaScript, no function to gtk-gnash yet.
00028 // 3. Once gnash::Player support multiple instance, it's possible to 
00029 //    the bridge between JavaScript and ActionScript.
00030 //
00031 // Todo:
00032 //    Support the methods listed in
00033 //    http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html
00034 
00035 #ifndef GNASH_PLUGIN_SCRIPT_OBJECT_H
00036 #define GNASH_PLUGIN_SCRIPT_OBJECT_H
00037 
00038 #include <map>
00039 #include <string>
00040 
00041 #include <glib.h>
00042 #include "npapi.h"
00043 #include "npruntime.h"
00044 #include "GnashNPVariant.h"
00045 
00046 #define READFD 0
00047 #define WRITEFD 1
00048 
00049 namespace gnash {
00050 
00054 void
00055 CopyVariantValue(const NPVariant& from, NPVariant& to);
00056 
00057 class GnashPluginScriptObject : public NPObject
00058 {
00059 public:
00060 
00061     GnashPluginScriptObject();
00062     GnashPluginScriptObject(NPP npp);
00063     ~GnashPluginScriptObject();
00064     
00065     static NPClass *marshalGetNPClass();
00066 
00067     // NPObject static Functions. These get used by the browser, which is
00068     // why they have to be static.
00069     static NPObject *marshalAllocate (NPP npp, NPClass *aClass);
00070     static void marshalDeallocate (NPObject *npobj);
00071     static void marshalInvalidate (NPObject *npobj);
00072     static bool marshalHasMethod (NPObject *npobj, NPIdentifier name);
00073     static bool marshalInvoke (NPObject *npobj, NPIdentifier name,
00074                                const NPVariant *args, uint32_t argCount,
00075                                NPVariant *result);
00076     static bool marshalInvokeDefault (NPObject *npobj, const NPVariant *args,
00077                                       uint32_t argCount, NPVariant *result);
00078     static bool marshalHasProperty (NPObject *npobj, NPIdentifier name);
00079     static bool marshalGetProperty (NPObject *npobj, NPIdentifier name,
00080                                     NPVariant *result);
00081     static bool marshalSetProperty (NPObject *npobj, NPIdentifier name,
00082                                     const NPVariant *value);
00083     static bool marshalRemoveProperty (NPObject *npobj, NPIdentifier name);
00084     static bool marshalEnumerate (NPObject *npobj, void***identifier,
00085                                   uint32_t *count);
00086     static bool marshalConstruct (NPObject *npobj, const NPVariant *data,
00087                                   uint32_t count, NPVariant *result);
00088     
00089     static NPClass _npclass;
00090 
00093 
00097     void setControlFD(int x);
00098     int getControlFD();
00099 
00103     void setHostFD(int x);
00104     int getHostFD();
00105 
00113     bool SetVariable(const std::string &name, const NPVariant& value);
00114     
00120     GnashNPVariant GetVariable(const std::string &name);
00121 
00122     // Create a socketpair so we can talk to the player.
00123     bool createPipe();
00124     
00125     // Close the socket
00126     bool closePipe();
00127     bool closePipe(int fd);
00128 
00129     // Check the pipe to see if it's ready, ie... is gnash connected yet ?
00130     bool checkPipe();
00131     bool checkPipe(int fd);
00132 
00133     int getReadFD()  { return _sockfds[READFD]; };
00134     GIOChannel *getReadChannel()  { return _iochan[READFD]; };
00135     int getWriteFD() { return _sockfds[WRITEFD]; };
00136     GIOChannel *getWriteChannel() { return _iochan[WRITEFD]; };
00137 
00138     // Write to the standalone player over the control socket
00139     int writePlayer(const std::string &data);
00140     int writePlayer(int fd, const std::string &data);
00141     
00142     // Read the standalone player over the control socket
00143     std::string readPlayer();
00144     std::string readPlayer(int fd);
00145     
00146     bool Invoke(NPObject *npobj, NPIdentifier name, const NPVariant *args,
00147                 uint32_t argCount, NPVariant *result);
00148     bool AddMethod(NPIdentifier name, NPInvokeFunctionPtr func);
00149     void AddProperty(const std::string &name, const std::string &str);
00150     void AddProperty(const std::string &name, double num);
00151     void AddProperty(const std::string &name, int num);
00152 
00153 protected:
00154     // Internal functions for the API
00155     void Deallocate();
00156     void Invalidate();
00157     bool HasMethod(NPIdentifier name);
00158 
00159     bool InvokeDefault(const NPVariant *args, uint32_t argCount,
00160                        NPVariant *result);
00161     bool HasProperty(NPIdentifier name);
00162     bool GetProperty(NPIdentifier name, NPVariant *result);
00163     bool SetProperty(NPIdentifier name, const NPVariant& value);
00164     bool RemoveProperty(NPIdentifier name);
00165     bool Enumerate(NPIdentifier **identifier, uint32_t *count);
00166     bool Construct(const NPVariant *data, uint32_t argCount, NPVariant *result);
00167 
00168     bool handleInvoke(GIOChannel *iochan, GIOCondition cond);
00169     
00171     //
00182     bool processPlayerRequest(gchar* buf, gsize len);
00183 private:
00184     static bool handleInvokeWrapper(GIOChannel* iochan, GIOCondition cond,
00185                                      GnashPluginScriptObject* plugin);
00186 
00187     void initializeIdentifiers();
00188     void setInstance(NPP inst) { _nppinstance = inst; };
00189     
00190     // _nppinstance->pdata should be the nsPluginInstance once NPP_New() is finished.
00191     NPP _nppinstance;
00192     
00193     std::map<NPIdentifier, GnashNPVariant> _properties;
00194     std::map<NPIdentifier, NPInvokeFunctionPtr> _methods;
00195     // 0 for reading, 1 for writing
00196     int         _sockfds[2];
00197     GIOChannel *_iochan[2];
00198     // ID to watch the read channel from the player
00199     int         _watchid;
00200 };
00201 
00202 } // end of gnash namespace
00203 
00204 #endif // GNASH_PLUGIN_SCRIPT_OBJECT_H
00205 
00206 // local Variables:
00207 // mode: C++
00208 // indent-tabs-mode: nil
00209 // End:

Generated on Thu Sep 30 2010 14:35:01 for Gnash by  doxygen 1.7.1