update copyright date
[gnash.git] / plugin / npapi / pluginScriptObject.h
blobab836d12c94d23023dd910b6fa2253bdfcb5307b
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
20 // Test:
21 // Use browser to open plugin/scriptable-test.html.
23 // Notes:
24 // 1. In order not to rewrite the whole Plugin, just be _scriptObject
25 // of nsPluginInstance.
26 // 2. GnashPluginScriptObject is the marshal object to response
27 // JavaScript, no function to gtk-gnash yet.
28 // 3. Once gnash::Player support multiple instance, it's possible to
29 // the bridge between JavaScript and ActionScript.
31 // Todo:
32 // Support the methods listed in
33 // http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html
35 #ifndef GNASH_PLUGIN_SCRIPT_OBJECT_H
36 #define GNASH_PLUGIN_SCRIPT_OBJECT_H
38 #include <map>
39 #include <string>
41 #include <glib.h>
42 #include "npapi.h"
43 #include "npruntime.h"
44 #include "GnashNPVariant.h"
46 #define READFD 0
47 #define WRITEFD 1
49 namespace gnash {
51 /// Makes a deep copy of a NPVariant.
52 /// @param from The source NPVariant to copy values from.
53 /// @param to The destination NPVariant.
54 void
55 CopyVariantValue(const NPVariant& from, NPVariant& to);
57 class GnashPluginScriptObject : public NPObject
59 public:
61 GnashPluginScriptObject();
62 GnashPluginScriptObject(NPP npp);
63 ~GnashPluginScriptObject();
65 static NPClass *marshalGetNPClass();
67 // NPObject static Functions. These get used by the browser, which is
68 // why they have to be static.
69 static NPObject *marshalAllocate (NPP npp, NPClass *aClass);
70 static void marshalDeallocate (NPObject *npobj);
71 static void marshalInvalidate (NPObject *npobj);
72 static bool marshalHasMethod (NPObject *npobj, NPIdentifier name);
73 static bool marshalInvoke (NPObject *npobj, NPIdentifier name,
74 const NPVariant *args, uint32_t argCount,
75 NPVariant *result);
76 static bool marshalInvokeDefault (NPObject *npobj, const NPVariant *args,
77 uint32_t argCount, NPVariant *result);
78 static bool marshalHasProperty (NPObject *npobj, NPIdentifier name);
79 static bool marshalGetProperty (NPObject *npobj, NPIdentifier name,
80 NPVariant *result);
81 static bool marshalSetProperty (NPObject *npobj, NPIdentifier name,
82 const NPVariant *value);
83 static bool marshalRemoveProperty (NPObject *npobj, NPIdentifier name);
84 static bool marshalEnumerate (NPObject *npobj, void***identifier,
85 uint32_t *count);
86 static bool marshalConstruct (NPObject *npobj, const NPVariant *data,
87 uint32_t count, NPVariant *result);
89 static NPClass _npclass;
91 /// Scripting API support. This is where all the protocol support
92 /// lives.
94 /// The ControlFD is the file descriptor for the socket connection
95 /// to the standalone player. This is used when writing to the
96 /// standalone player from this plugin.
97 void setControlFD(int x);
98 int getControlFD();
100 /// The HostFD is the file descriptor for the socket connection
101 /// to the standalone player. This is used by this plugin when reading
102 /// messages from the standalone player.
103 void setHostFD(int x);
104 int getHostFD();
106 /// Set a variable in the standalone player
108 /// @param name the name of the variable to set
110 /// @param value the value to set the variable to
112 /// @return true or false based on the status of the invoke call
113 bool SetVariable(const std::string &name, const NPVariant& value);
115 /// Get the value of a variable from the standalone player
117 /// @param name the name of the variable to set
119 /// @return the value as returned by the standalone player
120 GnashNPVariant GetVariable(const std::string &name);
122 // Create a socketpair so we can talk to the player.
123 bool createPipe();
125 // Close the socket
126 bool closePipe();
127 bool closePipe(int fd);
129 // Check the pipe to see if it's ready, ie... is gnash connected yet ?
130 bool checkPipe();
131 bool checkPipe(int fd);
133 int getReadFD() { return _sockfds[READFD]; };
134 GIOChannel *getReadChannel() { return _iochan[READFD]; };
135 int getWriteFD() { return _sockfds[WRITEFD]; };
136 GIOChannel *getWriteChannel() { return _iochan[WRITEFD]; };
138 // Write to the standalone player over the control socket
139 int writePlayer(const std::string &data);
140 int writePlayer(int fd, const std::string &data);
142 // Read the standalone player over the control socket
143 std::string readPlayer();
144 std::string readPlayer(int fd);
146 bool Invoke(NPObject *npobj, NPIdentifier name, const NPVariant *args,
147 uint32_t argCount, NPVariant *result);
148 bool AddMethod(NPIdentifier name, NPInvokeFunctionPtr func);
149 void AddProperty(const std::string &name, const std::string &str);
150 void AddProperty(const std::string &name, double num);
151 void AddProperty(const std::string &name, int num);
153 protected:
154 // Internal functions for the API
155 void Deallocate();
156 void Invalidate();
157 bool HasMethod(NPIdentifier name);
159 bool InvokeDefault(const NPVariant *args, uint32_t argCount,
160 NPVariant *result);
161 bool HasProperty(NPIdentifier name);
162 bool GetProperty(NPIdentifier name, NPVariant *result);
163 bool SetProperty(NPIdentifier name, const NPVariant& value);
164 bool RemoveProperty(NPIdentifier name);
165 bool Enumerate(NPIdentifier **identifier, uint32_t *count);
166 bool Construct(const NPVariant *data, uint32_t argCount, NPVariant *result);
168 bool handleInvoke(GIOChannel *iochan, GIOCondition cond);
170 /// Process a null-terminated request line
172 /// @param buf
173 /// The single request.
174 /// Caller is responsible for memory management, but give us
175 /// permission to modify the string.
177 /// @param len
178 /// Lenght of buffer.
180 /// @return true if the request was processed, false otherwise (bogus request..)
182 bool processPlayerRequest(gchar* buf, gsize len);
183 private:
184 static bool handleInvokeWrapper(GIOChannel* iochan, GIOCondition cond,
185 GnashPluginScriptObject* plugin);
187 void initializeIdentifiers();
188 void setInstance(NPP inst) { _nppinstance = inst; };
190 // _nppinstance->pdata should be the nsPluginInstance once NPP_New() is finished.
191 NPP _nppinstance;
193 std::map<NPIdentifier, GnashNPVariant> _properties;
194 std::map<NPIdentifier, NPInvokeFunctionPtr> _methods;
195 // 0 for reading, 1 for writing
196 int _sockfds[2];
197 GIOChannel *_iochan[2];
198 // ID to watch the read channel from the player
199 int _watchid;
202 } // end of gnash namespace
204 #endif // GNASH_PLUGIN_SCRIPT_OBJECT_H
206 // local Variables:
207 // mode: C++
208 // indent-tabs-mode: nil
209 // End: