cf3ad5247108ab94d94275d0647b404cfe8bacf8
[platform/framework/web/download-provider.git] / src / 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 "download-agent-client-mgr.h"
18 #include "download-agent-debug.h"
19 #include "download-agent-dl-mgr.h"
20 #include "download-agent-utils.h"
21 #include "download-agent-http-mgr.h"
22 #include "download-agent-file.h"
23 #include "download-agent-plugin-conf.h"
24
25
26 static da_result_t __cancel_download_with_slot_id(int slot_id);
27 static da_result_t __suspend_download_with_slot_id(int slot_id);
28
29
30 da_result_t requesting_download(stage_info *stage)
31 {
32         da_result_t ret = DA_RESULT_OK;
33         req_dl_info *request_session = DA_NULL;
34
35         DA_LOG_FUNC_START(Default);
36
37         if (!stage) {
38                 DA_LOG_ERR(Default, "stage is null..");
39                 ret = DA_ERR_INVALID_ARGUMENT;
40                 goto ERR;
41         }
42
43         ret = make_req_dl_info_http(stage, GET_STAGE_TRANSACTION_INFO(stage));
44         if (ret != DA_RESULT_OK)
45                 goto ERR;
46
47         request_session = GET_STAGE_TRANSACTION_INFO(stage);
48         ret = request_http_download(stage);
49         if (DA_RESULT_OK == ret) {
50                 DA_LOG(Default, "Http download is complete.");
51         } else {
52                 DA_LOG_ERR(Default, "Http download is failed. ret = %d", ret);
53                 goto ERR;
54         }
55 ERR:
56         return ret;
57 }
58
59 da_result_t handle_after_download(stage_info *stage)
60 {
61         da_result_t ret = DA_RESULT_OK;
62         da_mime_type_id_t mime_type = DA_MIME_TYPE_NONE;
63
64         DA_LOG_FUNC_START(Default);
65
66         mime_type = get_mime_type_id(
67                         GET_CONTENT_STORE_CONTENT_TYPE(GET_STAGE_CONTENT_STORE_INFO(stage)));
68
69         switch (mime_type) {
70                 case DA_MIME_TYPE_NONE:
71                         DA_LOG(Default, "DA_MIME_TYPE_NONE");
72                         ret = DA_ERR_MISMATCH_CONTENT_TYPE;
73                         break;
74                 default:
75                         CHANGE_DOWNLOAD_STATE(DOWNLOAD_STATE_FINISH, stage);
76                         break;
77         } /* end of switch */
78
79         return ret;
80 }
81
82 static da_result_t __cancel_download_with_slot_id(int slot_id)
83 {
84         da_result_t ret = DA_RESULT_OK;
85         download_state_t download_state;
86         stage_info *stage = DA_NULL;
87
88         DA_LOG_FUNC_START(Default);
89
90         _da_thread_mutex_lock (&mutex_download_state[slot_id]);
91         download_state = GET_DL_STATE_ON_ID(slot_id);
92         DA_LOG(Default, "download_state = %d", GET_DL_STATE_ON_ID(slot_id));
93
94         if (download_state == DOWNLOAD_STATE_FINISH ||
95                         download_state == DOWNLOAD_STATE_CANCELED) {
96                 DA_LOG_CRITICAL(Default, "Already download is finished. Do not send cancel request");
97                 _da_thread_mutex_unlock (&mutex_download_state[slot_id]);
98                 return ret;
99         }
100         _da_thread_mutex_unlock (&mutex_download_state[slot_id]);
101
102         stage = GET_DL_CURRENT_STAGE(slot_id);
103         if (!stage)
104                 return DA_RESULT_OK;
105
106         ret = request_to_cancel_http_download(stage);
107         if (ret != DA_RESULT_OK)
108                 goto ERR;
109         DA_LOG(Default, "Download cancel Successful for download id - %d", slot_id);
110 ERR:
111         return ret;
112 }
113
114 da_result_t cancel_download(int dl_id)
115 {
116         da_result_t ret = DA_RESULT_OK;
117
118         int slot_id = DA_INVALID_ID;
119
120         DA_LOG_FUNC_START(Default);
121
122         ret = get_slot_id_for_dl_id(dl_id, &slot_id);
123         if (ret != DA_RESULT_OK) {
124                 DA_LOG_ERR(Default, "dl req ID is not Valid");
125                 goto ERR;
126         }
127
128         if (DA_FALSE == is_valid_slot_id(slot_id)) {
129                 DA_LOG_ERR(Default, "Download ID is not Valid");
130                 ret = DA_ERR_INVALID_ARGUMENT;
131                 goto ERR;
132         }
133
134         ret = __cancel_download_with_slot_id(slot_id);
135
136 ERR:
137         return ret;
138
139 }
140
141 static da_result_t __suspend_download_with_slot_id(int slot_id)
142 {
143         da_result_t ret = DA_RESULT_OK;
144         download_state_t download_state;
145         stage_info *stage = DA_NULL;
146
147         DA_LOG_FUNC_START(Default);
148
149         _da_thread_mutex_lock (&mutex_download_state[slot_id]);
150         download_state = GET_DL_STATE_ON_ID(slot_id);
151         DA_LOG(Default, "download_state = %d", GET_DL_STATE_ON_ID(slot_id));
152         _da_thread_mutex_unlock (&mutex_download_state[slot_id]);
153
154         stage = GET_DL_CURRENT_STAGE(slot_id);
155         if (!stage)
156                 return DA_ERR_CANNOT_SUSPEND;
157
158         ret = request_to_suspend_http_download(stage);
159         if (ret != DA_RESULT_OK)
160                 goto ERR;
161         DA_LOG(Default, "Download Suspend Successful for download id-%d", slot_id);
162 ERR:
163         return ret;
164 }
165
166 da_result_t suspend_download(int dl_id, da_bool_t is_enable_cb)
167 {
168         da_result_t ret = DA_RESULT_OK;
169         int slot_id = DA_INVALID_ID;
170
171         DA_LOG_FUNC_START(Default);
172
173         ret = get_slot_id_for_dl_id(dl_id, &slot_id);
174         if (ret != DA_RESULT_OK) {
175                 DA_LOG_ERR(Default, "dl req ID is not Valid");
176                 goto ERR;
177         }
178         GET_DL_ENABLE_PAUSE_UPDATE(slot_id) = is_enable_cb;
179         if (DA_FALSE == is_valid_slot_id(slot_id)) {
180                 DA_LOG_ERR(Default, "Download ID is not Valid");
181                 ret = DA_ERR_INVALID_ARGUMENT;
182                 goto ERR;
183         }
184
185         ret = __suspend_download_with_slot_id(slot_id);
186
187 ERR:
188         return ret;
189
190 }
191
192 static da_result_t __resume_download_with_slot_id(int slot_id)
193 {
194         da_result_t ret = DA_RESULT_OK;
195         download_state_t download_state;
196         stage_info *stage = DA_NULL;
197
198         DA_LOG_FUNC_START(Default);
199
200         _da_thread_mutex_lock (&mutex_download_state[slot_id]);
201         download_state = GET_DL_STATE_ON_ID(slot_id);
202         DA_LOG(Default, "download_state = %d", GET_DL_STATE_ON_ID(slot_id));
203         _da_thread_mutex_unlock (&mutex_download_state[slot_id]);
204
205         stage = GET_DL_CURRENT_STAGE(slot_id);
206
207         ret = request_to_resume_http_download(stage);
208         if (ret != DA_RESULT_OK)
209                 goto ERR;
210         DA_LOG(Default, "Download Resume Successful for download id-%d", slot_id);
211 ERR:
212         return ret;
213 }
214
215 da_result_t resume_download(int dl_id)
216 {
217         da_result_t ret = DA_RESULT_OK;
218         int slot_id = DA_INVALID_ID;
219
220         DA_LOG_FUNC_START(Default);
221
222         ret = get_slot_id_for_dl_id(dl_id, &slot_id);
223         if (ret != DA_RESULT_OK)
224                 goto ERR;
225
226         if (DA_FALSE == is_valid_slot_id(slot_id)) {
227                 DA_LOG_ERR(Default, "Download ID is not Valid");
228                 ret = DA_ERR_INVALID_DL_REQ_ID;
229                 goto ERR;
230         }
231
232         ret = __resume_download_with_slot_id(slot_id);
233
234 ERR:
235         return ret;
236 }
237
238 da_result_t send_user_noti_and_finish_download_flow(
239                 int slot_id, char *installed_path, char *etag)
240 {
241         da_result_t ret = DA_RESULT_OK;
242         download_state_t download_state = DA_NULL;
243         da_bool_t need_destroy_download_info = DA_FALSE;
244
245         DA_LOG_FUNC_START(Default);
246
247         _da_thread_mutex_lock (&mutex_download_state[slot_id]);
248         download_state = GET_DL_STATE_ON_ID(slot_id);
249         DA_LOG(Default, "state = %d", download_state);
250         _da_thread_mutex_unlock (&mutex_download_state[slot_id]);
251
252         switch (download_state) {
253         case DOWNLOAD_STATE_FINISH:
254                 send_client_finished_info(slot_id, GET_DL_ID(slot_id),
255                         installed_path, DA_NULL, DA_RESULT_OK,
256                         get_http_status(slot_id));
257                 need_destroy_download_info = DA_TRUE;
258                 break;
259         case DOWNLOAD_STATE_CANCELED:
260                 send_client_finished_info(slot_id, GET_DL_ID(slot_id),
261                                 installed_path, etag, DA_RESULT_USER_CANCELED,
262                                 get_http_status(slot_id));
263                 need_destroy_download_info = DA_TRUE;
264                 break;
265 #ifdef PAUSE_EXIT
266         case DOWNLOAD_STATE_PAUSED:
267                 need_destroy_download_info = DA_TRUE;
268                 break;
269 #endif
270         default:
271                 DA_LOG(Default, "download state = %d", download_state);
272                 break;
273         }
274
275         if (need_destroy_download_info == DA_TRUE) {
276                 destroy_download_info(slot_id);
277         } else {
278                 DA_LOG_CRITICAL(Default, "download info is not destroyed");
279         }
280
281         return ret;
282 }
283
284 da_bool_t is_valid_download_id(int dl_id)
285 {
286
287         da_bool_t ret = DA_TRUE;
288         int slot_id = DA_INVALID_ID;
289
290         DA_LOG_VERBOSE(Default, "[is_valid_download_id]download_id : %d", dl_id);
291
292         ret = get_slot_id_for_dl_id(dl_id, &slot_id);
293         if (ret != DA_RESULT_OK) {
294                 DA_LOG_ERR(Default, "dl req ID is not Valid");
295                 ret = DA_FALSE;
296                 goto ERR;
297         } else {
298                 ret = DA_TRUE;
299         }
300
301         if (DA_FALSE == is_valid_slot_id(slot_id)) {
302                 DA_LOG_ERR(Default, "Download ID is not Valid");
303                 ret = DA_FALSE;
304                 goto ERR;
305         }
306         if (GET_DL_THREAD_ID(slot_id) < 1) {
307                 DA_LOG_ERR(Default, "Download thread is not alive");
308                 ret = DA_FALSE;
309                 goto ERR;
310         }
311
312 ERR:
313         return ret;
314 }