1998-09-17 00:55:35 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 02:47:15 +00:00
|
|
|
* 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/
|
1998-09-17 00:55:35 +00:00
|
|
|
*
|
1999-11-06 02:47:15 +00:00
|
|
|
* 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.
|
1998-09-17 00:55:35 +00:00
|
|
|
*
|
1999-11-06 02:47:15 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1998-09-17 00:55:35 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 02:47:15 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1998-09-17 00:55:35 +00:00
|
|
|
*/
|
1998-11-30 22:15:00 +00:00
|
|
|
|
1998-09-17 00:55:35 +00:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsIFactory.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsINetService.h"
|
|
|
|
#include "nsNetService.h"
|
1999-01-25 08:05:00 +00:00
|
|
|
#include "nsIServiceManager.h"
|
1999-03-09 09:44:27 +00:00
|
|
|
#include "nsIComponentManager.h"
|
1998-09-17 00:55:35 +00:00
|
|
|
|
|
|
|
/* This implementation of the network service factory is presently
|
|
|
|
* only taking advantage of the service retrieval benefit that the
|
|
|
|
* nsINetService interface provides. The module initialization and
|
|
|
|
* unloading is still bound to main program execution and closing.
|
|
|
|
* Once we break this dependence, the
|
|
|
|
* nsNetFactory will also appropriately handle loading/initializing/
|
|
|
|
* unloading of the library.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID);
|
|
|
|
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
|
|
|
static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
|
|
|
|
|
|
|
|
extern nsNetlibService *gNetlibService; // See nsNetService.cpp
|
|
|
|
|
|
|
|
class nsNetFactory : public nsIFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// nsISupports methods
|
|
|
|
NS_IMETHOD QueryInterface(const nsIID &aIID,
|
|
|
|
void **aResult);
|
|
|
|
NS_IMETHOD_(nsrefcnt) AddRef(void);
|
|
|
|
NS_IMETHOD_(nsrefcnt) Release(void);
|
|
|
|
|
|
|
|
// nsIFactory methods
|
|
|
|
NS_IMETHOD CreateInstance(nsISupports *aOuter,
|
|
|
|
const nsIID &aIID,
|
|
|
|
void **aResult);
|
|
|
|
|
|
|
|
NS_IMETHOD LockFactory(PRBool aLock);
|
|
|
|
|
|
|
|
nsNetFactory(const nsCID &aClass);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~nsNetFactory();
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsrefcnt mRefCnt;
|
|
|
|
nsCID mClassID;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsNetFactory::nsNetFactory(const nsCID &aClass)
|
|
|
|
{
|
|
|
|
mRefCnt = 0;
|
|
|
|
mClassID = aClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsNetFactory::~nsNetFactory()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsNetFactory::QueryInterface(const nsIID &aIID,
|
|
|
|
void **aResult)
|
|
|
|
{
|
|
|
|
if (aResult == NULL) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always NULL result, in case of failure
|
|
|
|
*aResult = NULL;
|
|
|
|
|
|
|
|
if (aIID.Equals(kISupportsIID)) {
|
|
|
|
*aResult = (void *)(nsISupports*)this;
|
|
|
|
} else if (aIID.Equals(kIFactoryIID)) {
|
|
|
|
*aResult = (void *)(nsIFactory*)this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*aResult == NULL) {
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddRef(); // Increase reference count for caller
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-10-08 01:49:06 +00:00
|
|
|
NS_IMPL_ADDREF(nsNetFactory);
|
|
|
|
NS_IMPL_RELEASE(nsNetFactory);
|
1998-09-17 00:55:35 +00:00
|
|
|
|
|
|
|
nsresult nsNetFactory::CreateInstance(nsISupports *aOuter,
|
|
|
|
const nsIID &aIID,
|
|
|
|
void **aResult)
|
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
if (aResult == NULL) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = NULL;
|
|
|
|
|
|
|
|
if (mClassID.Equals(kNetServiceCID)) {
|
1999-04-13 05:26:59 +00:00
|
|
|
#ifdef XP_PC
|
|
|
|
/* This ifdef fixes bug 2414. Unix and mac initialize NS_InitINetService in
|
|
|
|
NS_NewINetService so this initialization was being done twice. Therefore
|
|
|
|
to fix the bug we are ifdef-ing this section to be done for windows only */
|
|
|
|
|
1998-09-17 00:55:35 +00:00
|
|
|
// No need create a new one if we've already got one.
|
|
|
|
if (!gNetlibService) {
|
1998-10-07 23:19:39 +00:00
|
|
|
res = NS_InitINetService();
|
1998-09-17 00:55:35 +00:00
|
|
|
if (res != NS_OK)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-04-13 05:26:59 +00:00
|
|
|
#endif
|
1998-09-17 00:55:35 +00:00
|
|
|
|
|
|
|
// Hook the caller up.
|
|
|
|
res = NS_NewINetService((nsINetService**)aResult, aOuter);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsNetFactory::LockFactory(PRBool aLock)
|
|
|
|
{
|
|
|
|
// Not implemented in simplest case.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the proper factory to the caller
|
1998-11-30 22:15:00 +00:00
|
|
|
#if defined(XP_MAC) && defined(MAC_STATIC)
|
1999-02-26 10:17:14 +00:00
|
|
|
extern "C" NS_NET nsresult
|
|
|
|
NSGetFactory_NETLIB_DLL(nsISupports* serviceMgr,
|
|
|
|
const nsCID &aClass,
|
|
|
|
const char *aClassName,
|
|
|
|
const char *aProgID,
|
|
|
|
nsIFactory **aFactory)
|
1998-09-17 00:55:35 +00:00
|
|
|
#else
|
1999-02-26 10:17:14 +00:00
|
|
|
extern "C" NS_NET nsresult
|
|
|
|
NSGetFactory(nsISupports* serviceMgr,
|
|
|
|
const nsCID &aClass,
|
|
|
|
const char *aClassName,
|
|
|
|
const char *aProgID,
|
|
|
|
nsIFactory **aFactory)
|
1998-09-17 00:55:35 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (nsnull == aFactory) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aFactory = new nsNetFactory(aClass);
|
|
|
|
|
|
|
|
if (nsnull == aFactory) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*aFactory)->QueryInterface(kIFactoryIID, (void**)aFactory);
|
|
|
|
}
|