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