Add 3.0 APIs and sync APIs same as 2.4
[platform/core/convergence/service-adaptor.git] / adaptor / auth-adaptor / auth_adaptor.c
1 /*
2  * Auth Adaptor
3  *
4  * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <glib.h>
22
23 #include "auth_adaptor.h"
24 #include "service_adaptor_internal.h"
25
26 //******************************************************************************
27 //* Global variables and defines
28 //******************************************************************************
29
30 //******************************************************************************
31 //* Private interface
32 //******************************************************************************
33
34 //******************************************************************************
35 //* Private interface definition
36 //******************************************************************************
37
38 static void _auth_adaptor_free_plugin(auth_plugin_h plugin)
39 {
40 }
41
42 //******************************************************************************
43 //* Public interface definition
44 //******************************************************************************
45
46 API auth_adaptor_h auth_adaptor_create()
47 {
48         SAL_FN_CALL;
49
50         auth_adaptor_h auth = (auth_adaptor_h) g_malloc0(sizeof(auth_adaptor_s));
51
52         g_mutex_init(&auth->mutex);
53
54         return auth;
55 }
56
57 API service_adaptor_error_e auth_adaptor_destroy(auth_adaptor_h auth)
58 {
59         SAL_FN_CALL;
60
61         RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
62
63         g_mutex_lock(&auth->mutex);
64
65         if (0 != auth->start)
66         {
67                 auth_adaptor_stop(auth);
68         }
69
70         SAL_FREE(auth);
71
72         g_mutex_unlock(&auth->mutex);
73
74         return SERVICE_ADAPTOR_ERROR_NONE;
75 }
76
77 API service_adaptor_error_e auth_adaptor_start(auth_adaptor_h auth)
78 {
79         SAL_FN_CALL;
80
81         RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
82
83         g_mutex_lock(&auth->mutex);
84
85         auth->start = 1;
86
87         g_mutex_unlock(&auth->mutex);
88
89         return SERVICE_ADAPTOR_ERROR_NONE;
90 }
91
92 API service_adaptor_error_e auth_adaptor_stop(auth_adaptor_h auth)
93 {
94         SAL_FN_CALL;
95
96         RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
97
98         // TODO: notify auth adaptor stop to each plugin
99
100         g_mutex_lock(&auth->mutex);
101
102         auth->start = 0;
103
104         g_mutex_unlock(&auth->mutex);
105
106         return SERVICE_ADAPTOR_ERROR_NONE;
107 }
108
109 API service_adaptor_error_e auth_adaptor_register_listener(auth_adaptor_h auth, auth_adaptor_listener_h listener)
110 {
111         SAL_FN_CALL;
112
113         RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
114         RETV_IF(NULL == listener, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
115
116         // TODO: register insert/ update/ delete callback for service-adaptor
117
118         return SERVICE_ADAPTOR_ERROR_NONE;
119 }
120
121 API service_adaptor_error_e auth_adaptor_unregister_listener(auth_adaptor_h auth, auth_adaptor_listener_h listener)
122 {
123         SAL_FN_CALL;
124
125         RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
126         RETV_IF(NULL == listener, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
127
128         // TODO: unregister insert/ update/ delete callback for service-adaptor
129
130         return SERVICE_ADAPTOR_ERROR_NONE;
131 }
132
133 API service_adaptor_error_e auth_adaptor_create_plugin(const char *uri, const char *name, const char *package, auth_plugin_h *plugin)
134 {
135         SAL_FN_CALL;
136
137         RETV_IF(NULL == uri, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
138         RETV_IF(NULL == name, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
139         RETV_IF(NULL == package, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
140
141         auth_plugin_h auth_plugin = (auth_plugin_h) g_malloc0(sizeof(auth_plugin_s));
142         auth_plugin->uri = strdup(uri);
143         auth_plugin->name = strdup(name);
144         auth_plugin->package = strdup(package);
145
146         g_mutex_init(&auth_plugin->mutex);
147         g_cond_init(&auth_plugin->cond);
148
149         *plugin = auth_plugin;
150
151         return SERVICE_ADAPTOR_ERROR_NONE;
152 }
153
154 API service_adaptor_error_e auth_adaptor_destroy_plugin(auth_plugin_h plugin)
155 {
156         SAL_FN_CALL;
157
158         RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
159
160         SAL_FREE(plugin->uri);
161         SAL_FREE(plugin->name);
162         SAL_FREE(plugin);
163
164         return SERVICE_ADAPTOR_ERROR_NONE;
165 }
166
167 API service_adaptor_error_e auth_adaptor_register_plugin_service(auth_plugin_h plugin, GHashTable *service)
168 {
169         SAL_FN_CALL;
170
171         RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
172         RETV_IF(NULL == service, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
173
174         int ret = SERVICE_ADAPTOR_ERROR_NONE;
175
176         plugin->oauth1 = (oauth1_service_h) g_malloc0(sizeof(oauth1_service_s));
177         ret = oauth1_register_service(plugin->oauth1, service);
178
179         if (SERVICE_ADAPTOR_ERROR_NONE != ret)
180         {
181                 SAL_INFO("could not find the function for oauth 1.0");
182                 SAL_FREE(plugin->oauth1);
183         }
184
185         plugin->oauth2 = (oauth2_service_h) g_malloc0(sizeof(oauth2_service_s));
186         ret = oauth2_register_service(plugin->oauth2, service);
187
188         if (SERVICE_ADAPTOR_ERROR_NONE != ret)
189         {
190                 SAL_INFO("could not find the function for oauth 2.0");
191                 SAL_FREE(plugin->oauth2);
192         }
193
194         return SERVICE_ADAPTOR_ERROR_NONE;
195 }
196
197 API service_adaptor_error_e auth_adaptor_unregister_plugin_service(auth_plugin_h plugin)
198 {
199         SAL_FN_CALL;
200
201         RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
202
203         if (NULL != plugin->oauth1)
204         {
205                 oauth1_unregister_service(plugin->oauth1);
206                 SAL_FREE(plugin->oauth1);
207         }
208
209         if (NULL != plugin->oauth2)
210         {
211                 oauth2_unregister_service(plugin->oauth2);
212                 SAL_FREE(plugin->oauth2);
213         }
214
215         return SERVICE_ADAPTOR_ERROR_NONE;
216 }
217
218 API service_adaptor_error_e auth_adaptor_add_plugin(auth_adaptor_h auth, auth_plugin_h plugin)
219 {
220         SAL_FN_CALL;
221
222         RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
223         RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
224
225         g_mutex_lock(&auth->mutex);
226
227         auth->plugins = g_list_append(auth->plugins, plugin);
228
229         g_mutex_unlock(&auth->mutex);
230
231         return SERVICE_ADAPTOR_ERROR_NONE;
232 }
233
234 API service_adaptor_error_e auth_adaptor_remove_plugin(auth_adaptor_h auth, auth_plugin_h plugin)
235 {
236         SAL_FN_CALL;
237
238         RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
239         RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
240
241         g_mutex_lock(&auth->mutex);
242
243         RETV_IF(NULL == g_list_find(auth->plugins, plugin), SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
244
245         auth->plugins = g_list_remove(auth->plugins, plugin);
246
247         _auth_adaptor_free_plugin(plugin);
248
249         g_mutex_unlock(&auth->mutex);
250
251         return SERVICE_ADAPTOR_ERROR_NONE;
252 }
253
254 API auth_plugin_h auth_adaptor_get_plugin(auth_adaptor_h auth, const char *uri)
255 {
256         SAL_FN_CALL;
257
258         RETV_IF(NULL == auth, NULL);
259         RETV_IF(NULL == uri, NULL);
260
261         g_mutex_lock(&auth->mutex);
262
263         auth_plugin_h plugin = NULL;
264
265         for (GList *list = g_list_first(auth->plugins); list != NULL; list = list->next)
266         {
267                 auth_plugin_h this = (auth_plugin_h) list->data;
268
269                 if (0 == strcmp(this->uri, uri))
270                 {
271                         plugin = this;
272                         break;
273                 }
274         }
275
276         g_mutex_unlock(&auth->mutex);
277
278         return plugin;
279 }
280
281 API char *auth_adaptor_get_uri(auth_adaptor_h auth, const char *package)
282 {
283         SAL_FN_CALL;
284
285         RETV_IF(NULL == auth, NULL);
286         RETV_IF(NULL == package, NULL);
287
288         g_mutex_lock(&auth->mutex);
289
290         char *uri = NULL;
291
292         for (GList *list = g_list_first(auth->plugins); list != NULL; list = list->next)
293         {
294                 auth_plugin_h this = (auth_plugin_h) list->data;
295
296                 if (0 == strcmp(this->package, package))
297                 {
298                         uri = this->uri;
299                         break;
300                 }
301         }
302
303         g_mutex_unlock(&auth->mutex);
304
305         return uri;
306 }
307
308 API service_adaptor_error_e auth_adaptor_ref_plugin(auth_adaptor_h auth, const char *uri)
309 {
310         SAL_FN_CALL;
311
312         RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
313         RETV_IF(NULL == uri, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
314
315         g_mutex_lock(&auth->mutex);
316
317         int ret = SERVICE_ADAPTOR_ERROR_NO_DATA;
318
319         for (GList *list = g_list_first(auth->plugins); list != NULL; list = list->next)
320         {
321                 auth_plugin_h this = (auth_plugin_h) list->data;
322
323                 if (0 == strcmp(this->uri, uri))
324                 {
325                         ret = SERVICE_ADAPTOR_ERROR_NONE;
326                         // TODO: increase ref count
327                         break;
328                 }
329         }
330
331         g_mutex_unlock(&auth->mutex);
332
333         return ret;
334 }
335
336 API service_adaptor_error_e auth_adaptor_unref_plugin(auth_adaptor_h auth, const char *uri)
337 {
338         SAL_FN_CALL;
339
340         RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
341         RETV_IF(NULL == uri, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
342
343         g_mutex_lock(&auth->mutex);
344
345         int ret = SERVICE_ADAPTOR_ERROR_NO_DATA;
346
347         for (GList *list = g_list_first(auth->plugins); list != NULL; list = list->next)
348         {
349                 auth_plugin_h this = (auth_plugin_h) list->data;
350
351                 if (0 == strcmp(this->uri, uri))
352                 {
353                         ret = SERVICE_ADAPTOR_ERROR_NONE;
354                         // TODO: decrease ref count
355                         break;
356                 }
357         }
358
359         g_mutex_unlock(&auth->mutex);
360
361         return ret;
362 }