Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #define bool int
- #define true 1
- #define false 0
- #include <npapi.h>
- #include <npruntime.h>
- #include <npfunctions.h>
- static NPObject *so = NULL;
- NPNetscapeFuncs *npnfuncs = NULL;
- static NPP inst = NULL;
- #define _log(MSG, ...) do { \
- char buf[300]; \
- sprintf(buf, "echo \"%s: " MSG "\" >> /home/ivan/log.txt", __func__, ## __VA_ARGS__); \
- system(buf); \
- } while (0);
- #ifndef _WIN32
- static bool
- hasMethod(NPObject *obj, NPIdentifier methodName) {
- _log("methodName %s", npnfuncs->utf8fromidentifier(methodName));
- return false;
- }
- #else
- static bool
- hasMethod(NPObject *obj, NPIdentifier methodName) {
- return false;
- }
- #endif
- // Funcs
- static bool
- invokeDefault(NPObject *obj, const NPVariant *args, uint32_t argCount, NPVariant *result) {
- 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) {
- char *name;
- name = npnfuncs->utf8fromidentifier(methodName);
- _log("%s called", name);
- npnfuncs->setexception(obj, "function not found");
- return false;
- }
- enum { FSTRING, FINT };
- typedef struct {
- int type;
- char *name, *value;
- } tprop;
- static tprop props[] = {
- { .name = "example_prop", .value = "SKJY1107", .type = FSTRING, },
- { .name = "proptwo", .value = "e8:5b:5b:24:76:11", .type = FSTRING, },
- //
- { .name = NULL, .value = NULL, .type = FINT, },
- };
- #define FORE_PROP(IT) \
- tprop *IT = (tprop*) props; \
- for(; IT->name != NULL; IT++)
- static bool
- hasProperty(NPObject *obj, NPIdentifier propertyName) {
- char *name;
- int i;
- int cont;
- name = npnfuncs->utf8fromidentifier(propertyName);
- cont = 1;
- _log("hasProperty %s", name);
- FORE_PROP(prop) {
- cont = strcmp(prop->name, name);
- if (!cont)
- break;
- }
- return !cont;
- }
- static bool
- getProperty(NPObject *obj, NPIdentifier propertyName, NPVariant *result) {
- char *name;
- name = npnfuncs->utf8fromidentifier(propertyName);
- FORE_PROP(prop) {
- if (!strcmp(prop->name, name)) {
- if (prop->type == FSTRING) {
- STRINGZ_TO_NPVARIANT(strdup(prop->value), *result);
- }
- return true;
- }
- }
- return false;
- }
- //
- static NPClass npcRefObject = {
- NP_CLASS_STRUCT_VERSION,
- 0,
- 0,
- 0,
- hasMethod,
- invoke,
- invokeDefault,
- hasProperty,
- getProperty,
- 0,
- 0,
- };
- static NPError
- plugin_new(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char *argn[], char *argv[], NPSavedData *saved) {
- inst = instance;
- return NPERR_NO_ERROR;
- }
- static NPError
- plugin_destroy(NPP instance, NPSavedData **save) {
- if (so)
- npnfuncs->releaseobject(so);
- so = 0;
- return NPERR_NO_ERROR;
- }
- static NPError
- plugin_getValue(NPP instance, NPPVariable variable, void *value) {
- inst = instance;
- switch (variable) {
- default:
- return NPERR_GENERIC_ERROR;
- case NPPVpluginNameString:
- *((char **)value) = "Example Plugin";
- break;
- case NPPVpluginDescriptionString:
- *((char **)value) = "Plugin for Firefox";
- break;
- case NPPVpluginScriptableNPObject:
- if(!so)
- so = npnfuncs->createobject(instance, &npcRefObject);
- npnfuncs->retainobject(so);
- *(NPObject **)value = so;
- break;
- }
- return NPERR_NO_ERROR;
- }
- static NPError
- plugin_handleEvent(NPP instance, void *ev) {
- inst = instance;
- return NPERR_NO_ERROR;
- }
- static NPError
- plugin_setWindow(NPP instance, NPWindow *pNPWindow) {
- inst = instance;
- return NPERR_NO_ERROR;
- }
- // Define callbacks do plugin
- NPError OSCALL
- NP_GetEntryPoints(NPPluginFuncs *nppFuncs) {
- nppFuncs->version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
- nppFuncs->newp = plugin_new;
- nppFuncs->destroy = plugin_destroy;
- nppFuncs->getvalue = plugin_getValue;
- nppFuncs->event = plugin_handleEvent;
- nppFuncs->setwindow = plugin_setWindow;
- return NPERR_NO_ERROR;
- }
- #ifndef _WIN32
- #define HIBYTE(x) ((((uint32_t)(x)) & 0xff00) >> 8)
- #endif
- #ifndef _WIN32
- NPError OSCALL
- NP_Initialize(NPNetscapeFuncs *npnf, NPPluginFuncs *nppfuncs) {
- if (npnf == 0)
- return NPERR_INVALID_FUNCTABLE_ERROR;
- if (HIBYTE(npnf->version) > NP_VERSION_MAJOR)
- return NPERR_INVALID_FUNCTABLE_ERROR;
- npnfuncs = npnf;
- NP_GetEntryPoints(nppfuncs);
- return NPERR_NO_ERROR;
- }
- #else
- NPError OSCALL NP_Initialize(NPNetscapeFuncs* bFuncs) {
- npnfuncs = bFuncs;
- }
- #endif
- NPError OSCALL
- NP_Shutdown() {
- return NPERR_NO_ERROR;
- }
- const char *
- NP_GetMIMEDescription(void) {
- return "application/x-example-info:.exampleplugin:example.com";
- }
- NPError OSCALL
- NP_GetValue(void *npp, NPPVariable variable, void *value) {
- inst = (NPP) npp;
- return plugin_getValue((NPP) npp, variable, value);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement