ctsvc_client_handle_remove in CTS_MUTEX_CONNECTION
[platform/core/pim/contacts-service.git] / client / ctsvc_client_noti.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2012 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 <unistd.h>
22 #include <pims-ipc.h>
23 #include <pims-ipc-svc.h>
24 #include <pims-ipc-data.h>
25
26 #include "contacts.h"
27 #include "ctsvc_internal.h"
28 #include "ctsvc_ipc_define.h"
29 #include "ctsvc_ipc_marshal.h"
30 #include "ctsvc_mutex.h"
31 #include "ctsvc_client_ipc.h"
32 #include "contacts_extension.h"
33
34 typedef struct
35 {
36         contacts_db_change_cb_with_info cb;
37         void *user_data;
38 }db_callback_info_s;
39
40 typedef struct
41 {
42         char *view_uri;
43         GSList *callbacks;
44 }subscribe_info_s;
45
46 static int __ipc_pubsub_ref = 0;
47 static pims_ipc_h __ipc = NULL;
48 static GSList *__db_change_subscribe_list = NULL;
49
50 static void __ctsvc_db_subscriber_callback(pims_ipc_h ipc, pims_ipc_data_h data, void *user_data)
51 {
52         int ret;
53         char *str = NULL;
54         subscribe_info_s *info = user_data;
55
56         if (data) {
57                 ret = ctsvc_ipc_unmarshal_string(data, &str);
58                 WARN_IF(CONTACTS_ERROR_NONE != ret, "ctsvc_ipc_unmarshal_string() Fail(%d)", ret);
59         }
60
61         if (NULL == str) {
62                 CTS_ERR("There is no changed data");
63                 goto DATA_FREE;
64         }
65
66         if (info) {
67                 GSList *l;
68                 for (l = info->callbacks;l;l=l->next) {
69                         db_callback_info_s *cb_info = l->data;
70                         if (cb_info->cb) {
71                                 cb_info->cb(info->view_uri, str, cb_info->user_data);
72                         }
73                 }
74         }
75 DATA_FREE:
76         free(str);
77 }
78
79 /* This API should be called in CTS_MUTEX_PIMS_IPC_PUBSUB mutex */
80 pims_ipc_h ctsvc_ipc_get_handle_for_change_subsciption()
81 {
82         return __ipc;
83 }
84
85 int ctsvc_ipc_create_for_change_subscription()
86 {
87         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
88
89         if (0 < __ipc_pubsub_ref) {
90                 __ipc_pubsub_ref++;
91                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
92                 return CONTACTS_ERROR_NONE;
93         }
94
95         if (NULL == __ipc) {
96                 char sock_file[CTSVC_PATH_MAX_LEN] = {0};
97                 snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/.%s_for_subscribe", getuid(), CTSVC_IPC_SERVICE);
98                 __ipc = pims_ipc_create_for_subscribe(sock_file);
99                 if (NULL == __ipc) {
100                         CTS_ERR("pims_ipc_create_for_subscribe error\n");
101                         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
102                         return CONTACTS_ERROR_IPC;
103                 }
104         }
105         __ipc_pubsub_ref++;
106         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
107         return CONTACTS_ERROR_NONE;
108 }
109
110 int ctsvc_ipc_recover_for_change_subscription()
111 {
112         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
113         if (__ipc_pubsub_ref <= 0) {
114                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
115                 return CONTACTS_ERROR_NONE;
116         }
117         if (__ipc)
118                 ctsvc_ipc_unset_disconnected_cb(__ipc);
119         __ipc = pims_ipc_create_for_subscribe(CTSVC_IPC_SOCKET_PATH_FOR_CHANGE_SUBSCRIPTION);
120         if (!__ipc) {
121                 CTS_ERR("pims_ipc_create_for_subscribe error\n");
122                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
123                 return CONTACTS_ERROR_IPC;
124         }
125         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
126         return CONTACTS_ERROR_NONE;
127 }
128
129 int ctsvc_ipc_destroy_for_change_subscription()
130 {
131         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
132
133         if (1 == __ipc_pubsub_ref) {
134                 ctsvc_ipc_unset_disconnected_cb(__ipc);
135                 pims_ipc_destroy_for_subscribe(__ipc);
136                 __ipc = NULL;
137         }
138         else if (1 < __ipc_pubsub_ref) {
139                 CTS_DBG("ctsvc pubsub ipc ref count : %d", __ipc_pubsub_ref);
140         }
141         else {
142                 CTS_DBG("System : please call connection APIs, connection count is (%d)", __ipc_pubsub_ref);
143                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
144                 return CONTACTS_ERROR_INVALID_PARAMETER;
145         }
146
147         __ipc_pubsub_ref--;
148
149         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
150         return CONTACTS_ERROR_NONE;
151 }
152
153 API int contacts_db_add_changed_cb_with_info(const char* view_uri,
154                 contacts_db_change_cb_with_info cb, void* user_data)
155 {
156         GSList *it = NULL;
157         subscribe_info_s *info = NULL;
158         db_callback_info_s *cb_info;
159         bool result = false;
160         int ret;
161
162         RETVM_IF(view_uri == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "view_uri is NULL");
163         RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "cb is NULL");
164
165         if (STRING_EQUAL == strncmp(view_uri, CTSVC_VIEW_URI_PHONELOG, strlen(CTSVC_VIEW_URI_PHONELOG))) {
166                 ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_PHONELOG_READ, &result);
167                 RETVM_IF(ret != CONTACTS_ERROR_NONE, ret, "ctsvc_ipc_client_check_permission fail (%d)", ret);
168                 RETVM_IF(result == false, CONTACTS_ERROR_PERMISSION_DENIED, "Permission denied (phonelog read)");
169         }
170         else if (STRING_EQUAL == strncmp(view_uri, CTSVC_VIEW_URI_PERSON, strlen(CTSVC_VIEW_URI_PERSON))) {
171                 ret = ctsvc_ipc_client_check_permission(CTSVC_PERMISSION_CONTACT_READ, &result);
172                 RETVM_IF(ret != CONTACTS_ERROR_NONE, ret, "ctsvc_ipc_client_check_permission fail (%d)", ret);
173                 RETVM_IF(result == false, CONTACTS_ERROR_PERMISSION_DENIED, "Permission denied (contact read)");
174         }
175         else {
176                 CTS_ERR("We support this API for only %s and %s", CTSVC_VIEW_URI_PERSON, CTSVC_VIEW_URI_PHONELOG);
177                 return CONTACTS_ERROR_INVALID_PARAMETER;
178         }
179
180         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
181
182         for (it=__db_change_subscribe_list;it;it=it->next) {
183                 if (NULL == it->data) continue;
184
185                 info = it->data;
186                 if (STRING_EQUAL == strcmp(info->view_uri, view_uri))
187                         break;
188                 else
189                         info = NULL;
190         }
191
192         if (NULL == info) {
193                 info = calloc(1, sizeof(subscribe_info_s));
194                 if (NULL == info) {
195                         CTS_ERR("calloc() Fail");
196                         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
197                         return CONTACTS_ERROR_OUT_OF_MEMORY;
198                 }
199
200                 if (pims_ipc_subscribe(__ipc, CTSVC_IPC_SUBSCRIBE_MODULE, (char*)view_uri,
201                                         __ctsvc_db_subscriber_callback, (void*)info) != 0) {
202                         CTS_ERR("pims_ipc_subscribe error\n");
203                         free(info);
204                         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
205                         return CONTACTS_ERROR_IPC;
206                 }
207                 info->view_uri = strdup(view_uri);
208                 __db_change_subscribe_list = g_slist_append(__db_change_subscribe_list, info);
209         }
210         else {
211                 GSList *l;
212                 for (l = info->callbacks;l;l=l->next) {
213                         db_callback_info_s *cb_info = l->data;
214                         if (cb_info->cb == cb && cb_info->user_data == user_data) {
215                                 CTS_ERR("The same callback(%s) is already exist", view_uri);
216                                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
217                                 return CONTACTS_ERROR_INVALID_PARAMETER;
218                         }
219                 }
220         }
221
222         cb_info = calloc(1, sizeof(db_callback_info_s));
223         if (NULL == cb_info) {
224                 CTS_ERR("calloc() Failed");
225                 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
226                 return CONTACTS_ERROR_OUT_OF_MEMORY;
227         }
228         cb_info->user_data = user_data;
229         cb_info->cb = cb;
230         info->callbacks = g_slist_append(info->callbacks, cb_info);
231
232         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
233         return CONTACTS_ERROR_NONE;
234 }
235
236 API int contacts_db_remove_changed_cb_with_info(const char* view_uri,
237                 contacts_db_change_cb_with_info cb, void* user_data)
238 {
239         GSList *it = NULL;
240         subscribe_info_s *info = NULL;
241
242         RETVM_IF(view_uri == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "view_uri is NULL");
243         RETVM_IF(cb == NULL, CONTACTS_ERROR_INVALID_PARAMETER, "cb is NULL");
244
245         if (STRING_EQUAL != strcmp(view_uri, CTSVC_VIEW_URI_PHONELOG) &&
246                         STRING_EQUAL != strcmp(view_uri, CTSVC_VIEW_URI_PERSON)) {
247                 CTS_ERR("We support this API for only %s and %s", CTSVC_VIEW_URI_PERSON, CTSVC_VIEW_URI_PHONELOG);
248                 return CONTACTS_ERROR_INVALID_PARAMETER;
249         }
250
251         ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_PUBSUB);
252
253         for (it=__db_change_subscribe_list;it;it=it->next) {
254                 if (NULL == it->data) continue;
255
256                 info = it->data;
257                 if (STRING_EQUAL == strcmp(info->view_uri, view_uri))
258                         break;
259                 else
260                         info = NULL;
261         }
262
263         if (info) {
264                 GSList *l;
265                 for (l = info->callbacks;l;l=l->next) {
266                         db_callback_info_s *cb_info = l->data;
267                         if (cb == cb_info->cb && user_data == cb_info->user_data) {
268                                 info->callbacks = g_slist_remove(info->callbacks, cb_info);
269                                 free(cb_info);
270                                 break;
271                         }
272                 }
273                 if (g_slist_length(info->callbacks) == 0) {
274                         pims_ipc_unsubscribe(__ipc, CTSVC_IPC_SUBSCRIBE_MODULE, info->view_uri);
275                         __db_change_subscribe_list = g_slist_remove(__db_change_subscribe_list, info);
276                         free(info->view_uri);
277                         free(info);
278                 }
279         }
280
281         ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_PUBSUB);
282         return CONTACTS_ERROR_NONE;
283 }