From 3d8172178a5271a6c0ed7acfdff87f047aed00bd Mon Sep 17 00:00:00 2001 From: "av%netscape.com" Date: Fri, 31 Aug 2001 23:00:11 +0000 Subject: [PATCH] Bug 97488 -- sample code to reflect new approach to scripting, not part of the build, patch by jband and av, r=av --- .../plugin/samples/4x-scriptable/makefile.win | 7 +- .../plugin/samples/4x-scriptable/npp_gate.cpp | 4 +- .../4x-scriptable/nsI4xScriptablePlugin.idl | 29 +++++++ .../4x-scriptable/nsScriptablePeer.cpp | 76 ++----------------- .../samples/4x-scriptable/nsScriptablePeer.h | 46 +++++++---- .../4x-scriptable/nsScriptablePeer1.cpp | 65 +--------------- .../samples/4x-scriptable/nsScriptablePeer1.h | 43 ++++++++--- .../plugin/samples/4x-scriptable/plugin.cpp | 2 +- modules/plugin/samples/4x-scriptable/plugin.h | 4 +- 9 files changed, 114 insertions(+), 162 deletions(-) create mode 100644 modules/plugin/samples/4x-scriptable/nsI4xScriptablePlugin.idl diff --git a/modules/plugin/samples/4x-scriptable/makefile.win b/modules/plugin/samples/4x-scriptable/makefile.win index c76de7dc09d7..a963e67aa7d2 100644 --- a/modules/plugin/samples/4x-scriptable/makefile.win +++ b/modules/plugin/samples/4x-scriptable/makefile.win @@ -21,7 +21,7 @@ include <$(DEPTH)/config/config.mak> MODULE = np4xscr XPIDLSRCS = \ - .\nsI4xScrPlugin.idl \ + .\nsI4xScriptablePlugin.idl \ $(NULL) MAKE_OBJ_TYPE = DLL @@ -40,6 +40,11 @@ OBJS = \ WIN_LIBS = version.lib +#uncomment if you are building the nsScriptablePeer1 version +#LLIBS= $(LIBNSPR) \ +# $(DIST)\lib\xpcom.lib \ +# $(NULL) + include <$(DEPTH)/config/rules.mak> #MAKE_INSTALL=echo $(MAKE_INSTALL) diff --git a/modules/plugin/samples/4x-scriptable/npp_gate.cpp b/modules/plugin/samples/4x-scriptable/npp_gate.cpp index 401aae109022..107974758da2 100644 --- a/modules/plugin/samples/4x-scriptable/npp_gate.cpp +++ b/modules/plugin/samples/4x-scriptable/npp_gate.cpp @@ -132,11 +132,11 @@ NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value) if(pPlugin == NULL) return NPERR_GENERIC_ERROR; - static nsIID scriptableIID = NS_I4XSCRPLUGIN_IID; + static nsIID scriptableIID = NS_I4XSCRIPTABLEPLUGIN_IID; if (variable == NPPVpluginScriptableInstance) { // addref happens in getter, so we don't addref here - nsI4xScrPlugin * scriptablePeer = pPlugin->getScriptablePeer(); + nsI4xScriptablePlugin * scriptablePeer = pPlugin->getScriptablePeer(); *(nsISupports **)value = scriptablePeer; } else if (variable == NPPVpluginScriptableIID) { diff --git a/modules/plugin/samples/4x-scriptable/nsI4xScriptablePlugin.idl b/modules/plugin/samples/4x-scriptable/nsI4xScriptablePlugin.idl new file mode 100644 index 000000000000..7584c366ce01 --- /dev/null +++ b/modules/plugin/samples/4x-scriptable/nsI4xScriptablePlugin.idl @@ -0,0 +1,29 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * 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. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 2001 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + */ + +#include "nsISupports.idl" + +[scriptable, uuid(482e1890-1fe5-11d5-9cf8-0060b0fbd8ac)] +interface nsI4xScriptablePlugin : nsISupports { + void showVersion(); + void clear(); +}; diff --git a/modules/plugin/samples/4x-scriptable/nsScriptablePeer.cpp b/modules/plugin/samples/4x-scriptable/nsScriptablePeer.cpp index 22672dfee02d..0be4c7588f48 100644 --- a/modules/plugin/samples/4x-scriptable/nsScriptablePeer.cpp +++ b/modules/plugin/samples/4x-scriptable/nsScriptablePeer.cpp @@ -33,8 +33,8 @@ // #include "plugin.h" -static NS_DEFINE_IID(kI4xScrPluginIID, NS_I4XSCRPLUGIN_IID); -static NS_DEFINE_IID(kISecurityCheckedComponentIID, NS_ISECURITYCHECKEDCOMPONENT_IID); +static NS_DEFINE_IID(kI4xScriptablePluginIID, NS_I4XSCRIPTABLEPLUGIN_IID); +static NS_DEFINE_IID(kIClassInfoIID, NS_ICLASSINFO_IID); static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); nsScriptablePeer::nsScriptablePeer(CPlugin* aPlugin) @@ -72,20 +72,20 @@ NS_IMETHODIMP nsScriptablePeer::QueryInterface(const nsIID& aIID, void** aInstan if(!aInstancePtr) return NS_ERROR_NULL_POINTER; - if(aIID.Equals(kI4xScrPluginIID)) { - *aInstancePtr = static_cast(this); + if(aIID.Equals(kI4xScriptablePluginIID)) { + *aInstancePtr = static_cast(this); AddRef(); return NS_OK; } - if(aIID.Equals(kISecurityCheckedComponentIID)) { - *aInstancePtr = static_cast(this); + if(aIID.Equals(kIClassInfoIID)) { + *aInstancePtr = static_cast(this); AddRef(); return NS_OK; } if(aIID.Equals(kISupportsIID)) { - *aInstancePtr = static_cast(static_cast(this)); + *aInstancePtr = static_cast(static_cast(this)); AddRef(); return NS_OK; } @@ -111,65 +111,3 @@ NS_IMETHODIMP nsScriptablePeer::Clear() return NS_OK; } - -// -// the purpose of the rest of the code is to get succesfully -// through the Mozilla Security Manager -// -static const char gAllAccess[] = "AllAccess"; - -NS_IMETHODIMP nsScriptablePeer::CanCreateWrapper(const nsIID * iid, char **_retval) -{ - if (!_retval) - return NS_ERROR_NULL_POINTER; - - *_retval = (char*)NPN_MemAlloc(sizeof(gAllAccess)+1); - if (!*_retval) - return NS_ERROR_OUT_OF_MEMORY; - - strcpy(*_retval, gAllAccess); - - return NS_OK; -} - -NS_IMETHODIMP nsScriptablePeer::CanCallMethod(const nsIID * iid, const PRUnichar *methodName, char **_retval) -{ - if (!_retval) - return NS_ERROR_NULL_POINTER; - - *_retval = (char*)NPN_MemAlloc(sizeof(gAllAccess)+1); - if (!*_retval) - return NS_ERROR_OUT_OF_MEMORY; - - strcpy(*_retval, gAllAccess); - - return NS_OK; -} - -NS_IMETHODIMP nsScriptablePeer::CanGetProperty(const nsIID * iid, const PRUnichar *propertyName, char **_retval) -{ - if (!_retval) - return NS_ERROR_NULL_POINTER; - - *_retval = (char*)NPN_MemAlloc(sizeof(gAllAccess)+1); - if (!*_retval) - return NS_ERROR_OUT_OF_MEMORY; - - strcpy(*_retval, gAllAccess); - - return NS_OK; -} - -NS_IMETHODIMP nsScriptablePeer::CanSetProperty(const nsIID * iid, const PRUnichar *propertyName, char **_retval) -{ - if (!_retval) - return NS_ERROR_NULL_POINTER; - - *_retval = (char*)NPN_MemAlloc(sizeof(gAllAccess)+1); - if (!*_retval) - return NS_ERROR_OUT_OF_MEMORY; - - strcpy(*_retval, gAllAccess); - - return NS_OK; -} diff --git a/modules/plugin/samples/4x-scriptable/nsScriptablePeer.h b/modules/plugin/samples/4x-scriptable/nsScriptablePeer.h index 6d26264aea7b..de5dc478988f 100644 --- a/modules/plugin/samples/4x-scriptable/nsScriptablePeer.h +++ b/modules/plugin/samples/4x-scriptable/nsScriptablePeer.h @@ -30,17 +30,41 @@ #ifndef __nsScriptablePeer_h__ #define __nsScriptablePeer_h__ -#include "nsI4xScrPlugin.h" -#include "nsISecurityCheckedComponent.h" +#include "nsI4xScriptablePlugin.h" +#include "nsIClassInfo.h" class CPlugin; -// we should add nsISecurityCheckedComponent because the -// Mozilla Security Manager will ask scriptable object about -// priveleges the plugin requests in order to allow calls -// from JavaScript -class nsScriptablePeer : public nsI4xScrPlugin, - public nsISecurityCheckedComponent +// We must implement nsIClassInfo because it signals the +// Mozilla Security Manager to allow calls from JavaScript. + +class nsClassInfoMixin : public nsIClassInfo +{ + // These flags are used by the DOM and security systems to signal that + // JavaScript callers are allowed to call this object's scritable methods. + NS_IMETHOD GetFlags(PRUint32 *aFlags) + {*aFlags = nsIClassInfo::PLUGIN_OBJECT | nsIClassInfo::DOM_OBJECT; + return NS_OK;} + NS_IMETHOD GetImplementationLanguage(PRUint32 *aImplementationLanguage) + {*aImplementationLanguage = nsIProgrammingLanguage::CPLUSPLUS; + return NS_OK;} + // The rest of the methods can safely return error codes... + NS_IMETHOD GetInterfaces(PRUint32 *count, nsIID * **array) + {return NS_ERROR_NOT_IMPLEMENTED;} + NS_IMETHOD GetHelperForLanguage(PRUint32 language, nsISupports **_retval) + {return NS_ERROR_NOT_IMPLEMENTED;} + NS_IMETHOD GetContractID(char * *aContractID) + {return NS_ERROR_NOT_IMPLEMENTED;} + NS_IMETHOD GetClassDescription(char * *aClassDescription) + {return NS_ERROR_NOT_IMPLEMENTED;} + NS_IMETHOD GetClassID(nsCID * *aClassID) + {return NS_ERROR_NOT_IMPLEMENTED;} + NS_IMETHOD GetClassIDNoAlloc(nsCID *aClassIDNoAlloc) + {return NS_ERROR_NOT_IMPLEMENTED;} +}; + +class nsScriptablePeer : public nsI4xScriptablePlugin, + public nsClassInfoMixin { public: nsScriptablePeer(CPlugin* plugin); @@ -60,12 +84,6 @@ public: NS_IMETHOD ShowVersion(); NS_IMETHOD Clear(); - // methods from nsISecurityCheckedComponent - NS_IMETHOD CanCreateWrapper(const nsIID * iid, char **_retval); - NS_IMETHOD CanCallMethod(const nsIID * iid, const PRUnichar *methodName, char **_retval); - NS_IMETHOD CanGetProperty(const nsIID * iid, const PRUnichar *propertyName, char **_retval); - NS_IMETHOD CanSetProperty(const nsIID * iid, const PRUnichar *propertyName, char **_retval); - protected: CPlugin* mPlugin; }; diff --git a/modules/plugin/samples/4x-scriptable/nsScriptablePeer1.cpp b/modules/plugin/samples/4x-scriptable/nsScriptablePeer1.cpp index 9c03ecc101a5..19d781f2a997 100644 --- a/modules/plugin/samples/4x-scriptable/nsScriptablePeer1.cpp +++ b/modules/plugin/samples/4x-scriptable/nsScriptablePeer1.cpp @@ -44,7 +44,8 @@ nsScriptablePeer::~nsScriptablePeer() { } -NS_IMPL_ISUPPORTS2(nsScriptablePeer, nsI4xScrPlugin, nsISecurityCheckedComponent) +// Notice that we expose our claim to implement nsIClassInfo. +NS_IMPL_ISUPPORTS2(nsScriptablePeer, nsI4xScriptablePlugin, nsIClassInfo) // // the following two methods will be callable from JavaScript @@ -64,65 +65,3 @@ NS_IMETHODIMP nsScriptablePeer::Clear() return NS_OK; } - -// -// the purpose of the rest of the code is to get succesfully -// through the Mozilla Security Manager -// -static const char gAllAccess[] = "AllAccess"; - -NS_IMETHODIMP nsScriptablePeer::CanCreateWrapper(const nsIID * iid, char **_retval) -{ - if (!_retval) - return NS_ERROR_NULL_POINTER; - - *_retval = (char*)NPN_MemAlloc(sizeof(gAllAccess)+1); - if (!*_retval) - return NS_ERROR_OUT_OF_MEMORY; - - strcpy(*_retval, gAllAccess); - - return NS_OK; -} - -NS_IMETHODIMP nsScriptablePeer::CanCallMethod(const nsIID * iid, const PRUnichar *methodName, char **_retval) -{ - if (!_retval) - return NS_ERROR_NULL_POINTER; - - *_retval = (char*)NPN_MemAlloc(sizeof(gAllAccess)+1); - if (!*_retval) - return NS_ERROR_OUT_OF_MEMORY; - - strcpy(*_retval, gAllAccess); - - return NS_OK; -} - -NS_IMETHODIMP nsScriptablePeer::CanGetProperty(const nsIID * iid, const PRUnichar *propertyName, char **_retval) -{ - if (!_retval) - return NS_ERROR_NULL_POINTER; - - *_retval = (char*)NPN_MemAlloc(sizeof(gAllAccess)+1); - if (!*_retval) - return NS_ERROR_OUT_OF_MEMORY; - - strcpy(*_retval, gAllAccess); - - return NS_OK; -} - -NS_IMETHODIMP nsScriptablePeer::CanSetProperty(const nsIID * iid, const PRUnichar *propertyName, char **_retval) -{ - if (!_retval) - return NS_ERROR_NULL_POINTER; - - *_retval = (char*)NPN_MemAlloc(sizeof(gAllAccess)+1); - if (!*_retval) - return NS_ERROR_OUT_OF_MEMORY; - - strcpy(*_retval, gAllAccess); - - return NS_OK; -} diff --git a/modules/plugin/samples/4x-scriptable/nsScriptablePeer1.h b/modules/plugin/samples/4x-scriptable/nsScriptablePeer1.h index 8c666e47e923..402cb0ab01a0 100644 --- a/modules/plugin/samples/4x-scriptable/nsScriptablePeer1.h +++ b/modules/plugin/samples/4x-scriptable/nsScriptablePeer1.h @@ -30,25 +30,48 @@ #ifndef __nsScriptablePeer_h__ #define __nsScriptablePeer_h__ -#include "nsI4xScrPlugin.h" -#include "nsISecurityCheckedComponent.h" +#include "nsI4xScriptablePlugin.h" +#include "nsIClassInfo.h" class CPlugin; -// we should add nsISecurityCheckedComponent because the -// Mozilla Security Manager will ask scriptable object about -// priveleges the plugin requests in order to allow calls -// from JavaScript -class nsScriptablePeer : public nsI4xScrPlugin, - public nsISecurityCheckedComponent +// We must implement nsIClassInfo because it signals the +// Mozilla Security Manager to allow calls from JavaScript. + +class nsClassInfoMixin : public nsIClassInfo +{ + // These flags are used by the DOM and security systems to signal that + // JavaScript callers are allowed to call this object's scritable methods. + NS_IMETHOD GetFlags(PRUint32 *aFlags) + {*aFlags = nsIClassInfo::PLUGIN_OBJECT | nsIClassInfo::DOM_OBJECT; + return NS_OK;} + NS_IMETHOD GetImplementationLanguage(PRUint32 *aImplementationLanguage) + {*aImplementationLanguage = nsIProgrammingLanguage::CPLUSPLUS; + return NS_OK;} + // The rest of the methods can safely return error codes... + NS_IMETHOD GetInterfaces(PRUint32 *count, nsIID * **array) + {return NS_ERROR_NOT_IMPLEMENTED;} + NS_IMETHOD GetHelperForLanguage(PRUint32 language, nsISupports **_retval) + {return NS_ERROR_NOT_IMPLEMENTED;} + NS_IMETHOD GetContractID(char * *aContractID) + {return NS_ERROR_NOT_IMPLEMENTED;} + NS_IMETHOD GetClassDescription(char * *aClassDescription) + {return NS_ERROR_NOT_IMPLEMENTED;} + NS_IMETHOD GetClassID(nsCID * *aClassID) + {return NS_ERROR_NOT_IMPLEMENTED;} + NS_IMETHOD GetClassIDNoAlloc(nsCID *aClassIDNoAlloc) + {return NS_ERROR_NOT_IMPLEMENTED;} +}; + +class nsScriptablePeer : public nsI4xScriptablePlugin, + public nsClassInfoMixin { public: nsScriptablePeer(CPlugin* plugin); ~nsScriptablePeer(); NS_DECL_ISUPPORTS - NS_DECL_NSI4XSCRPLUGIN - NS_DECL_NSISECURITYCHECKEDCOMPONENT + NS_DECL_NSI4XSCRIPTABLEPLUGIN protected: CPlugin* mPlugin; diff --git a/modules/plugin/samples/4x-scriptable/plugin.cpp b/modules/plugin/samples/4x-scriptable/plugin.cpp index cb391a7a7102..ca4f5daa1e80 100644 --- a/modules/plugin/samples/4x-scriptable/plugin.cpp +++ b/modules/plugin/samples/4x-scriptable/plugin.cpp @@ -121,7 +121,7 @@ void CPlugin::clear() // ============================== // // this method will return the scriptable object (and create it if necessary) -nsI4xScrPlugin* CPlugin::getScriptablePeer() +nsI4xScriptablePlugin* CPlugin::getScriptablePeer() { if (!m_pScriptablePeer) { m_pScriptablePeer = new nsScriptablePeer(this); diff --git a/modules/plugin/samples/4x-scriptable/plugin.h b/modules/plugin/samples/4x-scriptable/plugin.h index a9e45aa13e10..4e49ec0dcd16 100644 --- a/modules/plugin/samples/4x-scriptable/plugin.h +++ b/modules/plugin/samples/4x-scriptable/plugin.h @@ -37,7 +37,7 @@ private: NPStream * m_pNPStream; BOOL m_bInitialized; - nsI4xScrPlugin * m_pScriptablePeer; + nsI4xScriptablePlugin * m_pScriptablePeer; public: char m_String[128]; @@ -53,7 +53,7 @@ public: void showVersion(); void clear(); - nsI4xScrPlugin* getScriptablePeer(); + nsI4xScriptablePlugin* getScriptablePeer(); }; #endif // __PLUGIN_H__