4 * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "auth_adaptor.h"
24 #include "service_adaptor_internal.h"
26 //******************************************************************************
27 //* Global variables and defines
28 //******************************************************************************
30 //******************************************************************************
32 //******************************************************************************
34 //******************************************************************************
35 //* Private interface definition
36 //******************************************************************************
38 static void _auth_adaptor_free_plugin(auth_plugin_h plugin)
42 //******************************************************************************
43 //* Public interface definition
44 //******************************************************************************
46 API auth_adaptor_h auth_adaptor_create()
50 auth_adaptor_h auth = (auth_adaptor_h) g_malloc0(sizeof(auth_adaptor_s));
52 g_mutex_init(&auth->mutex);
57 API service_adaptor_error_e auth_adaptor_destroy(auth_adaptor_h auth)
61 RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
63 g_mutex_lock(&auth->mutex);
67 auth_adaptor_stop(auth);
72 g_mutex_unlock(&auth->mutex);
74 return SERVICE_ADAPTOR_ERROR_NONE;
77 API service_adaptor_error_e auth_adaptor_start(auth_adaptor_h auth)
81 RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
83 g_mutex_lock(&auth->mutex);
87 g_mutex_unlock(&auth->mutex);
89 return SERVICE_ADAPTOR_ERROR_NONE;
92 API service_adaptor_error_e auth_adaptor_stop(auth_adaptor_h auth)
96 RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
98 // TODO: notify auth adaptor stop to each plugin
100 g_mutex_lock(&auth->mutex);
104 g_mutex_unlock(&auth->mutex);
106 return SERVICE_ADAPTOR_ERROR_NONE;
109 API service_adaptor_error_e auth_adaptor_register_listener(auth_adaptor_h auth, auth_adaptor_listener_h listener)
113 RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
114 RETV_IF(NULL == listener, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
116 // TODO: register insert/ update/ delete callback for service-adaptor
118 return SERVICE_ADAPTOR_ERROR_NONE;
121 API service_adaptor_error_e auth_adaptor_unregister_listener(auth_adaptor_h auth, auth_adaptor_listener_h listener)
125 RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
126 RETV_IF(NULL == listener, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
128 // TODO: unregister insert/ update/ delete callback for service-adaptor
130 return SERVICE_ADAPTOR_ERROR_NONE;
133 API service_adaptor_error_e auth_adaptor_create_plugin(const char *uri, const char *name, const char *package, auth_plugin_h *plugin)
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);
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);
146 g_mutex_init(&auth_plugin->mutex);
147 g_cond_init(&auth_plugin->cond);
149 *plugin = auth_plugin;
151 return SERVICE_ADAPTOR_ERROR_NONE;
154 API service_adaptor_error_e auth_adaptor_destroy_plugin(auth_plugin_h plugin)
158 RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
160 SAL_FREE(plugin->uri);
161 SAL_FREE(plugin->name);
164 return SERVICE_ADAPTOR_ERROR_NONE;
167 API service_adaptor_error_e auth_adaptor_register_plugin_service(auth_plugin_h plugin, GHashTable *service)
171 RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
172 RETV_IF(NULL == service, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
174 int ret = SERVICE_ADAPTOR_ERROR_NONE;
176 plugin->oauth1 = (oauth1_service_h) g_malloc0(sizeof(oauth1_service_s));
177 ret = oauth1_register_service(plugin->oauth1, service);
179 if (SERVICE_ADAPTOR_ERROR_NONE != ret)
181 SAL_INFO("could not find the function for oauth 1.0");
182 SAL_FREE(plugin->oauth1);
185 plugin->oauth2 = (oauth2_service_h) g_malloc0(sizeof(oauth2_service_s));
186 ret = oauth2_register_service(plugin->oauth2, service);
188 if (SERVICE_ADAPTOR_ERROR_NONE != ret)
190 SAL_INFO("could not find the function for oauth 2.0");
191 SAL_FREE(plugin->oauth2);
194 return SERVICE_ADAPTOR_ERROR_NONE;
197 API service_adaptor_error_e auth_adaptor_unregister_plugin_service(auth_plugin_h plugin)
201 RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
203 if (NULL != plugin->oauth1)
205 oauth1_unregister_service(plugin->oauth1);
206 SAL_FREE(plugin->oauth1);
209 if (NULL != plugin->oauth2)
211 oauth2_unregister_service(plugin->oauth2);
212 SAL_FREE(plugin->oauth2);
215 return SERVICE_ADAPTOR_ERROR_NONE;
218 API service_adaptor_error_e auth_adaptor_add_plugin(auth_adaptor_h auth, auth_plugin_h plugin)
222 RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
223 RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
225 g_mutex_lock(&auth->mutex);
227 auth->plugins = g_list_append(auth->plugins, plugin);
229 g_mutex_unlock(&auth->mutex);
231 return SERVICE_ADAPTOR_ERROR_NONE;
234 API service_adaptor_error_e auth_adaptor_remove_plugin(auth_adaptor_h auth, auth_plugin_h plugin)
238 RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
239 RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
241 g_mutex_lock(&auth->mutex);
243 RETV_IF(NULL == g_list_find(auth->plugins, plugin), SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
245 auth->plugins = g_list_remove(auth->plugins, plugin);
247 _auth_adaptor_free_plugin(plugin);
249 g_mutex_unlock(&auth->mutex);
251 return SERVICE_ADAPTOR_ERROR_NONE;
254 API auth_plugin_h auth_adaptor_get_plugin(auth_adaptor_h auth, const char *uri)
258 RETV_IF(NULL == auth, NULL);
259 RETV_IF(NULL == uri, NULL);
261 g_mutex_lock(&auth->mutex);
263 auth_plugin_h plugin = NULL;
265 for (GList *list = g_list_first(auth->plugins); list != NULL; list = list->next)
267 auth_plugin_h this = (auth_plugin_h) list->data;
269 if (0 == strcmp(this->uri, uri))
276 g_mutex_unlock(&auth->mutex);
281 API char *auth_adaptor_get_uri(auth_adaptor_h auth, const char *package)
285 RETV_IF(NULL == auth, NULL);
286 RETV_IF(NULL == package, NULL);
288 g_mutex_lock(&auth->mutex);
292 for (GList *list = g_list_first(auth->plugins); list != NULL; list = list->next)
294 auth_plugin_h this = (auth_plugin_h) list->data;
296 if (0 == strcmp(this->package, package))
303 g_mutex_unlock(&auth->mutex);
308 API service_adaptor_error_e auth_adaptor_ref_plugin(auth_adaptor_h auth, const char *uri)
312 RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
313 RETV_IF(NULL == uri, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
315 g_mutex_lock(&auth->mutex);
317 int ret = SERVICE_ADAPTOR_ERROR_NO_DATA;
319 for (GList *list = g_list_first(auth->plugins); list != NULL; list = list->next)
321 auth_plugin_h this = (auth_plugin_h) list->data;
323 if (0 == strcmp(this->uri, uri))
325 ret = SERVICE_ADAPTOR_ERROR_NONE;
326 // TODO: increase ref count
331 g_mutex_unlock(&auth->mutex);
336 API service_adaptor_error_e auth_adaptor_unref_plugin(auth_adaptor_h auth, const char *uri)
340 RETV_IF(NULL == auth, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
341 RETV_IF(NULL == uri, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
343 g_mutex_lock(&auth->mutex);
345 int ret = SERVICE_ADAPTOR_ERROR_NO_DATA;
347 for (GList *list = g_list_first(auth->plugins); list != NULL; list = list->next)
349 auth_plugin_h this = (auth_plugin_h) list->data;
351 if (0 == strcmp(this->uri, uri))
353 ret = SERVICE_ADAPTOR_ERROR_NONE;
354 // TODO: decrease ref count
359 g_mutex_unlock(&auth->mutex);