From f0b455401831038a9c34d581357f4cd6d303eb7c Mon Sep 17 00:00:00 2001 From: "jst@mozilla.org" Date: Fri, 21 Sep 2007 11:35:38 -0700 Subject: [PATCH] Adding npruntime scriptability sample. A dumb sample, but it's all we've got an I have no time to make it better now. Not part of the build. --- modules/plugin/samples/npruntime/Makefile.in | 68 ++ modules/plugin/samples/npruntime/np_entry.cpp | 189 +++++ modules/plugin/samples/npruntime/npn_gate.cpp | 334 ++++++++ modules/plugin/samples/npruntime/npp_gate.cpp | 280 +++++++ modules/plugin/samples/npruntime/nprt.def | 6 + modules/plugin/samples/npruntime/nprt.rc | 112 +++ modules/plugin/samples/npruntime/plugin.cpp | 767 ++++++++++++++++++ modules/plugin/samples/npruntime/plugin.h | 80 ++ modules/plugin/samples/npruntime/resource.h | 20 + modules/plugin/samples/npruntime/test.html | 61 ++ 10 files changed, 1917 insertions(+) create mode 100644 modules/plugin/samples/npruntime/Makefile.in create mode 100644 modules/plugin/samples/npruntime/np_entry.cpp create mode 100644 modules/plugin/samples/npruntime/npn_gate.cpp create mode 100644 modules/plugin/samples/npruntime/npp_gate.cpp create mode 100644 modules/plugin/samples/npruntime/nprt.def create mode 100644 modules/plugin/samples/npruntime/nprt.rc create mode 100644 modules/plugin/samples/npruntime/plugin.cpp create mode 100644 modules/plugin/samples/npruntime/plugin.h create mode 100644 modules/plugin/samples/npruntime/resource.h create mode 100644 modules/plugin/samples/npruntime/test.html diff --git a/modules/plugin/samples/npruntime/Makefile.in b/modules/plugin/samples/npruntime/Makefile.in new file mode 100644 index 00000000000..f5de94fad9d --- /dev/null +++ b/modules/plugin/samples/npruntime/Makefile.in @@ -0,0 +1,68 @@ +############################################################################ +## Makefile.in (Generic SANE Plugin Tree) +## +## 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://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. +## Contributor(s): +## +## Rusty Lynch +############################################################################ + +DEPTH = ../../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = plugin +LIBRARY_NAME = nprt +ifeq ($(OS_ARCH),WINNT) +RESFILE = nprt.res +DEFFILE = $(srcdir)/nprt.def +endif + +# plugins should always be shared, even in the "static" build +FORCE_SHARED_LIB = 1 + +# Force use of PIC +FORCE_USE_PIC = 1 + +NO_DIST_INSTALL = 1 +NO_INSTALL = 1 + +CPPSRCS = \ + np_entry.cpp \ + npn_gate.cpp \ + npp_gate.cpp \ + plugin.cpp \ + $(NULL) + +REQUIRES = \ + plugin \ + java \ + $(NULL) + +LOCAL_INCLUDES = -I$(srcdir)/.. -I$(srcdir)/../../public \ + -I/usr/lib/glib/include + +include $(topsrcdir)/config/rules.mk + +install-plugin: $(SHARED_LIBRARY) +ifdef SHARED_LIBRARY + $(INSTALL) $(SHARED_LIBRARY) $(DIST)/bin/plugins +endif + +libs:: install-plugin + +install:: $(SHARED_LIBRARY) +ifdef SHARED_LIBRARY + $(SYSINSTALL) $(IFLAGS2) $< $(DESTDIR)$(mozappdir)/plugins +endif diff --git a/modules/plugin/samples/npruntime/np_entry.cpp b/modules/plugin/samples/npruntime/np_entry.cpp new file mode 100644 index 00000000000..4a2d66b5083 --- /dev/null +++ b/modules/plugin/samples/npruntime/np_entry.cpp @@ -0,0 +1,189 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape 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://www.mozilla.org/NPL/ + * + * 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. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +////////////////////////////////////////////////////////////// +// +// Main plugin entry point implementation +// +#include "npapi.h" +#include "npupp.h" + +#ifndef HIBYTE +#define HIBYTE(x) ((((uint32)(x)) & 0xff00) >> 8) +#endif + +NPNetscapeFuncs NPNFuncs; + +#ifdef XP_WIN + +NPError OSCALL NP_GetEntryPoints(NPPluginFuncs* pFuncs) +{ + if(pFuncs == NULL) + return NPERR_INVALID_FUNCTABLE_ERROR; + + if(pFuncs->size < sizeof(NPPluginFuncs)) + return NPERR_INVALID_FUNCTABLE_ERROR; + + pFuncs->version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR; + pFuncs->newp = NPP_New; + pFuncs->destroy = NPP_Destroy; + pFuncs->setwindow = NPP_SetWindow; + pFuncs->newstream = NPP_NewStream; + pFuncs->destroystream = NPP_DestroyStream; + pFuncs->asfile = NPP_StreamAsFile; + pFuncs->writeready = NPP_WriteReady; + pFuncs->write = NPP_Write; + pFuncs->print = NPP_Print; + pFuncs->event = NPP_HandleEvent; + pFuncs->urlnotify = NPP_URLNotify; + pFuncs->getvalue = NPP_GetValue; + pFuncs->setvalue = NPP_SetValue; + pFuncs->javaClass = NULL; + + return NPERR_NO_ERROR; +} + +#endif /* XP_WIN */ + +char *NPP_GetMIMEDescription(); + +char * +NP_GetMIMEDescription() +{ + return NPP_GetMIMEDescription(); +} + +NPError +NP_GetValue(void* future, NPPVariable variable, void *value) +{ + return NPP_GetValue((NPP_t *)future, variable, value); +} + +NPError OSCALL +NP_Initialize(NPNetscapeFuncs* pFuncs +#ifdef XP_UNIX + , NPPluginFuncs* pluginFuncs +#endif + ) +{ + if(pFuncs == NULL) + return NPERR_INVALID_FUNCTABLE_ERROR; + + if(HIBYTE(pFuncs->version) > NP_VERSION_MAJOR) + return NPERR_INCOMPATIBLE_VERSION_ERROR; + + if(pFuncs->size < sizeof(NPNetscapeFuncs)) + return NPERR_INVALID_FUNCTABLE_ERROR; + + NPNFuncs.size = pFuncs->size; + NPNFuncs.version = pFuncs->version; + NPNFuncs.geturlnotify = pFuncs->geturlnotify; + NPNFuncs.geturl = pFuncs->geturl; + NPNFuncs.posturlnotify = pFuncs->posturlnotify; + NPNFuncs.posturl = pFuncs->posturl; + NPNFuncs.requestread = pFuncs->requestread; + NPNFuncs.newstream = pFuncs->newstream; + NPNFuncs.write = pFuncs->write; + NPNFuncs.destroystream = pFuncs->destroystream; + NPNFuncs.status = pFuncs->status; + NPNFuncs.uagent = pFuncs->uagent; + NPNFuncs.memalloc = pFuncs->memalloc; + NPNFuncs.memfree = pFuncs->memfree; + NPNFuncs.memflush = pFuncs->memflush; + NPNFuncs.reloadplugins = pFuncs->reloadplugins; + NPNFuncs.getJavaEnv = pFuncs->getJavaEnv; + NPNFuncs.getJavaPeer = pFuncs->getJavaPeer; + NPNFuncs.getvalue = pFuncs->getvalue; + NPNFuncs.setvalue = pFuncs->setvalue; + NPNFuncs.invalidaterect = pFuncs->invalidaterect; + NPNFuncs.invalidateregion = pFuncs->invalidateregion; + NPNFuncs.forceredraw = pFuncs->forceredraw; + NPNFuncs.getstringidentifier = pFuncs->getstringidentifier; + NPNFuncs.getstringidentifiers = pFuncs->getstringidentifiers; + NPNFuncs.getintidentifier = pFuncs->getintidentifier; + NPNFuncs.identifierisstring = pFuncs->identifierisstring; + NPNFuncs.utf8fromidentifier = pFuncs->utf8fromidentifier; + NPNFuncs.intfromidentifier = pFuncs->intfromidentifier; + NPNFuncs.createobject = pFuncs->createobject; + NPNFuncs.retainobject = pFuncs->retainobject; + NPNFuncs.releaseobject = pFuncs->releaseobject; + NPNFuncs.invoke = pFuncs->invoke; + NPNFuncs.invokeDefault = pFuncs->invokeDefault; + NPNFuncs.evaluate = pFuncs->evaluate; + NPNFuncs.getproperty = pFuncs->getproperty; + NPNFuncs.setproperty = pFuncs->setproperty; + NPNFuncs.removeproperty = pFuncs->removeproperty; + NPNFuncs.hasproperty = pFuncs->hasproperty; + NPNFuncs.hasmethod = pFuncs->hasmethod; + NPNFuncs.releasevariantvalue = pFuncs->releasevariantvalue; + NPNFuncs.setexception = pFuncs->setexception; + +#ifdef XP_UNIX + /* + * Set up the plugin function table that Netscape will use to + * call us. Netscape needs to know about our version and size + * and have a UniversalProcPointer for every function we + * implement. + */ + pluginFuncs->version = (NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR; + pluginFuncs->size = sizeof(NPPluginFuncs); + pluginFuncs->newp = NewNPP_NewProc(NPP_New); + pluginFuncs->destroy = NewNPP_DestroyProc(NPP_Destroy); + pluginFuncs->setwindow = NewNPP_SetWindowProc(NPP_SetWindow); + pluginFuncs->newstream = NewNPP_NewStreamProc(NPP_NewStream); + pluginFuncs->destroystream = NewNPP_DestroyStreamProc(NPP_DestroyStream); + pluginFuncs->asfile = NewNPP_StreamAsFileProc(NPP_StreamAsFile); + pluginFuncs->writeready = NewNPP_WriteReadyProc(NPP_WriteReady); + pluginFuncs->write = NewNPP_WriteProc(NPP_Write); + pluginFuncs->print = NewNPP_PrintProc(NPP_Print); + pluginFuncs->urlnotify = NewNPP_URLNotifyProc(NPP_URLNotify); + pluginFuncs->event = NULL; + pluginFuncs->getvalue = NewNPP_GetValueProc(NPP_GetValue); +#ifdef OJI + pluginFuncs->javaClass = NPP_GetJavaClass(); +#endif + + NPP_Initialize(); +#endif + + return NPERR_NO_ERROR; +} + +NPError OSCALL NP_Shutdown() +{ + return NPERR_NO_ERROR; +} diff --git a/modules/plugin/samples/npruntime/npn_gate.cpp b/modules/plugin/samples/npruntime/npn_gate.cpp new file mode 100644 index 00000000000..821e150a410 --- /dev/null +++ b/modules/plugin/samples/npruntime/npn_gate.cpp @@ -0,0 +1,334 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape 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://www.mozilla.org/NPL/ + * + * 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. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +//////////////////////////////////////////////////////////// +// +// Implementation of Netscape entry points (NPN_*) +// +#include "npapi.h" +#include "npupp.h" + +#ifndef HIBYTE +#define HIBYTE(x) ((((uint32)(x)) & 0xff00) >> 8) +#endif + +#ifndef LOBYTE +#define LOBYTE(W) ((W) & 0xFF) +#endif + +extern NPNetscapeFuncs NPNFuncs; + +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) +{ + int navMinorVers = NPNFuncs.version & 0xFF; + NPError rv = NPERR_NO_ERROR; + + if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) + rv = NPNFuncs.geturlnotify(instance, url, target, notifyData); + else + rv = NPERR_INCOMPATIBLE_VERSION_ERROR; + + return rv; +} + +NPError NPN_GetURL(NPP instance, const char *url, const char *target) +{ + NPError rv = NPNFuncs.geturl(instance, url, target); + return rv; +} + +NPError NPN_PostURLNotify(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file, void* notifyData) +{ + int navMinorVers = NPNFuncs.version & 0xFF; + NPError rv = NPERR_NO_ERROR; + + if( navMinorVers >= NPVERS_HAS_NOTIFICATION ) + rv = NPNFuncs.posturlnotify(instance, url, window, len, buf, file, notifyData); + else + rv = NPERR_INCOMPATIBLE_VERSION_ERROR; + + return rv; +} + +NPError NPN_PostURL(NPP instance, const char* url, const char* window, uint32 len, const char* buf, NPBool file) +{ + NPError rv = NPNFuncs.posturl(instance, url, window, len, buf, file); + return rv; +} + +NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList) +{ + NPError rv = NPNFuncs.requestread(stream, rangeList); + return rv; +} + +NPError NPN_NewStream(NPP instance, NPMIMEType type, const char* target, NPStream** stream) +{ + int navMinorVersion = NPNFuncs.version & 0xFF; + + NPError rv = NPERR_NO_ERROR; + + if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) + rv = NPNFuncs.newstream(instance, type, target, stream); + else + rv = NPERR_INCOMPATIBLE_VERSION_ERROR; + + return rv; +} + +int32 NPN_Write(NPP instance, NPStream *stream, int32 len, void *buffer) +{ + int navMinorVersion = NPNFuncs.version & 0xFF; + int32 rv = 0; + + if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) + rv = NPNFuncs.write(instance, stream, len, buffer); + else + rv = -1; + + return rv; +} + +NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPError reason) +{ + int navMinorVersion = NPNFuncs.version & 0xFF; + NPError rv = NPERR_NO_ERROR; + + if( navMinorVersion >= NPVERS_HAS_STREAMOUTPUT ) + rv = NPNFuncs.destroystream(instance, stream, reason); + else + rv = NPERR_INCOMPATIBLE_VERSION_ERROR; + + return rv; +} + +void NPN_Status(NPP instance, const char *message) +{ + NPNFuncs.status(instance, message); +} + +const char* NPN_UserAgent(NPP instance) +{ + const char * rv = NULL; + rv = NPNFuncs.uagent(instance); + return rv; +} + +void* NPN_MemAlloc(uint32 size) +{ + void * rv = NULL; + rv = NPNFuncs.memalloc(size); + return rv; +} + +void NPN_MemFree(void* ptr) +{ + NPNFuncs.memfree(ptr); +} + +uint32 NPN_MemFlush(uint32 size) +{ + uint32 rv = NPNFuncs.memflush(size); + return rv; +} + +void NPN_ReloadPlugins(NPBool reloadPages) +{ + NPNFuncs.reloadplugins(reloadPages); +} + +JRIEnv* NPN_GetJavaEnv(void) +{ + JRIEnv * rv = NULL; + rv = NPNFuncs.getJavaEnv(); + return rv; +} + +jref NPN_GetJavaPeer(NPP instance) +{ + jref rv; + rv = NPNFuncs.getJavaPeer(instance); + return rv; +} + +NPError NPN_GetValue(NPP instance, NPNVariable variable, void *value) +{ + NPError rv = NPNFuncs.getvalue(instance, variable, value); + return rv; +} + +NPError NPN_SetValue(NPP instance, NPPVariable variable, void *value) +{ + NPError rv = NPNFuncs.setvalue(instance, variable, value); + return rv; +} + +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, uint32_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); +} diff --git a/modules/plugin/samples/npruntime/npp_gate.cpp b/modules/plugin/samples/npruntime/npp_gate.cpp new file mode 100644 index 00000000000..07651171576 --- /dev/null +++ b/modules/plugin/samples/npruntime/npp_gate.cpp @@ -0,0 +1,280 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape 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://www.mozilla.org/NPL/ + * + * 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. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +//////////////////////////////////////////////////////////// +// +// Implementation of plugin entry points (NPP_*) +// most are just empty stubs for this particular plugin +// +#include "plugin.h" + +char* +NPP_GetMIMEDescription(void) +{ + return "application/mozilla-npruntime-scriptable-plugin:.foo:Scriptability Demo Plugin"; +} + + + +NPError NPP_Initialize(void) +{ + return NPERR_NO_ERROR; +} + +void NPP_Shutdown(void) +{ +} + +// here the plugin creates an instance of our CPlugin object which +// will be associated with this newly created plugin instance and +// will do all the neccessary job +NPError NPP_New(NPMIMEType pluginType, + NPP instance, + uint16 mode, + int16 argc, + char* argn[], + char* argv[], + NPSavedData* saved) +{ + if(instance == NULL) + return NPERR_INVALID_INSTANCE_ERROR; + + NPError rv = NPERR_NO_ERROR; + + CPlugin * pPlugin = new CPlugin(instance); + if(pPlugin == NULL) + return NPERR_OUT_OF_MEMORY_ERROR; + + instance->pdata = (void *)pPlugin; + return rv; +} + +// here is the place to clean up and destroy the CPlugin object +NPError NPP_Destroy (NPP instance, NPSavedData** save) +{ + if(instance == NULL) + return NPERR_INVALID_INSTANCE_ERROR; + + NPError rv = NPERR_NO_ERROR; + + CPlugin * pPlugin = (CPlugin *)instance->pdata; + if(pPlugin != NULL) { + pPlugin->shut(); + delete pPlugin; + } + return rv; +} + +// during this call we know when the plugin window is ready or +// is about to be destroyed so we can do some gui specific +// initialization and shutdown +NPError NPP_SetWindow (NPP instance, NPWindow* pNPWindow) +{ + if(instance == NULL) + return NPERR_INVALID_INSTANCE_ERROR; + + NPError rv = NPERR_NO_ERROR; + + if(pNPWindow == NULL) + return NPERR_GENERIC_ERROR; + + CPlugin * pPlugin = (CPlugin *)instance->pdata; + + if(pPlugin == NULL) + return NPERR_GENERIC_ERROR; + + // window just created + if(!pPlugin->isInitialized() && (pNPWindow->window != NULL)) { + if(!pPlugin->init(pNPWindow)) { + delete pPlugin; + pPlugin = NULL; + return NPERR_MODULE_LOAD_FAILED_ERROR; + } + } + + // window goes away + if((pNPWindow->window == NULL) && pPlugin->isInitialized()) + return NPERR_NO_ERROR; + + // window resized + if(pPlugin->isInitialized() && (pNPWindow->window != NULL)) + return NPERR_NO_ERROR; + + // this should not happen, nothing to do + if((pNPWindow->window == NULL) && !pPlugin->isInitialized()) + return NPERR_NO_ERROR; + + return rv; +} + +// ============================== +// ! Scriptability related code ! +// ============================== +// +// here the plugin is asked by Mozilla to tell if it is scriptable +// we should return a valid interface id and a pointer to +// nsScriptablePeer interface which we should have implemented +// and which should be defined in the corressponding *.xpt file +// in the bin/components folder +NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value) +{ + if(instance == NULL) + return NPERR_INVALID_INSTANCE_ERROR; + + NPError rv = NPERR_NO_ERROR; + + if(instance == NULL) + return NPERR_GENERIC_ERROR; + + CPlugin * plugin = (CPlugin *)instance->pdata; + if(plugin == NULL) + return NPERR_GENERIC_ERROR; + + switch (variable) { + case NPPVpluginNameString: + *((char **)value) = "NPRuntimeTest"; + break; + case NPPVpluginDescriptionString: + *((char **)value) = "NPRuntime scriptability API test plugin"; + break; + case NPPVpluginScriptableNPObject: + *(NPObject **)value = plugin->GetScriptableObject(); + break; + default: + rv = NPERR_GENERIC_ERROR; + } + + return rv; +} + +NPError NPP_NewStream(NPP instance, + NPMIMEType type, + NPStream* stream, + NPBool seekable, + uint16* stype) +{ + if(instance == NULL) + return NPERR_INVALID_INSTANCE_ERROR; + + NPError rv = NPERR_NO_ERROR; + return rv; +} + +int32 NPP_WriteReady (NPP instance, NPStream *stream) +{ + if(instance == NULL) + return NPERR_INVALID_INSTANCE_ERROR; + + int32 rv = 0x0fffffff; + return rv; +} + +int32 NPP_Write (NPP instance, NPStream *stream, int32 offset, int32 len, void *buffer) +{ + if(instance == NULL) + return NPERR_INVALID_INSTANCE_ERROR; + + int32 rv = len; + return rv; +} + +NPError NPP_DestroyStream (NPP instance, NPStream *stream, NPError reason) +{ + if(instance == NULL) + return NPERR_INVALID_INSTANCE_ERROR; + + NPError rv = NPERR_NO_ERROR; + return rv; +} + +void NPP_StreamAsFile (NPP instance, NPStream* stream, const char* fname) +{ + if(instance == NULL) + return; +} + +void NPP_Print (NPP instance, NPPrint* printInfo) +{ + if(instance == NULL) + return; +} + +void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData) +{ + if(instance == NULL) + return; +} + +NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value) +{ + if(instance == NULL) + return NPERR_INVALID_INSTANCE_ERROR; + + NPError rv = NPERR_NO_ERROR; + return rv; +} + +int16 NPP_HandleEvent(NPP instance, void* event) +{ + if(instance == NULL) + return 0; + + int16 rv = 0; + CPlugin * pPlugin = (CPlugin *)instance->pdata; + if (pPlugin) + rv = pPlugin->handleEvent(event); + + return rv; +} + +jref NPP_GetJavaClass (void) +{ + return NULL; +} + +NPObject *NPP_GetScriptableInstance(NPP instance) +{ + if(!instance) + return 0; + + NPObject *npobj = 0; + CPlugin * pPlugin = (CPlugin *)instance->pdata; + if (!pPlugin) + npobj = pPlugin->GetScriptableObject(); + + return npobj; +} diff --git a/modules/plugin/samples/npruntime/nprt.def b/modules/plugin/samples/npruntime/nprt.def new file mode 100644 index 00000000000..3aabd9cfd23 --- /dev/null +++ b/modules/plugin/samples/npruntime/nprt.def @@ -0,0 +1,6 @@ +LIBRARY NPRT + +EXPORTS + NP_GetEntryPoints @1 + NP_Initialize @2 + NP_Shutdown @3 diff --git a/modules/plugin/samples/npruntime/nprt.rc b/modules/plugin/samples/npruntime/nprt.rc new file mode 100644 index 00000000000..c1384c80651 --- /dev/null +++ b/modules/plugin/samples/npruntime/nprt.rc @@ -0,0 +1,112 @@ +//Microsoft Developer Studio generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "afxres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (U.S.) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +#ifdef _WIN32 +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) +#endif //_WIN32 + +#ifndef _MAC +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 1,0,0,1 + PRODUCTVERSION 1,0,0,1 + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x40004L + FILETYPE 0x2L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904e4" + BEGIN + VALUE "Comments", "\0" + VALUE "CompanyName", " \0" + VALUE "FileDescription", "nprt\0" + VALUE "FileExtents", "rts\0" + VALUE "FileOpenName", "nprt\0" + VALUE "FileVersion", "1, 0, 0, 1\0" + VALUE "InternalName", "nprt\0" + VALUE "LegalCopyright", "Copyright © 1999\0" + VALUE "LegalTrademarks", "\0" + VALUE "MIMEType", "application/mozilla-npruntime-scriptable-plugin\0" + VALUE "OriginalFilename", "nprt.dll\0" + VALUE "PrivateBuild", "\0" + VALUE "ProductName", "npruntime scriptable example plugin\0" + VALUE "ProductVersion", "1, 0, 0, 1\0" + VALUE "SpecialBuild", "\0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1252 + END +END + +#endif // !_MAC + + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE DISCARDABLE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE DISCARDABLE +BEGIN + "#include ""afxres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE DISCARDABLE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + +#endif // English (U.S.) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/modules/plugin/samples/npruntime/plugin.cpp b/modules/plugin/samples/npruntime/plugin.cpp new file mode 100644 index 00000000000..fffdad9b783 --- /dev/null +++ b/modules/plugin/samples/npruntime/plugin.cpp @@ -0,0 +1,767 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape 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://www.mozilla.org/NPL/ + * + * 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. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +////////////////////////////////////////////////// +// +// CPlugin class implementation +// +#ifdef XP_WIN +#include +#include +#endif + +#ifdef XP_MAC +#include +#endif + +#ifdef XP_UNIX +#include +#endif + +#include "plugin.h" +#include "npupp.h" + +static NPIdentifier sFoo_id; +static NPIdentifier sBar_id; +static NPIdentifier sDocument_id; +static NPIdentifier sBody_id; +static NPIdentifier sCreateElement_id; +static NPIdentifier sCreateTextNode_id; +static NPIdentifier sAppendChild_id; +static NPIdentifier sPluginType_id; +static NPObject *sWindowObj; + +// Helper class that can be used to map calls to the NPObject hooks +// into virtual methods on instances of classes that derive from this +// class. +class ScriptablePluginObjectBase : public NPObject +{ +public: + ScriptablePluginObjectBase(NPP npp) + : mNpp(npp) + { + } + + virtual ~ScriptablePluginObjectBase() + { + } + + // Virtual NPObject hooks called through this base class. Override + // as you see fit. + virtual void Invalidate(); + virtual bool HasMethod(NPIdentifier name); + virtual bool Invoke(NPIdentifier name, const NPVariant *args, + uint32_t argCount, NPVariant *result); + virtual bool InvokeDefault(const NPVariant *args, uint32_t argCount, + NPVariant *result); + virtual bool HasProperty(NPIdentifier name); + virtual bool GetProperty(NPIdentifier name, NPVariant *result); + virtual bool SetProperty(NPIdentifier name, const NPVariant *value); + virtual bool RemoveProperty(NPIdentifier name); + virtual bool Enumerate(NPIdentifier **identifier, uint32_t *count); + virtual bool Construct(const NPVariant *args, uint32_t argCount, + NPVariant *result); + +public: + static void _Deallocate(NPObject *npobj); + static void _Invalidate(NPObject *npobj); + static bool _HasMethod(NPObject *npobj, NPIdentifier name); + static bool _Invoke(NPObject *npobj, NPIdentifier name, + const NPVariant *args, uint32_t argCount, + NPVariant *result); + static bool _InvokeDefault(NPObject *npobj, const NPVariant *args, + uint32_t argCount, NPVariant *result); + static bool _HasProperty(NPObject * npobj, NPIdentifier name); + static bool _GetProperty(NPObject *npobj, NPIdentifier name, + NPVariant *result); + static bool _SetProperty(NPObject *npobj, NPIdentifier name, + const NPVariant *value); + static bool _RemoveProperty(NPObject *npobj, NPIdentifier name); + static bool _Enumerate(NPObject *npobj, NPIdentifier **identifier, + uint32_t *count); + static bool _Construct(NPObject *npobj, const NPVariant *args, + uint32_t argCount, NPVariant *result); + +protected: + NPP mNpp; +}; + +#define DECLARE_NPOBJECT_CLASS_WITH_BASE(_class, ctor) \ +static NPClass s##_class##_NPClass = { \ + NP_CLASS_STRUCT_VERSION_CTOR, \ + ctor, \ + ScriptablePluginObjectBase::_Deallocate, \ + ScriptablePluginObjectBase::_Invalidate, \ + ScriptablePluginObjectBase::_HasMethod, \ + ScriptablePluginObjectBase::_Invoke, \ + ScriptablePluginObjectBase::_InvokeDefault, \ + ScriptablePluginObjectBase::_HasProperty, \ + ScriptablePluginObjectBase::_GetProperty, \ + ScriptablePluginObjectBase::_SetProperty, \ + ScriptablePluginObjectBase::_RemoveProperty, \ + ScriptablePluginObjectBase::_Enumerate, \ + ScriptablePluginObjectBase::_Construct \ +} + +#define GET_NPOBJECT_CLASS(_class) &s##_class##_NPClass + +void +ScriptablePluginObjectBase::Invalidate() +{ +} + +bool +ScriptablePluginObjectBase::HasMethod(NPIdentifier name) +{ + return false; +} + +bool +ScriptablePluginObjectBase::Invoke(NPIdentifier name, const NPVariant *args, + uint32_t argCount, NPVariant *result) +{ + return false; +} + +bool +ScriptablePluginObjectBase::InvokeDefault(const NPVariant *args, + uint32_t argCount, NPVariant *result) +{ + return false; +} + +bool +ScriptablePluginObjectBase::HasProperty(NPIdentifier name) +{ + return false; +} + +bool +ScriptablePluginObjectBase::GetProperty(NPIdentifier name, NPVariant *result) +{ + return false; +} + +bool +ScriptablePluginObjectBase::SetProperty(NPIdentifier name, + const NPVariant *value) +{ + if (name == sBar_id) { + printf ("bar set\n"); + + return true; + } + + return false; +} + +bool +ScriptablePluginObjectBase::RemoveProperty(NPIdentifier name) +{ + return false; +} + +bool +ScriptablePluginObjectBase::Enumerate(NPIdentifier **identifier, + uint32_t *count) +{ + return false; +} + +bool +ScriptablePluginObjectBase::Construct(const NPVariant *args, uint32_t argCount, + NPVariant *result) +{ + return false; +} + +// static +void +ScriptablePluginObjectBase::_Deallocate(NPObject *npobj) +{ + // Call the virtual destructor. + delete (ScriptablePluginObjectBase *)npobj; +} + +// static +void +ScriptablePluginObjectBase::_Invalidate(NPObject *npobj) +{ + ((ScriptablePluginObjectBase *)npobj)->Invalidate(); +} + +// static +bool +ScriptablePluginObjectBase::_HasMethod(NPObject *npobj, NPIdentifier name) +{ + return ((ScriptablePluginObjectBase *)npobj)->HasMethod(name); +} + +// static +bool +ScriptablePluginObjectBase::_Invoke(NPObject *npobj, NPIdentifier name, + const NPVariant *args, uint32_t argCount, + NPVariant *result) +{ + return ((ScriptablePluginObjectBase *)npobj)->Invoke(name, args, argCount, + result); +} + +// static +bool +ScriptablePluginObjectBase::_InvokeDefault(NPObject *npobj, + const NPVariant *args, + uint32_t argCount, + NPVariant *result) +{ + return ((ScriptablePluginObjectBase *)npobj)->InvokeDefault(args, argCount, + result); +} + +// static +bool +ScriptablePluginObjectBase::_HasProperty(NPObject * npobj, NPIdentifier name) +{ + return ((ScriptablePluginObjectBase *)npobj)->HasProperty(name); +} + +// static +bool +ScriptablePluginObjectBase::_GetProperty(NPObject *npobj, NPIdentifier name, + NPVariant *result) +{ + return ((ScriptablePluginObjectBase *)npobj)->GetProperty(name, result); +} + +// static +bool +ScriptablePluginObjectBase::_SetProperty(NPObject *npobj, NPIdentifier name, + const NPVariant *value) +{ + return ((ScriptablePluginObjectBase *)npobj)->SetProperty(name, value); +} + +// static +bool +ScriptablePluginObjectBase::_RemoveProperty(NPObject *npobj, NPIdentifier name) +{ + return ((ScriptablePluginObjectBase *)npobj)->RemoveProperty(name); +} + +// static +bool +ScriptablePluginObjectBase::_Enumerate(NPObject *npobj, + NPIdentifier **identifier, + uint32_t *count) +{ + return ((ScriptablePluginObjectBase *)npobj)->Enumerate(identifier, count); +} + +// static +bool +ScriptablePluginObjectBase::_Construct(NPObject *npobj, const NPVariant *args, + uint32_t argCount, NPVariant *result) +{ + return ((ScriptablePluginObjectBase *)npobj)->Construct(args, argCount, + result); +} + + +class ConstructablePluginObject : public ScriptablePluginObjectBase +{ +public: + ConstructablePluginObject(NPP npp) + : ScriptablePluginObjectBase(npp) + { + } + + virtual bool Construct(const NPVariant *args, uint32_t argCount, + NPVariant *result); +}; + +static NPObject * +AllocateConstructablePluginObject(NPP npp, NPClass *aClass) +{ + return new ConstructablePluginObject(npp); +} + +DECLARE_NPOBJECT_CLASS_WITH_BASE(ConstructablePluginObject, + AllocateConstructablePluginObject); + +bool +ConstructablePluginObject::Construct(const NPVariant *args, uint32_t argCount, + NPVariant *result) +{ + printf("Creating new ConstructablePluginObject!\n"); + + NPObject *myobj = + NPN_CreateObject(mNpp, GET_NPOBJECT_CLASS(ConstructablePluginObject)); + if (!myobj) + return false; + + OBJECT_TO_NPVARIANT(myobj, *result); + + return true; +} + +class ScriptablePluginObject : public ScriptablePluginObjectBase +{ +public: + ScriptablePluginObject(NPP npp) + : ScriptablePluginObjectBase(npp) + { + } + + virtual bool HasMethod(NPIdentifier name); + virtual bool HasProperty(NPIdentifier name); + virtual bool GetProperty(NPIdentifier name, NPVariant *result); + virtual bool Invoke(NPIdentifier name, const NPVariant *args, + uint32_t argCount, NPVariant *result); + virtual bool InvokeDefault(const NPVariant *args, uint32_t argCount, + NPVariant *result); +}; + +static NPObject * +AllocateScriptablePluginObject(NPP npp, NPClass *aClass) +{ + return new ScriptablePluginObject(npp); +} + +DECLARE_NPOBJECT_CLASS_WITH_BASE(ScriptablePluginObject, + AllocateScriptablePluginObject); + +bool +ScriptablePluginObject::HasMethod(NPIdentifier name) +{ + return name == sFoo_id; +} + +bool +ScriptablePluginObject::HasProperty(NPIdentifier name) +{ + return (name == sBar_id || + name == sPluginType_id); +} + +bool +ScriptablePluginObject::GetProperty(NPIdentifier name, NPVariant *result) +{ + VOID_TO_NPVARIANT(*result); + + if (name == sBar_id) { + static int a = 17; + + INT32_TO_NPVARIANT(a, *result); + + a += 5; + + return true; + } + + if (name == sPluginType_id) { + NPObject *myobj = + NPN_CreateObject(mNpp, GET_NPOBJECT_CLASS(ConstructablePluginObject)); + if (!myobj) { + return false; + } + + OBJECT_TO_NPVARIANT(myobj, *result); + + return true; + } + + return true; +} + +bool +ScriptablePluginObject::Invoke(NPIdentifier name, const NPVariant *args, + uint32_t argCount, NPVariant *result) +{ + if (name == sFoo_id) { + printf ("foo called!\n"); + + NPVariant docv; + NPN_GetProperty(mNpp, sWindowObj, sDocument_id, &docv); + + NPObject *doc = NPVARIANT_TO_OBJECT(docv); + + NPVariant strv; + STRINGZ_TO_NPVARIANT("div", strv); + + NPVariant divv; + NPN_Invoke(mNpp, doc, sCreateElement_id, &strv, 1, &divv); + + STRINGZ_TO_NPVARIANT("I'm created by a plugin!", strv); + + NPVariant textv; + NPN_Invoke(mNpp, doc, sCreateTextNode_id, &strv, 1, &textv); + + NPVariant v; + NPN_Invoke(mNpp, NPVARIANT_TO_OBJECT(divv), sAppendChild_id, &textv, 1, + &v); + NPN_ReleaseVariantValue(&v); + + NPN_ReleaseVariantValue(&textv); + + NPVariant bodyv; + NPN_GetProperty(mNpp, doc, sBody_id, &bodyv); + + NPN_Invoke(mNpp, NPVARIANT_TO_OBJECT(bodyv), sAppendChild_id, &divv, 1, + &v); + NPN_ReleaseVariantValue(&v); + + NPN_ReleaseVariantValue(&divv); + NPN_ReleaseVariantValue(&bodyv); + + NPN_ReleaseVariantValue(&docv); + + STRINGZ_TO_NPVARIANT(strdup("foo return val"), *result); + + return PR_TRUE; + } + + return PR_FALSE; +} + +bool +ScriptablePluginObject::InvokeDefault(const NPVariant *args, uint32_t argCount, + NPVariant *result) +{ + printf ("ScriptablePluginObject default method called!\n"); + + STRINGZ_TO_NPVARIANT(strdup("default method return val"), *result); + + return PR_TRUE; +} + +CPlugin::CPlugin(NPP pNPInstance) : + m_pNPInstance(pNPInstance), + m_pNPStream(NULL), + m_bInitialized(FALSE), + m_pScriptableObject(NULL) +{ +#ifdef XP_WIN + m_hWnd = NULL; +#endif + + NPN_GetValue(m_pNPInstance, NPNVWindowNPObject, &sWindowObj); + + NPIdentifier n = NPN_GetStringIdentifier("foof"); + + sFoo_id = NPN_GetStringIdentifier("foo"); + sBar_id = NPN_GetStringIdentifier("bar"); + sDocument_id = NPN_GetStringIdentifier("document"); + sBody_id = NPN_GetStringIdentifier("body"); + sCreateElement_id = NPN_GetStringIdentifier("createElement"); + sCreateTextNode_id = NPN_GetStringIdentifier("createTextNode"); + sAppendChild_id = NPN_GetStringIdentifier("appendChild"); + sPluginType_id = NPN_GetStringIdentifier("PluginType"); + + NPVariant v; + INT32_TO_NPVARIANT(46, v); + + NPN_SetProperty(m_pNPInstance, sWindowObj, n, &v); + + NPVariant rval; + NPN_GetProperty(m_pNPInstance, sWindowObj, n, &rval); + + if (NPVARIANT_IS_INT32(rval)) { + printf("rval = %d\n", NPVARIANT_TO_INT32(rval)); + } + + n = NPN_GetStringIdentifier("document"); + + if (!NPN_IdentifierIsString(n)) { + NPString str; + str.utf8characters = "alert('NPN_IdentifierIsString() test failed!');"; + str.utf8length = strlen(str.utf8characters); + + NPN_Evaluate(m_pNPInstance, sWindowObj, &str, NULL); + } + + NPObject *doc; + + NPN_GetProperty(m_pNPInstance, sWindowObj, n, &rval); + + if (NPVARIANT_IS_OBJECT(rval) && (doc = NPVARIANT_TO_OBJECT(rval))) { + n = NPN_GetStringIdentifier("title"); + + NPN_GetProperty(m_pNPInstance, doc, n, &rval); + + if (NPVARIANT_IS_STRING(rval)) { + printf ("title = %s\n", NPVARIANT_TO_STRING(rval).utf8characters); + + NPN_ReleaseVariantValue(&rval); + } + + n = NPN_GetStringIdentifier("plugindoc"); + + OBJECT_TO_NPVARIANT(doc, v); + NPN_SetProperty(m_pNPInstance, sWindowObj, n, &v); + + NPString str; + str.utf8characters = "document.getElementById('result').innerHTML += '

