#ifndef _H_MODULE
#define _H_MODULE
#include "cssmint.h"
#include "cssmmds.h"
#include <security_cdsa_utilities/callback.h>
#include "modloader.h"
#include <set>
#include <unordered_map>
class Module : public MdsComponent {
NOCOPY(Module)
public:
Module(CssmManager *mgr, const MdsComponent &info, Plugin *plugin);
virtual ~Module();
CssmManager &cssm;
bool unload(const ModuleCallback &callback);
CSSM_HANDLE attach(const CSSM_VERSION &version,
uint32 subserviceId,
CSSM_SERVICE_TYPE subserviceType,
const CSSM_API_MEMORY_FUNCS &memoryOps,
CSSM_ATTACH_FLAGS attachFlags,
CSSM_KEY_HIERARCHY keyHierarchy,
CSSM_FUNC_NAME_ADDR *functionTable,
uint32 functionTableSize);
void detach(Attachment *attachment);
void add(const ModuleCallback &cb) { callbackSet.insert(cb); }
void remove(const ModuleCallback &cb) { callbackSet.erase(cb); }
unsigned int callbackCount() const { return callbackSet.size(); }
unsigned int attachmentCount() const { return (int)attachmentMap.size(); }
void safeLock() { if (!isThreadSafe()) mLock.lock(); }
void safeUnlock() { if (!isThreadSafe()) mLock.unlock(); }
public:
typedef std::unordered_map<CSSM_HANDLE, Attachment *> AttachmentMap;
Plugin *plugin;
private:
void spiEvent(CSSM_MODULE_EVENT event,
const Guid &guid,
uint32 subserviceId,
CSSM_SERVICE_TYPE serviceType);
static CSSM_RETURN spiEventRelay(const CSSM_GUID *ModuleGuid,
void *Context,
uint32 SubserviceId,
CSSM_SERVICE_TYPE ServiceType,
CSSM_MODULE_EVENT EventType);
private:
AttachmentMap attachmentMap; ModuleCallbackSet callbackSet;
Mutex mLock;
};
#endif //_H_MODULE