diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-06 03:01:46 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-06 03:01:46 +0000 |
commit | f8fe689a81f906d1b91bb3220acde2a4ecb14c5b (patch) | |
tree | 26484e9d7e2c67806c2d1760196ff01aaa858e8c /src/libs/xpcom18a4/xpcom/sample | |
parent | Initial commit. (diff) | |
download | virtualbox-f8fe689a81f906d1b91bb3220acde2a4ecb14c5b.tar.xz virtualbox-f8fe689a81f906d1b91bb3220acde2a4ecb14c5b.zip |
Adding upstream version 6.0.4-dfsg.upstream/6.0.4-dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/libs/xpcom18a4/xpcom/sample')
-rw-r--r-- | src/libs/xpcom18a4/xpcom/sample/.cvsignore | 2 | ||||
-rw-r--r-- | src/libs/xpcom18a4/xpcom/sample/Makefile.in | 113 | ||||
-rw-r--r-- | src/libs/xpcom18a4/xpcom/sample/nsISample.idl | 66 | ||||
-rw-r--r-- | src/libs/xpcom18a4/xpcom/sample/nsSample.cpp | 181 | ||||
-rw-r--r-- | src/libs/xpcom18a4/xpcom/sample/nsSample.h | 129 | ||||
-rw-r--r-- | src/libs/xpcom18a4/xpcom/sample/nsSample.js | 130 | ||||
-rw-r--r-- | src/libs/xpcom18a4/xpcom/sample/nsSampleModule.cpp | 131 | ||||
-rw-r--r-- | src/libs/xpcom18a4/xpcom/sample/nsTestSample.cpp | 158 | ||||
-rw-r--r-- | src/libs/xpcom18a4/xpcom/sample/win32.order | 1 | ||||
-rw-r--r-- | src/libs/xpcom18a4/xpcom/sample/xpconnect-sample.html | 220 |
10 files changed, 1131 insertions, 0 deletions
diff --git a/src/libs/xpcom18a4/xpcom/sample/.cvsignore b/src/libs/xpcom18a4/xpcom/sample/.cvsignore new file mode 100644 index 00000000..d1f93146 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/sample/.cvsignore @@ -0,0 +1,2 @@ +Makefile +nsTestSample diff --git a/src/libs/xpcom18a4/xpcom/sample/Makefile.in b/src/libs/xpcom18a4/xpcom/sample/Makefile.in new file mode 100644 index 00000000..5510e795 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/sample/Makefile.in @@ -0,0 +1,113 @@ +# +# ***** BEGIN LICENSE BLOCK ***** +# 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://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 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 of 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 MPL, 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 MPL, the GPL or the LGPL. +# +# ***** END LICENSE BLOCK ***** + +DEPTH = ../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = xpcomsample +XPIDL_MODULE = xpcomsample +LIBRARY_NAME = xpcomsample +SHORT_LIBNAME = xpcomsmp + +IS_COMPONENT = 1 +MODULE_NAME = nsSampleModule + +# Ensure that the xpcom classes that we build +# do not export themselves +DEFINES += -DXPCOM_GLUE + + +REQUIRES = string \ + xpcom \ + $(NULL) + +CPPSRCS = \ + nsSample.cpp \ + nsSampleModule.cpp \ + $(NULL) + +XPIDLSRCS = nsISample.idl + +TESTCPPSRCS = nsTestSample.cpp + +CPPSRCS += $(TESTCPPSRCS) + +SIMPLE_PROGRAMS = $(TESTCPPSRCS:.cpp=$(BIN_SUFFIX)) + +include $(topsrcdir)/config/config.mk + +EXTRA_COMPONENTS = nsSample.js + +# separate libraries linked in. +EXTRA_DSO_LDOPTS = \ + $(DIST)/lib/$(LIB_PREFIX)xpcomglue.$(LIB_SUFFIX) \ + $(NSPR_LIBS) \ + $(NULL) + +LIBS = \ + $(DIST)/lib/$(LIB_PREFIX)xpcomglue.$(LIB_SUFFIX) \ + $(NSPR_LIBS) \ + $(NULL) + +# Needed to resolve __yylex (?) +ifeq ($(OS_ARCH)$(OS_RELEASE),FreeBSD2) +LIBS += -lpcap +endif + +# Need to link with CoreFoundation on Mac +ifneq (,$(filter mac cocoa,$(MOZ_WIDGET_TOOLKIT))) +EXTRA_DSO_LDOPTS += \ + $(TK_LIBS) \ + $(NULL) + +LIBS += \ + $(TK_LIBS) \ + $(NULL) +endif + +include $(topsrcdir)/config/rules.mk + +libs:: $(TARGETS) + $(INSTALL) $(srcdir)/xpconnect-sample.html $(DIST)/bin/res/samples + +install:: $(TARGETS) + $(SYSINSTALL) $(IFLAGS1) $(srcdir)/xpconnect-sample.html $(DESTDIR)$(mozappdir)/res/samples + diff --git a/src/libs/xpcom18a4/xpcom/sample/nsISample.idl b/src/libs/xpcom18a4/xpcom/sample/nsISample.idl new file mode 100644 index 00000000..22454e7b --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/sample/nsISample.idl @@ -0,0 +1,66 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * 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://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 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 of 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 MPL, 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 MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +/** + * A sample of XPConnect. This file contains a sample interface. + * + */ + +#include "nsISupports.idl" + +/** + * The uuid is a unique number identifying the interface normally + * called IID. It can be generated as follows: + * + * Windows: guidgen.exe + * Unix: uuidgen which comes with e2fsprogs package + * Mac: ??? + * All platform: Using irc, connect to irc.mozilla.org and type in + * /join #mozilla + * /msg mozbot uuid + * + */ +[scriptable, uuid(7CB5B7A1-07D7-11d3-BDE2-000064657374)] +interface nsISample : nsISupports +{ + attribute string value; + void writeValue(in string aPrefix); + void poke(in string aValue); +}; + + + diff --git a/src/libs/xpcom18a4/xpcom/sample/nsSample.cpp b/src/libs/xpcom18a4/xpcom/sample/nsSample.cpp new file mode 100644 index 00000000..ac3d8449 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/sample/nsSample.cpp @@ -0,0 +1,181 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * 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://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 the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Pierre Phaneuf <pp@ludusdesign.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either of 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 MPL, 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 MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + + +/** + * + * A sample of XPConnect. This file contains an implementation nsSample + * of the interface nsISample. + * + */ +#include <stdio.h> + +#include "nsSample.h" +#include "nsMemory.h" + +#include "nsEmbedString.h" +//////////////////////////////////////////////////////////////////////// + +nsSampleImpl::nsSampleImpl() : mValue(nsnull) +{ + mValue = (char*)nsMemory::Clone("initial value", 14); +} + +nsSampleImpl::~nsSampleImpl() +{ + if (mValue) + nsMemory::Free(mValue); +} + +/** + * NS_IMPL_ISUPPORTS1 expands to a simple implementation of the nsISupports + * interface. This includes a proper implementation of AddRef, Release, + * and QueryInterface. If this class supported more interfaces than just + * nsISupports, + * you could use NS_IMPL_ADDREF() and NS_IMPL_RELEASE() to take care of the + * simple stuff, but you would have to create QueryInterface on your own. + * nsSampleFactory.cpp is an example of this approach. + * Notice that the second parameter to the macro is name of the interface, and + * NOT the #defined IID. + * + * The _CI variant adds support for nsIClassInfo, which permits introspection + * and interface flattening. + */ +NS_IMPL_ISUPPORTS1_CI(nsSampleImpl, nsISample) + +/** + * Notice that in the protoype for this function, the NS_IMETHOD macro was + * used to declare the return type. For the implementation, the return + * type is declared by NS_IMETHODIMP + */ +NS_IMETHODIMP +nsSampleImpl::GetValue(char** aValue) +{ + NS_PRECONDITION(aValue != nsnull, "null ptr"); + if (! aValue) + return NS_ERROR_NULL_POINTER; + + if (mValue) { + /** + * GetValue's job is to return data known by an instance of + * nsSampleImpl to the outside world. If we were to simply return + * a pointer to data owned by this instance, and the client were to + * free it, bad things would surely follow. + * On the other hand, if we create a new copy of the data for our + * client, and it turns out that client is implemented in JavaScript, + * there would be no way to free the buffer. The solution to the + * buffer ownership problem is the nsMemory singleton. Any buffer + * returned by an XPCOM method should be allocated by the nsMemory. + * This convention lets things like JavaScript reflection do their + * job, and simplifies the way C++ clients deal with returned buffers. + */ + *aValue = (char*) nsMemory::Clone(mValue, strlen(mValue) + 1); + if (! *aValue) + return NS_ERROR_NULL_POINTER; + } + else { + *aValue = nsnull; + } + return NS_OK; +} + +NS_IMETHODIMP +nsSampleImpl::SetValue(const char* aValue) +{ + NS_PRECONDITION(aValue != nsnull, "null ptr"); + if (! aValue) + return NS_ERROR_NULL_POINTER; + + if (mValue) { + nsMemory::Free(mValue); + } + + /** + * Another buffer passing convention is that buffers passed INTO your + * object ARE NOT YOURS. Keep your hands off them, unless they are + * declared "inout". If you want to keep the value for posterity, + * you will have to make a copy of it. + */ + mValue = (char*) nsMemory::Clone(aValue, strlen(aValue) + 1); + return NS_OK; +} + +NS_IMETHODIMP +nsSampleImpl::Poke(const char* aValue) +{ + return SetValue((char*) aValue); +} + + +static void GetStringValue(nsACString& aValue) +{ + NS_CStringSetData(aValue, "GetValue"); +} + +NS_IMETHODIMP +nsSampleImpl::WriteValue(const char* aPrefix) +{ + NS_PRECONDITION(aPrefix != nsnull, "null ptr"); + if (! aPrefix) + return NS_ERROR_NULL_POINTER; + + printf("%s %s\n", aPrefix, mValue); + + // This next part illustrates the nsEmbedString: + nsEmbedString foopy; + foopy.Append(PRUnichar('f')); + foopy.Append(PRUnichar('o')); + foopy.Append(PRUnichar('o')); + foopy.Append(PRUnichar('p')); + foopy.Append(PRUnichar('y')); + + const PRUnichar* f = foopy.get(); + PRUint32 l = foopy.Length(); + printf("%c%c%c%c%c %d\n", char(f[0]), char(f[1]), char(f[2]), char(f[3]), char(f[4]), l); + + nsEmbedCString foopy2; + GetStringValue(foopy2); + + //foopy2.AppendLiteral("foopy"); + const char* f2 = foopy2.get(); + PRUint32 l2 = foopy2.Length(); + + printf("%s %d\n", f2, l2); + + return NS_OK; +} diff --git a/src/libs/xpcom18a4/xpcom/sample/nsSample.h b/src/libs/xpcom18a4/xpcom/sample/nsSample.h new file mode 100644 index 00000000..40dec0b3 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/sample/nsSample.h @@ -0,0 +1,129 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * 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://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 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 of 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 MPL, 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 MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +/** + * A sample of XPConnect. This file is the header of an implementation + * nsSample of the nsISample interface. + * + */ + +#include "nsISample.h" + +/** + * SampleImpl is an implementation of the nsISample interface. In XPCOM, + * there can be more than one implementation of an given interface. Class + * IDs (CIDs) uniquely identify a particular implementation of an interface. + * Interface IDs (IIDs) uniquely identify an interface. + * + * The CID is also a unique number that looks just like an IID + * and uniquely identifies an implementation + * {7CB5B7A0-07D7-11d3-BDE2-000064657374} + */ + +#define NS_SAMPLE_CID \ +{ 0x7cb5b7a0, 0x7d7, 0x11d3, { 0xbd, 0xe2, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } } + +#define NS_SAMPLE_CONTRACTID "@mozilla.org/sample;1" + + +class nsSampleImpl : public nsISample +{ +public: + nsSampleImpl(); + + /** + * This macro expands into a declaration of the nsISupports interface. + * Every XPCOM component needs to implement nsISupports, as it acts + * as the gateway to other interfaces this component implements. You + * could manually declare QueryInterface, AddRef, and Release instead + * of using this macro, but why? + */ + // nsISupports interface + NS_DECL_ISUPPORTS + + /** + * This macro is defined in the nsISample.h file, and is generated + * automatically by the xpidl compiler. It expands to + * declarations of all of the methods required to implement the + * interface. xpidl will generate a NS_DECL_[INTERFACENAME] macro + * for each interface that it processes. + * + * The methods of nsISample are discussed individually below, but + * commented out (because this macro already defines them.) + */ + NS_DECL_NSISAMPLE + + /** + * The following is an explanation of how the interface header + * file expands to for a c++ implementation. NS_DELC_NSISAMPLE + * takes care of defining the right c++ implementation. + * + * The following if provided for more understanding. + * + * NS_IMETHOD expands to the standard XPCOM return type. XPCOM methods + * should never return any other type. The return value is used + * behind the scenes by the XPConnect runtime to figure out if the call + * failed in any way. + * These methods were generated by "attribute string Value" in + * nsISample.idl. When reflected into JavaScript, XPCOM will use these + * calls as Getter/Setter ops, so that they can be called transparently + * as "sample.Value='foo';" and "var val = sample.Value" + */ + /* NS_IMETHOD GetValue(char * *aValue); */ + /* NS_IMETHOD SetValue(char * aValue); */ + + /** + * The const came from the "in" specifier in nsISample.idl. "in" + * specifies that the value of this parameter is used only for input, + * this method is not allowed to modify the contents of the buffer. + */ + /* NS_IMETHOD WriteValue(const char *aPrefix); */ + + /** + * nsISample.idl specifies all of it's string types as string, instead + * of wstring (wide string), the Unicode type. If the world were a + * perfect place, all normal strings in XPCOM interfaces would be unicode. + * If this type had been specified as wstring, it would appear as + * PRUnichar * in C++, which is the NSPR type for unicode characters. + */ + /* NS_IMETHOD Poke(const char* aValue); */ + +private: + ~nsSampleImpl(); + + char* mValue; +}; diff --git a/src/libs/xpcom18a4/xpcom/sample/nsSample.js b/src/libs/xpcom18a4/xpcom/sample/nsSample.js new file mode 100644 index 00000000..1cc9a091 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/sample/nsSample.js @@ -0,0 +1,130 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* + * No magic constructor behaviour, as is de rigeur for XPCOM. + * If you must perform some initialization, and it could possibly fail (even + * due to an out-of-memory condition), you should use an Init method, which + * can convey failure appropriately (thrown exception in JS, + * NS_FAILED(nsresult) return in C++). + * + * In JS, you can actually cheat, because a thrown exception will cause the + * CreateInstance call to fail in turn, but not all languages are so lucky. + * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code + * for portability reasons -- and even when you're building completely + * platform-specific code, you can't throw across an XPCOM method boundary.) + */ +function mySample() { /* big comment for no code, eh? */ } + +/* decorate prototype to provide ``class'' methods and property accessors */ +mySample.prototype = { + /* + * get and set are new Magic in JS1.5, borrowing the intent -- if not + * the exact syntax -- from the JS2 design. They define accessors for + * properties on the JS object, follow the expected rules for prototype + * delegation, and make a mean cup of coffee. + */ + get value() { return this.val; }, + set value(newval) { return this.val = newval; }, + + writeValue: function (aPrefix) { + debug("mySample::writeValue => " + aPrefix + this.val + "\n"); + }, + poke: function (aValue) { this.val = aValue; }, + + QueryInterface: function (iid) { + if (iid.equals(Components.interfaces.nsISample) || + iid.equals(Components.interfaces.nsISupports)) + return this; + + Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE; + return null; + }, + + val: "<default value>" +} + +var myModule = { + firstTime: true, + + /* + * RegisterSelf is called at registration time (component installation + * or the only-until-release startup autoregistration) and is responsible + * for notifying the component manager of all components implemented in + * this module. The fileSpec, location and type parameters are mostly + * opaque, and should be passed on to the registerComponent call + * unmolested. + */ + registerSelf: function (compMgr, fileSpec, location, type) { + if (this.firstTime) { + debug("*** Deferring registration of sample JS components\n"); + this.firstTime = false; + throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN; + } + debug("*** Registering sample JS components\n"); + compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar); + compMgr.registerFactoryLocation(this.myCID, + "Sample JS Component", + this.myProgID, + fileSpec, + location, + type); + }, + + /* + * The GetClassObject method is responsible for producing Factory objects + */ + getClassObject: function (compMgr, cid, iid) { + if (!cid.equals(this.myCID)) + throw Components.results.NS_ERROR_NO_INTERFACE; + + if (!iid.equals(Components.interfaces.nsIFactory)) + throw Components.results.NS_ERROR_NOT_IMPLEMENTED; + + return this.myFactory; + }, + + /* CID for this class */ + myCID: Components.ID("{dea98e50-1dd1-11b2-9344-8902b4805a2e}"), + + /* ProgID for this class */ + myProgID: "@mozilla.org/jssample;1", + + /* factory object */ + myFactory: { + /* + * Construct an instance of the interface specified by iid, possibly + * aggregating it with the provided outer. (If you don't know what + * aggregation is all about, you don't need to. It reduces even the + * mightiest of XPCOM warriors to snivelling cowards.) + */ + createInstance: function (outer, iid) { + debug("CI: " + iid + "\n"); + if (outer != null) + throw Components.results.NS_ERROR_NO_AGGREGATION; + + return (new mySample()).QueryInterface(iid); + } + }, + + /* + * The canUnload method signals that the component is about to be unloaded. + * C++ components can return false to indicate that they don't wish to be + * unloaded, but the return value from JS components' canUnload is ignored: + * mark-and-sweep will keep everything around until it's no longer in use, + * making unconditional ``unload'' safe. + * + * You still need to provide a (likely useless) canUnload method, though: + * it's part of the nsIModule interface contract, and the JS loader _will_ + * call it. + */ + canUnload: function(compMgr) { + debug("*** Unloading sample JS components\n"); + return true; + } +}; + +function NSGetModule(compMgr, fileSpec) { + return myModule; +} + + diff --git a/src/libs/xpcom18a4/xpcom/sample/nsSampleModule.cpp b/src/libs/xpcom18a4/xpcom/sample/nsSampleModule.cpp new file mode 100644 index 00000000..f02b9a5c --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/sample/nsSampleModule.cpp @@ -0,0 +1,131 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * 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://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 the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Suresh Duddu <dp@netscape.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either of 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 MPL, 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 MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ +#include "nsIGenericFactory.h" + +#include "nsSample.h" + +//////////////////////////////////////////////////////////////////////// +// NOTE this file supercedes nsSampleFactory.cpp. nsSampleFactory has +// extensive comments, but it has been CVS removed to reduce clutter +// in this sample. It's available to the interested via CVS history: +// cvs up nsSampleFactory.cpp -r 1.10 +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +// With the below sample, you can define an implementation glue +// that talks with xpcom for creation of component nsSampleImpl +// that implement the interface nsISample. This can be extended for +// any number of components. +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +// Define the contructor function for the object nsSampleImpl +// +// What this does is defines a function nsSampleImplConstructor which we +// will specific in the nsModuleComponentInfo table. This function will +// be used by the generic factory to create an instance of nsSampleImpl. +// +// NOTE: This creates an instance of nsSampleImpl by using the default +// constructor nsSampleImpl::nsSampleImpl() +// +NS_GENERIC_FACTORY_CONSTRUCTOR(nsSampleImpl) + +//////////////////////////////////////////////////////////////////////// +// Define a table of CIDs implemented by this module along with other +// information like the function to create an instance, contractid, and +// class name. +// +// The Registration and Unregistration proc are optional in the structure. +// +static NS_METHOD nsSampleRegistrationProc(nsIComponentManager *aCompMgr, + nsIFile *aPath, + const char *registryLocation, + const char *componentType, + const nsModuleComponentInfo *info) +{ + // Do any registration specific activity like adding yourself to a + // category. The Generic Module will take care of registering your + // component with xpcom. You dont need to do that. Only any component + // specific additional activity needs to be done here. + + // This functions is optional. If you dont need it, dont add it to the structure. + + return NS_OK; +} + +static NS_METHOD nsSampleUnregistrationProc(nsIComponentManager *aCompMgr, + nsIFile *aPath, + const char *registryLocation, + const nsModuleComponentInfo *info) +{ + // Undo any component specific registration like adding yourself to a + // category here. The Generic Module will take care of unregistering your + // component from xpcom. You dont need to do that. Only any component + // specific additional activity needs to be done here. + + // This functions is optional. If you dont need it, dont add it to the structure. + + // Return value is not used from this function. + return NS_OK; +} + +// For each class that wishes to support nsIClassInfo, add a line like this +NS_DECL_CLASSINFO(nsSampleImpl) + +static const nsModuleComponentInfo components[] = +{ + { "Sample Component", NS_SAMPLE_CID, NS_SAMPLE_CONTRACTID, nsSampleImplConstructor, + nsSampleRegistrationProc /* NULL if you dont need one */, + nsSampleUnregistrationProc /* NULL if you dont need one */, + NULL /* no factory destructor */, + NS_CI_INTERFACE_GETTER_NAME(nsSampleImpl), + NULL /* no language helper */, + &NS_CLASSINFO_NAME(nsSampleImpl) + } +}; + +//////////////////////////////////////////////////////////////////////// +// Implement the NSGetModule() exported function for your module +// and the entire implementation of the module object. +// +// NOTE: If you want to use the module shutdown to release any +// module specific resources, use the macro +// NS_IMPL_NSGETMODULE_WITH_DTOR() instead of the vanilla +// NS_IMPL_NSGETMODULE() +// +NS_IMPL_NSGETMODULE(nsSampleModule, components) diff --git a/src/libs/xpcom18a4/xpcom/sample/nsTestSample.cpp b/src/libs/xpcom18a4/xpcom/sample/nsTestSample.cpp new file mode 100644 index 00000000..edf9ce78 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/sample/nsTestSample.cpp @@ -0,0 +1,158 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * 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://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 the Initial Developer are Copyright (C) 1998 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Suresh Duddi <dp@netscape.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either of 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 MPL, 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 MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + + +/** + * A Test application that exercises the sample moudule. This is intented + * to be a sample application for using xpcom standalone. + */ + +#include <stdio.h> + +#include "nsISample.h" +#include "nsIServiceManager.h" +#include "nsIComponentRegistrar.h" + +#ifdef XPCOM_GLUE +#include "nsXPCOMGlue.h" +#include "nsMemory.h" +#else +#include "nsXPIDLString.h" +#endif + +#define NS_SAMPLE_CONTRACTID "@mozilla.org/sample;1" + +int +main(void) +{ + nsresult rv; + +#ifdef XPCOM_GLUE + XPCOMGlueStartup(nsnull); +#endif + + // Initialize XPCOM + nsCOMPtr<nsIServiceManager> servMan; + rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull); + if (NS_FAILED(rv)) + { + printf("ERROR: XPCOM intialization error [%x].\n", rv); + return -1; + } + // register all components in our default component directory + nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); + NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); + registrar->AutoRegister(nsnull); + + nsCOMPtr<nsIComponentManager> manager = do_QueryInterface(registrar); + NS_ASSERTION(registrar, "Null nsIComponentManager"); + + // Create an instance of our component + nsCOMPtr<nsISample> mysample; + rv = manager->CreateInstanceByContractID(NS_SAMPLE_CONTRACTID, + nsnull, + NS_GET_IID(nsISample), + getter_AddRefs(mysample)); + if (NS_FAILED(rv)) + { + printf("ERROR: Cannot create instance of component " NS_SAMPLE_CONTRACTID " [%x].\n" + "Debugging hint:\n" + "\tsetenv NSPR_LOG_MODULES nsComponentManager:5\n" + "\tsetenv NSPR_LOG_FILE xpcom.log\n" + "\t./nsTestSample\n" + "\t<check the contents for xpcom.log for possible cause of error>.\n", + rv); + return -2; + } + + // Call methods on our sample to test it out. + rv = mysample->WriteValue("Inital print:"); + if (NS_FAILED(rv)) + { + printf("ERROR: Calling nsISample::WriteValue() [%x]\n", rv); + return -3; + } + + const char *testValue = "XPCOM defies gravity"; + rv = mysample->SetValue(testValue); + if (NS_FAILED(rv)) + { + printf("ERROR: Calling nsISample::SetValue() [%x]\n", rv); + return -3; + } + printf("Set value to: %s\n", testValue); +#ifndef XPCOM_GLUE + nsXPIDLCString str; + rv = mysample->GetValue(getter_Copies(str)); +#else + char *str; + rv = mysample->GetValue(&str); +#endif + + if (NS_FAILED(rv)) + { + printf("ERROR: Calling nsISample::GetValue() [%x]\n", rv); + return -3; + } + if (strcmp(str, testValue)) + { + printf("Test FAILED.\n"); + return -4; + } + +#ifdef XPCOM_GLUE + nsMemory::Free(str); +#endif + rv = mysample->WriteValue("Final print :"); + printf("Test passed.\n"); + + // All nsCOMPtr's must be deleted prior to calling shutdown XPCOM + // as we should not hold references passed XPCOM Shutdown. + servMan = 0; + registrar = 0; + manager = 0; + mysample = 0; + + // Shutdown XPCOM + NS_ShutdownXPCOM(nsnull); + +#ifdef XPCOM_GLUE + XPCOMGlueShutdown(); +#endif + return 0; +} diff --git a/src/libs/xpcom18a4/xpcom/sample/win32.order b/src/libs/xpcom18a4/xpcom/sample/win32.order new file mode 100644 index 00000000..bf181a81 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/sample/win32.order @@ -0,0 +1 @@ +_NSGetModule ; 1 diff --git a/src/libs/xpcom18a4/xpcom/sample/xpconnect-sample.html b/src/libs/xpcom18a4/xpcom/sample/xpconnect-sample.html new file mode 100644 index 00000000..78e91be1 --- /dev/null +++ b/src/libs/xpcom18a4/xpcom/sample/xpconnect-sample.html @@ -0,0 +1,220 @@ + +<center><b><font size=+2>XPConnect Sample</font></b> + +<p> +<a href="mailto:arielb@rice.edu">Ariel Blackenroth <arielb@rice.edu></a> +<br> +<a href="mailto:mang@subcarrier.org">Michael Ang <mang@subcarrier.org></a> +<br> +Last modified +<script> +document.write(document.lastModified); +</script> +</center> + +<p>In the spirit of "worse is better" this somewhat rough guide is being +released to the world. It will be expanded upon and improved. + +<p>XPConnect allows JavaScript +to transparantly access and manipulate XPCOM objects; this communication +between JavaScript and +native code is done by having their interfaces defined in the XPIDL interface +definition language. See the <a href="http://www.mozilla.org/scriptable/roadmap.html">Roadmap +for documentation on XPCOM, XPConnect, XPTCall and XPIDL</a> for more information. + +<p><b>Overview</b> + +<p> +This sample demonstrates accessing a XPCOM object through XPConnect. +The JavaScript executed when this page loads creates an instance +of the object by +using the <tt>Components</tt> object, then accesses it through +the <a href="http://lxr.mozilla.org/seamonkey/source/xpcom/sample/nsISample.idl">nsISample</a> interface by calling <tt>QueryInterface</tt>: +<br> +<pre> +netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); +var sample = Components.classes["@mozilla.org/sample;1"].createInstance(); +sample = sample.QueryInterface(Components.interfaces.nsISample); +</pre> + +<p> +The buttons on the form are connected to JavaScript event handlers which +call the methods defined in C++. + + +<p><b><a href="http://lxr.mozilla.org/mozilla/source/xpcom/sample/nsISample.idl">nsISample.idl</a></b> +<p>This is the interface declaration for the XPCOM object. It defines +two functions, their parameters, and one attribute. It also defines +the interface's id. The idl file is compiled by the xpidl compiler +into a C++ header, nsISample.h and a .xpt file which is a binary representation +of the interface used at runtime. +<br><tt>attribute string value;</tt> +<br><tt>void writeValue(in string aPrefix);</tt> +<br><tt>void poke(in string aValue);</tt><b></b> +<p><b><a href="http://lxr.mozilla.org/mozilla/source/xpcom/sample/nsSample.cpp">nsSample.cpp</a></b> +<p>This contains the implementation of nsISample.idl. SampleImpl +inherits from nsISample.h, the header dynamically created by the xpidl +compiler. The attribute Value has been expanded into a get and set +and the return values have been modified to NS_IMETHOD, a success status +for the method. The macro NS_DECL_ISUPPORTS, defined in <a href="http://lxr.mozilla.org/mozilla/source/xpcom/base/nsISupportsUtils.h">mozilla/xpcom/public/nsISupportsUtils.h</a> +defines the inherited methods from nsISupports.h. +<br><tt>NS_IMPL_ISUPPORTS1(SampleImpl, nsISample)</tt> +<br>In the constructor, the macro NS_INIT_REFCNT is called which sets the +reference count to 0.<p> +Note that the methods in the C++ bindings use InterCaps style, while the IDL +and JavaScript versions should use interCaps naming. The JavaScript binding +matches the case of the IDL, <b>except</b> <a +href="http://bugzilla.mozilla.org/show_bug.cgi?id=14460">QueryInterface</a>. +<p><b><a href="http://lxr.mozilla.org/mozilla/source/xpcom/sample/nsSampleFactory.cpp">nsSampleFactory.cpp</a></b> +<p>This is the class which builds the instance of the nsSample class. +The COM framework uses factories to create instance of implementations +rather than having the implementations instatiate themselves in order to +increase portability of code. This factory inherits from nsFactory, +which is also an XPCOM object. To gain more knowledge of factories +see the <a href="http://www.mozilla.org/projects/xpcom/generic-factory.html">generic +factory document</a> or the <a href="http://www.mozilla.org/docs/modunote.htm#Basics">Modularization techniques document</a>. +<p><b><a href="http://lxr.mozilla.org/mozilla/source/xpcom/sample/nsSample.js">nsSample.js</a></b> +<p>This file implements the nsISample interface, and associated factory glue, +in JavaScript. + +<p><b>Compiling the idl</b> + +<p>The XPIDL compiler (xpidl on Unix, xpidl.exe on Windows, and a CodeWarrior plugin on Mac) +is compiled at build time (except on Mac) thus +you will have to build mozilla in order to test this out. If you +have already built mozilla then the compiler will be located at <tt>mozilla\dist\WIN32_D.OBJ\bin\xpidl.exe</tt>. + +<p>Once you have the XPIDL compiler enter the following command at your +prompt: +<br><tt>D:\mozilla\xpcom\sample>d:\mozilla\dist\WIN32_D.OBJ\bin\xpidl -I +d:\mozilla\dist\idl -m header nsISample.idl</tt> + +<p>The <tt>-I d:\mozilla\dist\idl</tt> points the compiler to the folder +containing the other idl files, needed because nsISample.idl inherits from +nsISupports.idl. The <tt>-m header</tt> instruction tells the compiler +to build the C++ header. To build the .xpt file substitute <tt>-m +typelib</tt>. + +<p> +For more information on compilation see the <a href="http://www.mozilla.org/scriptable/xpidl/">xpidl +compiler page</a>. + +<p><b>Building the Sample</b> + +<p>To build the Sample just enter +<br><tt>d:\mozilla\xpcom\sample>nmake /f makefile.win</tt> + +<p>In order to do this you need to have your environment variables set +correctly. See the <a href="http://www.mozilla.org/build/">Build</a> +page for more information. + +<p><b>Running the sample</b> +<p>Using Mozilla, load +<a href="resource://res/samples/xpconnect-sample.html">resource://res/samples/xpconnect-sample.html</a> (i.e. what +you're reading now). Pay attention +to the console when clicking "write". Notice that the value +printed is calculated in C++ code defined in <a href="http://lxr.mozilla.org/seamonkey/source/xpcom/sample/nsSample.cpp">nsSample.cpp</a>. + +<!-- XXX keep in sync with stuff in pre tag below --> +<script> +/* to use nsSample.js version, use "@mozilla.org/jssample;1" */ +netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); +var sample = Components.classes["@mozilla.org/sample;1"].createInstance(); +sample = sample.QueryInterface(Components.interfaces.nsISample); +dump("sample = " + sample + "\n"); + +function get() +{ + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + var field = document.getElementById('Value'); + field.value = sample.value; +} + +function set() +{ + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + var field = document.getElementById('Value'); + sample.value = field.value; +} + +function poke() +{ + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + var field = document.getElementById('Value'); + sample.poke(field.value); +} + +function sampleWrite() +{ + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + sample.writeValue("here is what I'm writing: "); +} +</script> + +<p> +<form name="form"> +<input type="button" value="Get" onclick="get();"> +<input type="button" value="Set" onclick="set();"> +<input type="button" value="Poke" onclick="poke();"> +<input type="text" id="Value"> +<input type="button" value="Write" onclick="sampleWrite();"> +<form> + +<hr> + +<p> +JavaScript and form source: + +<!-- XXX keep in sync with actual script --> +<pre> +<script> +/* to use nsSample.js version, use "@mozilla.org/jssample;1" */ +netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); +var sample = Components.classes["@mozilla.org/sample;1"].createInstance(); +sample = sample.QueryInterface(Components.interfaces.nsISample); +dump("sample = " + sample + "\n"); + +function get() +{ + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); var field = document.getElementById('Value'); + field.value = sample.value; +} + +function set() +{ + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); var field = document.getElementById('Value'); + sample.value = field.value; +} + +function poke() +{ + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); var field = document.getElementById('Value'); + sample.poke(field.value); +} + +function sampleWrite() +{ + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); + sample.writeValue("here is what I'm writing: "); +} +</script> + +<form name="form"> +<input type="button" value="Get" onclick="get();"> +<input type="button" value="Set" onclick="set();"> +<input type="button" value="Poke" onclick="poke();"> +<input type="text" id="Value"> +<input type="button" value="Write" onclick="sampleWrite();"> +<form> + +</pre> + +<p> +<hr> +<b>Resources:</b> +<ul> +<li><a href="http://lxr.mozilla.org/seamonkey/source/xpcom/sample/">mozilla/xpcom/sample source directory</a> +</ul> +<hr> +<b>Comments to:</b> +<a href="mailto:mang@subcarrier.org?Subject=XPCOM sample documentation">Michael Ang <mang@subcarrier.org></a> |