' + 'NPN_Evaluate() test, document = ' + this + '

';"; + str.utf8length = strlen(str.utf8characters); + + NPN_Evaluate(m_pNPInstance, doc, &str, NULL); + + NPN_ReleaseObject(doc); + } + + NPVariant barval; + NPN_GetProperty(m_pNPInstance, sWindowObj, sBar_id, &barval); + + NPVariant arg; + OBJECT_TO_NPVARIANT(sWindowObj, arg); + + NPN_InvokeDefault(m_pNPInstance, NPVARIANT_TO_OBJECT(barval), &arg, 1, + &rval); + + if (NPVARIANT_IS_INT32(rval) && NPVARIANT_TO_INT32(rval) == 4) { + printf ("Default function call SUCCEEDED!\n"); + } else { + printf ("Default function call FAILED!\n"); + } + + NPN_ReleaseVariantValue(&barval); + NPN_ReleaseVariantValue(&rval); + + +#if 0 + n = NPN_GetStringIdentifier("prompt"); + + NPVariant vars[3]; + STRINGZ_TO_NPVARIANT("foo", vars[0]); + STRINGZ_TO_NPVARIANT("bar", vars[1]); + STRINGZ_TO_NPVARIANT("foof", vars[2]); + + NPN_Invoke(sWindowObj, n, vars, 3, &rval); + + if (NPVARIANT_IS_STRING(rval)) { + printf ("prompt returned '%s'\n", NPVARIANT_TO_STRING(rval).utf8characters); + } + + NPN_ReleaseVariantValue(&rval); +#endif + + NPObject *myobj = + NPN_CreateObject(m_pNPInstance, + GET_NPOBJECT_CLASS(ScriptablePluginObject)); + + n = NPN_GetStringIdentifier("pluginobj"); + + OBJECT_TO_NPVARIANT(myobj, v); + NPN_SetProperty(m_pNPInstance, sWindowObj, n, &v); + + NPN_GetProperty(m_pNPInstance, sWindowObj, n, &rval); + + printf ("Object set/get test "); + + if (NPVARIANT_IS_OBJECT(rval) && NPVARIANT_TO_OBJECT(rval) == myobj) { + printf ("succeeded!\n"); + } else { + printf ("FAILED!\n"); + } + + NPN_ReleaseVariantValue(&rval); + NPN_ReleaseObject(myobj); + + const char *ua = NPN_UserAgent(m_pNPInstance); + strcpy(m_String, ua); +} + +CPlugin::~CPlugin() +{ + if (sWindowObj) + NPN_ReleaseObject(sWindowObj); + if (m_pScriptableObject) + NPN_ReleaseObject(m_pScriptableObject); + + sWindowObj = 0; +} + +#ifdef XP_WIN +static LRESULT CALLBACK PluginWinProc(HWND, UINT, WPARAM, LPARAM); +static WNDPROC lpOldProc = NULL; +#endif + +NPBool CPlugin::init(NPWindow* pNPWindow) +{ + if(pNPWindow == NULL) + return FALSE; + +#ifdef XP_WIN + m_hWnd = (HWND)pNPWindow->window; + if(m_hWnd == NULL) + return FALSE; + + // subclass window so we can intercept window messages and + // do our drawing to it + lpOldProc = SubclassWindow(m_hWnd, (WNDPROC)PluginWinProc); + + // associate window with our CPlugin object so we can access + // it in the window procedure + SetWindowLong(m_hWnd, GWL_USERDATA, (LONG)this); +#endif + + m_Window = pNPWindow; + + m_bInitialized = TRUE; + return TRUE; +} + +void CPlugin::shut() +{ +#ifdef XP_WIN + // subclass it back + SubclassWindow(m_hWnd, lpOldProc); + m_hWnd = NULL; +#endif + + m_bInitialized = FALSE; +} + +NPBool CPlugin::isInitialized() +{ + return m_bInitialized; +} + +int16 CPlugin::handleEvent(void* event) +{ +#ifdef XP_MAC + NPEvent* ev = (NPEvent*)event; + if (m_Window) { + Rect box = { m_Window->y, m_Window->x, + m_Window->y + m_Window->height, m_Window->x + m_Window->width }; + if (ev->what == updateEvt) { + ::TETextBox(m_String, strlen(m_String), &box, teJustCenter); + } + } +#endif + return 0; +} + +// this will force to draw a version string in the plugin window +void CPlugin::showVersion() +{ + const char *ua = NPN_UserAgent(m_pNPInstance); + strcpy(m_String, ua); + +#ifdef XP_WIN + InvalidateRect(m_hWnd, NULL, TRUE); + UpdateWindow(m_hWnd); +#endif + + if (m_Window) { + NPRect r = + { + (uint16)m_Window->y, (uint16)m_Window->x, + (uint16)(m_Window->y + m_Window->height), + (uint16)(m_Window->x + m_Window->width) + }; + + NPN_InvalidateRect(m_pNPInstance, &r); + } +} + +// this will clean the plugin window +void CPlugin::clear() +{ + strcpy(m_String, ""); + +#ifdef XP_WIN + InvalidateRect(m_hWnd, NULL, TRUE); + UpdateWindow(m_hWnd); +#endif +} + +void CPlugin::getVersion(char* *aVersion) +{ + const char *ua = NPN_UserAgent(m_pNPInstance); + char*& version = *aVersion; + version = (char*)NPN_MemAlloc(1 + strlen(ua)); + if (version) + strcpy(version, ua); +} + +NPObject * +CPlugin::GetScriptableObject() +{ + if (!m_pScriptableObject) { + m_pScriptableObject = + NPN_CreateObject(m_pNPInstance, + GET_NPOBJECT_CLASS(ScriptablePluginObject)); + } + + if (m_pScriptableObject) { + NPN_RetainObject(m_pScriptableObject); + } + + return m_pScriptableObject; +} + +#ifdef XP_WIN +static LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) +{ + switch (msg) { + case WM_PAINT: + { + // draw a frame and display the string + PAINTSTRUCT ps; + HDC hdc = BeginPaint(hWnd, &ps); + RECT rc; + GetClientRect(hWnd, &rc); + FrameRect(hdc, &rc, GetStockBrush(BLACK_BRUSH)); + CPlugin * p = (CPlugin *)GetWindowLong(hWnd, GWL_USERDATA); + if(p) { + if (p->m_String[0] == 0) { + strcpy("foo", p->m_String); + } + + DrawText(hdc, p->m_String, strlen(p->m_String), &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER); + } + + EndPaint(hWnd, &ps); + } + break; + default: + break; + } + + return DefWindowProc(hWnd, msg, wParam, lParam); +} +#endif diff --git a/modules/plugin/samples/npruntime/plugin.h b/modules/plugin/samples/npruntime/plugin.h new file mode 100644 index 00000000000..70decab0650 --- /dev/null +++ b/modules/plugin/samples/npruntime/plugin.h @@ -0,0 +1,80 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: NPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Netscape 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://www.mozilla.org/NPL/ + * + * 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. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the NPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the NPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef __PLUGIN_H__ +#define __PLUGIN_H__ + +#include "npapi.h" +#include "npruntime.h" + +class CPlugin +{ +private: + NPP m_pNPInstance; + +#ifdef XP_WIN + HWND m_hWnd; +#endif + + NPWindow * m_Window; + + NPStream * m_pNPStream; + NPBool m_bInitialized; + + NPObject *m_pScriptableObject; + +public: + char m_String[128]; + +public: + CPlugin(NPP pNPInstance); + ~CPlugin(); + + NPBool init(NPWindow* pNPWindow); + void shut(); + NPBool isInitialized(); + + int16 handleEvent(void* event); + + void showVersion(); + void clear(); + void getVersion(char* *aVersion); + + NPObject *GetScriptableObject(); +}; + +#endif // __PLUGIN_H__ diff --git a/modules/plugin/samples/npruntime/resource.h b/modules/plugin/samples/npruntime/resource.h new file mode 100644 index 00000000000..43f7ebf6241 --- /dev/null +++ b/modules/plugin/samples/npruntime/resource.h @@ -0,0 +1,20 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Developer Studio generated include file. +// Used by np4xscr.rc +// +#define IDD_MAIN 101 +#define IDC_BUTTON_GO 1002 +#define IDC_STATIC_UA 1003 +#define IDC_BUTTON1 1005 +#define IDC_BUTTON_DONT 1005 + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 102 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1006 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/modules/plugin/samples/npruntime/test.html b/modules/plugin/samples/npruntime/test.html new file mode 100644 index 00000000000..8bded7e2145 --- /dev/null +++ b/modules/plugin/samples/npruntime/test.html @@ -0,0 +1,61 @@ + + +Scriptable Plug-in Test + + + +
+

Sample Scriptable Plug-in

+
+ +This page contains a testcase which demonstrates the work of +scriptable 4.x style Navigator plug-in with Mozilla. The example +plug-in occupies the area right below this text, and you should see a +frame the plug-in draws around its window. Below the plug-in window +there are two buttons. Clicking on the buttons will result in calling +native plugin methods from JavaScript. Show Version will instruct the +plug-in to retrieve the Mozilla user agent string and display it in +the plug-in window, Clear button will call plug-in method to erase the +window. + +

+ +
+ + + +
+

results go here:

+
+ +
+ +
+ + + +
+
+ + + +
+ + + + +
+ +
+ + +