This commit is contained in:
kipp%netscape.com 1999-09-28 00:50:17 +00:00
parent 9c7330f086
commit f46d23236e
2 changed files with 252 additions and 0 deletions

View File

@ -0,0 +1,207 @@
/* -*- 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.
*/
#include "nsPNGModule.h"
#include "nsPNGDecoder.h"
#include "nsIComponentManager.h"
#include "nsIGenericFactory.h"
#include "nsISupports.h"
#include "nsCOMPtr.h"
static NS_DEFINE_CID(kPNGDecoderCID, NS_PNGDECODER_CID);
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
//////////////////////////////////////////////////////////////////////
// Module Global
//
// This is used by the module to count object. Constructors and
// destructors of objects created by this module need to
// inc/dec the object count for the module. Unload decision will
// be taken based on this.
//
// Constructor:
// gModule->IncrementObjCount();
//
// Descructor:
// gModule->DecrementObjCount();
//
// WARNING: This is weak reference. XPCOM guarantees that this module
// object will be deleted just before the dll is unloaded. Hence,
// holding a weak reference is ok.
static nsPNGModule *gModule = NULL;
//////////////////////////////////////////////////////////////////////
// Module entry point
extern "C" NS_EXPORT nsresult NSGetModule(nsIComponentManager *servMgr,
nsIFileSpec* location,
nsIModule** return_cobj)
{
nsPNGModule *module;
nsresult rv = NS_OK;
NS_ASSERTION(return_cobj, "Null argument");
NS_ASSERTION(gModule == NULL, "nsPNGModule: Module already created.");
module = new nsPNGModule;
if (module == NULL) return NS_ERROR_OUT_OF_MEMORY;
rv = module->QueryInterface(nsIModule::GetIID(), (void **)return_cobj);
if (NS_FAILED(rv))
{
delete module;
module = NULL;
}
// WARNING: Weak Reference
gModule = module;
return rv;
}
//////////////////////////////////////////////////////////////////////
// PNG Decoder Factory using nsIGenericFactory
static NS_IMETHODIMP
nsPNGDecoderCreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult)
{
PNGDecoder *dec = NULL;
*aResult = NULL;
il_container* ic = NULL;
if (aOuter && !aIID.Equals(kISupportsIID))
return NS_NOINTERFACE;
ic = new il_container();
if (!ic)
{
return NS_ERROR_OUT_OF_MEMORY;
}
dec = new PNGDecoder(ic);
if (!dec)
{
delete ic;
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult res = dec->QueryInterface(aIID, aResult);
if (NS_FAILED(res))
{
*aResult = NULL;
delete dec;
}
delete ic; /* is a place holder */
return res;
}
//////////////////////////////////////////////////////////////////////
// PNG Decoder Module Implementation
NS_IMPL_ISUPPORTS(nsPNGModule, nsIModule::GetIID())
nsPNGModule::nsPNGModule(void)
: mObjCount(-1), mClassObject(NULL)
{
NS_INIT_ISUPPORTS();
}
nsPNGModule::~nsPNGModule(void)
{
NS_ASSERTION(mObjCount <= 0, "Module released while having outstanding objects.");
if (mClassObject)
{
int refcnt;
NS_RELEASE2(mClassObject, refcnt);
NS_ASSERTION(refcnt == 0, "nsPNGModule::~nsPNGModule() ClassObject refcount nonzero");
}
}
//
// The class object for us is just the factory and nothing more.
//
NS_IMETHODIMP
nsPNGModule::GetClassObject(nsIComponentManager *aCompMgr, const nsCID & aClass,
const nsIID &aIID, void **r_classObj)
{
nsresult rv;
if( !aClass.Equals(kPNGDecoderCID))
return NS_ERROR_FACTORY_NOT_REGISTERED;
// If this aint the first time, return the cached class object
if (mClassObject == NULL)
{
nsCOMPtr<nsIGenericFactory> fact;
rv = NS_NewGenericFactory(getter_AddRefs(fact), nsPNGDecoderCreateInstance);
if (NS_FAILED(rv)) return rv;
// Store the class object in our global
rv = fact->QueryInterface(NS_GET_IID(nsISupports), (void **)&mClassObject);
if (NS_FAILED(rv)) return rv;
}
rv = mClassObject->QueryInterface(aIID, r_classObj);
return rv;
}
NS_IMETHODIMP
nsPNGModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec *location,
const char *registryLocation,
const char *componentType)
{
nsresult rv;
rv = aCompMgr->RegisterComponentSpec(kPNGDecoderCID,
"Netscape PNGDec",
"component://netscape/image/decoder&type=image/png",
location, PR_TRUE, PR_TRUE);
return rv;
}
NS_IMETHODIMP
nsPNGModule::UnregisterSelf(nsIComponentManager *aCompMgr,
nsIFileSpec *location,
const char *registryLocation)
{
nsresult rv;
rv = aCompMgr->UnregisterComponentSpec(kPNGDecoderCID, location);
return rv;
}
NS_IMETHODIMP
nsPNGModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
{
if (mObjCount < 0)
{
// Dll doesn't count objects.
return NS_ERROR_FAILURE;
}
PRBool unloadable = PR_FALSE;
if (mObjCount == 0)
{
// This dll can be unloaded now
unloadable = PR_TRUE;
}
if (okToUnload) *okToUnload = unloadable;
return NS_OK;
}

View File

@ -0,0 +1,45 @@
/* -*- 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.
*/
#ifndef nsPNGModule_h___
#define nsPNGModule_h___
#include "nsIModule.h"
class nsPNGModule : public nsIModule
{
public:
NS_DECL_ISUPPORTS
// Construction, Init and desstruction
nsPNGModule();
virtual ~nsPNGModule();
// nsIModule Interfaces
NS_DECL_NSIMODULE
// Facility for counting object instances
int IncrementObjCount() { if (mObjCount == -1) mObjCount = 0; return ++mObjCount; }
int DecrementObjCount() { if (mObjCount == -1) mObjCount = 0; return --mObjCount; }
int GetObjCount() { return mObjCount; }
private:
int mObjCount;
nsISupports* mClassObject;
};
#endif /* nsPNGModule_h___ */