2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
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);
30 da_result_t requesting_download(stage_info *stage)
32 da_result_t ret = DA_RESULT_OK;
33 req_dl_info *request_session = DA_NULL;
35 DA_LOG_FUNC_START(Default);
38 DA_LOG_ERR(Default, "stage is null..");
39 ret = DA_ERR_INVALID_ARGUMENT;
43 ret = make_req_dl_info_http(stage, GET_STAGE_TRANSACTION_INFO(stage));
44 if (ret != DA_RESULT_OK)
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.");
52 DA_LOG_ERR(Default, "Http download is failed. ret = %d", ret);
59 da_result_t handle_after_download(stage_info *stage)
61 da_result_t ret = DA_RESULT_OK;
62 da_mime_type_id_t mime_type = DA_MIME_TYPE_NONE;
64 DA_LOG_FUNC_START(Default);
66 mime_type = get_mime_type_id(
67 GET_CONTENT_STORE_CONTENT_TYPE(GET_STAGE_CONTENT_STORE_INFO(stage)));
70 case DA_MIME_TYPE_NONE:
71 DA_LOG(Default, "DA_MIME_TYPE_NONE");
72 ret = DA_ERR_MISMATCH_CONTENT_TYPE;
75 CHANGE_DOWNLOAD_STATE(DOWNLOAD_STATE_FINISH, stage);
82 static da_result_t __cancel_download_with_slot_id(int slot_id)
84 da_result_t ret = DA_RESULT_OK;
85 download_state_t download_state;
86 stage_info *stage = DA_NULL;
88 DA_LOG_FUNC_START(Default);
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));
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]);
100 _da_thread_mutex_unlock (&mutex_download_state[slot_id]);
102 stage = GET_DL_CURRENT_STAGE(slot_id);
106 ret = request_to_cancel_http_download(stage);
107 if (ret != DA_RESULT_OK)
109 DA_LOG(Default, "Download cancel Successful for download id - %d", slot_id);
114 da_result_t cancel_download(int dl_id)
116 da_result_t ret = DA_RESULT_OK;
118 int slot_id = DA_INVALID_ID;
120 DA_LOG_FUNC_START(Default);
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");
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;
134 ret = __cancel_download_with_slot_id(slot_id);
141 static da_result_t __suspend_download_with_slot_id(int slot_id)
143 da_result_t ret = DA_RESULT_OK;
144 download_state_t download_state;
145 stage_info *stage = DA_NULL;
147 DA_LOG_FUNC_START(Default);
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]);
154 stage = GET_DL_CURRENT_STAGE(slot_id);
156 return DA_ERR_CANNOT_SUSPEND;
158 ret = request_to_suspend_http_download(stage);
159 if (ret != DA_RESULT_OK)
161 DA_LOG(Default, "Download Suspend Successful for download id-%d", slot_id);
166 da_result_t suspend_download(int dl_id, da_bool_t is_enable_cb)
168 da_result_t ret = DA_RESULT_OK;
169 int slot_id = DA_INVALID_ID;
171 DA_LOG_FUNC_START(Default);
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");
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;
185 ret = __suspend_download_with_slot_id(slot_id);
192 static da_result_t __resume_download_with_slot_id(int slot_id)
194 da_result_t ret = DA_RESULT_OK;
195 download_state_t download_state;
196 stage_info *stage = DA_NULL;
198 DA_LOG_FUNC_START(Default);
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]);
205 stage = GET_DL_CURRENT_STAGE(slot_id);
207 ret = request_to_resume_http_download(stage);
208 if (ret != DA_RESULT_OK)
210 DA_LOG(Default, "Download Resume Successful for download id-%d", slot_id);
215 da_result_t resume_download(int dl_id)
217 da_result_t ret = DA_RESULT_OK;
218 int slot_id = DA_INVALID_ID;
220 DA_LOG_FUNC_START(Default);
222 ret = get_slot_id_for_dl_id(dl_id, &slot_id);
223 if (ret != DA_RESULT_OK)
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;
232 ret = __resume_download_with_slot_id(slot_id);
238 da_result_t send_user_noti_and_finish_download_flow(
239 int slot_id, char *installed_path, char *etag)
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;
245 DA_LOG_FUNC_START(Default);
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]);
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;
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;
266 case DOWNLOAD_STATE_PAUSED:
267 need_destroy_download_info = DA_TRUE;
271 DA_LOG(Default, "download state = %d", download_state);
275 if (need_destroy_download_info == DA_TRUE) {
276 destroy_download_info(slot_id);
278 DA_LOG_CRITICAL(Default, "download info is not destroyed");
284 da_bool_t is_valid_download_id(int dl_id)
287 da_bool_t ret = DA_TRUE;
288 int slot_id = DA_INVALID_ID;
290 DA_LOG_VERBOSE(Default, "[is_valid_download_id]download_id : %d", dl_id);
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");
301 if (DA_FALSE == is_valid_slot_id(slot_id)) {
302 DA_LOG_ERR(Default, "Download ID is not Valid");
306 if (GET_DL_THREAD_ID(slot_id) < 1) {
307 DA_LOG_ERR(Default, "Download thread is not alive");