// webpi.h
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

// Definition of CWebPlugInRegistry and support classes :
//  CWebPlugInList, CWebPlugInDLLList
//
//-----------------------------------------------------------------------------
// $Modtime:   24 Mar 1999 13:48:00  $  $Revision:   1.22  $  $Author:   davidh  $
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

#if !defined(__WEBPI_H__)
#define __WEBPI_H__

#define __WEBPLUGINDIRECTORY _L("\\System\\Libs\\Webplgin\\")
#define KPlugInINIDirectory _L("c:\\System\\Data\\")

//System includes
#if !defined(__E32BASE_H__)
#include <e32base.h>
#endif

//Local includes
#if !defined(__WEBPIUTL_H__)
#include "webpiutl.h"	//For support classes
#endif
#if !defined(__WEBPIREG_H__)
#include "webpireg.h"	//For Registration API
#endif
#if !defined(__WEBPIFAC_H__)
#include "webpifac.h"	//For Factory API
#endif
#if !defined(__WEBDEF_H__)
#include "webdef.h"		
#endif
#if !defined(__WEBSTD_H__)
#include "webstd.h"
#endif
#if !defined(__WEBSVMOD_H__)
#include "websvmod.h"	//For services & logging		
#endif

//Forward declarations
class CWebPlugInRegistry;
class CWebPlugInRegistryData;
class CWebPlugInList;
class CWebPlugInDLLList;
class CWebPlugInData;
class CWebPlugInDLLData;

// Constants
const TInt KPIRegMajorVN=0;
const TInt KPIRegMinorVN=0;
const TInt KPIRegBuildVN=1;
#if defined(_UNICODE)
#define KUidWebEngCorePlugin KUidWebEngCorePlugin16
#else
#define KUidWebEngCorePlugin KUidWebEngCorePlugin8
#endif
const TInt KUidWebEngCorePlugin8 = 0x10000FCD;
const TInt KUidWebEngCorePlugin16 = 0x10003D14;
const TInt KNoOfPlugInTypes = 3;   //Number of different PI UIDs (per build variant)

//-----------------------------------------------------------------------------

