Remove sync 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 tzplatform_mkpath(TZ_SYS_RUN, "media-server/media_ipc_thumbcreator.socket")
36
37 static GQueue *g_manage_queue = NULL;
38 static GQueue *g_manage_raw_queue = NULL;
39
40
41 typedef struct {
42         GIOChannel *channel;
43         int msg_type;
44         unsigned int request_id;
45         bool isCanceled;
46         bool isRequested;
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         bool isRequested;
58         int request_id;
59         int source_id;
60         int width;
61         int height;
62         uid_t uid;
63         char *path;
64         thumbRawUserData *userData;
65 } thumbRawReq;
66
67 static int _media_thumb_send_request();
68 static int _media_thumb_raw_data_send_request();
69
70
71 int _media_thumb_get_error()
72 {
73         if (errno == EWOULDBLOCK) {
74                 thumb_err("Timeout. Can't try any more");
75                 return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
76         } else {
77                 thumb_stderror("recvfrom failed");
78                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
79         }
80 }
81
82 void __media_thumb_pop()
83 {
84         int len = 0;
85
86         if (g_manage_queue != NULL) {
87                 thumbReq *req = (thumbReq *)g_queue_pop_head(g_manage_queue);
88                 if (req != NULL) {
89                         GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
90
91                         g_io_channel_shutdown(req->channel, TRUE, NULL);
92                         g_io_channel_unref(req->channel);
93
94                         if (source_id != NULL) {
95                                 g_source_destroy(source_id);
96                         } else {
97                                 thumb_err("G_SOURCE_ID is NULL");
98                         }
99
100                         SAFE_FREE(req->path);
101                         SAFE_FREE(req->userData);
102                         SAFE_FREE(req);
103                 }
104
105                 /* Check manage queue */
106                 len = g_queue_get_length(g_manage_queue);
107                 if (len > 0)
108                         _media_thumb_send_request();
109         }
110 }
111
112 int __media_thumb_cancel(unsigned int request_id)
113 {
114         int len = 0, i;
115         bool flag = false;
116
117         if (g_manage_queue != NULL) {
118                 len = g_queue_get_length(g_manage_queue);
119
120                 for (i = 0; i < len; i++) {
121                         thumbReq *req = NULL;
122                         req = (thumbReq *)g_queue_peek_nth(g_manage_queue, i);
123                         if (req == NULL) continue;
124
125                         if (req->request_id == request_id) {
126                                 if (req->isRequested == true) {
127                                         req->isCanceled = true;
128                                 } else {
129                                         g_queue_pop_nth(g_manage_queue, i);
130
131                                         SAFE_FREE(req->path);
132                                         SAFE_FREE(req->userData);
133                                         SAFE_FREE(req);
134                                 }
135
136                                 flag = true;
137
138                                 break;
139                         }
140                 }
141         }
142
143         if (flag == false)
144                 return MS_MEDIA_ERR_INTERNAL;
145
146         return MS_MEDIA_ERR_NONE;
147 }
148
149 void __media_thumb_pop_raw_data()
150 {
151         int len = 0;
152
153         if (g_manage_raw_queue != NULL) {
154                 thumbRawReq *req = (thumbRawReq *)g_queue_pop_head(g_manage_raw_queue);
155                 if (req != NULL) {
156                         GSource *source_id = g_main_context_find_source_by_id(g_main_context_get_thread_default(), req->source_id);
157
158                         g_io_channel_shutdown(req->channel, TRUE, NULL);
159                         g_io_channel_unref(req->channel);
160
161                         if (source_id != NULL) {
162                                 g_source_destroy(source_id);
163                         } else {
164                                 thumb_err("G_SOURCE_ID is NULL");
165                         }
166
167                         SAFE_FREE(req->path);
168                         SAFE_FREE(req->userData);
169                         SAFE_FREE(req);
170                 }
171
172                 /* Check manage queue */
173                 len = g_queue_get_length(g_manage_raw_queue);
174                 if (len > 0)
175                         _media_thumb_raw_data_send_request();
176         }
177 }
178
179 int __media_thumb_cancel_raw_data(int request_id)
180 {
181         int len = 0, i;
182         bool flag = false;
183
184         if (g_manage_raw_queue != NULL) {
185                 len = g_queue_get_length(g_manage_raw_queue);
186
187                 for (i = 0; i < len; i++) {
188                         thumbRawReq *req = NULL;
189                         req = (thumbRawReq *)g_queue_peek_nth(g_manage_raw_queue, i);
190                         if (req == NULL) continue;
191
192                         if (req->request_id == request_id) {
193                                 if (req->isRequested == true) {
194                                         req->isCanceled = true;
195                                 } else {
196                                         g_queue_pop_nth(g_manage_raw_queue, i);
197
198                                         SAFE_FREE(req->path);
199                                         SAFE_FREE(req->userData);
200                                         SAFE_FREE(req);
201                                 }
202
203                                 flag = true;
204
205                                 break;
206                         }
207                 }
208         }
209
210         if (flag == false)
211                 return MS_MEDIA_ERR_INTERNAL;
212
213         return MS_MEDIA_ERR_NONE;
214
215 }
216
217 bool __media_thumb_check_cancel(void)
218 {
219         thumbReq *req = NULL;
220         req = (thumbReq *)g_queue_peek_head(g_manage_queue);
221
222         if (req == NULL) {
223                 return false;
224         } else {
225                 if (req->isCanceled)
226                         return false;
227                 else
228                         return true;
229         }
230 }
231
232 bool __media_thumb_check_cancel_for_raw(void)
233 {
234         thumbRawReq *req = NULL;
235         req = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
236
237         if (req == NULL) {
238                 return false;
239         } else {
240                 if (req->isCanceled)
241                         return false;
242                 else
243                         return true;
244         }
245 }
246
247 int _media_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
248 {
249         int recv_msg_len = 0;
250         int remain_size = 0;
251         int block_size = THUMB_SOCK_BLOCK_SIZE;
252         int recv_block = 0;
253         unsigned char *buf = NULL;
254         unsigned char *block_buf = NULL;
255
256         THUMB_MALLOC(buf, header_size);
257         THUMB_MALLOC(block_buf, THUMB_SOCK_BLOCK_SIZE);
258         if (buf == NULL || block_buf == NULL) {
259                 thumb_err("memory allocation failed");
260                 SAFE_FREE(buf);
261                 SAFE_FREE(block_buf);
262                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
263         }
264
265         if ((recv_msg_len = recv(sock, buf, header_size, 0)) <= 0) {
266                 thumb_stderror("recv failed");
267                 SAFE_FREE(buf);
268                 SAFE_FREE(block_buf);
269                 return _media_thumb_get_error();
270         }
271
272         memcpy(msg, buf, header_size);
273         //thumb_dbg("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
274
275         SAFE_FREE(buf);
276         if (msg->origin_path_size < 0 || msg->dest_path_size < 0 || msg->thumb_size < 0) {
277                 thumb_err("recv data is wrong");
278                 SAFE_FREE(block_buf);
279                 return MS_MEDIA_ERR_SOCKET_RECEIVE;
280         }
281
282         remain_size = msg->origin_path_size + msg->dest_path_size + msg->thumb_size;
283         THUMB_MALLOC(buf, remain_size);
284         if (buf == NULL) {
285                 SAFE_FREE(block_buf);
286                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
287         }
288
289         while (remain_size > 0) {
290                 if (remain_size < THUMB_SOCK_BLOCK_SIZE) {
291                         block_size = remain_size;
292                 }
293                 if ((recv_msg_len = recv(sock, block_buf, block_size, 0)) < 0) {
294                         thumb_stderror("recv failed");
295                         SAFE_FREE(buf);
296                         SAFE_FREE(block_buf);
297                         return _media_thumb_get_error();
298                 }
299                 memcpy(buf+recv_block, block_buf, block_size);
300                 recv_block += block_size;
301                 remain_size -= block_size;
302         }
303
304         SAFE_STRLCPY(msg->org_path, (char *)buf, sizeof(msg->org_path));
305         SAFE_STRLCPY(msg->dst_path, (char *)buf + msg->origin_path_size, sizeof(msg->dst_path));
306
307         SAFE_FREE(msg->thumb_data);
308         if (msg->thumb_size > 0) {
309                 THUMB_MALLOC(msg->thumb_data, msg->thumb_size);
310                 if (msg->thumb_data != NULL) {
311                         memcpy(msg->thumb_data, buf + msg->origin_path_size + msg->dest_path_size, msg->thumb_size);
312                 } else {
313                         SAFE_FREE(buf);
314                         SAFE_FREE(block_buf);
315
316                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
317                 }
318         }
319
320         SAFE_FREE(buf);
321         SAFE_FREE(block_buf);
322
323         return MS_MEDIA_ERR_NONE;
324 }
325
326 int _media_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
327 {
328         if (req_msg == NULL || buf == NULL) {
329                 return MS_MEDIA_ERR_INVALID_PARAMETER;
330         }
331
332         int org_path_len = 0;
333         int dst_path_len = 0;
334         int thumb_data_len = 0;
335         int size = 0;
336         int header_size = 0;
337
338         header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
339         org_path_len = req_msg->origin_path_size;
340         dst_path_len = req_msg->dest_path_size;
341         thumb_data_len = req_msg->thumb_size;
342
343         //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);
344
345         size = header_size + org_path_len + dst_path_len + thumb_data_len;
346         THUMB_MALLOC(*buf, size);
347         if (*buf == NULL) {
348                 *buf_size = 0;
349                 return 0;
350         }
351         memcpy(*buf, req_msg, header_size);
352         memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
353         memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
354         if (thumb_data_len > 0)
355                 memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, thumb_data_len);
356
357         *buf_size = size;
358
359         return MS_MEDIA_ERR_NONE;
360 }
361
362 gboolean _media_thumb_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
363 {
364         thumbMsg recv_msg;
365         int header_size = 0;
366         int sock = 0;
367         int err = MS_MEDIA_ERR_NONE;
368
369         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
370         sock = g_io_channel_unix_get_fd(src);
371
372         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
373
374         thumb_err("_media_thumb_write_socket socket : %d", sock);
375
376         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
377                 thumb_err("_media_thumb_recv_msg failed ");
378                 __media_thumb_pop();
379
380                 return FALSE;
381         }
382
383         if (recv_msg.status != MS_MEDIA_ERR_NONE) {
384                 err = recv_msg.status;
385                 thumb_err("Failed to make thumbnail (%d)", err);
386         }
387
388         if (__media_thumb_check_cancel()) {
389                 if (data) {
390                         thumbUserData* cb = (thumbUserData*)data;
391                         if (cb->func != NULL)
392                                 cb->func(err, recv_msg.dst_path, cb->user_data);
393                 }
394         }
395
396         __media_thumb_pop();
397
398         thumb_dbg("Done");
399
400         return FALSE;
401 }
402
403 gboolean _media_thumb_raw_data_write_socket(GIOChannel *src, GIOCondition condition, gpointer data)
404 {
405         thumbMsg recv_msg;
406         int header_size = 0;
407         int sock = 0;
408         int err = MS_MEDIA_ERR_NONE;
409
410         memset((void *)&recv_msg, 0, sizeof(thumbMsg));
411         sock = g_io_channel_unix_get_fd(src);
412
413         header_size = sizeof(thumbMsg) - MAX_PATH_SIZE*2 - sizeof(unsigned char *);
414
415         thumb_err("_media_thumb_write_socket socket : %d", sock);
416
417         if ((err = _media_thumb_recv_msg(sock, header_size, &recv_msg)) < 0) {
418                 thumb_err("_media_thumb_recv_msg failed ");
419                 __media_thumb_pop_raw_data();
420
421                 return FALSE;
422         }
423
424         if (recv_msg.status != MS_MEDIA_ERR_NONE) {
425                 err = recv_msg.status;
426                 thumb_err("Failed to make thumbnail (%d)", err);
427         }
428
429         if (__media_thumb_check_cancel_for_raw()) {
430                 if (data) {
431                         thumbRawUserData* cb = (thumbRawUserData*)data;
432                         if (cb->func != NULL)
433                                 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);
434                 }
435         }
436
437         __media_thumb_pop_raw_data();
438
439         thumb_dbg("Done");
440
441         return FALSE;
442 }
443
444 static int _media_thumb_send_request()
445 {
446         int err = MS_MEDIA_ERR_NONE;
447         int sock = -1;
448         struct sockaddr_un serv_addr;
449         thumbReq *req_manager = NULL;
450         int pid;
451
452         err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
453         if (err != MS_MEDIA_ERR_NONE) {
454                 thumb_err("ms_ipc_create_client_socket failed");
455                 return err;
456         }
457
458         memset(&serv_addr, 0, sizeof(serv_addr));
459         serv_addr.sun_family = AF_UNIX;
460         SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
461
462         GIOChannel *channel = NULL;
463         channel = g_io_channel_unix_new(sock);
464         int source_id = -1;
465
466         /* Connecting to the thumbnail server */
467         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
468                 thumb_stderror("connect");
469                 if (errno == EACCES)
470                         err = MS_MEDIA_ERR_PERMISSION_DENIED;
471                 else
472                         err = MS_MEDIA_ERR_SOCKET_CONN;
473
474                 g_io_channel_shutdown(channel, TRUE, NULL);
475                 g_io_channel_unref(channel);
476                 close(sock);
477
478                 return err;
479         }
480
481         req_manager = (thumbReq *)g_queue_peek_head(g_manage_queue);
482
483         if (req_manager == NULL) {
484                 thumb_err("queue peek fail");
485                 g_io_channel_shutdown(channel, TRUE, NULL);
486                 g_io_channel_unref(channel);
487                 close(sock);
488                 return MS_MEDIA_ERR_INVALID_PARAMETER;
489         }
490
491         GSource *source = NULL;
492         source = g_io_create_watch(channel, G_IO_IN);
493         g_source_set_callback(source, (GSourceFunc)_media_thumb_write_socket, req_manager->userData, NULL);
494         source_id = g_source_attach(source, g_main_context_get_thread_default());
495
496         thumbMsg req_msg;
497
498         memset((void *)&req_msg, 0, sizeof(thumbMsg));
499
500         pid = getpid();
501         req_msg.pid = pid;
502         req_msg.msg_type = req_manager->msg_type;
503         req_msg.request_id = 0;
504         req_msg.uid = req_manager->uid;
505         SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
506         req_msg.dst_path[0] = '\0';
507         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
508         req_msg.dest_path_size = 1;
509         req_msg.thumb_size = 0;
510
511         if (req_msg.origin_path_size > MAX_PATH_SIZE || req_msg.dest_path_size > MAX_PATH_SIZE) {
512                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
513                 g_io_channel_shutdown(channel, TRUE, NULL);
514                 g_io_channel_unref(channel);
515                 close(sock);
516                 return MS_MEDIA_ERR_INVALID_PARAMETER;
517         }
518
519         unsigned char *buf = NULL;
520         int buf_size = 0;
521         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
522
523         if (send(sock, buf, buf_size, 0) != buf_size) {
524                 thumb_err("sendto failed: %d", errno);
525                 SAFE_FREE(buf);
526                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
527                 g_io_channel_shutdown(channel, TRUE, NULL);
528                 g_io_channel_unref(channel);
529                 close(sock);
530                 return MS_MEDIA_ERR_SOCKET_SEND;
531         }
532
533         SAFE_FREE(buf);
534         thumb_dbg("Sending msg to thumbnail daemon is successful");
535
536         if (req_manager->msg_type == THUMB_REQUEST_DB_INSERT) {
537                 req_manager->channel = channel;
538                 req_manager->source_id = source_id;
539                 req_manager->isRequested = true;
540         }
541
542         return err;
543 }
544
545 static int _media_thumb_raw_data_send_request()
546 {
547         int err = MS_MEDIA_ERR_NONE;
548         int sock = -1;
549         struct sockaddr_un serv_addr;
550         thumbRawReq *req_manager = NULL;
551         int pid;
552
553         err = ms_ipc_create_client_socket(MS_TIMEOUT_SEC_10, &sock);
554         if (err != MS_MEDIA_ERR_NONE) {
555                 thumb_err("ms_ipc_create_client_socket failed");
556                 return err;
557         }
558
559         memset(&serv_addr, 0, sizeof(serv_addr));
560         serv_addr.sun_family = AF_UNIX;
561         SAFE_STRLCPY(serv_addr.sun_path, THUMB_IPC_PATH, sizeof(serv_addr.sun_path));
562
563         GIOChannel *channel = NULL;
564         channel = g_io_channel_unix_new(sock);
565         int source_id = -1;
566
567         /* Connecting to the thumbnail server */
568         if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
569                 thumb_stderror("connect error");
570                 if (errno == EACCES)
571                         err = MS_MEDIA_ERR_PERMISSION_DENIED;
572                 else
573                         err = MS_MEDIA_ERR_SOCKET_CONN;
574
575                 g_io_channel_shutdown(channel, TRUE, NULL);
576                 g_io_channel_unref(channel);
577                 close(sock);
578                 return err;
579         }
580
581         req_manager = (thumbRawReq *)g_queue_peek_head(g_manage_raw_queue);
582
583         if (req_manager == NULL) {
584                 thumb_err("queue peek fail");
585                 g_io_channel_shutdown(channel, TRUE, NULL);
586                 g_io_channel_unref(channel);
587                 close(sock);
588                 return MS_MEDIA_ERR_INVALID_PARAMETER;
589         }
590
591         GSource *source = NULL;
592         source = g_io_create_watch(channel, G_IO_IN);
593         g_source_set_callback(source, (GSourceFunc)_media_thumb_raw_data_write_socket, req_manager->userData, NULL);
594         source_id = g_source_attach(source, g_main_context_get_thread_default());
595
596         thumbMsg req_msg;
597         memset((void *)&req_msg, 0, sizeof(thumbMsg));
598
599         pid = getpid();
600         req_msg.pid = pid;
601         req_msg.msg_type = req_manager->msg_type;
602         req_msg.request_id = req_manager->request_id;
603         req_msg.thumb_width = req_manager->width;
604         req_msg.thumb_height = req_manager->height;
605         req_msg.uid = req_manager->uid;
606
607         SAFE_STRLCPY(req_msg.org_path, req_manager->path, sizeof(req_msg.org_path));
608         req_msg.dst_path[0] = '\0';
609
610         req_msg.origin_path_size = strlen(req_msg.org_path) + 1;
611         req_msg.dest_path_size = 1;
612         req_msg.thumb_size = 0;
613
614         if (req_msg.origin_path_size > MAX_PATH_SIZE) {
615                 thumb_err("path's length exceeds %d", MAX_PATH_SIZE);
616                 g_io_channel_shutdown(channel, TRUE, NULL);
617                 g_io_channel_unref(channel);
618                 return MS_MEDIA_ERR_INVALID_PARAMETER;
619         }
620
621         unsigned char *buf = NULL;
622         int buf_size = 0;
623         _media_thumb_set_buffer(&req_msg, &buf, &buf_size);
624
625         if (send(sock, buf, buf_size, 0) != buf_size) {
626                 thumb_err("sendto failed: %d", errno);
627                 SAFE_FREE(buf);
628                 g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), source_id));
629                 g_io_channel_shutdown(channel, TRUE, NULL);
630                 g_io_channel_unref(channel);
631                 return MS_MEDIA_ERR_SOCKET_SEND;
632         }
633
634         SAFE_FREE(buf);
635
636         if (req_manager->msg_type == THUMB_REQUEST_RAW_DATA) {
637                 req_manager->channel = channel;
638                 req_manager->source_id = source_id;
639                 req_manager->isRequested = true;
640         }
641         return MS_MEDIA_ERR_NONE;
642 }
643
644 int _media_thumb_request_async(int msg_type, unsigned int request_id, const char *origin_path, thumbUserData *userData, uid_t uid)
645 {
646         int err = MS_MEDIA_ERR_NONE;
647         int len = 0;
648
649         if (msg_type == THUMB_REQUEST_CANCEL_MEDIA)
650                 return __media_thumb_cancel(request_id);
651
652         if (g_manage_queue == NULL)
653                 g_manage_queue = g_queue_new();
654
655         thumbReq *thumb_req = NULL;
656         THUMB_MALLOC(thumb_req, sizeof(thumbReq));
657         thumb_retvm_if(thumb_req == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "Failed to create request element");
658
659         thumb_req->msg_type = msg_type;
660         thumb_req->path = g_strdup(origin_path);
661         thumb_req->userData = userData;
662         thumb_req->isCanceled = false;
663         thumb_req->isRequested = false;
664         thumb_req->request_id = request_id;
665         thumb_req->uid = uid;
666
667         len = g_queue_get_length(g_manage_queue);
668         g_queue_push_tail(g_manage_queue, (gpointer)thumb_req);
669
670         if (len == 0)
671                 err = _media_thumb_send_request();
672
673         return err;
674 }
675
676 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)
677 {
678         int err = MS_MEDIA_ERR_NONE;
679         int len = 0;
680         thumbRawReq *thumb_req = NULL;
681
682         if (msg_type == THUMB_REQUEST_CANCEL_RAW_DATA)
683                 return __media_thumb_cancel_raw_data(request_id);
684
685         if (g_manage_raw_queue == NULL)
686                 g_manage_raw_queue = g_queue_new();
687
688         THUMB_MALLOC(thumb_req, sizeof(thumbRawReq));
689         if (thumb_req == NULL) {
690                 thumb_err("Failed to create request element");
691                 return MS_MEDIA_ERR_INVALID_PARAMETER;
692         }
693
694         thumb_req->msg_type = msg_type;
695         thumb_req->request_id = request_id;
696         thumb_req->path = g_strdup(origin_path);
697         thumb_req->width = width;
698         thumb_req->height = height;
699         thumb_req->userData = userData;
700         thumb_req->isCanceled = false;
701         thumb_req->isRequested = false;
702         thumb_req->uid = uid;
703
704         len = g_queue_get_length(g_manage_raw_queue);
705         g_queue_push_tail(g_manage_raw_queue, (gpointer)thumb_req);
706
707         if (len == 0)
708                 err = _media_thumb_raw_data_send_request();
709
710         return err;
711 }
712
713 int _media_thumb_request_cancel_all()
714 {
715         if (g_manage_raw_queue != NULL) {
716                 while (g_queue_get_length(g_manage_raw_queue) > 0) {
717                         thumbRawReq *req = (thumbRawReq *)g_queue_peek_tail(g_manage_raw_queue);
718                         if (req != NULL) {
719                                 if (req->isRequested == true) {
720                                         req->isCanceled = true;
721                                         thumb_dbg("Last item!!");
722
723                                         break;
724                                 } else {
725                                         g_queue_pop_tail(g_manage_raw_queue);
726
727                                         SAFE_FREE(req->path);
728                                         SAFE_FREE(req->userData);
729                                         SAFE_FREE(req);
730                                 }
731                         } else {
732                                 thumb_dbg("Queue is empty!!");
733                                 break;
734                         }
735                 }
736         }
737
738         return MS_MEDIA_ERR_NONE;
739 }