#ifndef _SEC_SOSDATASOURCE_H_
#define _SEC_SOSDATASOURCE_H_
#include "keychain/SecureObjectSync/SOSManifest.h"
#include <Security/SecureObjectSync/SOSCloudCircle.h>
#include "utilities/array_size.h"
#include "utilities/SecCFRelease.h"
#include "utilities/SecCFError.h"
__BEGIN_DECLS
typedef struct SOSDataSourceFactory *SOSDataSourceFactoryRef;
typedef struct SOSDataSource *SOSDataSourceRef;
typedef struct __OpaqueSOSEngine *SOSEngineRef;
typedef struct __OpaqueSOSObject *SOSObjectRef;
typedef struct __OpaqueSOSTransaction *SOSTransactionRef;
struct SOSDataSourceFactory {
CFStringRef (*copy_name)(SOSDataSourceFactoryRef factory);
SOSDataSourceRef (*create_datasource)(SOSDataSourceFactoryRef factory, CFStringRef dataSourceName, CFErrorRef *error);
void (*release)(SOSDataSourceFactoryRef factory);
};
static inline CFStringRef SOSDataSourceFactoryCopyName(SOSDataSourceFactoryRef dsf) {
return dsf->copy_name(dsf);
}
static inline SOSDataSourceRef SOSDataSourceFactoryCreateDataSource(SOSDataSourceFactoryRef dsf, CFStringRef dataSourceName, CFErrorRef *error) {
(void)SecRequirementError(dsf != NULL, error, CFSTR("No datasource"));
return dsf ? dsf->create_datasource(dsf, dataSourceName, error) : NULL;
}
static inline void SOSDataSourceFactoryRelease(SOSDataSourceFactoryRef dsf) {
dsf->release(dsf);
}
enum SOSMergeResult {
kSOSMergeFailure = 0, kSOSMergeLocalObject, kSOSMergePeersObject, kSOSMergeCreatedObject, };
typedef CFIndex SOSMergeResult;
enum SOSDataSourceTransactionType {
kSOSDataSourceNoneTransactionType = 0,
kSOSDataSourceImmediateTransactionType,
kSOSDataSourceExclusiveTransactionType,
kSOSDataSourceNormalTransactionType,
kSOSDataSourceExclusiveRemoteTransactionType,
};
typedef CFOptionFlags SOSDataSourceTransactionType;
enum SOSDataSourceTransactionPhase {
kSOSDataSourceTransactionDidRollback = 0, kSOSDataSourceTransactionWillCommit, kSOSDataSourceTransactionDidCommit, };
typedef CFOptionFlags SOSDataSourceTransactionPhase;
enum SOSDataSourceTransactionSource {
kSOSDataSourceSOSTransaction = 0, kSOSDataSourceCKKSTransaction = 3, kSOSDataSourceAPITransaction = 1, };
typedef CFOptionFlags SOSDataSourceTransactionSource;
typedef void (^SOSDataSourceNotifyBlock)(SOSDataSourceRef ds, SOSTransactionRef txn, SOSDataSourceTransactionPhase phase, SOSDataSourceTransactionSource source, CFArrayRef changes);
struct SOSDataSource {
SOSEngineRef engine;
CFStringRef (*dsGetName)(SOSDataSourceRef ds);
void (*dsAddNotifyPhaseBlock)(SOSDataSourceRef ds, SOSDataSourceNotifyBlock notifyBlock);
SOSManifestRef (*dsCopyManifestWithViewNameSet)(SOSDataSourceRef ds, CFSetRef viewNameSet, CFErrorRef *error);
bool (*dsForEachObject)(SOSDataSourceRef ds, SOSTransactionRef txn, SOSManifestRef manifest, CFErrorRef *error, void (^handleObject)(CFDataRef key, SOSObjectRef object, bool *stop));
CFDataRef (*dsCopyStateWithKey)(SOSDataSourceRef ds, CFStringRef key, CFStringRef pdmn, SOSTransactionRef txn, CFErrorRef *error);
CFDataRef (*dsCopyItemDataWithKeys)(SOSDataSourceRef ds, CFDictionaryRef keys, CFErrorRef *error);
bool (*dsDeleteStateWithKey)(SOSDataSourceRef ds, CFStringRef key, CFStringRef pdmn, SOSTransactionRef txn, CFErrorRef *error);
bool (*dsWith)(SOSDataSourceRef ds, CFErrorRef *error, SOSDataSourceTransactionSource source, bool onCommitQueue, void(^transaction)(SOSTransactionRef txn, bool *commit));
bool (*dsRelease)(SOSDataSourceRef ds, CFErrorRef *error); bool (*dsReadWith)(SOSDataSourceRef ds, CFErrorRef *error, SOSDataSourceTransactionSource source, void(^perform)(SOSTransactionRef txn));
SOSMergeResult (*dsMergeObject)(SOSTransactionRef txn, SOSObjectRef object, SOSObjectRef *createdObject, CFErrorRef *error);
bool (*dsSetStateWithKey)(SOSDataSourceRef ds, SOSTransactionRef txn, CFStringRef pdmn, CFStringRef key, CFDataRef state, CFErrorRef *error);
bool (*dsRestoreObject)(SOSTransactionRef txn, uint64_t handle, CFDictionaryRef item, CFErrorRef *error);
CFDataRef (*objectCopyDigest)(SOSObjectRef object, CFErrorRef *error);
CFDateRef (*objectCopyModDate)(SOSObjectRef object, CFErrorRef *error);
SOSObjectRef (*objectCreateWithPropertyList)(CFDictionaryRef plist, CFErrorRef *error);
CFDictionaryRef (*objectCopyPropertyList)(SOSObjectRef object, CFErrorRef *error);
CFDictionaryRef (*objectCopyBackup)(SOSObjectRef object, uint64_t handle, CFErrorRef *error);
};
static inline SOSEngineRef SOSDataSourceGetSharedEngine(SOSDataSourceRef ds, CFErrorRef *error) {
return ds->engine;
}
static inline CFStringRef SOSDataSourceGetName(SOSDataSourceRef ds) {
return ds->dsGetName(ds);
}
static inline void SOSDataSourceAddNotifyPhaseBlock(SOSDataSourceRef ds, SOSDataSourceNotifyBlock notifyBlock) {
ds->dsAddNotifyPhaseBlock(ds, notifyBlock);
}
static inline SOSManifestRef SOSDataSourceCopyManifestWithViewNameSet(SOSDataSourceRef ds, CFSetRef viewNameSet, CFErrorRef *error) {
return ds->dsCopyManifestWithViewNameSet(ds, viewNameSet, error);
}
static inline bool SOSDataSourceForEachObject(SOSDataSourceRef ds, SOSTransactionRef txn, SOSManifestRef manifest, CFErrorRef *error, void (^handleObject)(CFDataRef digest, SOSObjectRef object, bool *stop)) {
return ds->dsForEachObject(ds, txn, manifest, error, handleObject);
}
static inline bool SOSDataSourceWith(SOSDataSourceRef ds, CFErrorRef *error,
void(^transaction)(SOSTransactionRef txn, bool *commit)) {
return ds->dsWith(ds, error, kSOSDataSourceSOSTransaction, false, transaction);
}
static inline bool SOSDataSourceWithCommitQueue(SOSDataSourceRef ds, CFErrorRef *error,
void(^transaction)(SOSTransactionRef txn, bool *commit)) {
return ds->dsWith(ds, error, kSOSDataSourceSOSTransaction, true, transaction);
}
static inline bool SOSDataSourceWithAPI(SOSDataSourceRef ds, bool isAPI, CFErrorRef *error,
void(^transaction)(SOSTransactionRef txn, bool *commit)) {
return ds->dsWith(ds, error, isAPI ? kSOSDataSourceAPITransaction : kSOSDataSourceSOSTransaction, false, transaction);
}
static inline bool SOSDataSourceReadWithCommitQueue(SOSDataSourceRef ds, CFErrorRef *error,
void(^perform)(SOSTransactionRef txn)) {
return ds->dsReadWith(ds, error, kSOSDataSourceSOSTransaction, perform);
}
static inline CFDataRef SOSDataSourceCopyStateWithKey(SOSDataSourceRef ds, CFStringRef key, CFStringRef pdmn, SOSTransactionRef txn, CFErrorRef *error)
{
return ds->dsCopyStateWithKey(ds, key, pdmn, txn, error);
}
static inline CFDataRef SOSDataSourceCopyItemDataWithKeys(SOSDataSourceRef ds, CFDictionaryRef keys, CFErrorRef *error) {
return ds->dsCopyItemDataWithKeys(ds, keys, error);
}
static inline bool SOSDataSourceDeleteStateWithKey(SOSDataSourceRef ds, CFStringRef key, CFStringRef pdmn, SOSTransactionRef txn, CFErrorRef *error)
{
return ds->dsDeleteStateWithKey(ds, key, pdmn, txn, error);
}
static inline bool SOSDataSourceRelease(SOSDataSourceRef ds, CFErrorRef *error) {
return !ds || ds->dsRelease(ds, error);
}
static inline SOSMergeResult SOSDataSourceMergeObject(SOSDataSourceRef ds, SOSTransactionRef txn, SOSObjectRef peersObject, SOSObjectRef *mergedObject, CFErrorRef *error) {
return ds->dsMergeObject(txn, peersObject, mergedObject, error);
}
static inline bool SOSDataSourceSetStateWithKey(SOSDataSourceRef ds, SOSTransactionRef txn, CFStringRef key, CFStringRef pdmn, CFDataRef state, CFErrorRef *error)
{
return ds->dsSetStateWithKey(ds, txn, key, pdmn, state, error);
}
static inline CFDataRef SOSObjectCopyDigest(SOSDataSourceRef ds, SOSObjectRef object, CFErrorRef *error) {
return ds->objectCopyDigest(object, error);
}
static inline CFDateRef SOSObjectCopyModificationDate(SOSDataSourceRef ds, SOSObjectRef object, CFErrorRef *error) {
return ds->objectCopyModDate(object, error);
}
static inline SOSObjectRef SOSObjectCreateWithPropertyList(SOSDataSourceRef ds, CFDictionaryRef plist, CFErrorRef *error) {
return ds->objectCreateWithPropertyList(plist, error);
}
static inline CFDictionaryRef SOSObjectCopyPropertyList(SOSDataSourceRef ds, SOSObjectRef object, CFErrorRef *error) {
return ds->objectCopyPropertyList(object, error);
}
static inline CFDictionaryRef SOSObjectCopyBackup(SOSDataSourceRef ds, SOSObjectRef object, uint64_t handle, CFErrorRef *error) {
return ds->objectCopyBackup(object, handle, error);
}
static inline bool SOSObjectRestoreObject(SOSDataSourceRef ds, SOSTransactionRef txn, uint64_t handle, CFDictionaryRef item, CFErrorRef *error) {
return ds->dsRestoreObject(txn, handle, item, error);
}
static inline SOSEngineRef SOSDataSourceFactoryGetEngineForDataSourceName(SOSDataSourceFactoryRef factory, CFStringRef dataSourceName, CFErrorRef *error)
{
SOSDataSourceRef ds = SOSDataSourceFactoryCreateDataSource(factory, dataSourceName, error);
SOSEngineRef engine = ds ? SOSDataSourceGetSharedEngine(ds, error) : (SOSEngineRef) NULL;
SOSDataSourceRelease(ds, NULL);
return engine;
}
__END_DECLS
#endif