1998-08-29 01:25:34 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 06:43:54 +03: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-08-29 01:25:34 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03: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-08-29 01:25:34 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1998-08-29 01:25:34 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-05-03 02:38:04 +04:00
|
|
|
* IBM Corp.
|
1998-08-29 01:25:34 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsIServiceManager.h"
|
2000-05-20 22:55:35 +04:00
|
|
|
#include "nsICategoryManager.h"
|
|
|
|
#include "nsXPIDLString.h"
|
2000-01-19 02:03:29 +03:00
|
|
|
#include "nsVoidArray.h"
|
1999-03-09 12:44:27 +03:00
|
|
|
#include "nsHashtable.h"
|
1998-08-29 01:25:34 +04:00
|
|
|
#include "prcmon.h"
|
1999-01-08 03:38:56 +03:00
|
|
|
#include "prthread.h" /* XXX: only used for the NSPR initialization hack (rick) */
|
2000-01-06 03:34:38 +03:00
|
|
|
#include "nsAutoLock.h"
|
1998-08-29 01:25:34 +04:00
|
|
|
|
2000-01-06 03:34:38 +03:00
|
|
|
nsIServiceManager* gServiceManager = NULL;
|
|
|
|
PRBool gShuttingDown = PR_FALSE;
|
|
|
|
|
1999-11-10 03:14:54 +03:00
|
|
|
nsresult
|
|
|
|
nsGetServiceByCID::operator()( const nsIID& aIID, void** aInstancePtr ) const
|
|
|
|
{
|
1999-11-23 08:55:33 +03:00
|
|
|
nsresult status;
|
|
|
|
// Too bad |nsServiceManager| isn't an |nsIServiceManager|, then this could have been one call
|
|
|
|
if ( mServiceManager )
|
|
|
|
status = mServiceManager->GetService(mCID, aIID, NS_REINTERPRET_CAST(nsISupports**, aInstancePtr), 0);
|
|
|
|
else
|
|
|
|
status = nsServiceManager::GetService(mCID, aIID, NS_REINTERPRET_CAST(nsISupports**, aInstancePtr), 0);
|
|
|
|
|
|
|
|
if ( !NS_SUCCEEDED(status) )
|
|
|
|
*aInstancePtr = 0;
|
|
|
|
|
1999-11-10 03:14:54 +03:00
|
|
|
if ( mErrorPtr )
|
|
|
|
*mErrorPtr = status;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2000-09-14 03:57:52 +04:00
|
|
|
nsGetServiceByContractID::operator()( const nsIID& aIID, void** aInstancePtr ) const
|
1999-11-10 03:14:54 +03:00
|
|
|
{
|
|
|
|
nsresult status;
|
2000-09-14 03:57:52 +04:00
|
|
|
if ( mContractID )
|
1999-11-23 08:55:33 +03:00
|
|
|
{
|
|
|
|
// Too bad |nsServiceManager| isn't an |nsIServiceManager|, then this could have been one call
|
|
|
|
if ( mServiceManager )
|
2000-09-14 03:57:52 +04:00
|
|
|
status = mServiceManager->GetService(mContractID, aIID, NS_REINTERPRET_CAST(nsISupports**, aInstancePtr), 0);
|
1999-11-23 08:55:33 +03:00
|
|
|
else
|
2000-09-14 03:57:52 +04:00
|
|
|
status = nsServiceManager::GetService(mContractID, aIID, NS_REINTERPRET_CAST(nsISupports**, aInstancePtr), 0);
|
1999-11-23 08:55:33 +03:00
|
|
|
|
|
|
|
if ( !NS_SUCCEEDED(status) )
|
|
|
|
*aInstancePtr = 0;
|
|
|
|
}
|
1999-11-10 03:14:54 +03:00
|
|
|
else
|
|
|
|
status = NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if ( mErrorPtr )
|
|
|
|
*mErrorPtr = status;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2000-05-20 22:55:35 +04:00
|
|
|
nsresult
|
|
|
|
nsGetServiceFromCategory::operator()( const nsIID& aIID, void** aInstancePtr)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
nsresult status;
|
|
|
|
nsXPIDLCString value;
|
|
|
|
nsCOMPtr<nsICategoryManager> catman =
|
2000-09-14 03:57:52 +04:00
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &status);
|
2000-05-20 22:55:35 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(status)) goto error;
|
|
|
|
|
|
|
|
if (!mCategory || !mEntry) {
|
|
|
|
// when categories have defaults, use that for null mEntry
|
|
|
|
status = NS_ERROR_NULL_POINTER;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2000-09-14 03:57:52 +04:00
|
|
|
/* find the contractID for category.entry */
|
2000-05-20 22:55:35 +04:00
|
|
|
status = catman->GetCategoryEntry(mCategory, mEntry,
|
|
|
|
getter_Copies(value));
|
|
|
|
if (NS_FAILED(status)) goto error;
|
|
|
|
if (!value) {
|
|
|
|
status = NS_ERROR_SERVICE_NOT_FOUND;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Too bad |nsServiceManager| isn't an |nsIServiceManager|, then
|
|
|
|
// this could have been one call.
|
|
|
|
if ( mServiceManager )
|
|
|
|
status =
|
|
|
|
mServiceManager->GetService(value, aIID,
|
|
|
|
NS_REINTERPRET_CAST(nsISupports**,
|
|
|
|
aInstancePtr), 0);
|
|
|
|
else
|
|
|
|
status =
|
|
|
|
nsServiceManager::GetService(value, aIID,
|
|
|
|
NS_REINTERPRET_CAST(nsISupports**,
|
|
|
|
aInstancePtr), 0);
|
|
|
|
if (NS_FAILED(status)) {
|
|
|
|
error:
|
|
|
|
*aInstancePtr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*mErrorPtr = status;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
1998-08-29 01:25:34 +04:00
|
|
|
class nsServiceEntry {
|
|
|
|
public:
|
|
|
|
|
1998-08-29 01:47:44 +04:00
|
|
|
nsServiceEntry(const nsCID& cid, nsISupports* service);
|
1998-08-29 01:25:34 +04:00
|
|
|
~nsServiceEntry();
|
|
|
|
|
|
|
|
nsresult AddListener(nsIShutdownListener* listener);
|
|
|
|
nsresult RemoveListener(nsIShutdownListener* listener);
|
|
|
|
nsresult NotifyListeners(void);
|
|
|
|
|
1998-08-29 01:47:44 +04:00
|
|
|
const nsCID& mClassID;
|
1998-08-29 01:25:34 +04:00
|
|
|
nsISupports* mService;
|
2000-01-19 02:03:29 +03:00
|
|
|
nsVoidArray* mListeners;
|
1999-01-27 05:02:22 +03:00
|
|
|
PRBool mShuttingDown;
|
1998-08-29 01:25:34 +04:00
|
|
|
};
|
|
|
|
|
1998-08-29 01:47:44 +04:00
|
|
|
nsServiceEntry::nsServiceEntry(const nsCID& cid, nsISupports* service)
|
1999-01-27 05:02:22 +03:00
|
|
|
: mClassID(cid), mService(service), mListeners(NULL), mShuttingDown(PR_FALSE)
|
1998-08-29 01:25:34 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsServiceEntry::~nsServiceEntry()
|
|
|
|
{
|
|
|
|
if (mListeners) {
|
2000-01-19 02:03:29 +03:00
|
|
|
NS_ASSERTION(mListeners->Count() == 0, "listeners not removed or notified");
|
1998-08-29 01:25:34 +04:00
|
|
|
#if 0
|
2000-01-19 02:03:29 +03:00
|
|
|
PRUint32 size = mListeners->Count();
|
1998-08-29 01:25:34 +04:00
|
|
|
for (PRUint32 i = 0; i < size; i++) {
|
|
|
|
nsIShutdownListener* listener = (nsIShutdownListener*)(*mListeners)[i];
|
1999-03-09 12:44:27 +03:00
|
|
|
NS_RELEASE(listener);
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
delete mListeners;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsServiceEntry::AddListener(nsIShutdownListener* listener)
|
|
|
|
{
|
|
|
|
if (listener == NULL)
|
|
|
|
return NS_OK;
|
|
|
|
if (mListeners == NULL) {
|
2000-01-19 02:03:29 +03:00
|
|
|
mListeners = new nsVoidArray();
|
1998-08-29 01:25:34 +04:00
|
|
|
if (mListeners == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2000-01-19 02:03:29 +03:00
|
|
|
PRInt32 rv = mListeners->AppendElement(listener);
|
1999-03-09 12:44:27 +03:00
|
|
|
NS_ADDREF(listener);
|
|
|
|
return rv == -1 ? NS_ERROR_FAILURE : NS_OK;
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsServiceEntry::RemoveListener(nsIShutdownListener* listener)
|
|
|
|
{
|
|
|
|
if (listener == NULL)
|
|
|
|
return NS_OK;
|
|
|
|
NS_ASSERTION(mListeners, "no listeners added yet");
|
2000-01-19 02:03:29 +03:00
|
|
|
if ( mListeners->RemoveElement(listener) )
|
|
|
|
return NS_OK;
|
1998-08-29 01:25:34 +04:00
|
|
|
NS_ASSERTION(0, "unregistered shutdown listener");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsServiceEntry::NotifyListeners(void)
|
|
|
|
{
|
|
|
|
if (mListeners) {
|
2000-01-19 02:03:29 +03:00
|
|
|
PRUint32 size = mListeners->Count();
|
1998-08-29 01:25:34 +04:00
|
|
|
for (PRUint32 i = 0; i < size; i++) {
|
|
|
|
nsIShutdownListener* listener = (nsIShutdownListener*)(*mListeners)[0];
|
1999-03-09 12:44:27 +03:00
|
|
|
nsresult rv = listener->OnShutdown(mClassID, mService);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
NS_RELEASE(listener);
|
2000-01-19 02:03:29 +03:00
|
|
|
mListeners->RemoveElementAt(0);
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
2000-01-19 02:03:29 +03:00
|
|
|
NS_ASSERTION(mListeners->Count() == 0, "failed to notify all listeners");
|
1998-08-29 01:25:34 +04:00
|
|
|
delete mListeners;
|
|
|
|
mListeners = NULL;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1998-09-01 04:17:47 +04:00
|
|
|
class nsServiceManagerImpl : public nsIServiceManager {
|
1998-08-29 01:25:34 +04:00
|
|
|
public:
|
|
|
|
|
1999-03-09 12:44:27 +03:00
|
|
|
NS_IMETHOD
|
|
|
|
RegisterService(const nsCID& aClass, nsISupports* aService);
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
UnregisterService(const nsCID& aClass);
|
|
|
|
|
1998-08-29 01:25:34 +04:00
|
|
|
NS_IMETHOD
|
|
|
|
GetService(const nsCID& aClass, const nsIID& aIID,
|
|
|
|
nsISupports* *result,
|
|
|
|
nsIShutdownListener* shutdownListener = NULL);
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
ReleaseService(const nsCID& aClass, nsISupports* service,
|
|
|
|
nsIShutdownListener* shutdownListener = NULL);
|
|
|
|
|
1999-03-24 03:40:29 +03:00
|
|
|
NS_IMETHOD
|
2000-09-14 03:57:52 +04:00
|
|
|
RegisterService(const char* aContractID, nsISupports* aService);
|
1999-03-24 03:40:29 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
2000-09-14 03:57:52 +04:00
|
|
|
UnregisterService(const char* aContractID);
|
1999-03-24 03:40:29 +03:00
|
|
|
|
1999-03-11 01:53:17 +03:00
|
|
|
NS_IMETHOD
|
2000-09-14 03:57:52 +04:00
|
|
|
GetService(const char* aContractID, const nsIID& aIID,
|
1999-03-11 01:53:17 +03:00
|
|
|
nsISupports* *result,
|
|
|
|
nsIShutdownListener* shutdownListener = NULL);
|
|
|
|
|
|
|
|
NS_IMETHOD
|
2000-09-14 03:57:52 +04:00
|
|
|
ReleaseService(const char* aContractID, nsISupports* service,
|
1999-03-11 01:53:17 +03:00
|
|
|
nsIShutdownListener* shutdownListener = NULL);
|
|
|
|
|
1998-09-01 04:17:47 +04:00
|
|
|
nsServiceManagerImpl(void);
|
1999-03-09 12:44:27 +03:00
|
|
|
|
1998-08-29 01:25:34 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
1998-09-01 04:17:47 +04:00
|
|
|
virtual ~nsServiceManagerImpl(void);
|
1998-08-29 01:25:34 +04:00
|
|
|
|
1999-07-30 11:58:55 +04:00
|
|
|
nsObjectHashtable/*<nsServiceEntry>*/* mServices;
|
1999-09-02 08:12:08 +04:00
|
|
|
PRMonitor* mMonitor;
|
1998-08-29 01:25:34 +04:00
|
|
|
};
|
|
|
|
|
2000-05-03 02:38:04 +04:00
|
|
|
static PRBool PR_CALLBACK
|
1998-09-01 04:17:47 +04:00
|
|
|
DeleteEntry(nsHashKey *aKey, void *aData, void* closure)
|
1998-08-29 01:25:34 +04:00
|
|
|
{
|
|
|
|
nsServiceEntry* entry = (nsServiceEntry*)aData;
|
1999-08-15 01:32:29 +04:00
|
|
|
entry->NotifyListeners();
|
1999-03-09 12:44:27 +03:00
|
|
|
NS_RELEASE(entry->mService);
|
1998-08-29 01:25:34 +04:00
|
|
|
delete entry;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
1999-07-30 11:58:55 +04:00
|
|
|
nsServiceManagerImpl::nsServiceManagerImpl(void)
|
2000-01-06 03:34:38 +03:00
|
|
|
: mMonitor(0)
|
1999-07-30 11:58:55 +04:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
mServices = new nsObjectHashtable(nsnull, nsnull, // should never be cloned
|
|
|
|
DeleteEntry, nsnull,
|
|
|
|
256, PR_TRUE); // Get a threadSafe hashtable
|
|
|
|
NS_ASSERTION(mServices, "out of memory already?");
|
1999-09-02 08:12:08 +04:00
|
|
|
|
|
|
|
/* XXX: This is a hack to force NSPR initialization.. This should be
|
|
|
|
* removed once PR_CEnterMonitor(...) initializes NSPR... (rick)
|
|
|
|
*/
|
|
|
|
(void)PR_GetCurrentThread();
|
2000-05-13 03:39:34 +04:00
|
|
|
mMonitor = nsAutoMonitor::NewMonitor("nsServiceManagerImpl");
|
1999-09-02 08:12:08 +04:00
|
|
|
NS_ASSERTION(mMonitor, "unable to get service manager monitor. Uh oh.");
|
1999-07-30 11:58:55 +04:00
|
|
|
}
|
|
|
|
|
1998-09-01 04:17:47 +04:00
|
|
|
nsServiceManagerImpl::~nsServiceManagerImpl(void)
|
1998-08-29 01:25:34 +04:00
|
|
|
{
|
1999-03-09 12:44:27 +03:00
|
|
|
if (mServices) {
|
|
|
|
delete mServices;
|
|
|
|
}
|
1999-09-02 08:12:08 +04:00
|
|
|
|
|
|
|
if (mMonitor) {
|
2000-05-13 03:39:34 +04:00
|
|
|
nsAutoMonitor::DestroyMonitor(mMonitor);
|
1999-09-02 08:12:08 +04:00
|
|
|
mMonitor = 0;
|
|
|
|
}
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
|
1999-08-23 14:14:16 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsServiceManagerImpl, nsIServiceManager)
|
1998-08-29 01:25:34 +04:00
|
|
|
|
1999-03-09 12:44:27 +03:00
|
|
|
NS_IMETHODIMP
|
1998-09-01 04:17:47 +04:00
|
|
|
nsServiceManagerImpl::GetService(const nsCID& aClass, const nsIID& aIID,
|
1998-12-16 08:40:20 +03:00
|
|
|
nsISupports* *result,
|
|
|
|
nsIShutdownListener* shutdownListener)
|
1998-08-29 01:25:34 +04:00
|
|
|
{
|
2000-01-06 03:34:38 +03:00
|
|
|
nsAutoMonitor mon(mMonitor);
|
|
|
|
|
|
|
|
// test this first, since there's no point in returning a service during
|
|
|
|
// shutdown -- whether it's available or not would depend on the order it
|
|
|
|
// occurs in the list
|
|
|
|
if (gShuttingDown) {
|
|
|
|
// When processing shutdown, dont process new GetService() requests
|
|
|
|
#ifdef DEBUG_dp
|
|
|
|
NS_WARN_IF_FALSE(PR_FALSE, "Creating new service on shutdown. Denied.");
|
|
|
|
#endif /* DEBUG_dp */
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
1998-08-29 01:25:34 +04:00
|
|
|
|
2000-01-06 03:34:38 +03:00
|
|
|
nsresult rv = NS_OK;
|
1999-01-27 05:02:22 +03:00
|
|
|
nsIDKey key(aClass);
|
1998-08-29 01:25:34 +04:00
|
|
|
nsServiceEntry* entry = (nsServiceEntry*)mServices->Get(&key);
|
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
nsISupports* service;
|
1999-03-09 12:44:27 +03:00
|
|
|
rv = entry->mService->QueryInterface(aIID, (void**)&service);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2000-06-23 06:02:48 +04:00
|
|
|
// The refcount acquired in QI() above is "returned" to
|
|
|
|
// the caller.
|
|
|
|
|
1999-03-09 12:44:27 +03:00
|
|
|
rv = entry->AddListener(shutdownListener);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1998-08-29 01:25:34 +04:00
|
|
|
*result = service;
|
1999-01-27 05:02:22 +03:00
|
|
|
|
|
|
|
// If someone else requested the service to be shut down,
|
|
|
|
// and we just asked to get it again before it could be
|
|
|
|
// released, then cancel their shutdown request:
|
|
|
|
if (entry->mShuttingDown) {
|
|
|
|
entry->mShuttingDown = PR_FALSE;
|
1999-03-09 12:44:27 +03:00
|
|
|
NS_ADDREF(service); // Released in UnregisterService
|
1999-01-27 05:02:22 +03:00
|
|
|
}
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
}
|
2000-01-06 03:34:38 +03:00
|
|
|
return rv;
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
2000-01-06 03:34:38 +03:00
|
|
|
|
|
|
|
nsISupports* service;
|
|
|
|
// We need to not be holding the service manager's monitor while calling
|
|
|
|
// CreateInstance, because it invokes user code which could try to re-enter
|
|
|
|
// the service manager:
|
|
|
|
mon.Exit();
|
|
|
|
rv = nsComponentManager::CreateInstance(aClass, NULL, aIID, (void**)&service);
|
|
|
|
mon.Enter();
|
2000-06-23 06:02:48 +04:00
|
|
|
|
|
|
|
// If you hit this assertion, it means that someone tried (and
|
|
|
|
// succeeded!) to get your service during your service's
|
|
|
|
// initialization. This will mean that one instance of your
|
|
|
|
// service will leak, and may mean that the client that
|
|
|
|
// successfully acquired the service got a partially constructed
|
|
|
|
// object.
|
|
|
|
NS_ASSERTION(!mServices->Get(&key), "re-entrant call to service manager created service twice!");
|
|
|
|
|
2000-01-06 03:34:38 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
entry = new nsServiceEntry(aClass, service);
|
|
|
|
if (entry == NULL) {
|
|
|
|
NS_RELEASE(service);
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = entry->AddListener(shutdownListener);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mServices->Put(&key, entry);
|
|
|
|
*result = service;
|
|
|
|
NS_ADDREF(service); // Released in service manager destructor
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
else {
|
2000-01-06 03:34:38 +03:00
|
|
|
NS_RELEASE(service);
|
|
|
|
delete entry;
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-03-09 12:44:27 +03:00
|
|
|
return rv;
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
|
1999-03-09 12:44:27 +03:00
|
|
|
NS_IMETHODIMP
|
1998-09-01 04:17:47 +04:00
|
|
|
nsServiceManagerImpl::ReleaseService(const nsCID& aClass, nsISupports* service,
|
1999-01-27 05:02:22 +03:00
|
|
|
nsIShutdownListener* shutdownListener)
|
1998-08-29 01:25:34 +04:00
|
|
|
{
|
1999-08-04 13:36:12 +04:00
|
|
|
PRBool serviceFound = PR_FALSE;
|
1999-03-09 12:44:27 +03:00
|
|
|
nsresult rv = NS_OK;
|
1998-08-29 01:25:34 +04:00
|
|
|
|
1999-08-03 04:53:55 +04:00
|
|
|
#ifndef NS_DEBUG
|
|
|
|
// Do entry lookup only if there is a shutdownlistener to be removed.
|
|
|
|
//
|
|
|
|
// For Debug builds, Consistency check for entry always. Releasing service
|
1999-08-04 13:36:12 +04:00
|
|
|
// when the service is not with the servicemanager is mostly wrong.
|
1999-08-03 04:53:55 +04:00
|
|
|
if (shutdownListener)
|
|
|
|
#endif
|
|
|
|
{
|
2000-05-13 03:39:34 +04:00
|
|
|
nsAutoMonitor mon(mMonitor);
|
1999-08-03 04:53:55 +04:00
|
|
|
nsIDKey key(aClass);
|
|
|
|
nsServiceEntry* entry = (nsServiceEntry*)mServices->Get(&key);
|
1998-08-29 01:25:34 +04:00
|
|
|
|
1999-08-03 04:53:55 +04:00
|
|
|
if (entry) {
|
|
|
|
rv = entry->RemoveListener(shutdownListener);
|
1999-08-05 16:58:04 +04:00
|
|
|
serviceFound = PR_TRUE;
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
}
|
1999-08-03 04:53:55 +04:00
|
|
|
|
1999-08-04 13:36:12 +04:00
|
|
|
nsrefcnt cnt;
|
|
|
|
NS_RELEASE2(service, cnt);
|
|
|
|
|
|
|
|
// Consistency check: Service ref count cannot go to zero because of the
|
|
|
|
// extra addref the service manager does, unless the service has been
|
|
|
|
// unregistered (ie) not found in the service managers hash table.
|
|
|
|
//
|
|
|
|
NS_ASSERTION(cnt > 0 || !serviceFound,
|
|
|
|
"*** Service in hash table but is being deleted. Dangling pointer\n"
|
|
|
|
"*** in service manager hash table.");
|
|
|
|
|
1999-03-09 12:44:27 +03:00
|
|
|
return rv;
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
|
1999-03-09 12:44:27 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsServiceManagerImpl::RegisterService(const nsCID& aClass, nsISupports* aService)
|
1998-08-29 01:25:34 +04:00
|
|
|
{
|
1999-03-09 12:44:27 +03:00
|
|
|
nsresult rv = NS_OK;
|
2000-01-06 03:34:38 +03:00
|
|
|
nsAutoMonitor mon(mMonitor);
|
1999-03-09 12:44:27 +03:00
|
|
|
|
|
|
|
nsIDKey key(aClass);
|
|
|
|
nsServiceEntry* entry = (nsServiceEntry*)mServices->Get(&key);
|
|
|
|
if (entry) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
else {
|
1999-05-28 09:29:26 +04:00
|
|
|
entry = new nsServiceEntry(aClass, aService);
|
1999-03-09 12:44:27 +03:00
|
|
|
if (entry == NULL)
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
else {
|
|
|
|
mServices->Put(&key, entry);
|
1999-08-03 04:53:55 +04:00
|
|
|
NS_ADDREF(aService); // Released in DeleteEntry from UnregisterService
|
1999-03-09 12:44:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsServiceManagerImpl::UnregisterService(const nsCID& aClass)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2000-01-06 03:34:38 +03:00
|
|
|
nsAutoMonitor mon(mMonitor);
|
1998-08-29 01:25:34 +04:00
|
|
|
|
1999-01-27 05:02:22 +03:00
|
|
|
nsIDKey key(aClass);
|
1998-08-29 01:25:34 +04:00
|
|
|
nsServiceEntry* entry = (nsServiceEntry*)mServices->Get(&key);
|
|
|
|
|
|
|
|
if (entry == NULL) {
|
1999-03-09 12:44:27 +03:00
|
|
|
rv = NS_ERROR_SERVICE_NOT_FOUND;
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
else {
|
1999-08-03 04:53:55 +04:00
|
|
|
rv = entry->NotifyListeners(); // break the cycles
|
1999-01-27 05:02:22 +03:00
|
|
|
entry->mShuttingDown = PR_TRUE;
|
1999-08-03 04:53:55 +04:00
|
|
|
mServices->RemoveAndDelete(&key); // This will call the delete entry func
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
1999-03-09 12:44:27 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-03-24 03:40:29 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2000-09-14 03:57:52 +04:00
|
|
|
// let's do it again, this time with ContractIDs...
|
1999-03-24 03:40:29 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-09-14 03:57:52 +04:00
|
|
|
nsServiceManagerImpl::RegisterService(const char* aContractID, nsISupports* aService)
|
1999-03-24 03:40:29 +03:00
|
|
|
{
|
|
|
|
nsCID aClass;
|
|
|
|
nsresult rv;
|
2000-09-14 03:57:52 +04:00
|
|
|
rv = nsComponentManager::ContractIDToClassID(aContractID, &aClass);
|
1999-03-24 03:40:29 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return RegisterService(aClass, aService);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-09-14 03:57:52 +04:00
|
|
|
nsServiceManagerImpl::UnregisterService(const char* aContractID)
|
1999-03-24 03:40:29 +03:00
|
|
|
{
|
|
|
|
nsCID aClass;
|
|
|
|
nsresult rv;
|
2000-09-14 03:57:52 +04:00
|
|
|
rv = nsComponentManager::ContractIDToClassID(aContractID, &aClass);
|
1999-03-24 03:40:29 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return UnregisterService(aClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-09-14 03:57:52 +04:00
|
|
|
nsServiceManagerImpl::GetService(const char* aContractID, const nsIID& aIID,
|
1999-03-24 03:40:29 +03:00
|
|
|
nsISupports* *result,
|
|
|
|
nsIShutdownListener* shutdownListener)
|
|
|
|
{
|
|
|
|
nsCID aClass;
|
|
|
|
nsresult rv;
|
2000-09-14 03:57:52 +04:00
|
|
|
rv = nsComponentManager::ContractIDToClassID(aContractID, &aClass);
|
1999-03-24 03:40:29 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return GetService(aClass, aIID, result, shutdownListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-09-14 03:57:52 +04:00
|
|
|
nsServiceManagerImpl::ReleaseService(const char* aContractID, nsISupports* service,
|
1999-03-24 03:40:29 +03:00
|
|
|
nsIShutdownListener* shutdownListener)
|
|
|
|
{
|
|
|
|
nsCID aClass;
|
|
|
|
nsresult rv;
|
2000-09-14 03:57:52 +04:00
|
|
|
rv = nsComponentManager::ContractIDToClassID(aContractID, &aClass);
|
1999-03-24 03:40:29 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return ReleaseService(aClass, service, shutdownListener);
|
|
|
|
}
|
|
|
|
|
1999-03-09 12:44:27 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewServiceManager(nsIServiceManager* *result)
|
|
|
|
{
|
|
|
|
nsServiceManagerImpl* servMgr = new nsServiceManagerImpl();
|
|
|
|
if (servMgr == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(servMgr);
|
|
|
|
*result = servMgr;
|
|
|
|
return NS_OK;
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Global service manager interface (see nsIServiceManager.h)
|
|
|
|
|
|
|
|
nsresult
|
1998-09-01 04:17:47 +04:00
|
|
|
nsServiceManager::GetGlobalServiceManager(nsIServiceManager* *result)
|
1998-08-29 01:25:34 +04:00
|
|
|
{
|
2000-01-06 03:34:38 +03:00
|
|
|
if (gShuttingDown)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
1999-03-24 02:28:32 +03:00
|
|
|
nsresult rv = NS_OK;
|
2000-01-06 03:34:38 +03:00
|
|
|
if (gServiceManager == NULL) {
|
1999-03-24 02:28:32 +03:00
|
|
|
// XPCOM not initialized yet. Let us do initialization of our module.
|
2000-01-06 04:05:13 +03:00
|
|
|
rv = NS_InitXPCOM(NULL, NULL);
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
1999-03-24 02:28:32 +03:00
|
|
|
// No ADDREF as we are advicing no release of this.
|
2000-01-06 03:34:38 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
*result = gServiceManager;
|
1999-03-09 12:44:27 +03:00
|
|
|
|
1999-03-24 02:28:32 +03:00
|
|
|
return rv;
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
|
1999-03-27 05:22:33 +03:00
|
|
|
nsresult
|
|
|
|
nsServiceManager::ShutdownGlobalServiceManager(nsIServiceManager* *result)
|
|
|
|
{
|
2000-01-06 03:34:38 +03:00
|
|
|
if (gServiceManager != NULL) {
|
1999-12-08 05:00:30 +03:00
|
|
|
nsrefcnt cnt;
|
2000-01-06 03:34:38 +03:00
|
|
|
NS_RELEASE2(gServiceManager, cnt);
|
1999-12-08 05:00:30 +03:00
|
|
|
NS_ASSERTION(cnt == 0, "Service Manager being held past XPCOM shutdown.");
|
2000-01-06 03:34:38 +03:00
|
|
|
gServiceManager = NULL;
|
1999-03-27 05:22:33 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1998-08-29 01:25:34 +04:00
|
|
|
nsresult
|
1998-09-01 04:17:47 +04:00
|
|
|
nsServiceManager::GetService(const nsCID& aClass, const nsIID& aIID,
|
1998-08-29 01:25:34 +04:00
|
|
|
nsISupports* *result,
|
|
|
|
nsIShutdownListener* shutdownListener)
|
|
|
|
{
|
|
|
|
nsIServiceManager* mgr;
|
1999-03-09 12:44:27 +03:00
|
|
|
nsresult rv = GetGlobalServiceManager(&mgr);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1998-08-29 01:25:34 +04:00
|
|
|
return mgr->GetService(aClass, aIID, result, shutdownListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-01 04:17:47 +04:00
|
|
|
nsServiceManager::ReleaseService(const nsCID& aClass, nsISupports* service,
|
1998-08-29 01:25:34 +04:00
|
|
|
nsIShutdownListener* shutdownListener)
|
|
|
|
{
|
1999-09-21 00:54:09 +04:00
|
|
|
// Don't create the global service manager here because we might be shutting
|
|
|
|
// down, and releasing all the services in its destructor
|
2000-01-06 03:34:38 +03:00
|
|
|
if (gServiceManager)
|
|
|
|
return gServiceManager->ReleaseService(aClass, service, shutdownListener);
|
1999-09-21 00:54:09 +04:00
|
|
|
// If there wasn't a global service manager, just release the object:
|
|
|
|
NS_RELEASE(service);
|
|
|
|
return NS_OK;
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1999-03-09 12:44:27 +03:00
|
|
|
nsServiceManager::RegisterService(const nsCID& aClass, nsISupports* aService)
|
|
|
|
{
|
|
|
|
nsIServiceManager* mgr;
|
|
|
|
nsresult rv = GetGlobalServiceManager(&mgr);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return mgr->RegisterService(aClass, aService);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsServiceManager::UnregisterService(const nsCID& aClass)
|
1998-08-29 01:25:34 +04:00
|
|
|
{
|
|
|
|
nsIServiceManager* mgr;
|
1999-03-09 12:44:27 +03:00
|
|
|
nsresult rv = GetGlobalServiceManager(&mgr);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return mgr->UnregisterService(aClass);
|
1998-08-29 01:25:34 +04:00
|
|
|
}
|
|
|
|
|
1999-04-02 09:17:42 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2000-09-14 03:57:52 +04:00
|
|
|
// let's do it again, this time with ContractIDs...
|
1999-04-02 09:17:42 +04:00
|
|
|
|
|
|
|
nsresult
|
2000-09-14 03:57:52 +04:00
|
|
|
nsServiceManager::GetService(const char* aContractID, const nsIID& aIID,
|
1999-04-02 09:17:42 +04:00
|
|
|
nsISupports* *result,
|
|
|
|
nsIShutdownListener* shutdownListener)
|
|
|
|
{
|
|
|
|
nsIServiceManager* mgr;
|
|
|
|
nsresult rv = GetGlobalServiceManager(&mgr);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-09-14 03:57:52 +04:00
|
|
|
return mgr->GetService(aContractID, aIID, result, shutdownListener);
|
1999-04-02 09:17:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2000-09-14 03:57:52 +04:00
|
|
|
nsServiceManager::ReleaseService(const char* aContractID, nsISupports* service,
|
1999-04-02 09:17:42 +04:00
|
|
|
nsIShutdownListener* shutdownListener)
|
|
|
|
{
|
1999-10-01 01:40:27 +04:00
|
|
|
// Don't create the global service manager here because we might
|
|
|
|
// be shutting down, and releasing all the services in its
|
|
|
|
// destructor
|
2000-01-06 03:34:38 +03:00
|
|
|
if (gServiceManager)
|
2000-09-14 03:57:52 +04:00
|
|
|
return gServiceManager->ReleaseService(aContractID, service, shutdownListener);
|
1999-11-30 00:02:51 +03:00
|
|
|
// If there wasn't a global service manager, just release the object:
|
|
|
|
NS_RELEASE(service);
|
1999-10-01 01:40:27 +04:00
|
|
|
return NS_OK;
|
1999-04-02 09:17:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2000-09-14 03:57:52 +04:00
|
|
|
nsServiceManager::RegisterService(const char* aContractID, nsISupports* aService)
|
1999-04-02 09:17:42 +04:00
|
|
|
{
|
|
|
|
nsIServiceManager* mgr;
|
|
|
|
nsresult rv = GetGlobalServiceManager(&mgr);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-09-14 03:57:52 +04:00
|
|
|
return mgr->RegisterService(aContractID, aService);
|
1999-04-02 09:17:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2000-09-14 03:57:52 +04:00
|
|
|
nsServiceManager::UnregisterService(const char* aContractID)
|
1999-04-02 09:17:42 +04:00
|
|
|
{
|
1999-10-01 01:40:27 +04:00
|
|
|
// Don't create the global service manager here because we might
|
|
|
|
// be shutting down, and releasing all the services in its
|
|
|
|
// destructor
|
2000-01-06 03:34:38 +03:00
|
|
|
if (gServiceManager)
|
2000-09-14 03:57:52 +04:00
|
|
|
return gServiceManager->UnregisterService(aContractID);
|
1999-09-21 00:54:09 +04:00
|
|
|
return NS_OK;
|
1999-04-02 09:17:42 +04:00
|
|
|
}
|
|
|
|
|
1998-08-29 01:25:34 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|