Initialize Tizen 2.3
[apps/livebox/livebox-cpp.git] / src / CModule.cpp
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <dlfcn.h>
22
23 #include <dlog.h>
24 #include <livebox-service.h>
25 #include <livebox-errno.h>
26
27 #include "debug.h"
28 #include "livebox-cpp.h"
29 #include "livebox-impl.h"
30 #include "CModule.h"
31 #include "dlist.h"
32
33 const char *CModule::m_sModulePath = "/opt/usr/live/%s/libexec/liblive-%s.so";
34 struct dlist *CModule::m_pModuleList = NULL;
35
36 CModule *CModule::Load(const char *pkgname)
37 {
38         CModule *inst;
39         char *tmp;
40         char *module_path;
41         int ret;
42
43         tmp = strdup(pkgname);
44         if (!tmp) {
45                 ErrPrint("Memory: %s\n", strerror(errno));
46                 return NULL;
47         }
48
49         try {
50                 inst = new CModule(tmp);
51         } catch (...) {
52                 ErrPrint("Memory\n");
53                 free(tmp);
54                 return NULL;
55         }
56
57         module_path = livebox_service_libexec(pkgname);
58         if (!module_path) {
59                 delete inst;
60                 free(tmp);
61                 return NULL;
62         }
63
64         inst->m_pHandle = dlopen(module_path, RTLD_LOCAL | RTLD_NOW | RTLD_DEEPBIND);
65         free(module_path);
66
67         if (!inst->m_pHandle) {
68                 ErrPrint("dlopen: %s\n", dlerror());
69                 delete inst;
70                 free(tmp);
71                 return NULL;
72         }
73
74         inst->m_fNew = (void *(*)(void))dlsym(inst->m_pHandle, "livebox_new");
75         if (!inst->m_fNew) {
76                 ErrPrint("symbol: livebox_new is not found: %s\n", dlerror());
77                 dlclose(inst->m_pHandle);
78                 delete inst;
79                 free(tmp);
80                 return NULL;
81         }
82
83         inst->m_fNeedToCreate = (int (*)(const char *, const char *))dlsym(inst->m_pHandle, "livebox_need_to_create");
84         if (!inst->m_fNeedToCreate) {
85                 DbgPrint("%s has no livebox_need_to_create\n", pkgname);
86         }
87
88         inst->m_fInitialize = (int (*)(const char *))dlsym(inst->m_pHandle, "livebox_initialize");
89         if (!inst->m_fInitialize) {
90                 DbgPrint("%s has no livebox_initialize\n", pkgname);
91         } else {
92                 ret = inst->m_fInitialize(pkgname);
93                 if (ret < 0) {
94                         ErrPrint("livebox_finalize(%s) returns %d\n", pkgname, ret);
95                         dlclose(inst->m_pHandle);
96                         delete inst;
97                         free(tmp);
98                         return NULL;
99                 }
100         }
101
102         inst->m_fFinalize = (int (*)(void))dlsym(inst->m_pHandle, "livebox_finalize");
103         if (!inst->m_fFinalize) {
104                 DbgPrint("%s has no livebox_finalize\n", pkgname);
105         }
106
107         m_pModuleList = dlist_append(m_pModuleList, inst);
108         return inst;
109 }
110
111 int CModule::Unload(void)
112 {
113         struct dlist *l;
114
115         l = dlist_find_data(m_pModuleList, this);
116         m_pModuleList = dlist_remove(m_pModuleList, l);
117
118         if (m_fFinalize) {
119                 int ret;
120                 ret = m_fFinalize();
121                 DbgPrint("livebox_finalize of %s returns %d\n", m_sPkgname, ret);
122         }
123
124         dlclose(m_pHandle);
125         free(m_sPkgname);
126         delete this;
127         return LB_STATUS_SUCCESS;
128 }
129
130 int CModule::Create(const char *filename, const char *content, const char *cluster, const char *category)
131 {
132         CLiveBoxImpl *inst;
133
134         inst = (CLiveBoxImpl *)m_fNew();
135         if (inst) {
136                 int ret;
137                 ret = inst->Create(filename, content, cluster, category);
138                 if (ret < 0) {
139                         delete inst;
140                         return ret;
141                 }
142
143                 m_pList = dlist_append(m_pList, inst);
144                 return 0;
145         }
146
147         return LB_STATUS_ERROR_FAULT;
148 }
149
150 int CModule::Destroy(CLiveBoxImpl *inst)
151 {
152         struct dlist *l;
153
154         l = dlist_find_data(m_pList, inst);
155         if (!l) {
156                 return LB_STATUS_ERROR_NOT_EXIST;
157         }
158
159         m_pList = dlist_remove(m_pList, l);
160         delete inst;
161         return LB_STATUS_SUCCESS;
162 }
163
164 CLiveBoxImpl *CModule::FindLiveBox(const char *filename)
165 {
166         struct dlist *l;
167         void *item;
168         CLiveBoxImpl *box;
169
170         dlist_foreach(m_pList, l, item) {
171                 box = (CLiveBoxImpl *)item;
172                 if (!strcmp(filename, box->Filename())) {
173                         return box;
174                 }
175         }
176
177         return NULL;
178 }
179
180 int CModule::NeedToCreate(const char *cluster, const char *category)
181 {
182         if (!m_fNeedToCreate) {
183                 return 0;
184         }
185
186         return m_fNeedToCreate(cluster, category);
187 }
188
189 CModule *CModule::FindModule(const char *pkgname)
190 {
191         struct dlist *l;
192         void *item;
193         CModule *module;
194
195         dlist_foreach(m_pModuleList, l, item) {
196                 module = (CModule *)item;
197                 if (!strcmp(pkgname, module->PackageName())) {
198                         return module;
199                 }
200         }
201
202         return NULL;
203 }
204
205 /* End of a file */