/* Class CWebPlugInRegistry
This class is the PlugIn registry.  It implements both the functions in 
MWebPlugInRegistry and MWebPlugInFactory.
This class is responsible for all aspects of PI management from registering
the PIs to providing factory functions to load and unload the PIs. 

Using the Registry
--------------------
From the users point of view there is not a great deal of interaction required with
the Registry.  The Registry basically manages two lists - a list of data about Plug
Ins that it knows about, and a list of Plug In DLLs that it knows about.  A Plug
In DLL can contain any number of individual Plug Ins, and it's interface may
be found in WEBPIDLL.H.  Before the registry can be used, it must be instructed to
search for and register the Plug Ins on the system.  This is done by calling
RegisterPIsL().  This function may be called at any time to rebuild the list BUT NOT
WHEN ANY PLUG INS ARE ACTIVE as their pointers may get corrupted.  This will
be fixed later.  Once the Registry has been built, it may be queried for data.
NumPlugIns() returns the number of PlugIns; PlugInListL() returns a list of their ID
numbers.  Using a valid ID number (you'll get paniced if not ...) use PlugInData(i)
to get all the info you need about a PlugIn.  You can also call this function using a
pointer to a plugin instance as a paramter.  The interface to the data classes is fairly
self explanatory - see WEBPIUTL.H.  Similarly, data about the DLLs can be got from
PlugInDLLData(i).  The only 'interactive' function here at the moment is SetEnabled()
which is self explanatory.
NOTES
- The returned data classes are const for a reason :)
- In future versions PlugInListL() will be overloaded to search out PIs according
  to various criteria.  Hence the array return.
- Internalize & Externalize work, but are not used internally.  In particular, if you
  call InternalizeL() while the factory is in use the registry will rapidly explode.

Using the Factory
--------------------
The factory consists of only two functions, the only client of these would be the 
Web Engine.  Given a valid plug in ID, RetrievePlugInL() will return a pointer to 
an instance of that Plug In.  This may either be a brand new instance, or possibly
a reuse of old instance.  Which you get depends on the Plug In itself and the context
in which you asked for it.  There is only one rule about this instance - DO NOT DELETE
IT!  EVER!  Once a Plug In is finished with, call DestroyPlugInL() with its pointer.  The
factory will clean up the Plug In and if necessary unload the host DLL.  Again a warning.
once this has been done the pointer is no longer valid; don't try to use it again!
*/
class CWebPlugInRegistry: public CBase, public MWebPlugInRegistry, public MWebPlugInFactory
	{
public:
	IMPORT_C static CWebPlugInRegistry* NewL(CWebServicesModel* aWebServices);
	IMPORT_C TVersion WebPlugInRegistryVersion() const;
	~CWebPlugInRegistry();
	IMPORT_C void ExternalizeListL(RWriteStream& aStream);
	IMPORT_C void InternalizeListL(RReadStream& aStream);

	//Pure virtuals from MWebPlugInRegistry
	IMPORT_C virtual MWebPlugInRegistry::TWebPlugInRegistryError RegisterPIsL(CWebModel* aModel, TBool aForceRebuild = EFalse);
	IMPORT_C virtual TVersion RegistryVersion() const;
	IMPORT_C virtual TInt NumPlugIns () const;
	IMPORT_C virtual TInt NumPlugInDLLs () const;
	IMPORT_C virtual void PlugInListL (CArrayFixFlat<TInt>* aArray) const;
	IMPORT_C virtual void PlugInListL (CArrayFixFlat<TInt>* aArray, TWebPlugInType aPIType) const;
	IMPORT_C virtual void PlugInListL (CArrayFixFlat<TInt>* aArray, TWebPlugInType aPIType, const TDesC& aTarget) const;
	IMPORT_C virtual void PlugInList8L (CArrayFixFlat<TInt>* aArray, TWebPlugInType aPIType, const TDesC8& aTarget) const;
	IMPORT_C virtual void PlugInListL (CArrayFixFlat<TInt>* aArray, TWebPlugInType aPIType, const TDesC& aTarget, const TDesC& aMarkupLang, const TVersion& aMarkupLangVersion) const;
	IMPORT_C virtual const TPlugInAllParams PlugInData(TInt aPIID);
	IMPORT_C virtual const TPlugInAllParams PlugInData(MWebPlugIn* aPtr);
	IMPORT_C virtual const CWebPlugInDLLData* PlugInDLLData(TInt aPIDLLID);
	IMPORT_C virtual void SetEnabled(TBool aEnabled, TInt aPIID);
	IMPORT_C virtual TInt RelatedUIPlugIn(TInt aEnginePIID);
	IMPORT_C virtual CBase* CBasePtr();

	//Pure virtuals from MWebPlugInFactory
	IMPORT_C virtual MWebPlugIn* RetrievePlugInL (
		TInt aPIID, TWebPlugInFactoryError& aErrorCode,
		TInt aFrameID, MWebPlugIn* aContainer, CWebModel* aModel);
	IMPORT_C virtual TBool DestroyPlugIn (MWebPlugIn*& aPlugIn);
	IMPORT_C virtual MWebUIPlugIn* RetrieveUiPlugInL (
		TInt aPIID, TWebPlugInFactoryError& aErrorCode,
		CWebModel* aModel);
	IMPORT_C virtual TBool DestroyUiPlugIn (MWebUIPlugIn*& aPlugIn);
	IMPORT_C virtual TInt PtrToID(TAny* aPIPtr);
	IMPORT_C virtual TInt StoreLC(TInt aPIID, CDictionaryStore*& aStore);
	IMPORT_C virtual TInt StoreLC(MWebPlugIn* aPtr, CDictionaryStore*& aStore);
	IMPORT_C virtual TInt StoreLC(MWebUIPlugIn* aPtr, CDictionaryStore*& aStore);

private:
	//Data
	CWebServicesModel* iWebServices;
	CWebPlugInList* iPlugInList;
	CWebPlugInDLLList* iPlugInDLLList;

	//Methods
	CWebPlugInRegistry(CWebServicesModel* aWebServices);
	void ConstructL();
	void GetPlugInDataL (MWebPlugInRegistrationAPI* aPlugIn, CWebPlugInData* aData);
	void SetUIDs(TInt aUIDloop, TUidType& aPIUID);
	MWebPlugInRegistry::TWebPlugInRegistryError RegisterOnePIL(const TParse& aParser, RFs& aFsSession, CWebModel* aModel);
	MWebPlugInRegistry::TWebPlugInRegistryError ProcessDLLL(RLibrary aLibrary, const TParse& aParser, RFs aFsSession, TInt aUID, CWebModel* aModel);
	void DeleteDLLRef(TInt aIndex, CWebPlugInDLLData*& aDLLData);
	CWebPlugInDLLData* AddDLLL (const TParse& aParser, RFs& aFsSession, TInt aUID);
	void CleanUpOrphans();

	__DECLARE_LOG;
	};

