Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/browser-plugin/glue.cpp
blob: 48562b46ed864f7d4df64564415905de43615c66 (plain)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
/* this file is part of evince, a gnome document viewer
 *
 *  Copyright (C) 2009 litl, LLC
 *
 * Evince is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Evince is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include <string.h>
#include <dlfcn.h>

#include "evince-view.h"
#include "plugin.h"

#ifndef HIBYTE
#define HIBYTE(x) ((((uint32_t)(x)) & 0xff00) >> 8)
#endif

#ifndef LOBYTE
#define LOBYTE(W) ((W) & 0xFF)
#endif

NPNetscapeFuncs NPNFuncs;

char *
NP_GetMIMEDescription (void)
{
	ev_init ();

	GString *desc = g_string_new (NULL);
	GList *all_types = ev_backends_manager_get_all_types_info ();

	for (GList *l = all_types; l; l = g_list_next (l)){
		EvTypeInfo *info = (EvTypeInfo *)l->data;
		const char *mime;
		int i = 0;

		while ((mime = info->mime_types[i++])) {
			g_string_append_printf (desc, "%s:%s:;",
						mime, info->desc);
		}
	}

	return g_string_free (desc, FALSE);
}

NPError
NP_Initialize (NPNetscapeFuncs *aNPNFuncs, NPPluginFuncs *aNPPFuncs)
{
#ifndef USE_PLUGIN_KEEP_IN_MEMORY
	/* Keep libevview in memory. NPPVpluginKeepLibraryInMemory is broken.
	   See https://bugzilla.mozilla.org/show_bug.cgi?id=359870 */

	GModule *module = g_module_open ("libevview", (GModuleFlags)0);
	if (module == NULL) {
		return NPERR_MODULE_LOAD_FAILED_ERROR;
	}
	g_module_make_resident (module);
	g_module_close (module);
#endif

	ev_init ();

	if (aNPNFuncs == NULL || aNPPFuncs == NULL) {
		return NPERR_INVALID_FUNCTABLE_ERROR;
	}

	if ((aNPNFuncs->version >> 8) > NP_VERSION_MAJOR) {
		return NPERR_INCOMPATIBLE_VERSION_ERROR;
	}

	if (aNPNFuncs->size < sizeof (NPNetscapeFuncs)) {
		return NPERR_INVALID_FUNCTABLE_ERROR;
	}

	if (aNPPFuncs->size < sizeof (NPPluginFuncs)) {
		return NPERR_INVALID_FUNCTABLE_ERROR;
	}

	memcpy (&NPNFuncs, aNPNFuncs, sizeof (NPNetscapeFuncs));
	NPNFuncs.size = sizeof (NPNetscapeFuncs);

	aNPPFuncs->size = sizeof (NPPluginFuncs);
	aNPPFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR;
	aNPPFuncs->newp = NewNPP_NewProc (NPP_New);
	aNPPFuncs->getvalue = NewNPP_GetValueProc (NPP_GetValue);
	aNPPFuncs->setvalue = NewNPP_SetValueProc (NPP_SetValue);
	aNPPFuncs->setwindow = NewNPP_SetWindowProc (NPP_SetWindow);
	aNPPFuncs->destroy = NewNPP_DestroyProc (NPP_Destroy);
	aNPPFuncs->newstream = NewNPP_NewStreamProc (NPP_NewStream);
	aNPPFuncs->destroystream = NewNPP_DestroyStreamProc (NPP_DestroyStream);
	aNPPFuncs->asfile = NewNPP_StreamAsFileProc (NPP_StreamAsFile);
	aNPPFuncs->writeready = NewNPP_WriteReadyProc (NPP_WriteReady);
	aNPPFuncs->write = NewNPP_WriteProc (NPP_Write);
	aNPPFuncs->print = NewNPP_PrintProc (NPP_Print);
	aNPPFuncs->event = NewNPP_HandleEventProc (NPP_HandleEvent);
	aNPPFuncs->urlnotify = NewNPP_URLNotifyProc (NPP_URLNotify);

	return NPERR_NO_ERROR;
}

NPError
NP_Shutdown (void)
{
	return NPERR_NO_ERROR;
}

NPError
NPP_NewStream (NPP instance, NPMIMEType type,
               NPStream* stream, NPBool seekable,
               uint16* stype)
{
	*stype = NP_ASFILEONLY;

	return NPERR_NO_ERROR;
}

NPError
NPP_DestroyStream (NPP instance, NPStream* stream, NPReason reason)
{
	return NPERR_NO_ERROR;
}

int32
NPP_WriteReady (NPP instance, NPStream* stream)
{
	return -1;
}

int32 NPP_Write (NPP instance, NPStream* stream,
                 int32 offset, int32 len, void* buffer)
{
	return -1;
}

void NPP_StreamAsFile (NPP instance, NPStream* stream, const char* fname)
{
	Plugin *plugin = reinterpret_cast<Plugin*> (instance->pdata);
	plugin->Load (fname);
}

