support Pause/Resume with new connection
[framework/web/download-provider.git] / src / download-provider-receiver.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/socket.h>
4 #include <sys/un.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <time.h>
8 #include <sys/time.h>
9
10 #include <glib.h>
11
12 #include <net_connection.h>
13
14 #include "download-provider-config.h"
15 #include "download-provider-log.h"
16 #include "download-provider-pthread.h"
17 #include "download-provider-notification.h"
18 #include "download-provider-ipc.h"
19 #include "download-provider-db.h"
20 #include "download-provider-utils.h"
21
22 #include "download-agent-defs.h"
23 #include "download-agent-interface.h"
24
25 int _init_agent(void);
26 void _deinit_agent(void);
27 static void __downloading_info_cb(user_downloading_info_t *download_info,
28                                         void *user_data);
29 static void __download_info_cb(user_download_info_t *download_info,
30                                         void *user_data);
31 static void __notify_cb(user_notify_info_t *notify_info, void *user_data);
32 static int __change_error(int err);
33 static int __change_state(da_state state);
34
35 void TerminateDaemon(int signo);
36
37 pthread_attr_t g_download_provider_thread_attr;
38 fd_set g_download_provider_socket_readset;
39 fd_set g_download_provider_socket_exceptset;
40
41 void *_start_download(void *args)
42 {
43         int da_ret = -1;
44         int req_dl_id = -1;
45
46         download_clientinfo_slot *clientinfoslot =
47                 (download_clientinfo_slot *) args;
48         if (!clientinfoslot) {
49                 TRACE_DEBUG_MSG("[NULL-CHECK] download_clientinfo_slot");
50                 return 0;
51         }
52         download_clientinfo *clientinfo =
53                 (download_clientinfo *) clientinfoslot->clientinfo;
54         if (!clientinfo) {
55                 TRACE_DEBUG_MSG("[NULL-CHECK] download_clientinfo");
56                 return 0;
57         }
58
59         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
60         clientinfo->state = DOWNLOAD_STATE_READY;
61         clientinfo->err = DOWNLOAD_ERROR_NONE;
62         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
63
64         // call start_download() of download-agent
65         if (clientinfo->requestinfo->headers.rows > 0) {
66                 int len = 0;
67                 int i = 0;
68                 char **req_header = NULL;
69                 len = clientinfo->requestinfo->headers.rows;
70                 req_header = calloc(len, sizeof(char *));
71                 if (!req_header) {
72                         TRACE_DEBUG_MSG("fail to calloc");
73                         return 0;
74                 }
75                 for (i = 0; i < len; i++)
76                         req_header[i] =
77                                 strdup(clientinfo->requestinfo->headers.str[i].str);
78                 if (clientinfo->requestinfo->install_path.length > 1) {
79                         if (clientinfo->requestinfo->filename.length > 1)
80                                 da_ret =
81                                         da_start_download_with_extension(clientinfo->requestinfo->
82                                                         url.str, &req_dl_id,
83                                                         DA_FEATURE_REQUEST_HEADER,
84                                                         req_header, &len,
85                                                         DA_FEATURE_INSTALL_PATH,
86                                                         clientinfo->requestinfo->install_path.str,
87                                                         DA_FEATURE_FILE_NAME,
88                                                         clientinfo->requestinfo->filename.str,
89                                                         DA_FEATURE_USER_DATA,
90                                                         (void *)clientinfoslot,
91                                                         NULL);
92                         else
93                                 da_ret =
94                                         da_start_download_with_extension(clientinfo->requestinfo->
95                                                         url.str, &req_dl_id,
96                                                         DA_FEATURE_REQUEST_HEADER,
97                                                         req_header, &len,
98                                                         DA_FEATURE_INSTALL_PATH,
99                                                         clientinfo->requestinfo->install_path.str,
100                                                         DA_FEATURE_USER_DATA,
101                                                         (void *)clientinfoslot,
102                                                         NULL);
103                 } else {
104                         if (clientinfo->requestinfo->filename.length > 1)
105                                 da_ret =
106                                         da_start_download_with_extension(clientinfo->requestinfo->
107                                                         url.str, &req_dl_id,
108                                                         DA_FEATURE_REQUEST_HEADER,
109                                                         req_header, &len,
110                                                         DA_FEATURE_FILE_NAME,
111                                                         clientinfo->requestinfo->filename.str,
112                                                         DA_FEATURE_USER_DATA,
113                                                         (void *)clientinfoslot,
114                                                         NULL);
115                         else
116                                 da_ret =
117                                         da_start_download_with_extension(clientinfo->requestinfo->
118                                                         url.str, &req_dl_id,
119                                                         DA_FEATURE_REQUEST_HEADER,
120                                                         req_header, &len,
121                                                         DA_FEATURE_USER_DATA,
122                                                         (void *)clientinfoslot,
123                                                         NULL);
124                 }
125                 for (i = 0; i < len; i++) {
126                         if (req_header[i])
127                                 free(req_header[i]);
128                 }
129         } else {
130                 if (clientinfo->requestinfo->install_path.length > 1) {
131                         if (clientinfo->requestinfo->filename.length > 1)
132                                 da_ret =
133                                         da_start_download_with_extension(clientinfo->requestinfo->
134                                                         url.str, &req_dl_id,
135                                                         DA_FEATURE_INSTALL_PATH,
136                                                         clientinfo->requestinfo->install_path.str,
137                                                         DA_FEATURE_FILE_NAME,
138                                                         clientinfo->requestinfo->filename.str,
139                                                         DA_FEATURE_USER_DATA,
140                                                         (void *)clientinfoslot,
141                                                         NULL);
142                         else
143                                 da_ret =
144                                         da_start_download_with_extension(clientinfo->requestinfo->
145                                                         url.str, &req_dl_id,
146                                                         DA_FEATURE_INSTALL_PATH,
147                                                         clientinfo->requestinfo->install_path.str,
148                                                         DA_FEATURE_USER_DATA,
149                                                         (void *)clientinfoslot,
150                                                         NULL);
151                 } else {
152                         if (clientinfo->requestinfo->filename.length > 1)
153                                 da_ret =
154                                         da_start_download_with_extension(clientinfo->requestinfo->
155                                                         url.str, &req_dl_id,
156                                                         DA_FEATURE_FILE_NAME,
157                                                         clientinfo->requestinfo->filename.str,
158                                                         DA_FEATURE_USER_DATA,
159                                                         (void *)clientinfoslot,
160                                                         NULL);
161                         else
162                                 da_ret =
163                                         da_start_download_with_extension(clientinfo->requestinfo->
164                                                         url.str, &req_dl_id,
165                                                         DA_FEATURE_USER_DATA,
166                                                         (void *)clientinfoslot,
167                                                         NULL);
168                 }
169         }
170
171         // if start_download() return error cause of maximun download limitation, set state to DOWNLOAD_STATE_PENDED.
172         if (da_ret == DA_ERR_ALREADY_MAX_DOWNLOAD) {
173                 TRACE_DEBUG_INFO_MSG("change to pended request [%d]", da_ret);
174                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
175                 clientinfo->state = DOWNLOAD_STATE_PENDED;
176                 clientinfo->err = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
177                 download_provider_db_requestinfo_update_column(clientinfo,
178                                                                         DOWNLOAD_DB_STATE);
179                 ipc_send_request_stateinfo(clientinfo);
180                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
181                 return 0;
182         } else if (da_ret != DA_RESULT_OK) {
183                 TRACE_DEBUG_INFO_MSG("Fail to request start [%d]", da_ret);
184                 /* FIXME : need to seperate in detail according to error return values */
185                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
186                 clientinfo->state = DOWNLOAD_STATE_FAILED;
187                 clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
188                 download_provider_db_requestinfo_remove(clientinfo->
189                                                         requestinfo->requestid);
190                 ipc_send_request_stateinfo(clientinfo);
191                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
192                 return 0;
193         }
194
195         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
196
197         TRACE_DEBUG_INFO_MSG("started download [%d]", da_ret);
198
199         clientinfo->req_id = req_dl_id;
200         clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
201         clientinfo->err = DOWNLOAD_ERROR_NONE;
202
203         download_provider_db_requestinfo_update_column(clientinfo,
204                                                                 DOWNLOAD_DB_STATE);
205
206         // sync return  // client should be alive till this line at least.
207         ipc_send_request_stateinfo(clientinfo);
208
209         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
210         return 0;
211 }
212
213 int _handle_new_connection(download_clientinfo_slot *clientinfo_list, download_clientinfo *request_clientinfo)
214 {
215         uint searchslot = 0;
216
217         // NULL - checking
218         if (!clientinfo_list || !request_clientinfo ) {
219                 TRACE_DEBUG_MSG("NULL-CHECK");
220                 return -1;
221         }
222
223         CLIENT_MUTEX_INIT(&(request_clientinfo->client_mutex), NULL);
224
225 #ifdef SO_PEERCRED
226         socklen_t cr_len =
227                 sizeof(request_clientinfo->credentials);
228         if (getsockopt
229                 (request_clientinfo->clientfd, SOL_SOCKET,
230                 SO_PEERCRED, &request_clientinfo->credentials,
231                 &cr_len) == 0) {
232                 TRACE_DEBUG_INFO_MSG
233                         ("Client Info : pid=%d, uid=%d, gid=%d\n",
234                         request_clientinfo->credentials.pid,
235                         request_clientinfo->credentials.uid,
236                         request_clientinfo->credentials.gid);
237         }
238 #endif
239
240         download_controls type =
241                 ipc_receive_header(request_clientinfo->clientfd);
242         TRACE_DEBUG_INFO_MSG("[ACCEPT] HEADER : [%d] ", type);
243         // first of all, receive requestinfo .
244         if (type <= 0 || ipc_receive_request_msg(request_clientinfo) < 0) {
245                 TRACE_DEBUG_MSG("Ignore this connection, Invalid command");
246                 clear_clientinfo(request_clientinfo);
247                 return -1;
248         }
249
250         if (type == DOWNLOAD_CONTROL_STOP
251                 || type == DOWNLOAD_CONTROL_GET_STATE_INFO
252                 || type == DOWNLOAD_CONTROL_RESUME
253                 || type == DOWNLOAD_CONTROL_PAUSE) {
254                 // get requestid from socket.
255                 if (request_clientinfo->requestinfo
256                         && request_clientinfo->requestinfo->requestid > 0) {
257                         // search requestid in slots.
258                         int searchindex = get_same_request_slot_index
259                                                         (clientinfo_list,
260                                                         request_clientinfo->requestinfo->requestid);
261                         if (type == DOWNLOAD_CONTROL_STOP) {
262                                 TRACE_DEBUG_INFO_MSG("Request : DOWNLOAD_CONTROL_STOP");
263                                 if (searchindex >= 0) {
264                                         if (da_cancel_download
265                                                 (clientinfo_list[searchindex].clientinfo->req_id)
266                                                 == DA_RESULT_OK) {
267                                                 request_clientinfo->state = DOWNLOAD_STATE_STOPPED;
268                                                 request_clientinfo->err = DOWNLOAD_ERROR_NONE;
269                                                 if (clientinfo_list[searchindex].clientinfo->requestinfo
270                                                         && clientinfo_list[searchindex].clientinfo->requestinfo->notification)
271                                                         set_downloadedinfo_appfw_notification(clientinfo_list[searchindex].clientinfo);
272                                                 download_provider_db_requestinfo_remove(request_clientinfo->requestinfo->requestid);
273                                                 download_provider_db_history_new(clientinfo_list[searchindex].clientinfo);
274                                         } else {
275                                                 request_clientinfo->state = DOWNLOAD_STATE_FAILED;
276                                                 request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
277                                         }
278                                 } else { // no found
279                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
280                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
281                                 }
282                                 ipc_send_stateinfo(request_clientinfo);
283                         } else if (type == DOWNLOAD_CONTROL_GET_STATE_INFO) {
284                                 // search slots/downloading db/history db
285                                 if (searchindex > 0) { // exist in slots (memory)
286                                         request_clientinfo->state =
287                                                 clientinfo_list[searchindex].clientinfo->state;
288                                         request_clientinfo->err =
289                                                 clientinfo_list[searchindex].clientinfo->err;
290                                 } else {  //search downloading db or history db
291                                         download_dbinfo* dbinfo =
292                                                 download_provider_db_get_info(
293                                                         request_clientinfo->requestinfo->requestid);
294                                         if (dbinfo) { // found in downloading db..it means crashed job
295                                                 request_clientinfo->state = DOWNLOAD_STATE_PENDED;
296                                                 request_clientinfo->err = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
297                                         } else { // no exist in downloading db
298                                                 dbinfo = download_provider_db_history_get_info(
299                                                         request_clientinfo->requestinfo->requestid);
300                                                 if (!dbinfo) { // no info anywhere
301                                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
302                                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
303                                                 } else { //history info
304                                                         request_clientinfo->state = dbinfo->state;
305                                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
306                                                 }
307                                         }
308                                         download_provider_db_info_free(dbinfo);
309                                         free(dbinfo);
310                                 }
311                                 ipc_send_stateinfo(request_clientinfo);
312                                 // estabilish the spec of return value.
313                         } else if (type == DOWNLOAD_CONTROL_PAUSE) {
314                                 if (searchindex >= 0) {
315                                         if (da_suspend_download
316                                                 (clientinfo_list[searchindex].clientinfo->req_id)
317                                                 == DA_RESULT_OK) {
318                                                 request_clientinfo->state = DOWNLOAD_STATE_PAUSE_REQUESTED;
319                                                 request_clientinfo->err = DOWNLOAD_ERROR_NONE;
320                                         } else {
321                                                 request_clientinfo->state = DOWNLOAD_STATE_FAILED;
322                                                 request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
323                                         }
324                                 } else { // no found
325                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
326                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
327                                 }
328                                 ipc_send_stateinfo(request_clientinfo);
329                         } else if (type == DOWNLOAD_CONTROL_RESUME) {
330                                 if (searchindex >= 0) {
331                                         if (da_resume_download
332                                                 (clientinfo_list[searchindex].clientinfo->req_id)
333                                                 == DA_RESULT_OK) {
334                                                 request_clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
335                                                 request_clientinfo->err = DOWNLOAD_ERROR_NONE;
336                                         } else {
337                                                 request_clientinfo->state = DOWNLOAD_STATE_FAILED;
338                                                 request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
339                                         }
340                                 } else { // no found
341                                         request_clientinfo->state = DOWNLOAD_STATE_NONE;
342                                         request_clientinfo->err = DOWNLOAD_ERROR_NONE;
343                                 }
344                                 ipc_send_stateinfo(request_clientinfo);
345                         }
346                 }
347                 clear_clientinfo(request_clientinfo);
348                 return 0;
349         }
350
351         if (type != DOWNLOAD_CONTROL_START) {
352                 TRACE_DEBUG_MSG
353                         ("Now, DOWNLOAD_CONTROL_START is only supported");
354                 clear_clientinfo(request_clientinfo);
355                 return -1;
356         }
357
358         // check whether requestinfo has requestid or not.
359         if (request_clientinfo->requestinfo
360                 && request_clientinfo->requestinfo->requestid > 0) {
361                 // search same request id.
362                 int searchindex = get_same_request_slot_index(clientinfo_list,
363                                                 request_clientinfo->requestinfo->requestid);
364                 if (searchindex < 0) {
365                         TRACE_DEBUG_INFO_MSG("Not Found Same Request ID");
366                         // Invalid id
367                         request_clientinfo->state = DOWNLOAD_STATE_FAILED;
368                         request_clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
369                         ipc_send_request_stateinfo(request_clientinfo);
370                         clear_clientinfo(request_clientinfo);
371                         return 0;
372                 } else {        // found request id. // how to deal etag ?
373                         // connect to slot.
374                         TRACE_DEBUG_INFO_MSG("Found Same Request ID slot[%d]", searchindex);
375                         CLIENT_MUTEX_LOCK(&(request_clientinfo->client_mutex));
376                         // close previous socket.
377                         if (clientinfo_list[searchindex].clientinfo->clientfd > 0)
378                                 close(clientinfo_list[searchindex].clientinfo->clientfd);
379                         // change to new socket.
380                         clientinfo_list[searchindex].clientinfo->clientfd =
381                                 request_clientinfo->clientfd;
382                         ipc_send_request_stateinfo(clientinfo_list[searchindex].clientinfo);
383                         // update some info.
384                         clientinfo_list[searchindex].clientinfo->requestinfo->callbackinfo =
385                                 request_clientinfo->requestinfo->callbackinfo;
386                         clientinfo_list[searchindex].clientinfo->requestinfo->notification =
387                                 request_clientinfo->requestinfo->notification;
388                         request_clientinfo->clientfd = 0;       // prevent to not be disconnected.
389                         CLIENT_MUTEX_UNLOCK(&(request_clientinfo->client_mutex));
390                         clear_clientinfo(request_clientinfo);
391                         return 0;
392                 }
393         }
394
395         // new request.
396         searchslot = get_empty_slot_index(clientinfo_list);
397         if (searchslot < 0) {
398                 TRACE_DEBUG_MSG("download-provider is busy, try later");
399                 clear_clientinfo(request_clientinfo);
400                 sleep(5);       // provider need the time of refresh.
401                 return -1;
402         }
403         // create new unique id, and insert info to DB.
404         if (request_clientinfo->requestinfo
405                 && request_clientinfo->requestinfo->requestid <= 0) {
406                 request_clientinfo->requestinfo->requestid =
407                         get_download_request_id();
408                 if (download_provider_db_requestinfo_new
409                         (request_clientinfo) < 0) {
410                         clear_clientinfo(request_clientinfo);
411                         sleep(5);       // provider need the time of refresh.
412                         return -1;
413                 }
414         }
415
416         clientinfo_list[searchslot].clientinfo = request_clientinfo;
417
418         TRACE_DEBUG_INFO_MSG("New Connection slot [%d] max [%d] max once [%d]",
419                                                                                         searchslot,
420                                                                                         MAX_CLIENT,
421                                                                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE);
422
423         if (get_downloading_count(clientinfo_list) >=
424                 DA_MAX_DOWNLOAD_REQ_AT_ONCE) {
425                 CLIENT_MUTEX_LOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
426                 // deal as pended job.
427                 clientinfo_list[searchslot].clientinfo->state = DOWNLOAD_STATE_PENDED;
428                 clientinfo_list[searchslot].clientinfo->err = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
429                 download_provider_db_requestinfo_update_column
430                         (clientinfo_list[searchslot].clientinfo,
431                         DOWNLOAD_DB_STATE);
432                 ipc_send_request_stateinfo(clientinfo_list[searchslot].clientinfo);
433                 TRACE_DEBUG_INFO_MSG ("Pended Request is saved to [%d/%d]",
434                         searchslot, MAX_CLIENT);
435                 CLIENT_MUTEX_UNLOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
436                 sleep(5);       // provider need the time of refresh.
437         } else {
438                 // create thread for receiving the reqeust info from client.
439                 // and if possible, it will create the thread for listening the event.
440                 if (pthread_create
441                         (&clientinfo_list[searchslot].clientinfo->thread_pid,
442                         &g_download_provider_thread_attr, _start_download,
443                         &clientinfo_list[searchslot]) != 0) {
444                         TRACE_DEBUG_INFO_MSG("failed to call pthread_create for client");
445                         TRACE_DEBUG_INFO_MSG("Change to pended job");
446                         CLIENT_MUTEX_LOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
447                         clientinfo_list[searchslot].clientinfo->state =
448                                 DOWNLOAD_STATE_PENDED;
449                         clientinfo_list[searchslot].clientinfo->err =
450                                 DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
451                         download_provider_db_requestinfo_update_column
452                                 (clientinfo_list[searchslot].clientinfo,
453                                 DOWNLOAD_DB_STATE);
454                         ipc_send_request_stateinfo(clientinfo_list[searchslot].clientinfo);
455                         CLIENT_MUTEX_UNLOCK(&(clientinfo_list[searchslot].clientinfo->client_mutex));
456                         sleep(5);       // provider need the time of refresh.
457                 }
458         }
459         return 0;
460 }
461
462 int _handle_client_request(download_clientinfo* clientinfo)
463 {
464         int da_ret = 0;
465         int msgType = 0;
466
467         // NULL - checking
468         if (!clientinfo) {
469                 TRACE_DEBUG_MSG("NULL-CHECK");
470                 return -1;
471         }
472
473         switch (msgType = ipc_receive_header(clientinfo->clientfd)) {
474         case DOWNLOAD_CONTROL_STOP:
475                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_STOP");
476                 da_ret = da_cancel_download(clientinfo->req_id);
477                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
478                 if (da_ret != DA_RESULT_OK) {
479                         /* FIXME : need to seperate in detail according to error return values */
480                         clientinfo->state = DOWNLOAD_STATE_FAILED;
481                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
482                         TRACE_DEBUG_MSG("Fail to request cancel [%d]", da_ret);
483                 } else {
484                         clientinfo->state = DOWNLOAD_STATE_STOPPED;
485                         clientinfo->err = DOWNLOAD_ERROR_NONE;
486                         if (clientinfo->requestinfo) {
487                                 if (clientinfo->requestinfo->notification)
488                                         set_downloadedinfo_appfw_notification(clientinfo);
489                                 download_provider_db_requestinfo_remove(clientinfo->
490                                         requestinfo->requestid);
491                         }
492                         download_provider_db_history_new(clientinfo);
493                 }
494                 ipc_send_stateinfo(clientinfo);
495                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
496                 break;
497         case DOWNLOAD_CONTROL_PAUSE:
498                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_PAUSE");
499                 da_ret = da_suspend_download(clientinfo->req_id);
500                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
501                 if (da_ret != DA_RESULT_OK) {
502                         /* FIXME : need to seperate in detail according to error return values */
503                         clientinfo->state = DOWNLOAD_STATE_FAILED;
504                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
505                         TRACE_DEBUG_MSG("Fail to request suspend [%d]", da_ret);
506                 } else {
507                         clientinfo->state = DOWNLOAD_STATE_PAUSE_REQUESTED;
508                         clientinfo->err = DOWNLOAD_ERROR_NONE;
509                 }
510                 ipc_send_stateinfo(clientinfo);
511                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
512                 break;
513         case DOWNLOAD_CONTROL_RESUME:
514                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_RESUME");
515                 da_ret = da_resume_download(clientinfo->req_id);
516                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
517                 if (da_ret != DA_RESULT_OK) {
518                         /* FIXME : need to seperate in detail according to error return values */
519                         clientinfo->state = DOWNLOAD_STATE_FAILED;
520                         clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
521                         TRACE_DEBUG_MSG("Fail to request resume [%d]", da_ret);
522                 } else {
523                         clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
524                         clientinfo->err = DOWNLOAD_ERROR_NONE;
525                 }
526                 ipc_send_stateinfo(clientinfo);
527                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
528                 break;
529         case DOWNLOAD_CONTROL_GET_STATE_INFO:   // sync call
530                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_GET_STATE_INFO");
531                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
532                 ipc_send_stateinfo(clientinfo);
533                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
534                 break;
535         case DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO:        // sync call
536                 TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO");
537                 CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
538                 ipc_send_downloadinfo(clientinfo);
539                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
540                 break;
541         case -1:
542         case 0:
543                 TRACE_DEBUG_MSG("(Closed Socket ) terminate event thread (%d)",
544                         msgType);
545                 // bloken socket... it seems the client is dead or closed by agent thread.
546                 // close socket, this will break the loop. and terminate this thread.
547                 clear_socket(clientinfo);
548                 return -1;
549         default:
550                 TRACE_DEBUG_MSG("Unknow message [%d]", msgType);
551                 return -1;
552         }
553         return 0;
554 }
555
556 void *run_manage_download_server(void *args)
557 {
558         int listenfd = 0;       // main socket to be albe to listen the new connection
559         int maxfd;
560         int ret = 0;
561         fd_set rset, exceptset;
562         struct timeval timeout;
563         long flexible_timeout;
564         download_clientinfo_slot *clientinfo_list;
565         uint searchslot = 0;
566         uint count_downloading_threads = 0;
567         download_clientinfo *request_clientinfo;
568         int check_retry = 1;
569         int i = 0;
570
571         socklen_t clientlen;
572         struct sockaddr_un listenaddr, clientaddr;
573
574         GMainLoop *mainloop = (GMainLoop *) args;
575
576         ret = _init_agent();
577         if (ret != DOWNLOAD_ERROR_NONE) {
578                 TRACE_DEBUG_MSG("failed to init agent");
579                 TerminateDaemon(SIGTERM);
580                 return 0;
581         }
582         clear_downloadinginfo_appfw_notification();
583
584         if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
585                 TRACE_DEBUG_MSG("failed to create socket");
586                 TerminateDaemon(SIGTERM);
587                 return 0;
588         }
589
590         bzero(&listenaddr, sizeof(listenaddr));
591         listenaddr.sun_family = AF_UNIX;
592         strcpy(listenaddr.sun_path, DOWNLOAD_PROVIDER_IPC);
593
594         if (bind(listenfd, (struct sockaddr *)&listenaddr, sizeof listenaddr) !=
595                 0) {
596                 TRACE_DEBUG_MSG("failed to call bind");
597                 TerminateDaemon(SIGTERM);
598                 return 0;
599         }
600
601         if (chmod(listenaddr.sun_path, 0777) < 0) {
602                 TRACE_DEBUG_MSG
603                         ("failed to change the permission of socket file");
604                 TerminateDaemon(SIGTERM);
605                 return 0;
606         }
607
608         if (listen(listenfd, MAX_CLIENT) != 0) {
609                 TRACE_DEBUG_MSG("failed to call listen");
610                 TerminateDaemon(SIGTERM);
611                 return 0;
612         }
613
614         maxfd = listenfd;
615         TRACE_DEBUG_INFO_MSG("Ready to listen IPC [%d][%s]", listenfd,
616                         DOWNLOAD_PROVIDER_IPC);
617
618         // allocation the array structure for managing the clients.
619         clientinfo_list =
620                 (download_clientinfo_slot *) calloc(MAX_CLIENT,
621                                                         sizeof(download_clientinfo_slot));
622         if (clientinfo_list == NULL) {
623                 TRACE_DEBUG_MSG("failed to allocate the memory for client list");
624                 TerminateDaemon(SIGTERM);
625                 return 0;
626         }
627
628         if (pthread_attr_init(&g_download_provider_thread_attr) != 0) {
629                 TRACE_DEBUG_MSG("failed to call pthread_attr_init for client");
630                 TerminateDaemon(SIGTERM);
631                 return 0;
632         }
633         if (pthread_attr_setdetachstate(&g_download_provider_thread_attr,
634                                                                         PTHREAD_CREATE_DETACHED) != 0) {
635                 TRACE_DEBUG_MSG("failed to set detach option");
636                 TerminateDaemon(SIGTERM);
637                 return 0;
638         }
639
640         flexible_timeout = DOWNLOAD_PROVIDER_CARE_CLIENT_MIN_INTERVAL;
641
642         FD_ZERO(&g_download_provider_socket_readset);
643         FD_ZERO(&g_download_provider_socket_exceptset);
644         FD_SET(listenfd, &g_download_provider_socket_readset);
645         FD_SET(listenfd, &g_download_provider_socket_exceptset);
646
647         while (g_main_loop_is_running(mainloop)) {
648
649                 // clean slots
650                 for (i=0; i < MAX_CLIENT; i++) {
651                         if (!clientinfo_list[i].clientinfo)
652                                 continue;
653                         // clear slot.
654                         if (clientinfo_list[i].clientinfo->state >= DOWNLOAD_STATE_FINISHED) {
655                                 clear_clientinfoslot(&clientinfo_list[i]);
656                                 continue;
657                         }
658                 }
659
660                 rset = g_download_provider_socket_readset;
661                 exceptset = g_download_provider_socket_exceptset;
662
663                 memset(&timeout, 0x00, sizeof(struct timeval));
664                 timeout.tv_sec = flexible_timeout;
665
666                 if (select((maxfd + 1), &rset, 0, &exceptset, &timeout) < 0) {
667                         TRACE_DEBUG_MSG
668                                 ("select error, provider can't receive any request from client.");
669                         TerminateDaemon(SIGTERM);
670                         break;
671                 }
672
673                 for (i=0; i < MAX_CLIENT; i++) {  // find the socket received the packet.
674                         if (!clientinfo_list[i].clientinfo)
675                                 continue;
676                         // ignore it is not started yet.
677                         if (clientinfo_list[i].clientinfo->state <= DOWNLOAD_STATE_READY)
678                                 continue;
679                         // ignore if finished
680                         if (clientinfo_list[i].clientinfo->state >= DOWNLOAD_STATE_FINISHED)
681                                 continue;
682                         //Even if no socket, downloading should be progressed.
683                         if (clientinfo_list[i].clientinfo->clientfd <= 0)
684                                 continue;
685                         if (FD_ISSET(clientinfo_list[i].clientinfo->clientfd, &rset) > 0) {
686                                 TRACE_DEBUG_INFO_MSG("FD_ISSET [%d] readset slot[%d]",
687                                         clientinfo_list[i].clientinfo->clientfd, i);
688                                 _handle_client_request(clientinfo_list[i].clientinfo);
689                         } else if (FD_ISSET(clientinfo_list[i].clientinfo->clientfd, &exceptset) > 0) {
690                                 TRACE_DEBUG_MSG("FD_ISSET [%d] exceptset slot[%d]", clientinfo_list[i].clientinfo->clientfd, i);
691                                 clear_clientinfoslot(&clientinfo_list[i]);
692                         }
693                 } // MAX_CLIENT
694
695                 if (FD_ISSET(listenfd, &exceptset) > 0) {
696                         TRACE_DEBUG_MSG("meet listenfd Exception of socket");
697                         TerminateDaemon(SIGTERM);
698                         break;
699                 } else if (FD_ISSET(listenfd, &rset) > 0) { // new connection
700                         TRACE_DEBUG_INFO_MSG("FD_ISSET listenfd rset");
701                         // reset timeout.
702                         flexible_timeout =
703                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MIN_INTERVAL;
704                         // ready the buffer.
705                         request_clientinfo =
706                                 (download_clientinfo *) calloc(1,
707                                                         sizeof(download_clientinfo));
708                         if (!request_clientinfo) {
709                                 TRACE_DEBUG_MSG
710                                         ("download-provider can't allocate the memory, try later");
711                                 clientlen = sizeof(clientaddr);
712                                 int clientfd = accept(listenfd,
713                                         (struct sockaddr *)&clientaddr, &clientlen);
714                                 close(clientfd);        // disconnect.
715                                 sleep(5);       // provider need the time of refresh.
716                                 continue;
717                         }
718                         // accept client.
719                         clientlen = sizeof(clientaddr);
720                         request_clientinfo->clientfd = accept(listenfd,
721                                                                 (struct sockaddr*)&clientaddr,
722                                                                 &clientlen);
723                         if (request_clientinfo->clientfd < 0) {
724                                 clear_clientinfo(request_clientinfo);
725                                 sleep(5);       // provider need the time of refresh.
726                                 continue;
727                         }
728                         if (_handle_new_connection(clientinfo_list, request_clientinfo) < 0) {
729                                 sleep(1);
730                                 continue;
731                         }
732                         // after starting the download by DA, event thread will start to get the event from client.
733                         if (request_clientinfo && request_clientinfo->clientfd > 0) {
734                                 FD_SET(request_clientinfo->clientfd, &g_download_provider_socket_readset);      // add new descriptor to set
735                                 FD_SET(request_clientinfo->clientfd, &g_download_provider_socket_exceptset);
736                                 if (request_clientinfo->clientfd > maxfd )
737                                         maxfd = request_clientinfo->clientfd;   /* for select */
738                         }
739                 }
740
741                 if (i >= MAX_CLIENT) { // timeout
742                         // If there is better solution to be able to know
743                         // the number of downloading threads, replace below rough codes.
744                         count_downloading_threads =
745                                 get_downloading_count(clientinfo_list);
746                         // check whether the number of downloading is already maximum.
747                         if (count_downloading_threads >=
748                                 DA_MAX_DOWNLOAD_REQ_AT_ONCE)
749                                 continue;
750
751                         // search pended request
752                         for (searchslot = 0; searchslot < MAX_CLIENT;
753                                 searchslot++) {
754                                 if (clientinfo_list[searchslot].clientinfo) {
755                                         if (clientinfo_list[searchslot].clientinfo->state ==
756                                                 DOWNLOAD_STATE_PENDED) {
757                                                 TRACE_DEBUG_INFO_MSG
758                                                         ("Retry Pended Request [%d/%d] state [%d/%d]",
759                                                         searchslot, MAX_CLIENT,
760                                                         count_downloading_threads,
761                                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE);
762                                                 // create thread for restarting the pended download.
763                                                 if (pthread_create
764                                                         (&clientinfo_list[searchslot].clientinfo->thread_pid,
765                                                         &g_download_provider_thread_attr,
766                                                         _start_download,
767                                                         &clientinfo_list[searchslot]) != 0) {
768                                                         TRACE_DEBUG_MSG
769                                                                 ("failed to call pthread_create for client");
770                                                 }
771                                                 count_downloading_threads++;
772                                                 usleep(1000);   // sleep in busy state.
773                                                 break;
774                                         }
775                                 }
776                         }
777
778                         if (check_retry
779                                 && count_downloading_threads <
780                                 DA_MAX_DOWNLOAD_REQ_AT_ONCE) {
781                                 // Auto re-download feature. ethernet may be connected with other downloading items.
782                                 connection_h network_handle = NULL;
783                                 if (connection_create(&network_handle) < 0) {
784                                         TRACE_DEBUG_MSG
785                                                 ("Failed connection_create");
786                                         continue;
787                                 }
788
789                                 connection_ethernet_state_e system_network_state
790                                         = CONNECTION_ETHERNET_STATE_DISCONNECTED;
791                                 if (connection_get_ethernet_state(network_handle,
792                                                                                         &system_network_state) !=
793                                         CONNECTION_ERROR_NONE)
794                                         TRACE_DEBUG_MSG
795                                                 ("Failed connection_get_ethernet_state");
796                                 TRACE_DEBUG_INFO_MSG
797                                         ("ethernet check result : [%d]", (int)system_network_state);
798
799                                 connection_cellular_state_e system_cellular_state
800                                         = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
801                                 if (connection_get_cellular_state(network_handle,
802                                                                                         &system_cellular_state) !=
803                                         CONNECTION_ERROR_NONE)
804                                         TRACE_DEBUG_MSG
805                                                 ("Failed connection_get_ethernet_state");
806                                 TRACE_DEBUG_INFO_MSG
807                                         ("cellula check result : [%d]", (int)system_cellular_state);
808
809                                 connection_wifi_state_e system_wifi_state
810                                         = CONNECTION_WIFI_STATE_DEACTIVATED;
811                                 if (connection_get_wifi_state(network_handle,
812                                                                                         &system_wifi_state) !=
813                                         CONNECTION_ERROR_NONE)
814                                         TRACE_DEBUG_MSG
815                                                 ("Failed connection_get_ethernet_state");
816                                 TRACE_DEBUG_INFO_MSG
817                                         ("wifi check result : [%d]", (int)system_wifi_state);
818
819                                 if (connection_destroy(network_handle) !=
820                                         CONNECTION_ERROR_NONE)
821                                         TRACE_DEBUG_MSG
822                                                 ("Failed connection_destroy");
823
824                                 if (!(system_network_state
825                                                 == CONNECTION_ETHERNET_STATE_CONNECTED
826                                         || system_cellular_state
827                                                 == CONNECTION_CELLULAR_STATE_AVAILABLE
828                                         || system_wifi_state
829                                                 == CONNECTION_WIFI_STATE_CONNECTED))
830                                         continue;
831
832                                 // check auto-retrying list regardless state. pended state is also included to checking list.
833                                 int i = 0;
834                                 download_dbinfo_list *db_list =
835                                         download_provider_db_get_list(DOWNLOAD_STATE_NONE);
836                                 if (!db_list || db_list->count <= 0) {
837                                         TRACE_DEBUG_INFO_MSG
838                                                 ("provider does not need to check DB anymore. in this life.");
839                                         check_retry = 0;        // provider does not need to check DB anymore. in this life.
840                                         if (db_list)
841                                                 download_provider_db_list_free(db_list);
842                                         continue;
843                                 }
844                                 for (i = 0;
845                                         count_downloading_threads <
846                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE
847                                         && i < db_list->count; i++) {
848                                         if (db_list->item[i].requestid <= 0)
849                                                 continue;
850                                         if (get_same_request_slot_index
851                                                 (clientinfo_list,db_list->item[i].requestid) < 0) {
852                                                 // not found requestid in memory
853                                                 TRACE_DEBUG_INFO_MSG
854                                                         ("Retry download [%d]",
855                                                         db_list->item[i].requestid);
856                                                 //search empty slot. copy db info to slot.
857                                                 searchslot =
858                                                         get_empty_slot_index(clientinfo_list);
859                                                 if (searchslot < 0) {
860                                                         TRACE_DEBUG_INFO_MSG
861                                                                 ("download-provider is busy, try later");
862                                                         flexible_timeout =
863                                                                 flexible_timeout * 2;
864                                                         break;
865                                                 }
866                                                 // allocte requestinfo to empty slot.
867                                                 request_clientinfo =
868                                                         (download_clientinfo *)
869                                                         calloc(1, sizeof(download_clientinfo));
870                                                 if (!request_clientinfo)
871                                                         continue;
872                                                 request_clientinfo->requestinfo
873                                                         =
874                                                         download_provider_db_get_requestinfo
875                                                         (&db_list->item[i]);
876                                                 if (!request_clientinfo->requestinfo) {
877                                                         free(request_clientinfo);
878                                                         request_clientinfo = NULL;
879                                                         continue;
880                                                 }
881
882                                                 CLIENT_MUTEX_INIT(&(request_clientinfo->client_mutex), NULL);
883                                                 request_clientinfo->state = DOWNLOAD_STATE_READY;
884                                                 clientinfo_list[searchslot].clientinfo =
885                                                         request_clientinfo;
886
887                                                 TRACE_DEBUG_INFO_MSG
888                                                         ("Retry download [%d/%d][%d/%d]",
889                                                         searchslot, MAX_CLIENT,
890                                                         count_downloading_threads,
891                                                         DA_MAX_DOWNLOAD_REQ_AT_ONCE);
892                                                 if (pthread_create
893                                                         (&clientinfo_list[searchslot].clientinfo->thread_pid,
894                                                         &g_download_provider_thread_attr,
895                                                         _start_download,
896                                                         &clientinfo_list[searchslot])
897                                                         != 0) {
898                                                         TRACE_DEBUG_MSG
899                                                                 ("failed to call pthread_create for client");
900                                                         clientinfo_list[searchslot].clientinfo->state =
901                                                                 DOWNLOAD_STATE_PENDED;
902                                                         clientinfo_list[searchslot].clientinfo->err =
903                                                                 DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
904                                                         sleep(5);
905                                                 }
906                                                 count_downloading_threads++;
907                                                 usleep(1000);   // sleep in busy state.
908                                         }
909                                 }
910                                 if (i >= db_list->count)        // do not search anymore.
911                                         check_retry = 0;
912                                 download_provider_db_list_free(db_list);
913                         }
914
915                         // save system resource (CPU)
916                         if (check_retry == 0 && count_downloading_threads == 0
917                                 && flexible_timeout <
918                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL)
919                                 flexible_timeout = flexible_timeout * 2;
920                         if (flexible_timeout >
921                                 DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL)
922                                 flexible_timeout =
923                                         DOWNLOAD_PROVIDER_CARE_CLIENT_MAX_INTERVAL;
924                         TRACE_DEBUG_INFO_MSG("Next Timeout after [%ld] sec",
925                                         flexible_timeout);
926
927                 } // if (i >= MAX_CLIENT) { // timeout
928         }
929
930         FD_CLR(listenfd, &rset);
931         FD_CLR(listenfd, &exceptset);
932         FD_CLR(listenfd, &g_download_provider_socket_readset);
933         FD_CLR(listenfd, &g_download_provider_socket_exceptset);
934
935         // close accept socket.
936         if (listenfd)
937                 close(listenfd);
938
939         _deinit_agent();
940
941         // close all sockets for client. .. 
942         // client thread will terminate by itself through catching this closing.
943         for (searchslot = 0; searchslot < MAX_CLIENT; searchslot++)
944                 if (clientinfo_list[searchslot].clientinfo)
945                         clear_clientinfoslot(&clientinfo_list[searchslot]);
946
947         if (clientinfo_list)
948                 free(clientinfo_list);
949
950         pthread_exit(NULL);
951         return 0;
952 }
953
954 void __download_info_cb(user_download_info_t *download_info, void *user_data)
955 {
956         int len = 0;
957         if (!user_data) {
958                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
959                 return;
960         }
961         download_clientinfo_slot *clientinfoslot =
962                 (download_clientinfo_slot *) user_data;
963         download_clientinfo *clientinfo =
964                 (download_clientinfo *) clientinfoslot->clientinfo;
965         if (!clientinfo) {
966                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
967                 return;
968         }
969         TRACE_DEBUG_INFO_MSG("id[%d],size[%lu]",
970                         download_info->da_dl_req_id, download_info->file_size);
971
972         if (clientinfo->req_id != download_info->da_dl_req_id) {
973                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
974                                 clientinfo->req_id,
975                                 download_info->da_dl_req_id);
976                 return;
977         }
978         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
979         if (!clientinfo->downloadinfo)
980                 clientinfo->downloadinfo =
981                         (download_content_info *) calloc(1, sizeof(download_content_info));
982         if (clientinfo->downloadinfo)
983                 clientinfo->downloadinfo->file_size = download_info->file_size;
984         if (download_info->file_type) {
985                 TRACE_DEBUG_INFO_MSG("mime[%s]", download_info->file_type);
986
987                 len = strlen(download_info->file_type);
988                 if (len > (DP_MAX_STR_LEN - 1))
989                         len = DP_MAX_STR_LEN - 1;
990                 if (clientinfo->downloadinfo) {
991                         strncpy(clientinfo->downloadinfo->mime_type,
992                                 download_info->file_type, len);
993                         download_provider_db_requestinfo_update_column
994                                 (clientinfo, DOWNLOAD_DB_MIMETYPE);
995                 }
996         }
997         if (download_info->tmp_saved_path) {
998                 char *str = NULL;
999                 TRACE_DEBUG_INFO_MSG("tmp path[%s]", download_info->tmp_saved_path);
1000                 clientinfo->tmp_saved_path =
1001                         strdup(download_info->tmp_saved_path);
1002                 download_provider_db_requestinfo_update_column(clientinfo,
1003                                                                         DOWNLOAD_DB_SAVEDPATH);
1004                 str = strrchr(download_info->tmp_saved_path, '/');
1005                 if (str) {
1006                         str++;
1007                         len = strlen(str);
1008                         if (len > (DP_MAX_STR_LEN - 1))
1009                                 len = DP_MAX_STR_LEN - 1;
1010                         if (clientinfo->downloadinfo) {
1011                                 strncpy(clientinfo->downloadinfo->content_name,
1012                                         str, len);
1013                                 download_provider_db_requestinfo_update_column
1014                                         (clientinfo, DOWNLOAD_DB_FILENAME);
1015                                 TRACE_DEBUG_INFO_MSG("content_name[%s]",
1016                                                 clientinfo->downloadinfo->
1017                                                 content_name);
1018                         }
1019                 }
1020         }
1021
1022         if (clientinfo->requestinfo->callbackinfo.started)
1023                 ipc_send_downloadinfo(clientinfo);
1024
1025         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1026 }
1027
1028 void __downloading_info_cb(user_downloading_info_t *download_info,
1029                            void *user_data)
1030 {
1031         int len = 0;
1032         if (!user_data) {
1033                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
1034                 return;
1035         }
1036         download_clientinfo_slot *clientinfoslot =
1037                 (download_clientinfo_slot *) user_data;
1038         download_clientinfo *clientinfo =
1039                 (download_clientinfo *) clientinfoslot->clientinfo;
1040         if (!clientinfo) {
1041                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
1042                 return;
1043         }
1044         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
1045         if (clientinfo->req_id != download_info->da_dl_req_id) {
1046                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
1047                                 clientinfo->req_id,
1048                                 download_info->da_dl_req_id);
1049                 CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1050                 return;
1051         }
1052         if (!clientinfo->downloadinginfo)
1053                 clientinfo->downloadinginfo = (downloading_state_info *) calloc(1,
1054                                                                         sizeof(downloading_state_info));
1055         if (clientinfo->downloadinginfo)
1056                 clientinfo->downloadinginfo->received_size =
1057                         download_info->total_received_size;
1058         if (download_info->saved_path) {
1059                 TRACE_DEBUG_INFO_MSG("tmp path[%s]", download_info->saved_path);
1060                 len = strlen(download_info->saved_path);
1061                 if (len > (DP_MAX_PATH_LEN - 1))
1062                         len = DP_MAX_PATH_LEN - 1;
1063                 if (clientinfo->downloadinginfo)
1064                         strncpy(clientinfo->downloadinginfo->saved_path,
1065                                 download_info->saved_path, len);
1066                 /* FIXME : This should be reviewd again after smack rules is applied */
1067                 if (chown(clientinfo->downloadinginfo->saved_path,
1068                                 clientinfo->credentials.uid,
1069                                 clientinfo->credentials.gid) < 0)
1070                         TRACE_DEBUG_INFO_MSG("Fail to chown [%s]", strerror(errno));
1071         }
1072
1073         static size_t updated_second;
1074         time_t tt = time(NULL);
1075         struct tm *localTime = localtime(&tt);
1076
1077         if (updated_second != localTime->tm_sec || download_info->saved_path) { // every 1 second.
1078                 if (clientinfo->requestinfo
1079                         && clientinfo->requestinfo->notification)
1080                         set_downloadinginfo_appfw_notification(clientinfo);
1081                 if (clientinfo->requestinfo->callbackinfo.progress)
1082                         ipc_send_downloadinginfo(clientinfo);
1083                 updated_second = localTime->tm_sec;
1084         }
1085         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1086 }
1087
1088 void __notify_cb(user_notify_info_t *notify_info, void *user_data)
1089 {
1090         if (!user_data) {
1091                 TRACE_DEBUG_MSG("[CRITICAL] clientinfoslot is NULL");
1092                 return;
1093         }
1094         download_clientinfo_slot *clientinfoslot =
1095                 (download_clientinfo_slot *) user_data;
1096         download_clientinfo *clientinfo =
1097                 (download_clientinfo *) clientinfoslot->clientinfo;
1098         if (!clientinfo) {
1099                 TRACE_DEBUG_MSG("[CRITICAL] clientinfo is NULL");
1100                 return;
1101         }
1102
1103         TRACE_DEBUG_INFO_MSG("id[%d],state[%d],err[%d]",
1104                         notify_info->da_dl_req_id,
1105                         notify_info->state, notify_info->err);
1106         if (clientinfo->req_id != notify_info->da_dl_req_id) {
1107                 TRACE_DEBUG_MSG("[CRITICAL] req_id[%d] da_dl_req_id[%d}",
1108                                 clientinfo->req_id, notify_info->da_dl_req_id);
1109                 return;
1110         }
1111
1112         CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
1113
1114         clientinfo->state = __change_state(notify_info->state);
1115         clientinfo->err = __change_error(notify_info->err);
1116         if (clientinfo->state == DOWNLOAD_STATE_FINISHED ||
1117                         clientinfo->state == DOWNLOAD_STATE_FAILED) {
1118                 if (clientinfo->requestinfo) {
1119                         if (clientinfo->requestinfo->notification)
1120                                 set_downloadedinfo_appfw_notification(clientinfo);
1121                         download_provider_db_requestinfo_remove(clientinfo->
1122                                 requestinfo->requestid);
1123                 }
1124                 download_provider_db_history_new(clientinfo);
1125                 TRACE_DEBUG_INFO_MSG("[TEST]Finish clientinfo[%p],fd[%d]",
1126                         clientinfo, clientinfo->clientfd);
1127         }
1128
1129         TRACE_DEBUG_INFO_MSG("state[%d]", clientinfo->state);
1130         ipc_send_stateinfo(clientinfo);
1131
1132         CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
1133 }
1134
1135 int __change_state(da_state state)
1136 {
1137         int ret = DOWNLOAD_STATE_NONE;
1138         switch (state) {
1139         case DA_STATE_WAITING:
1140         case DA_STATE_DOWNLOAD_STARTED:
1141                 TRACE_DEBUG_INFO_MSG("DA_STATE_DOWNLOAD_STARTED");
1142                 ret = DOWNLOAD_STATE_READY;
1143                 break;
1144         case DA_STATE_DOWNLOADING:
1145                 TRACE_DEBUG_INFO_MSG("DA_STATE_DOWNLOADING");
1146                 ret = DOWNLOAD_STATE_DOWNLOADING;
1147                 break;
1148         case DA_STATE_DOWNLOAD_COMPLETE:
1149                 TRACE_DEBUG_INFO_MSG("DA_STATE_COMPLETE");
1150                 ret = DOWNLOAD_STATE_INSTALLING;
1151                 break;
1152         case DA_STATE_CANCELED:
1153                 TRACE_DEBUG_INFO_MSG("DA_STATE_CANCELED");
1154                 ret = DOWNLOAD_STATE_STOPPED;
1155                 break;
1156         case DA_STATE_CANCELED_ALL:
1157                 TRACE_DEBUG_INFO_MSG("DA_STATE_CANCELED_ALL");
1158                 break;
1159         case DA_STATE_SUSPENDED:
1160                 TRACE_DEBUG_INFO_MSG("DA_STATE_SUSPENDED");
1161                 ret = DOWNLOAD_STATE_PAUSED;
1162                 break;
1163         case DA_STATE_SUSPENDED_ALL:
1164                 TRACE_DEBUG_INFO_MSG("DA_STATE_SUSPENDED_ALL");
1165                 break;
1166         case DA_STATE_RESUMED:
1167                 TRACE_DEBUG_INFO_MSG("DA_STATE_RESUMED");
1168                 ret = DOWNLOAD_STATE_DOWNLOADING;
1169                 break;
1170         case DA_STATE_RESUMED_ALL:
1171                 TRACE_DEBUG_INFO_MSG("DA_STATE_RESUMED_ALL");
1172                 break;
1173         case DA_STATE_FINISHED:
1174                 TRACE_DEBUG_INFO_MSG("DA_STATE_FINISHED");
1175                 ret = DOWNLOAD_STATE_FINISHED;
1176                 break;
1177         case DA_STATE_FAILED:
1178                 TRACE_DEBUG_INFO_MSG("DA_STATE_FAILED");
1179                 ret = DOWNLOAD_STATE_FAILED;
1180                 break;
1181         default:
1182                 break;
1183         }
1184         return ret;
1185 }
1186
1187 int __change_error(int err)
1188 {
1189         int ret = DOWNLOAD_ERROR_UNKOWN;
1190         switch (err) {
1191         case DA_RESULT_OK:
1192                 ret = DOWNLOAD_ERROR_NONE;
1193                 break;
1194         case DA_ERR_INVALID_ARGUMENT:
1195                 ret = DOWNLOAD_ERROR_INVALID_PARAMETER;
1196                 break;
1197         case DA_ERR_FAIL_TO_MEMALLOC:
1198                 ret = DOWNLOAD_ERROR_OUT_OF_MEMORY;
1199                 break;
1200         case DA_ERR_UNREACHABLE_SERVER:
1201                 ret = DOWNLOAD_ERROR_NETWORK_UNREACHABLE;
1202                 break;
1203         case DA_ERR_HTTP_TIMEOUT:
1204                 ret = DOWNLOAD_ERROR_CONNECTION_TIMED_OUT;
1205                 break;
1206         case DA_ERR_DISK_FULL:
1207                 ret = DOWNLOAD_ERROR_NO_SPACE;
1208                 break;
1209         case DA_ERR_INVALID_STATE:
1210                 ret = DOWNLOAD_ERROR_INVALID_STATE;
1211                 break;
1212         case DA_ERR_NETWORK_FAIL:
1213                 ret = DOWNLOAD_ERROR_CONNECTION_FAILED;
1214                 break;
1215         case DA_ERR_INVALID_URL:
1216                 ret = DOWNLOAD_ERROR_INVALID_URL;
1217                 break;
1218         case DA_ERR_INVALID_INSTALL_PATH:
1219                 ret = DOWNLOAD_ERROR_INVALID_DESTINATION;
1220                 break;
1221         case DA_ERR_ALREADY_MAX_DOWNLOAD:
1222                 ret = DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS;
1223                 break;
1224         case DA_ERR_FAIL_TO_INSTALL_FILE:
1225                 ret = DOWNLOAD_ERROR_INSTALL_FAIL;
1226                 break;
1227         case DA_ERR_FAIL_TO_CREATE_THREAD:
1228         case DA_ERR_FAIL_TO_OBTAIN_MUTEX:
1229         case DA_ERR_FAIL_TO_ACCESS_FILE:
1230         case DA_ERR_FAIL_TO_GET_CONF_VALUE:
1231         case DA_ERR_FAIL_TO_ACCESS_STORAGE:
1232         case DA_ERR_DLOPEN_FAIL:
1233                 ret = DOWNLOAD_ERROR_IO_ERROR;
1234                 break;
1235         }
1236         return ret;
1237 }
1238
1239 int _init_agent()
1240 {
1241         int da_ret = 0;
1242         da_client_cb_t da_cb = {
1243                 __notify_cb,
1244                 __download_info_cb,
1245                 __downloading_info_cb
1246         };
1247         da_ret = da_init(&da_cb, DA_DOWNLOAD_MANAGING_METHOD_AUTO);
1248         if (da_ret != DA_RESULT_OK) {
1249                 return DOWNLOAD_ERROR_FAIL_INIT_AGENT;
1250         }
1251         return DOWNLOAD_ERROR_NONE;
1252 }
1253
1254 void _deinit_agent()
1255 {
1256         da_deinit();
1257 }