forked from mikma/npsimple
-
Notifications
You must be signed in to change notification settings - Fork 1
/
npsimple.c
290 lines (253 loc) · 7.15 KB
/
npsimple.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* ***** BEGIN LICENSE BLOCK *****
* (C)opyright 2008-2009 Aplix Corporation. [email protected]
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http:https://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
* ***** END LICENSE BLOCK ***** */
#include <stdio.h>
#include <string.h>
#if defined(XULRUNNER_SDK)
#include <npapi.h>
#include <npupp.h>
#include <npruntime.h>
#elif defined(ANDROID)
#undef HAVE_LONG_LONG
#include <jni.h>
#include <npapi.h>
#include <npfunctions.h>
#include <npruntime.h>
#define OSCALL
#define NPP_WRITE_TYPE (NPP_WriteProcPtr)
#define NPStringText UTF8Characters
#define NPStringLen UTF8Length
extern JNIEnv *pluginJniEnv;
typedef int16_t int16;
typedef uint16_t uint16;
typedef uint32_t uint32;
#undef STRINGN_TO_NPVARIANT
#define STRINGN_TO_NPVARIANT(_val, _len, _v) \
NP_BEGIN_MACRO \
(_v).type = NPVariantType_String; \
NPString str = { _val, (uint32_t)(_len) }; \
(_v).value.stringValue = str; \
NP_END_MACRO
#elif defined(WEBKIT_DARWIN_SDK)
#include <Webkit/npapi.h>
#include <WebKit/npfunctions.h>
#include <WebKit/npruntime.h>
#define OSCALL
#elif defined(WEBKIT_WINMOBILE_SDK) /* WebKit SDK on Windows */
#ifndef PLATFORM
#define PLATFORM(x) defined(x)
#endif
#include <npfunctions.h>
#ifndef OSCALL
#define OSCALL WINAPI
#endif
#endif
static NPObject *so = NULL;
static NPNetscapeFuncs *npnfuncs = NULL;
static NPP inst = NULL;
/* NPN */
static void logmsg(const char *msg) {
#if defined(ANDROID)
FILE *out = fopen("/sdcard/npsimple.log", "a");
if(out) {
fputs(msg, out);
fclose(out);
}
#elif !defined(_WINDOWS)
fputs(msg, stderr);
#else
FILE *out = fopen("\\npsimple.log", "a");
if(out) {
fputs(msg, out);
fclose(out);
}
#endif
}
static bool
hasMethod(NPObject* obj, NPIdentifier methodName) {
logmsg("npsimple: hasMethod\n");
return true;
}
static bool
invokeDefault(NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result) {
logmsg("npsimple: invokeDefault\n");
result->type = NPVariantType_Int32;
result->value.intValue = 42;
return true;
}
static bool
invoke(NPObject* obj, NPIdentifier methodName, const NPVariant *args, uint32_t argCount, NPVariant *result) {
logmsg("npsimple: invoke\n");
int error = 1;
char *name = npnfuncs->utf8fromidentifier(methodName);
if(name) {
if(!strcmp(name, "foo")) {
logmsg("npsimple: invoke foo\n");
return invokeDefault(obj, args, argCount, result);
}
else if(!strcmp(name, "callback")) {
if(argCount == 1 && args[0].type == NPVariantType_Object) {
static NPVariant v, r;
const char kHello[] = "Hello";
char *txt = (char *)npnfuncs->memalloc(strlen(kHello));
logmsg("npsimple: invoke callback function\n");
memcpy(txt, kHello, strlen(kHello));
STRINGN_TO_NPVARIANT(txt, strlen(kHello), v);
/* INT32_TO_NPVARIANT(42, v); */
if(npnfuncs->invokeDefault(inst, NPVARIANT_TO_OBJECT(args[0]), &v, 1, &r))
return invokeDefault(obj, args, argCount, result);
}
}
}
// aim exception handling
npnfuncs->setexception(obj, "exception during invocation");
return false;
}
static bool
hasProperty(NPObject *obj, NPIdentifier propertyName) {
logmsg("npsimple: hasProperty\n");
return false;
}
static bool
getProperty(NPObject *obj, NPIdentifier propertyName, NPVariant *result) {
logmsg("npsimple: getProperty\n");
return false;
}
static NPClass npcRefObject = {
NP_CLASS_STRUCT_VERSION,
NULL,
NULL,
NULL,
hasMethod,
invoke,
invokeDefault,
hasProperty,
getProperty,
NULL,
NULL,
};
/* NPP */
static NPError
nevv(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char *argn[], char *argv[], NPSavedData *saved) {
inst = instance;
logmsg("npsimple: new\n");
return NPERR_NO_ERROR;
}
static NPError
destroy(NPP instance, NPSavedData **save) {
if(so)
npnfuncs->releaseobject(so);
so = NULL;
logmsg("npsimple: destroy\n");
return NPERR_NO_ERROR;
}
static NPError
getValue(NPP instance, NPPVariable variable, void *value) {
inst = instance;
switch(variable) {
default:
logmsg("npsimple: getvalue - default\n");
return NPERR_GENERIC_ERROR;
case NPPVpluginNameString:
logmsg("npsimple: getvalue - name string\n");
*((char **)value) = "AplixFooPlugin";
break;
case NPPVpluginDescriptionString:
logmsg("npsimple: getvalue - description string\n");
*((char **)value) = "<a href=\"http:https://www.aplix.co.jp/\">AplixFooPlugin</a> plugin.";
break;
case NPPVpluginScriptableNPObject:
logmsg("npsimple: getvalue - scriptable object\n");
if(!so)
so = npnfuncs->createobject(instance, &npcRefObject);
npnfuncs->retainobject(so);
*(NPObject **)value = so;
break;
#if defined(XULRUNNER_SDK)
case NPPVpluginNeedsXEmbed:
logmsg("npsimple: getvalue - xembed\n");
*((PRBool *)value) = PR_FALSE;
break;
#endif
}
return NPERR_NO_ERROR;
}
static NPError /* expected by Safari on Darwin */
handleEvent(NPP instance, void *ev) {
inst = instance;
logmsg("npsimple: handleEvent\n");
return NPERR_NO_ERROR;
}
static NPError /* expected by Opera */
setWindow(NPP instance, NPWindow* pNPWindow) {
inst = instance;
logmsg("npsimple: setWindow\n");
return NPERR_NO_ERROR;
}
/* EXPORT */
#ifdef __cplusplus
extern "C" {
#endif
NPError OSCALL
NP_GetEntryPoints(NPPluginFuncs *nppfuncs) {
logmsg("npsimple: NP_GetEntryPoints\n");
nppfuncs->version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
nppfuncs->newp = nevv;
nppfuncs->destroy = destroy;
nppfuncs->getvalue = getValue;
nppfuncs->event = handleEvent;
nppfuncs->setwindow = setWindow;
return NPERR_NO_ERROR;
}
#ifndef HIBYTE
#define HIBYTE(x) ((((uint32)(x)) & 0xff00) >> 8)
#endif
NPError OSCALL
NP_Initialize(NPNetscapeFuncs *npnf
#if defined(ANDROID)
, NPPluginFuncs *nppfuncs, JNIEnv *env, jobject plugin_object
#elif !defined(_WINDOWS) && !defined(WEBKIT_DARWIN_SDK)
, NPPluginFuncs *nppfuncs
#endif
)
{
logmsg("npsimple: NP_Initialize\n");
if(npnf == NULL)
return NPERR_INVALID_FUNCTABLE_ERROR;
if(HIBYTE(npnf->version) > NP_VERSION_MAJOR)
return NPERR_INCOMPATIBLE_VERSION_ERROR;
npnfuncs = npnf;
#if !defined(_WINDOWS) && !defined(WEBKIT_DARWIN_SDK)
NP_GetEntryPoints(nppfuncs);
#endif
return NPERR_NO_ERROR;
}
NPError
OSCALL NP_Shutdown() {
logmsg("npsimple: NP_Shutdown\n");
return NPERR_NO_ERROR;
}
char *
NP_GetMIMEDescription(void) {
logmsg("npsimple: NP_GetMIMEDescription\n");
return "application/x-vnd-aplix-foo:.foo:[email protected]";
}
NPError OSCALL /* needs to be present for WebKit based browsers */
NP_GetValue(void *npp, NPPVariable variable, void *value) {
inst = (NPP)npp;
return getValue((NPP)npp, variable, value);
}
#ifdef __cplusplus
}
#endif