Gnash  0.8.11dev
pluginScriptObject.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2010, 2011, 2012 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.
13 //
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
17 //
18 
19 
20 // Test:
21 // Use browser to open plugin/scriptable-test.html.
22 //
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.
30 //
31 // Todo:
32 // Support the methods listed in
33 // http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html
34 
35 #ifndef GNASH_PLUGIN_SCRIPT_OBJECT_H
36 #define GNASH_PLUGIN_SCRIPT_OBJECT_H
37 
38 #include <map>
39 #include <string>
40 
41 #include <glib.h>
42 #include "npapi.h"
43 #include "npruntime.h"
44 #include "GnashNPVariant.h"
45 
46 #define READFD 0
47 #define WRITEFD 1
48 
49 namespace gnash {
50 
54 void
55 CopyVariantValue(const NPVariant& from, NPVariant& to);
56 
57 class GnashPluginScriptObject : public NPObject
58 {
59 public:
60 
62  GnashPluginScriptObject(NPP npp);
64 
65  static NPClass *marshalGetNPClass();
66 
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);
88 
89  static NPClass _npclass;
90 
93 
97  void setControlFD(int x);
98  int getControlFD();
99 
103  void setHostFD(int x);
104  int getHostFD();
105 
113  bool SetVariable(const std::string &name, const NPVariant& value);
114 
120  GnashNPVariant GetVariable(const std::string &name);
121 
122 
123  int getReadFD() { return _hostfd; };
124  int getWriteFD() { return _controlfd; };
125 
126  // Write to the standalone player over the control socket
127  int writePlayer(const std::string &data);
128  int writePlayer(int fd, const std::string &data);
129 
130  // Read the standalone player over the control socket
131  std::string readPlayer();
132  std::string readPlayer(int fd);
133 
134  bool Invoke(NPObject *npobj, NPIdentifier name, const NPVariant *args,
135  uint32_t argCount, NPVariant *result);
136  bool AddMethod(NPIdentifier name, NPInvokeFunctionPtr func);
137  void AddProperty(const std::string &name, const std::string &str);
138  void AddProperty(const std::string &name, double num);
139  void AddProperty(const std::string &name, int num);
140 
141 protected:
142  // Internal functions for the API
143  void Deallocate();
144  void Invalidate();
145  bool HasMethod(NPIdentifier name);
146 
147  bool InvokeDefault(const NPVariant *args, uint32_t argCount,
148  NPVariant *result);
149  bool HasProperty(NPIdentifier name);
150  bool GetProperty(NPIdentifier name, NPVariant *result);
151  bool SetProperty(NPIdentifier name, const NPVariant& value);
152  bool RemoveProperty(NPIdentifier name);
153  bool Enumerate(NPIdentifier **identifier, uint32_t *count);
154  bool Construct(const NPVariant *data, uint32_t argCount, NPVariant *result);
155 
156 private:
157  void initializeIdentifiers();
158  void setInstance(NPP inst) { _nppinstance = inst; };
159 
160  // _nppinstance->pdata should be the nsPluginInstance once NPP_New() is finished.
161  NPP _nppinstance;
162 
163  std::map<NPIdentifier, GnashNPVariant> _properties;
164  std::map<NPIdentifier, NPInvokeFunctionPtr> _methods;
165  // 0 for reading, 1 for writing
166 
170  int _controlfd;
174  int _hostfd;
175 };
176 
177 } // end of gnash namespace
178 
179 #endif // GNASH_PLUGIN_SCRIPT_OBJECT_H
180 
181 // local Variables:
182 // mode: C++
183 // indent-tabs-mode: nil
184 // End:
int getReadFD()
Definition: pluginScriptObject.h:123
bool Invoke(NPObject *npobj, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result)
Definition: pluginScriptObject.cpp:508
static bool marshalConstruct(NPObject *npobj, const NPVariant *data, uint32_t count, NPVariant *result)
Definition: pluginScriptObject.cpp:407
static NPClass * marshalGetNPClass()
Definition: pluginScriptObject.cpp:294
~GnashPluginScriptObject()
Definition: pluginScriptObject.cpp:287
GnashNPVariant GetVariable(const std::string &name)
Definition: pluginScriptObject.cpp:605
bool GetProperty(NPIdentifier name, NPVariant *result)
Definition: pluginScriptObject.cpp:434
int getControlFD()
Definition: pluginScriptObject.cpp:653
SimpleBuffer data
Definition: LocalConnection_as.cpp:153
static NPClass _npclass
Definition: pluginScriptObject.h:89
static bool marshalInvoke(NPObject *npobj, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result)
Definition: pluginScriptObject.cpp:342
static bool marshalHasMethod(NPObject *npobj, NPIdentifier name)
Definition: pluginScriptObject.cpp:323
Definition: pluginScriptObject.h:57
std::string readPlayer()
Definition: pluginScriptObject.cpp:698
bool SetProperty(NPIdentifier name, const NPVariant &value)
Definition: pluginScriptObject.cpp:455
void AddProperty(const std::string &name, const std::string &str)
Definition: pluginScriptObject.cpp:107
static bool marshalHasProperty(NPObject *npobj, NPIdentifier name)
Definition: pluginScriptObject.cpp:363
int writePlayer(const std::string &data)
Definition: pluginScriptObject.cpp:678
int getHostFD()
Definition: pluginScriptObject.cpp:668
bool HasMethod(NPIdentifier name)
Definition: pluginScriptObject.cpp:493
int getWriteFD()
Definition: pluginScriptObject.h:124
boost::int32_t x
Definition: BitmapData_as.cpp:434
bool Construct(const NPVariant *data, uint32_t argCount, NPVariant *result)
Definition: pluginScriptObject.cpp:484
bool Enumerate(NPIdentifier **identifier, uint32_t *count)
Definition: pluginScriptObject.cpp:476
bool AddMethod(NPIdentifier name, NPInvokeFunctionPtr func)
Definition: pluginScriptObject.cpp:557
static void marshalInvalidate(NPObject *npobj)
Definition: pluginScriptObject.cpp:316
void setHostFD(int x)
Definition: pluginScriptObject.cpp:661
bool RemoveProperty(NPIdentifier name)
Definition: pluginScriptObject.cpp:463
bool HasProperty(NPIdentifier name)
Definition: pluginScriptObject.cpp:419
static bool marshalEnumerate(NPObject *npobj, void ***identifier, uint32_t *count)
Definition: pluginScriptObject.cpp:395
bool InvokeDefault(const NPVariant *args, uint32_t argCount, NPVariant *result)
Definition: pluginScriptObject.cpp:540
static bool marshalInvokeDefault(NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
Definition: pluginScriptObject.cpp:352
static bool marshalSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
Definition: pluginScriptObject.cpp:380
bool SetVariable(const std::string &name, const NPVariant &value)
Definition: pluginScriptObject.cpp:577
void setControlFD(int x)
Definition: pluginScriptObject.cpp:646
static NPObject * marshalAllocate(NPP npp, NPClass *aClass)
Definition: pluginScriptObject.cpp:301
static bool marshalRemoveProperty(NPObject *npobj, NPIdentifier name)
Definition: pluginScriptObject.cpp:388
This class holds ownership of (a copy of) an NPVariant.
Definition: GnashNPVariant.h:115
static void marshalDeallocate(NPObject *npobj)
Definition: pluginScriptObject.cpp:309
void CopyVariantValue(const NPVariant &from, NPVariant &to)
Definition: GnashNPVariant.h:55
GnashPluginScriptObject()
Definition: pluginScriptObject.cpp:263
std::string name
Definition: LocalConnection_as.cpp:151
static bool marshalGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
Definition: pluginScriptObject.cpp:371