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