Remove profile build dependencies
[platform/core/context/context-service.git] / src / ProviderLoader.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <set>
18 #include <Types.h>
19 #include <ContextProvider.h>
20 #include <ProviderList.h>
21 #include "ProviderLoader.h"
22
23 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
24
25 using namespace ctx;
26
27 typedef ContextProvider* (*create_t)(const char *subject);
28
29 std::map<const char*, const char*, CompareSubjectName> ProviderLoader::__providerLibMap;
30
31 ProviderLoader::ProviderLoader() :
32         __soHandle(NULL)
33 {
34 }
35
36 ProviderLoader::~ProviderLoader()
37 {
38         __unload();
39 }
40
41 ContextProvider* ProviderLoader::load(const char *subject)
42 {
43         ProviderLibMap::iterator it = __providerLibMap.find(subject);
44         if (it == __providerLibMap.end()) {
45                 _W("No provider for '%s'", subject);
46                 return NULL;
47         }
48
49         std::string path(_LIBDIR_ LIB_PREFIX);
50         path = path + it->second + LIB_EXTENSION;
51
52         return __load(path.c_str(), subject);
53 }
54
55 ContextProvider* ProviderLoader::__load(const char *soPath, const char *subject)
56 {
57         _SI("Load '%s' from '%s'", subject, soPath);
58
59         __soHandle = g_module_open(soPath, G_MODULE_BIND_LAZY);
60         IF_FAIL_RETURN_TAG(__soHandle, NULL, _E, "%s", g_module_error());
61
62         gpointer symbol;
63
64         if (!g_module_symbol(__soHandle, "CreateProvider", &symbol) || symbol == NULL) {
65                 _W("%s", g_module_error());
66                 g_module_close(__soHandle);
67                 __soHandle = NULL;
68                 return NULL;
69         }
70
71         create_t create = reinterpret_cast<create_t>(symbol);
72
73         ContextProvider *prvd = create(subject);
74         if (!prvd) {
75                 _W("No provider for '%s'", subject);
76                 g_module_close(__soHandle);
77                 __soHandle = NULL;
78                 return NULL;
79         }
80
81         return prvd;
82 }
83
84 void ProviderLoader::__unload()
85 {
86         if (!__soHandle)
87                 return;
88
89         g_module_close(__soHandle);
90         __soHandle = NULL;
91 }
92
93 bool ProviderLoader::init()
94 {
95         int size = ARRAY_SIZE(subjectLibraryList);
96
97         for (int i = 0; i < size; ++i) {
98                 __providerLibMap[subjectLibraryList[i].subject] = subjectLibraryList[i].library;
99                 _SD("'%s' -> '%s'", subjectLibraryList[i].subject, subjectLibraryList[i].library);
100         }
101
102         return true;
103 }
104
105 bool ProviderLoader::popTriggerTemplate(std::string &subject, int &operation, Json &attribute, Json &option)
106 {
107         static int i = 0;
108         static int size = ARRAY_SIZE(triggerTemplateList);
109
110         if (i == size)
111                 return false;
112
113         subject = triggerTemplateList[i].subject;
114         operation = triggerTemplateList[i].operation;
115         attribute = triggerTemplateList[i].attribute;
116         option = triggerTemplateList[i].option;
117
118         ++i;
119         return true;
120 }