diff --git a/mozilla/xpcom/sample/makefile.win b/mozilla/xpcom/sample/makefile.win new file mode 100644 index 00000000000..0baf9bcc689 --- /dev/null +++ b/mozilla/xpcom/sample/makefile.win @@ -0,0 +1,48 @@ +#!gmake +# +# The contents of this file are subject to the Netscape Public License +# Version 1.0 (the "NPL"); you may not use this file except in +# compliance with the NPL. You may obtain a copy of the NPL at +# http://www.mozilla.org/NPL/ +# +# Software distributed under the NPL is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL +# for the specific language governing rights and limitations under the +# NPL. +# +# The Initial Developer of this code under the NPL is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All Rights +# Reserved. + +DEPTH=..\.. +MODULE=sample + +MAKE_OBJ_TYPE=DLL +DLLNAME=$(MODULE) +DLL=.\$(OBJDIR)\$(DLLNAME).dll + +XPIDLSRCS= .\nsISample.idl \ + $(NULL) + +CPP_OBJS=\ + .\$(OBJDIR)\nsSample.obj \ + .\$(OBJDIR)\nsSampleFactory.obj \ + $(NULL) + +LLIBS=\ + $(DIST)\lib\xpcom32.lib \ + $(DIST)\lib\plc3.lib \ + $(NULL) + +LINCS=\ + -I$(PUBLIC)\sample \ + -I$(PUBLIC)\raptor \ + -I$(PUBLIC)\xpcom \ + $(NULL) + +include <$(DEPTH)\config\rules.mak> + +install:: $(DLL) + $(MAKE_INSTALL) $(DLL) $(DIST)\bin\components + diff --git a/mozilla/xpcom/sample/nsISample.idl b/mozilla/xpcom/sample/nsISample.idl new file mode 100644 index 00000000000..29bb704d9ff --- /dev/null +++ b/mozilla/xpcom/sample/nsISample.idl @@ -0,0 +1,43 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +/* + + A sample of XPConnect. This file contains a sample interface. + + */ + +#include "nsISupports.idl" + +[scriptable, uuid(7CB5B7A1-07D7-11d3-BDE2-000064657374)] +interface nsISample : nsISupports +{ + attribute string Value; + void WriteValue(in string aPrefix); + void Poke(in string aValue); +}; + + +%{ C++ +// {7CB5B7A0-07D7-11d3-BDE2-000064657374} +#define NS_SAMPLE_CID \ +{ 0x7cb5b7a0, 0x7d7, 0x11d3, { 0xbd, 0xe2, 0x0, 0x0, 0x64, 0x65, 0x73, 0x74 } } + +extern nsresult +NS_NewSample(nsISample** aSample); +%} diff --git a/mozilla/xpcom/sample/nsSample.cpp b/mozilla/xpcom/sample/nsSample.cpp new file mode 100644 index 00000000000..06d05cecfa9 --- /dev/null +++ b/mozilla/xpcom/sample/nsSample.cpp @@ -0,0 +1,142 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + + +/* + + A sample of XPConnect. This file contains an implementation of + nsISample. + +*/ +#include "nscore.h" +#include "nsISample.h" +#include "nsIAllocator.h" +#include "plstr.h" +#include "stdio.h" + +class SampleImpl : public nsISample +{ +public: + SampleImpl(); + virtual ~SampleImpl(); + + // nsISupports interface + NS_DECL_ISUPPORTS + + // nsISample interface + NS_IMETHOD GetValue(char * *aValue); + NS_IMETHOD SetValue(char * aValue); + + NS_IMETHOD WriteValue(const char *aPrefix); + + NS_IMETHOD Poke(const char* aValue); + +private: + char* mValue; +}; + +//////////////////////////////////////////////////////////////////////// + +nsresult +NS_NewSample(nsISample** aSample) +{ + NS_PRECONDITION(aSample != nsnull, "null ptr"); + if (! aSample) + return NS_ERROR_NULL_POINTER; + + *aSample = new SampleImpl(); + if (! *aSample) + return NS_ERROR_OUT_OF_MEMORY; + + NS_ADDREF(*aSample); + return NS_OK; +} + +//////////////////////////////////////////////////////////////////////// + + +SampleImpl::SampleImpl() : mValue(nsnull) +{ + NS_INIT_REFCNT(); + mValue = PL_strdup("initial value"); +} + + +SampleImpl::~SampleImpl() +{ + if (mValue) + PL_strfree(mValue); +} + + + +NS_IMPL_ISUPPORTS(SampleImpl, nsISample::GetIID()); + + +NS_IMETHODIMP +SampleImpl::GetValue(char** aValue) +{ + NS_PRECONDITION(aValue != nsnull, "null ptr"); + if (! aValue) + return NS_ERROR_NULL_POINTER; + + if (mValue) { + *aValue = (char*) nsAllocator::Alloc(PL_strlen(mValue) + 1); + if (! *aValue) + return NS_ERROR_NULL_POINTER; + + PL_strcpy(*aValue, mValue); + } + else { + *aValue = nsnull; + } + return NS_OK; +} + + +NS_IMETHODIMP +SampleImpl::SetValue(char* aValue) +{ + NS_PRECONDITION(aValue != nsnull, "null ptr"); + if (! aValue) + return NS_ERROR_NULL_POINTER; + + if (mValue) { + PL_strfree(mValue); + } + + mValue = PL_strdup(aValue); + return NS_OK; +} + +NS_IMETHODIMP +SampleImpl::Poke(const char* aValue) +{ + return SetValue((char*) aValue); +} + +NS_IMETHODIMP +SampleImpl::WriteValue(const char* aPrefix) +{ + NS_PRECONDITION(aPrefix != nsnull, "null ptr"); + if (! aPrefix) + return NS_ERROR_NULL_POINTER; + + printf("%s %s\n", aPrefix, mValue); + return NS_OK; +} diff --git a/mozilla/xpcom/sample/nsSampleFactory.cpp b/mozilla/xpcom/sample/nsSampleFactory.cpp new file mode 100644 index 00000000000..55972f628fb --- /dev/null +++ b/mozilla/xpcom/sample/nsSampleFactory.cpp @@ -0,0 +1,206 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +/* + + A sample of XPConnect. This file contains the XPCOM factory the + creates for SampleImpl objects. + +*/ + +#include "nsCOMPtr.h" +#include "nscore.h" +#include "nsISample.h" +#include "nsIComponentManager.h" +#include "nsIServiceManager.h" +#include "nsXPComFactory.h" + +static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); +static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID); +static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID); +static NS_DEFINE_CID(kSampleCID, NS_SAMPLE_CID); + +class SampleFactoryImpl : public nsIFactory +{ +public: + SampleFactoryImpl(const nsCID &aClass, const char* className, const char* progID); + + // nsISupports methods + NS_DECL_ISUPPORTS + + // nsIFactory methods + NS_IMETHOD CreateInstance(nsISupports *aOuter, + const nsIID &aIID, + void **aResult); + + NS_IMETHOD LockFactory(PRBool aLock); + +protected: + virtual ~SampleFactoryImpl(); + +protected: + nsCID mClassID; + const char* mClassName; + const char* mProgID; +}; + +//////////////////////////////////////////////////////////////////////// + +SampleFactoryImpl::SampleFactoryImpl(const nsCID &aClass, + const char* className, + const char* progID) + : mClassID(aClass), mClassName(className), mProgID(progID) +{ + NS_INIT_REFCNT(); +} + +SampleFactoryImpl::~SampleFactoryImpl() +{ + NS_ASSERTION(mRefCnt == 0, "non-zero refcnt at destruction"); +} + +NS_IMETHODIMP +SampleFactoryImpl::QueryInterface(const nsIID &aIID, void **aResult) +{ + if (! aResult) + return NS_ERROR_NULL_POINTER; + + // Always NULL result, in case of failure + *aResult = nsnull; + + if (aIID.Equals(kISupportsIID)) { + *aResult = NS_STATIC_CAST(nsISupports*, this); + AddRef(); + return NS_OK; + } else if (aIID.Equals(kIFactoryIID)) { + *aResult = NS_STATIC_CAST(nsIFactory*, this); + AddRef(); + return NS_OK; + } + return NS_NOINTERFACE; +} + +NS_IMPL_ADDREF(SampleFactoryImpl); +NS_IMPL_RELEASE(SampleFactoryImpl); + +NS_IMETHODIMP +SampleFactoryImpl::CreateInstance(nsISupports *aOuter, + const nsIID &aIID, + void **aResult) +{ + if (! aResult) + return NS_ERROR_NULL_POINTER; + + if (aOuter) + return NS_ERROR_NO_AGGREGATION; + + *aResult = nsnull; + + nsresult rv; + + nsISupports *inst = nsnull; + if (mClassID.Equals(kSampleCID)) { + if (NS_FAILED(rv = NS_NewSample((nsISample**) &inst))) + return rv; + } + else { + return NS_ERROR_NO_INTERFACE; + } + + if (! inst) + return NS_ERROR_OUT_OF_MEMORY; + + if (NS_FAILED(rv = inst->QueryInterface(aIID, aResult))) { + // We didn't get the right interface. + NS_ERROR("didn't support the interface you wanted"); + } + + NS_IF_RELEASE(inst); + return rv; +} + +nsresult SampleFactoryImpl::LockFactory(PRBool aLock) +{ + // Not implemented in simplest case. + return NS_OK; +} + +//////////////////////////////////////////////////////////////////////// + + + +// return the proper factory to the caller +extern "C" PR_IMPLEMENT(nsresult) +NSGetFactory(nsISupports* aServMgr, + const nsCID &aClass, + const char *aClassName, + const char *aProgID, + nsIFactory **aFactory) +{ + if (! aFactory) + return NS_ERROR_NULL_POINTER; + + SampleFactoryImpl* factory = new SampleFactoryImpl(aClass, aClassName, aProgID); + if (factory == nsnull) + return NS_ERROR_OUT_OF_MEMORY; + + NS_ADDREF(factory); + *aFactory = factory; + return NS_OK; +} + + + +extern "C" PR_IMPLEMENT(nsresult) +NSRegisterSelf(nsISupports* aServMgr , const char* aPath) +{ + nsresult rv; + + nsCOMPtr servMgr(do_QueryInterface(aServMgr, &rv)); + if (NS_FAILED(rv)) return rv; + + NS_WITH_SERVICE(nsIComponentManager, compMgr, kComponentManagerCID, &rv); + if (NS_FAILED(rv)) return rv; + + rv = compMgr->RegisterComponent(kSampleCID, + "Sample World Component", + "component://netscape/sample/sample-world", + aPath, PR_TRUE, PR_TRUE); + + if (NS_FAILED(rv)) return rv; + + return NS_OK; +} + + +extern "C" PR_IMPLEMENT(nsresult) +NSUnregisterSelf(nsISupports* aServMgr, const char* aPath) +{ + nsresult rv; + + nsCOMPtr servMgr(do_QueryInterface(aServMgr, &rv)); + if (NS_FAILED(rv)) return rv; + + NS_WITH_SERVICE(nsIComponentManager, compMgr, kComponentManagerCID, &rv); + if (NS_FAILED(rv)) return rv; + + rv = compMgr->UnregisterComponent(kSampleCID, aPath); + if (NS_FAILED(rv)) return rv; + + return NS_OK; +}