Apply tizen coding rule
[platform/framework/web/download-provider.git] / agent / download-agent-dl-mgr.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdlib.h>
18 #include <sys/syscall.h>
19 #include <signal.h>
20
21 #include "download-agent-dl-mgr.h"
22 #include "download-agent-dl-info.h"
23 #include "download-agent-http-mgr.h"
24
25 void __thread_clean_up_handler_for_start_download(void *arg)
26 {
27         DA_LOGI("cleanup for thread id[%lu]", pthread_self());
28 }
29
30 da_ret_t __download_content(da_info_t *da_info)
31 {
32         da_ret_t ret = DA_RESULT_OK;
33
34         DA_LOGV("");
35         if (!da_info) {
36                 DA_LOGE("NULL CHECK!: da_info");
37                 ret = DA_ERR_INVALID_ARGUMENT;
38                 return ret;
39         }
40
41         ret = request_http_download(da_info);
42         return ret;
43 }
44
45
46 static void *__thread_start_download(void *data)
47 {
48         da_info_t *da_info = DA_NULL;
49         req_info_t *req_info = DA_NULL;
50         int da_id = DA_INVALID_ID;
51
52 //      DA_LOGV("");
53
54         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, DA_NULL);
55
56         da_info = (da_info_t *)data;
57         NULL_CHECK_RET_OPT(da_info, DA_NULL);
58         req_info = da_info->req_info;
59         NULL_CHECK_RET_OPT(req_info, DA_NULL);
60
61         da_id = da_info->da_id;
62         pthread_cleanup_push(__thread_clean_up_handler_for_start_download, DA_NULL);
63         __download_content(da_info);
64         destroy_da_info(da_id);
65         pthread_cleanup_pop(0);
66         DA_LOGI("=====EXIT thread : da_id[%d]=====", da_id);
67         pthread_exit((void *)DA_NULL);
68         return DA_NULL;
69 }
70
71 da_ret_t start_download(da_info_t *da_info)
72 {
73         da_ret_t ret = DA_RESULT_OK;
74         pthread_attr_t thread_attr;
75         pthread_t tid;
76         if (pthread_attr_init(&thread_attr) != 0) {
77                 ret = DA_ERR_FAIL_TO_CREATE_THREAD;
78                 goto ERR;
79         }
80
81         if (pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED) != 0) {
82                 ret = DA_ERR_FAIL_TO_CREATE_THREAD;
83                 goto ERR;
84         }
85
86         if (pthread_create(&(tid), &thread_attr,
87                         __thread_start_download, da_info) < 0) {
88                 DA_LOGE("Fail to make thread:id[%d]", da_info->da_id);
89                 ret = DA_ERR_FAIL_TO_CREATE_THREAD;
90         } else {
91                 if (tid < 1) {
92                         DA_LOGE("The thread start is failed before calling this");
93 // When http resource is leaked, the thread ID is initialized at error handling section of thread_start_download()
94 // Because the thread ID is initialized, the ptrhead_detach should not be called. This is something like timing issue between threads.
95 // thread info and basic_dl_input is freed at thread_start_download(). And it should not returns error code in this case.
96                         ret = DA_ERR_FAIL_TO_CREATE_THREAD;
97                         goto ERR;
98                 }
99         }
100         da_info->thread_id = tid;
101         DA_LOGI("Thread create:thread id[%lu]", da_info->thread_id);
102 ERR:
103         if (DA_RESULT_OK != ret)
104                 destroy_da_info(da_info->da_id);
105         return ret;
106 }
107
108 da_ret_t cancel_download(int dl_id, da_bool_t is_enable_cb)
109 {
110         da_ret_t ret = DA_RESULT_OK;
111         da_info_t *da_info = DA_NULL;
112
113         DA_LOGV("");
114
115         ret = get_da_info_with_da_id(dl_id, &da_info);
116         if (ret != DA_RESULT_OK || !da_info)
117                 return DA_ERR_INVALID_ARGUMENT;
118         da_info->is_cb_update = is_enable_cb;
119         ret = request_to_cancel_http_download(da_info);
120         if (ret != DA_RESULT_OK)
121                 goto ERR;
122         DA_LOGI("Download cancel Successful for download id[%d]", da_info->da_id);
123
124 ERR:
125         return ret;
126 }
127
128 da_ret_t suspend_download(int dl_id, da_bool_t is_enable_cb)
129 {
130         da_ret_t ret = DA_RESULT_OK;
131         da_info_t *da_info = DA_NULL;
132
133         DA_LOGV("");
134
135         ret = get_da_info_with_da_id(dl_id, &da_info);
136         if (ret != DA_RESULT_OK || !da_info)
137                 return DA_ERR_INVALID_ARGUMENT;
138         da_info->is_cb_update = is_enable_cb;
139         ret = request_to_suspend_http_download(da_info);
140         if (ret != DA_RESULT_OK)
141                 goto ERR;
142         DA_LOGV("Download Suspend Successful for download id[%d]", da_info->da_id);
143 ERR:
144         return ret;
145
146 }
147
148 da_ret_t resume_download(int dl_id)
149 {
150         da_ret_t ret = DA_RESULT_OK;
151         da_info_t *da_info = DA_NULL;
152
153         DA_LOGV("");
154
155         ret = get_da_info_with_da_id(dl_id, &da_info);
156         if (ret != DA_RESULT_OK || !da_info)
157                 return DA_ERR_INVALID_ARGUMENT;
158         da_info->is_cb_update = DA_TRUE;
159         ret = request_to_resume_http_download(da_info);
160         if (ret != DA_RESULT_OK)
161                 goto ERR;
162         DA_LOGV("Download Resume Successful for download id[%d]", da_info->da_id);
163 ERR:
164         return ret;
165 }
166