aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_online/wasabi.cpp
blob: e6edf6ae21feb797ccd8352627d312f47b889435 (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
#include "main.h"
#include "./api__ml_online.h"
#include <api/service/waservicefactory.h>
#include "../nu/Singleton.h"
#include "handler.h"
#include "JSAPI2_Creator.h"

static ULONG wasabiRef = 0;

api_service *WASABI_API_SVC = NULL;
api_application *WASABI_API_APP = NULL;
api_config *AGAVE_API_CONFIG = NULL;
api_language *WASABI_API_LNG = NULL;
api_explorerfindfile *WASABI_API_EXPLORERFINDFILE = NULL;
api_memmgr *WASABI_API_MEMMNGR = NULL;
svc_imageLoader *pngLoaderApi = NULL;
obj_ombrowser *browserManager = NULL;
ifc_omservicemanager *serviceManager = NULL;
ifc_omutility *omUtility = NULL;
JSAPI2::api_security *AGAVE_API_JSAPI2_SECURITY = NULL;
api_auth *AGAVE_API_AUTH = 0;
static JSAPI2Factory jsapi2Factory;
HINSTANCE WASABI_API_LNG_HINST = NULL;
HINSTANCE WASABI_API_ORIG_HINST = NULL;
static BOOL fDefaultsLoaded = FALSE;
static OnlineServicesURIHandler uri_handler;
static SingletonServiceFactory<svc_urihandler, OnlineServicesURIHandler> uri_handler_factory;

EXTERN_C const GUID pngLoaderGUID = 
{ 0x5e04fb28, 0x53f5, 0x4032, { 0xbd, 0x29, 0x3, 0x2b, 0x87, 0xec, 0x37, 0x25 } };

void *Wasabi_QueryInterface(REFGUID interfaceGuid)
{
	waServiceFactory *serviceFactory = WASABI_API_SVC->service_getServiceByGuid(interfaceGuid);
	return (NULL != serviceFactory) ? serviceFactory->getInterface() : NULL;
}

void Wasabi_ReleaseInterface(REFGUID interfaceGuid, void *pInstance)
{
	if (NULL == pInstance) return;
	waServiceFactory *serviceFactory = WASABI_API_SVC->service_getServiceByGuid(interfaceGuid);
	if (NULL != serviceFactory) serviceFactory->releaseInterface(pInstance);
}


HRESULT WasabiApi_Initialize(HINSTANCE hInstance, api_service *serviceApi)
{
	if (NULL != WASABI_API_SVC)
		return S_FALSE;

	fDefaultsLoaded = FALSE;

	WASABI_API_SVC = serviceApi;
	if ((api_service *)1 == WASABI_API_SVC)
		WASABI_API_SVC = NULL;

	if (NULL == WASABI_API_SVC) 
		return E_FAIL;
	
	WASABI_API_ORIG_HINST = hInstance;
	WASABI_API_LNG_HINST = WASABI_API_ORIG_HINST;

	WasabiApi_AddRef();
	return S_OK;
}

HRESULT WasabiApi_LoadDefaults()
{
	if (NULL == WASABI_API_SVC) 
		return E_UNEXPECTED;

	if (FALSE != fDefaultsLoaded)
		return S_FALSE;

	WASABI_API_APP = QueryWasabiInterface(api_application, applicationApiServiceGuid);
	WASABI_API_LNG = QueryWasabiInterface(api_language, languageApiGUID);
	WASABI_API_EXPLORERFINDFILE = QueryWasabiInterface(api_explorerfindfile, ExplorerFindFileApiGUID);
	AGAVE_API_CONFIG = QueryWasabiInterface(api_config, AgaveConfigGUID);
	AGAVE_API_JSAPI2_SECURITY = QueryWasabiInterface(JSAPI2::api_security, JSAPI2::api_securityGUID);
	OMBROWSERMNGR = QueryWasabiInterface(obj_ombrowser, OBJ_OmBrowser);
	OMSERVICEMNGR = QueryWasabiInterface(ifc_omservicemanager, IFC_OmServiceManager);
	OMUTILITY = QueryWasabiInterface(ifc_omutility, IFC_OmUtility);
	AGAVE_API_AUTH = QueryWasabiInterface(api_auth, AuthApiGUID);
	
	if (NULL != WASABI_API_LNG)
		WASABI_API_LNG_HINST = WASABI_API_LNG->StartLanguageSupport(WASABI_API_ORIG_HINST, MlOnlineLangGUID);

	WASABI_API_SVC->service_register(&jsapi2Factory);	
	uri_handler_factory.Register(WASABI_API_SVC, &uri_handler);

	fDefaultsLoaded = TRUE;
	return S_OK;
}

static void WasabiApi_Uninitialize()
{
	if (NULL != WASABI_API_SVC)
	{	
		ReleaseWasabiInterface(applicationApiServiceGuid, WASABI_API_APP);
		ReleaseWasabiInterface(languageApiGUID, WASABI_API_LNG);
		ReleaseWasabiInterface(ExplorerFindFileApiGUID, WASABI_API_EXPLORERFINDFILE);
		ReleaseWasabiInterface(AgaveConfigGUID, AGAVE_API_CONFIG);
		ReleaseWasabiInterface(memMgrApiServiceGuid, WASABI_API_MEMMNGR);
		ReleaseWasabiInterface(pngLoaderGUID, WASABI_API_PNGLOADER);
		ReleaseWasabiInterface(JSAPI2::api_securityGUID, AGAVE_API_JSAPI2_SECURITY);
		ReleaseWasabiInterface(AuthApiGUID, AGAVE_API_AUTH);
		ReleaseWasabiInterface(OBJ_OmBrowser, OMBROWSERMNGR);
		ReleaseWasabiInterface(IFC_OmServiceManager, OMSERVICEMNGR);
		ReleaseWasabiInterface(IFC_OmUtility, OMUTILITY);
		WASABI_API_SVC->service_deregister(&jsapi2Factory);
		uri_handler_factory.Deregister(WASABI_API_SVC);
	}

	WASABI_API_SVC = NULL;
	WASABI_API_APP = NULL;
	WASABI_API_LNG = NULL;
	WASABI_API_EXPLORERFINDFILE = NULL;
	AGAVE_API_CONFIG = NULL;
	AGAVE_API_JSAPI2_SECURITY = NULL;
	AGAVE_API_AUTH = NULL;
	OMBROWSERMNGR = NULL;
	OMSERVICEMNGR = NULL;
	OMUTILITY = NULL;

	fDefaultsLoaded = FALSE;
}

ULONG WasabiApi_AddRef()
{
	return InterlockedIncrement((LONG*)&wasabiRef);
}

ULONG WasabiApi_Release()
{
	if (0 == wasabiRef)
		return wasabiRef;
	
	LONG r = InterlockedDecrement((LONG*)&wasabiRef);
	if (0 == r)
	{
		WasabiApi_Uninitialize();
	}
	return r;
}