Append queue checker for cancel request
[platform/core/multimedia/libmedia-thumbnail.git] / src / ipc / media-thumb-ipc.c
1 /*
2  * libmedia-thumbnail
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include "media-thumbnail.h"
23 #include "media-thumb-ipc.h"
24 #include "media-thumb-util.h"
25 #include "media-thumb-db.h"
26 #include "media-thumb-debug.h"
27 #include <glib.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <grp.h>
32 #include <pwd.h>
33
34 #define THUMB_SOCK_BLOCK_SIZE 512
35 #define THUMB_IPC_PATH "/var/run/media-server/media_ipc_thumbcreator.socket"
36
37 static GQueue *g_request_queue = NULL;
38 static GQueue *g_manage_queue = NULL;
39 static GQueue *g_request_raw_queue = NULL;
40 static GQueue *g_manage_raw_queue = NULL;
41
42
43 typedef struct {
44         GIOChannel *channel;
45         int msg_type;
46         bool isCanceled;
47         int source_id;
48         uid_t uid;
49         char *path;
50         thumbUserData *userData;
51 } thumbReq;
52
53 typedef struct {
54         GIOChannel *channel;
55         int msg_type;
56         bool isCanceled;
57         int request_id;
58         int source_id;
59         int width;
60         int height;
61         uid_t uid;
62         char *path;
63         thumbRawUserData *userData;
64 } thumbRawReq;
65
66 int _media_thumb_send_request();
67 int _media_thumb_raw_data_send_request();
68
69
70 int _media_thumb_get_error()
71 {
72         if (errno == EWOULDBLOCK) {
73                 thumb_err("Timeout. Can't try any more");
74                 return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
75         } else {
76                 thumb_stderror("recvfrom failed");
77                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
78         }
79 }
80
81 void __media_thumb_shutdown_channel(bool only_shutdown)
82 {
83         int len = -1;
84         thumbReq *req = (thumbReq *)g_queue_peek_head(g_request_queue);
85
86         len = g_queue_get_length(g_manage_queue);
87
88         if (req != NULL) {
89                 if (!only_shutdown) {
90                         if (len == 0 && req->isCanceled == true) {
91                                 thumb_dbg("The manage queue will be released");
92                         } else {
93                                 thumb_dbg("There is remain item in the manage queue");
94                                 return;
95                         }
96                 }
97                 GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
98                 if (source_id != NULL) {
99                         g_source_destroy(source_id);
100                 } else {
101                         thumb_err("G_SOURCE_ID is NULL");
102                 }
103
104                 g_io_channel_shutdown(req->channel, TRUE, NULL);
105                 g_io_channel_unref(req->channel);
106                 g_queue_pop_head(g_request_queue);
107
108                 SAFE_FREE(req->path);
109                 SAFE_FREE(req->userData);
110                 SAFE_FREE(req);
111
112                 if(g_manage_queue && len == 0) {
113                         g_queue_free(g_manage_queue);
114                         g_manage_queue = NULL;
115                 }
116         }
117 }
118
119 int __media_thumb_pop_req_queue(const char *path)
120 {
121         int req_len = 0;
122
123         req_len = g_queue_get_length(g_request_queue);
124
125         if (req_len <= 0) {
126 //              thumb_dbg("There is no request in the queue");
127         } else {
128                 __media_thumb_shutdown_channel(true);
129         }
130
131         /* Check manage queue */
132         if(g_manage_queue) {
133                 req_len = g_queue_get_length(g_manage_queue);
134
135                 if(req_len > 0)
136                         _media_thumb_send_request();
137         }
138
139         return MS_MEDIA_ERR_NONE;
140 }
141
142 int __media_thumb_check_req_queue_for_cancel(const char *path)
143 {
144         int req_len = 0;
145
146         req_len = g_queue_get_length(g_request_queue);
147
148         if (req_len <= 0) {
149 //              thumb_dbg("There is no request in the queue");
150         } else {
151                 thumbReq *req = NULL;
152                 req = (thumbReq *)g_queue_peek_head(g_request_queue);
153
154                 if (req != NULL && strncmp(path, req->path, strlen(path)) == 0) {
155                         req->isCanceled = true;
156                         __media_thumb_shutdown_channel(false);
157
158                         return MS_MEDIA_ERR_NONE;
159                 }
160         }
161
162         return MS_MEDIA_ERR_INTERNAL;
163 }
164
165 int __media_thumb_pop_manage_queue(const char *path)
166 {
167         int req_len = 0, i;
168         bool flag = false;
169
170         req_len = g_queue_get_length(g_manage_queue);
171
172         if (req_len < 0) {
173 //              thumb_dbg("There is no request in the queue");
174         } else {
175                 for (i = 0; i < req_len; i++) {
176                         thumbReq *req = NULL;
177                         req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
178                         if (req == NULL) continue;
179
180                         if (strncmp(path, req->path, strlen(path)) == 0) {
181                                 g_queue_pop_nth(g_manage_queue, i);
182
183                                 SAFE_FREE(req->path);
184                                 SAFE_FREE(req->userData);
185                                 SAFE_FREE(req);
186                                 flag = true;
187                                 break;
188                         }
189                 }
190                 if (!flag) {
191                         return __media_thumb_check_req_queue_for_cancel(path);
192                 } else {
193                         __media_thumb_shutdown_channel(false);
194
195                         return MS_MEDIA_ERR_NONE;
196                 }
197         }
198
199         return MS_MEDIA_ERR_NONE;
200 }
201
202
203 int __media_thumb_pop_raw_data_req_queue(int request_id, bool shutdown_channel)
204 {
205         int req_len = 0, i;
206
207         req_len = g_queue_get_length(g_request_raw_queue);
208
209         if (req_len <= 0) {
210 //              thumb_dbg("There is no request in the queue");
211         } else {
212
213                 for (i = 0; i < req_len; i++) {
214                         thumbRawReq *req = NULL;
215                         req = (thumbRawReq *)g_queue_peek_nth(g_request_raw_queue, i);
216                         if (req == NULL) continue;
217
218                         if (request_id == req->request_id) {
219                                 if (shutdown_channel) {
220                                         GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
221                                         if (source_id != NULL) {
222                                                 g_source_destroy(source_id);
223                                         } else {
224                                                 thumb_err("G_SOURCE_ID is NULL");
225                                         }
226
227                                         g_io_channel_shutdown(req->channel, TRUE, NULL);
228                                         g_io_channel_unref(req->channel);
229                                 }
230                                 g_queue_pop_nth(g_request_raw_queue, i);
231
232                                 SAFE_FREE(req->userData);
233                                 SAFE_FREE(req);
234
235                                 break;
236                         }
237                 }
238                 if (i == req_len) {
239 //                      thumb_dbg("There's no %s in the queue", path);
240                 }
241         }
242
243         return MS_MEDIA_ERR_NONE;
244 }
245
246 int __media_thumb_check_raw_data_req_queue_for_cancel(int request_id)
247 {
248         int req_len = 0;
249
250         req_len = g_queue_get_length(g_request_raw_queue);
251
252         if (req_len <= 0) {
253 //              thumb_dbg("There is no request in the queue");
254         } else {
255                 thumbRawReq *req = NULL;
256                 req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
257
258                 if (req != NULL && request_id == req->request_id) {
259                         req->isCanceled = true;
260                         return MS_MEDIA_ERR_NONE;
261                 }
262         }
263
264         return MS_MEDIA_ERR_INTERNAL;
265 }
266
267 int __media_thumb_pop_raw_data_manage_queue(int request_id)
268 {
269         int req_len = 0, i;
270         int flag = false;
271
272         req_len = g_queue_get_length(g_manage_raw_queue);
273
274         if (req_len < 0) {
275 //              thumb_dbg("There is no request in the queue");
276         } else {
277
278                 for (i = 0; i < req_len; i++) {
279                         thumbRawReq *req = NULL;
280                         req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
281                         if (req == NULL) continue;
282
283                         if (request_id == req->request_id) {
284                                 g_queue_pop_nth(g_manage_raw_queue, i);
285
286                                 SAFE_FREE(req->userData);
287                                 SAFE_FREE(req);
288                                 flag = true;
289                                 break;
290                         }
291                 }
292                 if (!flag) {
293                         return __media_thumb_check_raw_data_req_queue_for_cancel(request_id);
294                 }
295         }
296
297         return MS_MEDIA_ERR_NONE;
298 }
299
300 int __media_thumb_check_req_queue(const char *path)
301 {
302         int req_len = 0, i;
303
304         req_len = g_queue_get_length(g_request_queue);
305
306 //      thumb_dbg("Queue length : %d", req_len);
307 //      thumb_dbg("Queue path : %s", path);
308
309         if (req_len <= 0) {
310 //              thumb_dbg("There is no request in the queue");
311         } else {
312
313                 for (i = 0; i < req_len; i++) {
314                         thumbReq *req = NULL;
315                         req = (thumbReq *)g_queue_peek_nth(g_request_queue, i);
316                         if (req == NULL) continue;
317
318                         if (strncmp(path, req->path, strlen(path)) == 0) {
319                                 //thumb_dbg("Same Request - %s", path);
320                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
321
322                                 break;
323                         }
324                 }
325         }
326
327         return MS_MEDIA_ERR_NONE;
328 }
329
330 bool __media_thumb_check_cancel(void)
331 {
332         thumbReq *req = NULL;
333         req = (thumbReq *)g_queue_peek_head(g_request_queue);
334
335         if (req == NULL) {
336                 return false;
337         } else {
338                 if (req->isCanceled)
339                         return false;
340                 else
341                         return true;
342         }
343 }
344
345 bool __media_thumb_check_cancel_for_raw(void)
346 {
347         thumbRawReq *req = NULL;
348         req = (thumbRawReq *)g_queue_peek_head(g_request_raw_queue);
349
350         if (req == NULL) {
351                 return false;
352         } else {
353                 if (req->isCanceled)
354                         return false;
355                 else
356                         return true;
357         }
358 }
359
360 int
361 _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
362 {
363         int recv_msg_len = 0;
364         int remain_size = 0;
365         int block_size = THUMB_SOCK_BLOCK_SIZE;
366         int recv_block = 0;
367         unsigned char *buf = NULL;
368         unsigned char *block_buf = NULL;
369
370         THUMB_MALLOC(buf, header_size);
371         THUMB_MALLOC(block_buf, THUMB_SOCK_BLOCK_SIZE);
372         if (buf == NULL || block_buf == NULL) {
373                 thumb_err("memory allocation failed");
374                 SAFE_FREE(buf);
375                 SAFE_FREE(block_buf);
376                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
377         }
378
379         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
380                 thumb_stderror("recv failed");
381                 SAFE_FREE(buf);
382                 SAFE_FREE(block_buf);
383                 return _media_thumb_get_error();
384         }
385
386         memcpy(msg, buf, header_size);
387         //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
388
389         SAFE_FREE(buf);
390         if (msg->origin_path_size < 0 || msg->dest_path_size < 0 || msg->thumb_size < 0) {
391                 thumb_err("recv data is wrong");
392                 SAFE_FREE(block_buf);
393                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
394         }
395
396         remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size;
397         THUMB_MALLOC(buf, remain_size);
398         if (buf == NULL) {
399                 SAFE_FREE(block_buf);
400                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
401         }
402
403         while (remain_size > 0) {
404                 if (remain_size < THUMB_SOCK_BLOCK_SIZE) {
405                         block_size = remain_size;
406                 }
407                 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
408                         thumb_stderror("recv failed");
409                         SAFE_FREE(buf);
410                         SAFE_FREE(block_buf);
411                         return _media_thumb_get_error();
412                 }
413                 memcpy(buf+recv_block, block_buf, block_size);
414                 recv_block += block_size;
415                 remain_size -= block_size;
416         }
417
418         strncpy(msg->org_path, (char *)buf, msg->origin_path_size);
419         strncpy(msg->dst_path, (char *)buf + msg->origin_path_size, msg->dest_path_size);
420
421         SAFE_FREE(msg->thumb_data);
422         if (msg->thumb_size > 0) {
423                 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
424                 if (msg->thumb_data != NULL) {
425                         memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size, msg->thumb_size);
426                 } else {
427                         SAFE_FREE(buf);
428                         SAFE_FREE(block_buf);
429
430                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
431                 }
432         }
433
434         SAFE_FREE(buf);
435         SAFE_FREE(block_buf);
436
437         return MS_MEDIA_ERR_NONE;
438 }
439
440 int
441 _media_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size)
442 {
443         int recv_msg_len = 0;
444         unsigned int from_addr_size = sizeof(struct sockaddr_un);
445         unsigned char *buf = NULL;
446
447         THUMB_MALLOC(buf, sizeof(thumbMsg));
448         if (buf == NULL) {
449                 thumb_err("memory allocation failed");
450                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
451         }
452
453         if ((recv_msg_len = recvfrom(sock, buf, sizeof(thumbMsg), 0, (struct sockaddr *)from_addr, &from_addr_size)) < 0) {
454                 thumb_stderror("recvform failed");
455                 SAFE_FREE(buf);
456                 return _media_thumb_get_error();
457         }
458
459         memcpy(msg, buf, header_size);
460
461         if (msg->origin_path_size <= 0 || msg->origin_path_size > MAX_PATH_SIZE) {
462                 SAFE_FREE(buf);
463                 thumb_err("msg->origin_path_size is invalid %d", msg->origin_path_size);
464                 return MS_MEDIA_ERR_INVALID_PARAMETER;
465         }
466
467         strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
468
469         if (msg->dest_path_size <= 0 || msg->dest_path_size > MAX_PATH_SIZE) {
470                 SAFE_FREE(buf);
471                 thumb_err("msg->origin_path_size is invalid %d", msg->dest_path_size);
472                 return MS_MEDIA_ERR_INVALID_PARAMETER;
473         }
474
475         strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
476
477         SAFE_FREE(buf);
478         *from_size = from_addr_size;
479
480         return MS_MEDIA_ERR_NONE;
481 }
482
483 int
484 _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
485 {
486         if (req_msg == NULL || buf == NULL) {
487                 return MS_MEDIA_ERR_INVALID_PARAMETER;
488         }
489
490         int org_path_len = 0;
491         int dst_path_len = 0;
492         int thumb_data_len = 0;
493         int size = 0;
494         int header_size = 0;
495
496         header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
497         org_path_len = req_msg->origin_path_size;
498         dst_path_len = req_msg->dest_path_size;
499         thumb_data_len = req_msg->thumb_size;
500
501         //thumb_dbg("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d], thumb_data_len : %d", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len, thumb_data_len);
502
503         size = header_size + org_path_len + dst_path_len + thumb_data_len;
504         THUMB_MALLOC(*buf, size);
505         if (*buf == NULL) {
506                 *buf_size = 0;
507                 return 0;
508         }
509         memcpy(*buf, req_msg, header_size);
510         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
511         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
512         if (thumb_data_len > 0)
513                 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
514
515         *buf_size = size;
516
517         return MS_MEDIA_ERR_NONE;
518 }
519
520 int
521 _media_thumb_request(int msg_type, const char *origin_path, char *thumb_path, int max_length, media_thumb_info *thumb_info, uid_t uid)
522 {
523         int sock = -1;
524         struct sockaddr_un serv_addr;
525         ms_sock_info_s sock_info;
526         int recv_str_len = 0;
527         int err = MS_MEDIA_ERR_NONE;
528         int pid;
529         sock_info.port = MS_THUMB_CREATOR_PORT;
530
531         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
532         if (err != MS_MEDIA_ERR_NONE) {
533                 thumb_err("ms_ipc_create_client_socket failed");
534                 return err;
535         }
536
537         memset(&serv_addr, 0, sizeof(serv_addr));
538         sock = sock_info.sock_fd;
539         serv_addr.sun_family = AF_UNIX;
540         strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
541
542         /* Connecting to the thumbnail server */
543         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
544                 thumb_stderror("connect");
545                 ms_ipc_delete_client_socket(&sock_info);
546                 return MS_MEDIA_ERR_SOCKET_CONN;
547         }
548
549         thumbMsg req_msg;
550         thumbMsg recv_msg;
551
552         memset((void *)&req_msg, 0, sizeof(thumbMsg));
553         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
554
555         /* Get PID of client*/
556         pid = getpid();
557         req_msg.pid = pid;
558
559         /* Set requset message */
560         req_msg.msg_type = msg_type;
561         req_msg.uid = uid;
562         strncpy(req_msg.org_path, origin_path, sizeof(req_msg.org_path));
563         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
564
565         if (msg_type == THUMB_REQUEST_SAVE_FILE) {
566                 strncpy(req_msg.dst_path, thumb_path, sizeof(req_msg.dst_path));
567                 req_msg.dst_path[strlen(req_msg.dst_path)] = '\0';
568         }
569
570         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
571         req_msg.dest_path_size = strlen(req_msg.dst_path) + 1;
572         req_msg.thumb_size = 0;
573
574         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
575                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
576                 ms_ipc_delete_client_socket(&sock_info);
577                 return MS_MEDIA_ERR_INVALID_PARAMETER;
578         }
579
580         unsigned char *buf = NULL;
581         int buf_size = 0;
582         int header_size = 0;
583
584         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
585         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
586
587         if (send(sock, buf, buf_size, 0) != buf_size) {
588                 thumb_err("sendto failed: %d", errno);
589                 SAFE_FREE(buf);
590                 ms_ipc_delete_client_socket(&sock_info);
591                 return MS_MEDIA_ERR_SOCKET_SEND;
592         }
593
594         thumb_dbg("Sending msg to thumbnail daemon is successful");
595
596         SAFE_FREE(buf);
597
598         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
599                 thumb_err("_media_thumb_recv_msg failed ");
600                 ms_ipc_delete_client_socket(&sock_info);
601                 return err;
602         }
603
604         recv_str_len = strlen(recv_msg.org_path);
605         thumb_dbg_slog("recv %s(%d) from thumb daemon is successful", recv_msg.org_path, recv_str_len);
606
607         ms_ipc_delete_client_socket(&sock_info);
608
609         if (recv_str_len > max_length) {
610                 thumb_err("user buffer is too small. Output's length is %d", recv_str_len);
611                 SAFE_FREE(recv_msg.thumb_data);
612                 return MS_MEDIA_ERR_INVALID_PARAMETER;
613         }
614
615         if (recv_msg.status == THUMB_FAIL) {
616                 thumb_err("Failed to make thumbnail");
617                 SAFE_FREE(recv_msg.thumb_data);
618                 return MS_MEDIA_ERR_INVALID_PARAMETER;
619         }
620
621         if (msg_type != THUMB_REQUEST_SAVE_FILE) {
622                 strncpy(thumb_path, recv_msg.dst_path, max_length);
623         }
624
625         thumb_info->origin_width = recv_msg.origin_width;
626         thumb_info->origin_height = recv_msg.origin_height;
627
628         SAFE_FREE(recv_msg.thumb_data);
629
630         return MS_MEDIA_ERR_NONE;
631 }
632
633 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
634 {
635         thumbMsg recv_msg;
636         int header_size = 0;
637         int sock = 0;
638         int err = MS_MEDIA_ERR_NONE;
639
640         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
641         sock = g_io_channel_unix_get_fd(src);
642
643         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
644
645         thumb_err("_media_thumb_write_socket socket : %d", sock);
646
647         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
648                 thumb_err("_media_thumb_recv_msg failed ");
649                 if (recv_msg.origin_path_size > 0) {
650                         __media_thumb_pop_req_queue(recv_msg.org_path);
651                 } else {
652                         thumb_err("origin path size is wrong.");
653                 }
654
655                 return FALSE;
656         }
657
658         //thumb_dbg("Completed..%s", recv_msg.org_path);
659
660         if (recv_msg.status == THUMB_FAIL) {
661                 thumb_err("Failed to make thumbnail");
662                 err = MS_MEDIA_ERR_INTERNAL;
663         }
664
665         if (__media_thumb_check_cancel()) {
666                 if (data) {
667                         thumbUserData* cb = (thumbUserData*)data;
668                         if (cb->func != NULL)
669                                 cb->func(err, recv_msg.dst_path, cb->user_data);
670                 }
671         }
672
673         __media_thumb_pop_req_queue(recv_msg.org_path);
674
675         thumb_dbg("Done");
676
677         return FALSE;
678 }
679
680 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
681 {
682         thumbMsg recv_msg;
683         int header_size = 0;
684         int sock = 0;
685         int len = -1;
686         int err = MS_MEDIA_ERR_NONE;
687
688         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
689         sock = g_io_channel_unix_get_fd(src);
690
691         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
692
693         thumb_err("_media_thumb_write_socket socket : %d", sock);
694
695         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
696                 thumb_err("_media_thumb_recv_msg failed ");
697                 if (recv_msg.request_id > 0) {
698                         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, TRUE);
699                 } else {
700                         thumb_err("origin path size is wrong.");
701                 }
702
703                 return FALSE;
704         }
705
706         g_io_channel_shutdown(src, TRUE, NULL);
707         g_io_channel_unref(src);
708
709         if (recv_msg.status == THUMB_FAIL) {
710                 thumb_err("Failed to make thumbnail");
711                 err = MS_MEDIA_ERR_INTERNAL;
712         }
713
714         if (__media_thumb_check_cancel_for_raw()) {
715                 if (data) {
716                         thumbRawUserData* cb = (thumbRawUserData*)data;
717                         if (cb->func != NULL)
718                                 cb->func(err, recv_msg.request_id, recv_msg.org_path, recv_msg.thumb_width, recv_msg.thumb_height, recv_msg.thumb_data, recv_msg.thumb_size, cb->user_data);
719                 }
720         }
721
722         __media_thumb_pop_raw_data_req_queue(recv_msg.request_id, FALSE);
723
724         thumb_dbg("Done");
725
726         SAFE_FREE(recv_msg.thumb_data);
727
728         /* Check manage queue */
729         if (g_manage_raw_queue) {
730                 len = g_queue_get_length(g_manage_raw_queue);
731
732                 if (len > 0) {
733                         _media_thumb_raw_data_send_request();
734                 } else {
735                         g_queue_free(g_manage_raw_queue);
736                         g_manage_raw_queue = NULL;
737                 }
738         }
739
740         return FALSE;
741 }
742
743 int _media_thumb_send_request()
744 {
745         int err = MS_MEDIA_ERR_NONE;
746         int sock = -1;
747         struct sockaddr_un serv_addr;
748         ms_sock_info_s sock_info;
749         thumbReq *req_manager = NULL;
750         int pid;
751         sock_info.port = MS_THUMB_CREATOR_PORT;
752
753         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
754         if (err != MS_MEDIA_ERR_NONE) {
755                 thumb_err("ms_ipc_create_client_socket failed");
756                 return err;
757         }
758
759         memset(&serv_addr, 0, sizeof(serv_addr));
760         sock = sock_info.sock_fd;
761         serv_addr.sun_family = AF_UNIX;
762         strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
763
764         GIOChannel *channel = NULL;
765         channel = g_io_channel_unix_new(sock);
766         int source_id = -1;
767
768         /* Connecting to the thumbnail server */
769         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
770                 thumb_stderror("connect");
771                 g_io_channel_shutdown(channel, TRUE, NULL);
772                 g_io_channel_unref(channel);
773                 return MS_MEDIA_ERR_SOCKET_CONN;
774         }
775         req_manager = (thumbReq *)g_queue_pop_head(g_manage_queue);
776         GSource *source = NULL;
777         source = g_io_create_watch(channel, G_IO_IN);
778         g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
779         source_id = g_source_attach(source, g_main_context_get_thread_default());
780
781         thumbMsg req_msg;
782
783         memset((void *)&req_msg, 0, sizeof(thumbMsg));
784
785         pid = getpid();
786         req_msg.pid = pid;
787         req_msg.msg_type = req_manager->msg_type;
788         req_msg.request_id = 0;
789         req_msg.uid = req_manager->uid;
790         strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
791         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
792         req_msg.dst_path[0] = '\0';
793         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
794         req_msg.dest_path_size = 1;
795         req_msg.thumb_size = 0;
796
797         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
798                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
799                 g_io_channel_shutdown(channel, TRUE, NULL);
800                 g_io_channel_unref(channel);
801                 ms_ipc_delete_client_socket(&sock_info);
802                 return MS_MEDIA_ERR_INVALID_PARAMETER;
803         }
804
805         unsigned char *buf = NULL;
806         int buf_size = 0;
807         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
808
809         if (send(sock, buf, buf_size, 0) != buf_size) {
810                 thumb_err("sendto failed: %d", errno);
811                 SAFE_FREE(buf);
812                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
813                 g_io_channel_shutdown(channel, TRUE, NULL);
814                 g_io_channel_unref(channel);
815                 ms_ipc_delete_client_socket(&sock_info);
816                 return MS_MEDIA_ERR_SOCKET_SEND;
817         }
818
819         SAFE_FREE(buf);
820         thumb_dbg("Sending msg to thumbnail daemon is successful");
821
822
823         if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
824                 if (g_request_queue == NULL) {
825                         g_request_queue = g_queue_new();
826                 }
827
828                 thumbReq *thumb_req = NULL;
829                 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
830                 if (thumb_req == NULL) {
831                         thumb_err("Failed to create request element");
832                         return MS_MEDIA_ERR_INVALID_PARAMETER;
833                 }
834
835                 thumb_req->channel = channel;
836                 thumb_req->path = strdup(req_manager->path);
837                 thumb_req->source_id = source_id;
838                 thumb_req->userData = req_manager->userData;
839
840                 g_queue_push_tail(g_request_queue, (gpointer)thumb_req);
841         }
842
843         return err;
844 }
845
846 int _media_thumb_raw_data_send_request()
847 {
848         int err = MS_MEDIA_ERR_NONE;
849         int sock = -1;
850         struct sockaddr_un serv_addr;
851         ms_sock_info_s sock_info;
852         thumbRawReq *req_manager = NULL;
853         int pid;
854         sock_info.port = MS_THUMB_CREATOR_PORT;
855
856         err = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
857         if (err != MS_MEDIA_ERR_NONE) {
858                 thumb_err("ms_ipc_create_client_socket failed");
859                 return err;
860         }
861
862         memset(&serv_addr, 0, sizeof(serv_addr));
863         sock = sock_info.sock_fd;
864         serv_addr.sun_family = AF_UNIX;
865         strncpy(serv_addr.sun_path, THUMB_IPC_PATH, strlen(THUMB_IPC_PATH));
866
867         GIOChannel *channel = NULL;
868         channel = g_io_channel_unix_new(sock);
869         int source_id = -1;
870
871         /* Connecting to the thumbnail server */
872         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
873                 thumb_stderror("connect error");
874                 g_io_channel_shutdown(channel, TRUE, NULL);
875                 g_io_channel_unref(channel);
876                 return MS_MEDIA_ERR_SOCKET_CONN;
877         }
878
879         req_manager = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
880         GSource *source = NULL;
881         source = g_io_create_watch(channel, G_IO_IN);
882         g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
883         source_id = g_source_attach(source, g_main_context_get_thread_default());
884
885         thumbMsg req_msg;
886         memset((void *)&req_msg, 0, sizeof(thumbMsg));
887
888         pid = getpid();
889         req_msg.pid = pid;
890         req_msg.msg_type = req_manager->msg_type;
891         req_msg.request_id = req_manager->request_id;
892         req_msg.thumb_width = req_manager->width;
893         req_msg.thumb_height = req_manager->height;
894         req_msg.uid = req_manager->uid;
895
896         strncpy(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
897         req_msg.org_path[strlen(req_msg.org_path)] = '\0';
898         req_msg.dst_path[0] = '\0';
899
900         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
901         req_msg.dest_path_size = 1;
902         req_msg.thumb_size = 0;
903
904         if (req_msg.origin_path_size > MAX_PATH_SIZE) {
905                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
906                 g_io_channel_shutdown(channel, TRUE, NULL);
907                 g_io_channel_unref(channel);
908                 return MS_MEDIA_ERR_INVALID_PARAMETER;
909         }
910
911         unsigned char *buf = NULL;
912         int buf_size = 0;
913         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
914
915         if (send(sock, buf, buf_size, 0) != buf_size) {
916                 thumb_err("sendto failed: %d", errno);
917                 SAFE_FREE(buf);
918                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
919                 g_io_channel_shutdown(channel, TRUE, NULL);
920                 g_io_channel_unref(channel);
921                 return MS_MEDIA_ERR_SOCKET_SEND;
922         }
923
924         SAFE_FREE(buf);
925
926         if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
927                 if (g_request_raw_queue == NULL) {
928                         g_request_raw_queue = g_queue_new();
929                 }
930                 thumbRawReq *thumb_req = NULL;
931                 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
932                 if (thumb_req == NULL) {
933                         thumb_err("Failed to create request element");
934                         return MS_MEDIA_ERR_INVALID_PARAMETER;
935                 }
936                 thumb_req->channel = channel;
937                 thumb_req->request_id = req_manager->request_id;
938                 thumb_req->source_id = source_id;
939                 thumb_req->userData = req_manager->userData;
940
941                 g_queue_push_tail(g_request_raw_queue, (gpointer)thumb_req);
942         }
943         return MS_MEDIA_ERR_NONE;
944 }
945
946 int _media_thumb_request_async(int msg_type, const char *origin_path, thumbUserData *userData, uid_t uid)
947 {
948         int err = MS_MEDIA_ERR_NONE;
949
950         if (g_manage_queue == NULL) {
951                 if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
952                         return MS_MEDIA_ERR_INTERNAL;
953
954                 g_manage_queue = g_queue_new();
955
956                 thumbReq *thumb_req = NULL;
957                 THUMB_MALLOC(thumb_req, sizeof(thumbReq));
958                 if (thumb_req == NULL) {
959                         thumb_err("Failed to create request element");
960                         return MS_MEDIA_ERR_INVALID_PARAMETER;
961                 }
962
963                 thumb_req->msg_type = msg_type;
964                 thumb_req->path = strdup(origin_path);
965                 thumb_req->userData = userData;
966                 thumb_req->isCanceled = false;
967                 thumb_req->uid = uid;
968
969                 g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
970
971                 /* directly request at first time */
972                 err = _media_thumb_send_request();
973
974         } else {
975                 if (msg_type != THUMB_REQUEST_CANCEL_MEDIA) {
976                         /* Enqueue */
977                         if ((msg_type == THUMB_REQUEST_DB_INSERT) && (__media_thumb_check_req_queue(origin_path) < 0)) {
978                                 thumb_err("duplicated request");
979                                 return MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST;
980                         }
981
982                         thumbReq *thumb_req = NULL;
983                         THUMB_MALLOC(thumb_req, sizeof(thumbReq));
984                         if (thumb_req == NULL) {
985                                 thumb_err("Failed to create request element");
986                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
987                         }
988
989                         thumb_req->msg_type = msg_type;
990                         thumb_req->path = strdup(origin_path);
991                         thumb_req->userData = userData;
992                         thumb_req->isCanceled = false;
993                         thumb_req->uid = uid;
994
995                         g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
996                 } else {
997                         /* Dequeue */
998                         err = __media_thumb_pop_manage_queue(origin_path);
999                 }
1000         }
1001
1002         return err;
1003 }
1004
1005 int _media_thumb_request_raw_data_async(int msg_type, int request_id, const char *origin_path, int width, int height, thumbRawUserData *userData, uid_t uid)
1006 {
1007         int err = MS_MEDIA_ERR_NONE;
1008
1009         if (g_manage_raw_queue == NULL) {
1010                 if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
1011                         return MS_MEDIA_ERR_INTERNAL;
1012
1013                 g_manage_raw_queue = g_queue_new();
1014
1015                 thumbRawReq *thumb_req = NULL;
1016                 THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1017                 if (thumb_req == NULL) {
1018                         thumb_err("Failed to create request element");
1019                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1020                 }
1021
1022                 thumb_req->msg_type = msg_type;
1023                 thumb_req->request_id = request_id;
1024                 thumb_req->path = strdup(origin_path);
1025                 thumb_req->width = width;
1026                 thumb_req->height = height;
1027                 thumb_req->userData = userData;
1028                 thumb_req->isCanceled = false;
1029                 thumb_req->uid = uid;
1030
1031                 g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1032
1033                 /* directly request at first time */
1034                 err = _media_thumb_raw_data_send_request();
1035
1036         } else {
1037                 if (msg_type != THUMB_REQUEST_CANCEL_RAW_DATA) {
1038                         /* Enqueue */
1039                         thumbRawReq *thumb_req = NULL;
1040                         THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
1041                         if (thumb_req == NULL) {
1042                                 thumb_err("Failed to create request element");
1043                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1044                         }
1045
1046                         thumb_req->msg_type = msg_type;
1047                         thumb_req->request_id = request_id;
1048                         thumb_req->path = strdup(origin_path);
1049                         thumb_req->width = width;
1050                         thumb_req->height = height;
1051                         thumb_req->userData = userData;
1052                         thumb_req->isCanceled = false;
1053                         thumb_req->uid = uid;
1054
1055                         g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
1056
1057                 } else {
1058                         /* Dequeue */
1059                         err = __media_thumb_pop_raw_data_manage_queue(request_id);
1060                 }
1061         }
1062
1063         return err;
1064 }
1065
1066 int _media_thumb_request_cancel_all(bool isRaw)
1067 {
1068         int i;
1069         int req_len = -1;
1070         int len = 0;
1071
1072         if (isRaw) {
1073                 thumbRawReq *tmp_raw_req = NULL;
1074                 if (g_manage_raw_queue == NULL) {
1075                         thumb_err("manage_queue is NULL");
1076                         if (g_request_raw_queue != NULL) {
1077                                 /* Check request queue */
1078                                 len = g_queue_get_length(g_request_raw_queue);
1079                                 if (len > 0) {
1080                                         tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1081                                         if (tmp_raw_req != NULL)
1082                                                 tmp_raw_req->isCanceled = true;
1083                                         return MS_MEDIA_ERR_NONE;
1084                                 }
1085                         }
1086                         thumb_err("request_queue is NULL");
1087                         return MS_MEDIA_ERR_INTERNAL;
1088                 }
1089                 req_len = g_queue_get_length(g_manage_raw_queue);
1090                 for (i = 0; i < req_len; i++) {
1091                         tmp_raw_req = g_queue_pop_tail(g_manage_raw_queue);
1092                         if (tmp_raw_req) {
1093                                 SAFE_FREE(tmp_raw_req->path);
1094                                 SAFE_FREE(tmp_raw_req->userData);
1095                                 SAFE_FREE(tmp_raw_req);
1096                                 tmp_raw_req = NULL;
1097                         }
1098                 }
1099                 g_queue_free(g_manage_raw_queue);
1100                 g_manage_raw_queue = NULL;
1101                 if (g_request_raw_queue != NULL) {
1102                         /* Check request queue */
1103                         len = g_queue_get_length(g_request_raw_queue);
1104                         if (len > 0) {
1105                                 tmp_raw_req = g_queue_peek_head(g_request_raw_queue);
1106                                 if (tmp_raw_req != NULL)
1107                                         tmp_raw_req->isCanceled = true;
1108                         }
1109                 }
1110         } else {
1111                 thumbReq *tmp_req = NULL;
1112                 if (g_manage_queue == NULL) {
1113                         thumb_err("manage_queue is NULL");
1114                         if (g_request_queue != NULL) {
1115                                 /* Check request queue */
1116                                 len = g_queue_get_length(g_request_queue);
1117                                 if (len > 0) {
1118                                         tmp_req = g_queue_peek_head(g_request_queue);
1119                                         if (tmp_req != NULL)
1120                                                 tmp_req->isCanceled = true;
1121                                         return MS_MEDIA_ERR_NONE;
1122                                 }
1123                         }
1124                         thumb_err("request_queue is NULL");
1125                         return MS_MEDIA_ERR_INTERNAL;
1126                 }
1127                 req_len = g_queue_get_length(g_manage_queue);
1128                 for (i = 0; i < req_len; i++) {
1129                         tmp_req = g_queue_pop_tail(g_manage_queue);
1130                         if (tmp_req) {
1131                                 SAFE_FREE(tmp_req->path);
1132                                 SAFE_FREE(tmp_req->userData);
1133                                 SAFE_FREE(tmp_req);
1134                                 tmp_req = NULL;
1135                         }
1136                 }
1137                 g_queue_free(g_manage_queue);
1138                 g_manage_queue = NULL;
1139                 if (g_request_queue != NULL) {
1140                         /* Check request queue */
1141                         len = g_queue_get_length(g_request_queue);
1142                         if (len > 0) {
1143                                 tmp_req = g_queue_peek_head(g_request_queue);
1144                                 if (tmp_req != NULL)
1145                                         tmp_req->isCanceled = true;
1146                         }
1147                 }
1148         }
1149
1150         return MS_MEDIA_ERR_NONE;
1151 }