void NPP_Print (NPP instance, NPPrint* platformPrint)
{
}

int16 NPP_HandleEvent (NPP instance, void* event)
{
	return FALSE;
}

void
NPP_URLNotify (NPP instance, const char* url, NPReason reason, void* notifyData)
{
}

NPError NPP_SetValue (NPP instance, NPNVariable variable, void *value)
{
	return NPERR_NO_ERROR;
}
 
NPError
NPP_New (NPMIMEType mimetype,
         NPP instance,
         uint16_t mode,
         int16_t argc,
         char *argn[],
         char *argv[],
         NPSavedData *savedData)
{
	Plugin *plugin = new Plugin (instance);
	instance->pdata = reinterpret_cast<void*> (plugin);

#ifdef USE_PLUGIN_KEEP_IN_MEMORY
	NPNFuncs.setvalue (instance, NPPVpluginKeepLibraryInMemory, (void *)PR_TRUE);
#endif

	return NPERR_NO_ERROR;
}

NPError
NPP_Destroy (NPP instance, NPSavedData** save)
{
	Plugin *plugin = reinterpret_cast<Plugin*> (instance->pdata);

	delete plugin;

	return NPERR_NO_ERROR;
}

NPError
NPP_GetValue(NPP instance, NPPVariable variable, void *value)
{
	NPError err = NPERR_NO_ERROR;
	Plugin *plugin = reinterpret_cast<Plugin*> (instance->pdata);

	switch (variable) {
		case NPPVpluginNeedsXEmbed:
			*(PRBool *)value = PR_TRUE;
			break;
        	case NPPVpluginScriptableNPObject:
            		*(NPObject **)value = plugin->GetScriptableNPObject ();
            		break;

		default:
			err = NPERR_GENERIC_ERROR;
	}

	return err;
}

NPError
NPP_SetWindow (NPP instance, NPWindow *window)
{
	Plugin *plugin = reinterpret_cast<Plugin*> (instance->pdata);
	plugin->SetWindow ((GdkNativeWindow)window->window);

	return NPERR_NO_ERROR;
}

void NPN_Version (int *plugin_major, int *plugin_minor, int *netscape_major,
                  int *netscape_minor)
{
	*plugin_major = NP_VERSION_MAJOR;
	*plugin_minor = NP_VERSION_MINOR;
	*netscape_major = HIBYTE (NPNFuncs.version);
	*netscape_minor = LOBYTE (NPNFuncs.version);
}

NPError NPN_GetURLNotify (NPP instance, const char *url,
                          const char *target, void *notifyData)
{
	return NPNFuncs.geturlnotify (instance, url, target, notifyData);
}

NPError NPN_GetURL (NPP instance, const char *url, const char *target)
{
	return NPNFuncs.geturl (instance, url, target);
}

NPError NPN_PostURLNotify (NPP instance, const char *url, const char *window,
                           uint32_t len, const char *buf, NPBool file,
                           void *notifyData)
{
	return NPNFuncs.posturlnotify (instance, url, window,
				       len, buf, file, notifyData);
}

NPError NPN_PostURL (NPP instance, const char *url, const char *window,
                     uint32_t len, const char *buf, NPBool file)
{
	return NPNFuncs.posturl (instance, url, window, len, buf, file);
} 

NPError NPN_RequestRead (NPStream* stream, NPByteRange* rangeList)
{
	return NPNFuncs.requestread (stream, rangeList);
}

NPError NPN_NewStream (NPP instance, NPMIMEType type,
                       const char *target, NPStream **stream)
{
	return NPNFuncs.newstream (instance, type, target, stream);
}

int32_t NPN_Write (NPP instance, NPStream *stream, int32_t len, void *buffer)
{
	return NPNFuncs.write (instance, stream, len, buffer);
}

NPError NPN_DestroyStream (NPP instance, NPStream* stream, NPError reason)
{
	return NPNFuncs.destroystream (instance, stream, reason);
}

void NPN_Status (NPP instance, const char *message)
{
	NPNFuncs.status (instance, message);
}

const char* NPN_UserAgent(NPP instance)
{
	return NPNFuncs.uagent (instance);
}

void* NPN_MemAlloc (uint32 size)
{
	return NPNFuncs.memalloc (size);
}

void NPN_MemFree (void* ptr)
{
	/* Make it null-safe */
	if (!ptr) {
		return;
	}

	NPNFuncs.memfree (ptr);
}

uint32_t NPN_MemFlush (uint32_t size)
{
	return NPNFuncs.memflush (size);
}

void NPN_ReloadPlugins (NPBool reloadPages)
{
	NPNFuncs.reloadplugins (reloadPages);
}

JRIEnv* NPN_GetJavaEnv (void)
{
	return NPNFuncs.getJavaEnv ();
}

