gecko-dev/xpcom/tests/dynamic/TestDynamic.cpp

178 строки
4.8 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* 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/
*
* 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) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Pierre Phaneuf <pp@ludusdesign.com>
*/
#include <iostream.h>
#include "pratom.h"
#include "TestFactory.h"
#include "nsCom.h"
#include "nsISupports.h"
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
#include "nsCOMPtr.h"
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
NS_DEFINE_CID(kTestLoadedFactoryCID, NS_TESTLOADEDFACTORY_CID);
/**
* ITestClass implementation
*/
class TestDynamicClassImpl: public ITestClass {
NS_DECL_ISUPPORTS
TestDynamicClassImpl() {
NS_INIT_REFCNT();
}
void Test();
};
NS_IMPL_ISUPPORTS(TestDynamicClassImpl, NS_GET_IID(ITestClass));
void TestDynamicClassImpl::Test() {
cout << "hello, dynamic world!\n";
}
/**
* TestFactory implementation
*/
static PRInt32 g_FactoryCount = 0;
static PRInt32 g_LockCount = 0;
class TestDynamicFactory: public nsIFactory {
NS_DECL_ISUPPORTS
TestDynamicFactory() {
NS_INIT_REFCNT();
PR_AtomicIncrement(&g_FactoryCount);
}
virtual ~TestDynamicFactory() {
PR_AtomicDecrement(&g_FactoryCount);
}
NS_IMETHOD CreateInstance(nsISupports *aDelegate,
const nsIID &aIID,
void **aResult);
NS_IMETHOD LockFactory(PRBool aLock) {
if (aLock) {
PR_AtomicIncrement(&g_LockCount);
} else {
PR_AtomicDecrement(&g_LockCount);
}
return NS_OK;
};
};
NS_IMPL_ISUPPORTS(TestDynamicFactory, NS_GET_IID(nsIFactory));
nsresult TestDynamicFactory::CreateInstance(nsISupports *aDelegate,
const nsIID &aIID,
void **aResult)
{
if (aDelegate != NULL) {
return NS_ERROR_NO_AGGREGATION;
}
TestDynamicClassImpl *t = new TestDynamicClassImpl();
if (t == NULL) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult res = t->QueryInterface(aIID, aResult);
if (NS_FAILED(res)) {
*aResult = NULL;
delete t;
}
return res;
}
extern "C" NS_EXPORT nsresult NSGetFactory(nsISupports* aServMgr,
const nsCID &aClass,
const char *aClassName,
const char *aProgID,
nsIFactory **aFactory)
{
if (aFactory == NULL) {
return NS_ERROR_NULL_POINTER;
}
if (aClass.Equals(kTestLoadedFactoryCID)) {
TestDynamicFactory *factory = new TestDynamicFactory();
nsresult res = factory->QueryInterface(NS_GET_IID(nsIFactory), (void **) aFactory);
if (NS_FAILED(res)) {
*aFactory = NULL;
delete factory;
}
return res;
}
return NS_NOINTERFACE;
}
extern "C" NS_EXPORT PRBool NSCanUnload(nsISupports* aServMgr) {
return PRBool(g_FactoryCount == 0 && g_LockCount == 0);
}
extern "C" NS_EXPORT nsresult NSRegisterSelf(nsISupports* aServMgr , const char *path)
{
nsresult rv;
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
if (NS_FAILED(rv)) return rv;
nsIComponentManager* compMgr;
rv = servMgr->GetService(kComponentManagerCID,
NS_GET_IID(nsIComponentManager),
(nsISupports**)&compMgr);
if (NS_FAILED(rv)) return rv;
rv = compMgr->RegisterComponent(kTestLoadedFactoryCID, NULL, NULL, path,
PR_TRUE, PR_TRUE);
(void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
return rv;
}
extern "C" NS_EXPORT nsresult NSUnregisterSelf(nsISupports* aServMgr, const char *path)
{
nsresult rv;
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
if (NS_FAILED(rv)) return rv;
nsIComponentManager* compMgr;
rv = servMgr->GetService(kComponentManagerCID,
NS_GET_IID(nsIComponentManager),
(nsISupports**)&compMgr);
if (NS_FAILED(rv)) return rv;
rv = compMgr->UnregisterComponent(kTestLoadedFactoryCID, path);
(void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
return rv;
}