Gnash  0.8.11dev
pluginScriptObject.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2010, 2011, 2012, 2016 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 
142  // nppinstance->pdata should be the nsPluginInstance once NPP_New() is finished.
144 
145 protected:
146  // Internal functions for the API
147  void Deallocate();
148  void Invalidate();
149  bool HasMethod(NPIdentifier name);
150 
151  bool InvokeDefault(const NPVariant *args, uint32_t argCount,
152  NPVariant *result);
153  bool HasProperty(NPIdentifier name);
154  bool GetProperty(NPIdentifier name, NPVariant *result);
155  bool SetProperty(NPIdentifier name, const NPVariant& value);
156  bool RemoveProperty(NPIdentifier name);
157  bool Enumerate(NPIdentifier **identifier, uint32_t *count);
158  bool Construct(const NPVariant *data, uint32_t argCount, NPVariant *result);
159 
160 private:
161  void initializeIdentifiers();
162  void setInstance(NPP inst) { nppinstance = inst; };
163 
164  std::map<NPIdentifier, GnashNPVariant> _properties;
165  std::map<NPIdentifier, NPInvokeFunctionPtr> _methods;
166  // 0 for reading, 1 for writing
167 
171  int _controlfd;
175  int _hostfd;
176 };
177 
178 } // end of gnash namespace
179 
180 #endif // GNASH_PLUGIN_SCRIPT_OBJECT_H
181 
182 // local Variables:
183 // mode: C++
184 // indent-tabs-mode: nil
185 // End:
int getReadFD()
Definition: pluginScriptObject.h:123
bool Invoke(NPObject *npobj, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result)
Definition: pluginScriptObject.cpp:494
static bool marshalConstruct(NPObject *npobj, const NPVariant *data, uint32_t count, NPVariant *result)
Definition: pluginScriptObject.cpp:393
static NPClass * marshalGetNPClass()
Definition: pluginScriptObject.cpp:280
~GnashPluginScriptObject()
Definition: pluginScriptObject.cpp:273
GnashNPVariant GetVariable(const std::string &name)
Definition: pluginScriptObject.cpp:591
bool GetProperty(NPIdentifier name, NPVariant *result)
Definition: pluginScriptObject.cpp:420
int getControlFD()
Definition: pluginScriptObject.cpp:639
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
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:328
static bool marshalHasMethod(NPObject *npobj, NPIdentifier name)
Definition: pluginScriptObject.cpp:309
Definition: pluginScriptObject.h:57
NPP nppinstance
Plugin instance object.
Definition: pluginScriptObject.h:143
std::string readPlayer()
Definition: pluginScriptObject.cpp:684
bool SetProperty(NPIdentifier name, const NPVariant &value)
Definition: pluginScriptObject.cpp:441
void AddProperty(const std::string &name, const std::string &str)
Definition: pluginScriptObject.cpp:93
static bool marshalHasProperty(NPObject *npobj, NPIdentifier name)
Definition: pluginScriptObject.cpp:349
int writePlayer(const std::string &data)
Definition: pluginScriptObject.cpp:664
int getHostFD()
Definition: pluginScriptObject.cpp:654
bool HasMethod(NPIdentifier name)
Definition: pluginScriptObject.cpp:479
int getWriteFD()
Definition: pluginScriptObject.h:124
std::int32_t x
Definition: BitmapData_as.cpp:434
bool Construct(const NPVariant *data, uint32_t argCount, NPVariant *result)
Definition: pluginScriptObject.cpp:470
bool Enumerate(NPIdentifier **identifier, uint32_t *count)
Definition: pluginScriptObject.cpp:462
bool AddMethod(NPIdentifier name, NPInvokeFunctionPtr func)
Definition: pluginScriptObject.cpp:543
static void marshalInvalidate(NPObject *npobj)
Definition: pluginScriptObject.cpp:302
void setHostFD(int x)
Definition: pluginScriptObject.cpp:647
bool RemoveProperty(NPIdentifier name)
Definition: pluginScriptObject.cpp:449
bool HasProperty(NPIdentifier name)
Definition: pluginScriptObject.cpp:405
static bool marshalEnumerate(NPObject *npobj, void ***identifier, uint32_t *count)
Definition: pluginScriptObject.cpp:381
bool InvokeDefault(const NPVariant *args, uint32_t argCount, NPVariant *result)
Definition: pluginScriptObject.cpp:526
static bool marshalInvokeDefault(NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result)
Definition: pluginScriptObject.cpp:338
static bool marshalSetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value)
Definition: pluginScriptObject.cpp:366
bool SetVariable(const std::string &name, const NPVariant &value)
Definition: pluginScriptObject.cpp:563
void setControlFD(int x)
Definition: pluginScriptObject.cpp:632
static NPObject * marshalAllocate(NPP npp, NPClass *aClass)
Definition: pluginScriptObject.cpp:287
static bool marshalRemoveProperty(NPObject *npobj, NPIdentifier name)
Definition: pluginScriptObject.cpp:374
This class holds ownership of (a copy of) an NPVariant.
Definition: GnashNPVariant.h:115
static void marshalDeallocate(NPObject *npobj)
Definition: pluginScriptObject.cpp:295
void CopyVariantValue(const NPVariant &from, NPVariant &to)
Definition: GnashNPVariant.h:55
GnashPluginScriptObject()
Definition: pluginScriptObject.cpp:249
std::string name
Definition: LocalConnection_as.cpp:149
static bool marshalGetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result)
Definition: pluginScriptObject.cpp:357