jref NPN_GetJavaPeer (NPP instance)
{
	return NPNFuncs.getJavaPeer (instance);
}

NPError NPN_GetValue (NPP instance, NPNVariable variable, void *value)
{
	return NPNFuncs.getvalue (instance, variable, value);
}

NPError NPN_SetValue (NPP instance, NPPVariable variable, void *value)
{
	return NPNFuncs.setvalue (instance, variable, value);
}

void NPN_InvalidateRect (NPP instance, NPRect *invalidRect)
{
	NPNFuncs.invalidaterect (instance, invalidRect);
}

void NPN_InvalidateRegion (NPP instance, NPRegion invalidRegion)
{
	NPNFuncs.invalidateregion (instance, invalidRegion);
}

void NPN_ForceRedraw (NPP instance)
{
	NPNFuncs.forceredraw (instance);
}

NPIdentifier NPN_GetStringIdentifier (const NPUTF8 *name)
{
	return NPNFuncs.getstringidentifier (name);
}

void NPN_GetStringIdentifiers (const NPUTF8 **names, int32_t nameCount,
                               NPIdentifier *identifiers)
{
	return NPNFuncs.getstringidentifiers (names, nameCount, identifiers);
}

NPIdentifier NPN_GetStringIdentifier (int32_t intid)
{
	return NPNFuncs.getintidentifier (intid);
}

bool NPN_IdentifierIsString (NPIdentifier identifier)
{
	return NPNFuncs.identifierisstring (identifier);
}

NPUTF8 *NPN_UTF8FromIdentifier (NPIdentifier identifier)
{
	return NPNFuncs.utf8fromidentifier (identifier);
}

int32_t NPN_IntFromIdentifier (NPIdentifier identifier)
{
	return NPNFuncs.intfromidentifier (identifier);
}

NPObject *NPN_CreateObject (NPP npp, NPClass *aClass)
{
	return NPNFuncs.createobject (npp, aClass);
}

NPObject *NPN_RetainObject (NPObject *obj)
{
	return NPNFuncs.retainobject (obj);
}

void NPN_ReleaseObject(NPObject *obj)
{
	return NPNFuncs.releaseobject (obj);
}

bool NPN_Invoke (NPP npp, NPObject *obj, NPIdentifier methodName,
                 const NPVariant *args, uint32_t argCount, NPVariant *result)
{
	return NPNFuncs.invoke (npp, obj, methodName, args, argCount, result);
}

bool NPN_InvokeDefault (NPP npp, NPObject *obj, const NPVariant *args,
                        uint32_t argCount, NPVariant *result)
{
	return NPNFuncs.invokeDefault (npp, obj, args, argCount, result);
}

bool NPN_Evaluate (NPP npp, NPObject *obj, NPString *script,
                   NPVariant *result)
{
	return NPNFuncs.evaluate (npp, obj, script, result);
}

bool NPN_GetProperty (NPP npp, NPObject *obj, NPIdentifier propertyName,
                      NPVariant *result)
{
	return NPNFuncs.getproperty (npp, obj, propertyName, result);
}

bool NPN_SetProperty (NPP npp, NPObject *obj, NPIdentifier propertyName,
                      const NPVariant *value)
{
	return NPNFuncs.setproperty (npp, obj, propertyName, value);
}

bool NPN_RemoveProperty (NPP npp, NPObject *obj, NPIdentifier propertyName)
{
	return NPNFuncs.removeproperty (npp, obj, propertyName);
}

bool NPN_Enumerate (NPP npp, NPObject *obj, NPIdentifier **identifier,
                    uint32_t *count)
{
	return NPNFuncs.enumerate (npp, obj, identifier, count);
}

bool NPN_Construct (NPP npp, NPObject *obj, const NPVariant *args,
                    uint32_t argCount, NPVariant *result)
{
	return NPNFuncs.construct (npp, obj, args, argCount, result);
}

bool NPN_HasProperty (NPP npp, NPObject *obj, NPIdentifier propertyName)
{
	return NPNFuncs.hasproperty (npp, obj, propertyName);
}

bool NPN_HasMethod (NPP npp, NPObject *obj, NPIdentifier methodName)
{
	return NPNFuncs.hasmethod (npp, obj, methodName);
}

void NPN_ReleaseVariantValue (NPVariant *variant)
{
	NPNFuncs.releasevariantvalue (variant);
}

void NPN_SetException (NPObject *obj, const NPUTF8 *message)
{
	NPNFuncs.setexception (obj, message);
}

void* NPN_MemDup (const void *aMem, uint32 aLen)
{
	if (!aMem || !aLen) {
		return NULL;
	}

	void *dup = NPN_MemAlloc (aLen);
	if (!dup) {
		return NULL;
	}

	return memcpy (dup, aMem, aLen);
}

char* NPN_StrDup (const char *aString)
{
	return (char *) NPN_MemDup(aString, strlen (aString) + 1);
}