//-----------------------------------------------------------------------------

/* Class CWebPlugInList
A class which maintains a list of CWebPlugInData and CWebPlugInRegistryData
objects and provides facilities for building, searching, and persisting the list.  */
class CWebPlugInList: public CBase
	{
public:
	
	static CWebPlugInList* NewL(CWebServicesModel* aWebServices);
	void InternaliseL(RReadStream& aStream, CWebPlugInDLLList* aDllList);
	void ExternaliseL(RWriteStream& aStream, CWebPlugInDLLList* aDllList) const;
	TInt NumPlugIns();
	TPlugInAllParams Entry(TInt aPIID) const;
	void AddDataL(TPlugInAllParams aData);
	void InternalizeL(RReadStream& aStream, CWebPlugInDLLList* aDllList);
    void ExternalizeL(RWriteStream& aStream, CWebPlugInDLLList* aDllList) const;
	void ClearList();
	TBool FindInstance(TInt& aIndex, TAny* aPtrTarget, TPlugInAllParams& aData ) const;
	void RemoveAndDestroy(TInt aIndex);
	TBool FindIndex(TInt& aIndex, CWebPlugInDLLData* aDllDataPtr, TInt aStartIndex=0 ) const;

	~CWebPlugInList();

private:
	CWebServicesModel* iWebServices;
		
	CWebPlugInList(CWebServicesModel* aWebServices);
	void ConstructL();

private:
	CArrayFixFlat<TPlugInAllParams>* iDataArray;
	__DECLARE_LOG;
	};

//-----------------------------------------------------------------------------

/* Class CWebPlugInDLLList
A class which maintains a list of CWebPlugInDLLData 
objects and provides facilities for building, searching, and persisting the list.  */
class CWebPlugInDLLList: public CBase
	{
public:
	
	static CWebPlugInDLLList* NewL(CWebServicesModel* aWebServices);
	void InternaliseL(RReadStream& aStream);
	void ExternaliseL(RWriteStream& aStream) const;
	TInt NumDLLs();
	CWebPlugInDLLData* Entry(TInt aPIID) const;
	void AddDataL(CWebPlugInDLLData* aData);
	void InternalizeL(RReadStream& aStream);
    void ExternalizeL(RWriteStream& aStream) const;
	void ClearList();
	TBool AnyPIsInUse();
	TInt NumActivePIs();
	TBool FindInstance(TInt& aIndex, TAny* aPtrTarget, CWebPlugInDLLData*& aDllData ) const;
	TBool FindInstance(TInt& aIndex, const TParse& aParser, CWebPlugInDLLData*& aDllData ) const;
	TBool FindIndex(TInt& aIndex, CWebPlugInDLLData* aDllDataPtr ) const;
	void RemoveAndDestroy(TInt aIndex);
	void ClearChecks();
	~CWebPlugInDLLList();

private:
	CWebServicesModel* iWebServices;
		
	CWebPlugInDLLList(CWebServicesModel* aWebServices);
	void ConstructL();

private:
	CArrayFixFlat<CWebPlugInDLLData*>* iDLLArray;
	__DECLARE_LOG;
	};


#endif  // !defined(__WEBPI_H__)
