aboutsummaryrefslogtreecommitdiff
path: root/Src/omBrowser/serviceManager.cpp
blob: 3c66e983e1558c2afb853188e70a0218587a43cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "main.h"
#include "./serviceManager.h"
#include "./service.h"

#include "./ifc_wasabihelper.h"

#include "./storageIni.h"
#include "./storageXml.h"
#include "./storageUrl.h"
#include "./storageEnum.h"

#include <shlwapi.h>
#include <strsafe.h>

OmServiceManager::OmServiceManager() 
	: ref(1), registerStorage(TRUE)
{
}

OmServiceManager::~OmServiceManager()
{	
	size_t index = storageList.size();
	while(index--)
	{
		storageList[index]->Release();
	}
}

OmServiceManager *OmServiceManager::CreateInstance()
{
	return new OmServiceManager();
}

size_t OmServiceManager::AddRef()
{
	return InterlockedIncrement((LONG*)&ref);
}

size_t OmServiceManager::Release()
{
	if (0 == ref)
		return ref;
	
	LONG r = InterlockedDecrement((LONG*)&ref);
	if (0 == r)
		delete(this);
	
	return r;
}

int OmServiceManager::QueryInterface(GUID interface_guid, void **object)
{
	if (NULL == object) return E_POINTER;

	if (IsEqualIID(interface_guid, IFC_OmServiceManager))
		*object = static_cast<ifc_omservicemanager*>(this);
	else
	{
		*object = NULL;
		return E_NOINTERFACE;
	}

	if (NULL == *object)
		return E_UNEXPECTED;

	AddRef();

	return S_OK;
}

HRESULT OmServiceManager::RegisterDefaultStorage()
{
	ifc_omstorage *storage;
	if (SUCCEEDED(OmStorageIni::CreateInstance((OmStorageIni**)&storage)))
	{
		RegisterStorage(storage);
		storage->Release();
	}

	if (SUCCEEDED(OmStorageXml::CreateInstance((OmStorageXml**)&storage)))
	{
		RegisterStorage(storage);
		storage->Release();
	}

	if (SUCCEEDED(OmStorageUrl::CreateInstance((OmStorageUrl**)&storage)))
	{
		RegisterStorage(storage);
		storage->Release();
	}

	return S_OK;
}

HRESULT OmServiceManager::RegisterStorage(ifc_omstorage *storage)
{
	if (NULL == storage) return E_INVALIDARG;
	
	HRESULT hr;
	GUID storageId, testId;

	if (FAILED(storage->GetId(&storageId)))
		return E_FAIL;

	size_t index = storageList.size();
	while(index--)
	{
		if(SUCCEEDED(storageList[index]->GetId(&testId)) && IsEqualGUID(storageId, testId))
			break;
	}

	if (((size_t)-1) == index)
	{
		storageList.push_back(storage);
		storage->AddRef();
		hr = S_OK;
	}
	else
	{
		hr = S_FALSE;
	}
	return hr;
}

HRESULT OmServiceManager::UnregisterStorage(const GUID *storageId)
{
	if (NULL == storageId) return E_INVALIDARG;

	HRESULT hr = S_FALSE;
	GUID testId;

	size_t index = storageList.size();
	while(index--)
	{
		ifc_omstorage *storage = storageList[index];
		if(SUCCEEDED(storage->GetId(&testId)) && IsEqualGUID(*storageId, testId))
		{
			storageList.erase(storageList.begin() + index);
			storage->Release();
			hr = S_OK;
			break;
		}
	}
	return hr;
}

HRESULT OmServiceManager::QueryStorage(const GUID *storageId, ifc_omstorage **storageOut)
{
	if (NULL == storageOut) return E_POINTER;
	*storageOut = NULL;

	if (NULL == storageId) 
		return E_INVALIDARG;
	
	if (FALSE != registerStorage && SUCCEEDED(RegisterDefaultStorage()))
		registerStorage = FALSE;

	HRESULT hr = S_FALSE;
	GUID testId;

	size_t index = storageList.size();
	while(index--)
	{
		ifc_omstorage *storage = storageList[index];
		if(SUCCEEDED(storage->GetId(&testId)) && IsEqualGUID(*storageId, testId))
		{
			storage->AddRef();
			*storageOut = storage;
			hr = S_OK;
			break;
		}
	}

	return hr;
}

HRESULT OmServiceManager::EnumStorage(const GUID *filterType, UINT filterCapabilities, ifc_omstorageenumerator **enumOut)
{
	if (FALSE != registerStorage && SUCCEEDED(RegisterDefaultStorage()))
		registerStorage = FALSE;

	return OmStorageEnumerator::CreateInstance(storageList.size() ? &storageList.at(0) : nullptr, storageList.size(),
											   filterType, filterCapabilities,
											   (OmStorageEnumerator**)enumOut);
}

HRESULT OmServiceManager::CreateService(UINT serviceId, ifc_omservicehost *host, ifc_omservice **serviceOut)
{
	if (NULL == serviceOut) return E_POINTER;

	OmService *service = NULL;
	HRESULT hr = OmService::CreateInstance(serviceId, host, &service);
	if (FAILED(hr)) 
	{
		*serviceOut = NULL;
		return hr;
	}

	*serviceOut = service;
	return hr;
}

#define CBCLASS OmServiceManager
START_DISPATCH;
CB(ADDREF, AddRef)
CB(RELEASE, Release)
CB(QUERYINTERFACE, QueryInterface)
CB(API_REGISTERSTORAGE, RegisterStorage)
CB(API_UNREGISTERSTORAGE, UnregisterStorage)
CB(API_QUERYSTORAGE, QueryStorage)
CB(API_ENUMSTORAGE, EnumStorage)
CB(API_CREATESERVICE, CreateService)
END_DISPATCH;
#undef CBCLASS