summaryrefslogtreecommitdiff
path: root/src/agent/download-agent-dl-mgr.c
blob: fc654d4437053f254ec2042c5f9354a1a44ca788 (plain)
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * Download Agent
 *
 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Jungki Kwak <jungki.kwak@samsung.com>, Keunsoon Lee <keunsoon.lee@samsung.com>
 *
 * 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.
 *
 * @file		download-agent-dl-mgr.c
 * @brief		common functions for oma and direct download
 * @author		Keunsoon Lee(keunsoon.lee@samsung.com)
 * @author		Jungki Kwak(jungki.kwak@samsung.com)
 ***/

#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-installation.h"
#include "download-agent-file.h"
#include "download-agent-plugin-conf.h"


static da_result_t __cancel_download_with_download_id(int download_id);
static da_result_t __suspend_download_with_download_id(int download_id);


da_result_t requesting_download(stage_info *stage)
{
	da_result_t ret = DA_RESULT_OK;
	req_dl_info *request_session = DA_NULL;
	download_state_t download_state = DA_INVALID_ID;

	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;
	}

	_da_thread_mutex_lock (&mutex_download_state[GET_STAGE_DL_ID(stage)]);
	download_state = GET_DL_STATE_ON_STAGE(stage);
	_da_thread_mutex_unlock (&mutex_download_state[GET_STAGE_DL_ID(stage)]);

	/* Ignore  install content process if the download is failed or paused or canceled */
	if (download_state == DOWNLOAD_STATE_PAUSED ||
		download_state == DOWNLOAD_STATE_CANCELED) {
		return ret;
	}

ERR:
	return ret;
}

da_result_t handle_after_download(stage_info *stage)
{
	da_result_t ret = DA_RESULT_OK;

	DA_LOG_FUNC_START(Default);

	if (DA_TRUE == is_this_client_manual_download_type()) {
		if (DA_RESULT_OK == ret) {
			CHANGE_DOWNLOAD_STATE(DOWNLOAD_STATE_FINISH,stage);
		}
		return ret;
	}
	CHANGE_DOWNLOAD_STATE(DOWNLOAD_STATE_READY_TO_INSTAL,stage);

	return ret;
}

da_result_t process_install(stage_info *stage)
{
	da_result_t ret = DA_RESULT_OK;

	DA_LOG_FUNC_START(Default);

	if (DA_NULL == stage)
		return DA_ERR_INVALID_ARGUMENT;

	/* install process. */
	ret = install_content(stage);

	if (DA_RESULT_OK == ret) {
		int download_id = GET_STAGE_DL_ID(stage);
		char *content_type = DA_NULL;

		if (GET_CONTENT_STORE_CONTENT_TYPE(GET_STAGE_CONTENT_STORE_INFO(stage))) {
			content_type = strdup(
		                GET_CONTENT_STORE_CONTENT_TYPE(GET_STAGE_CONTENT_STORE_INFO(stage)));
		} else {
			/* Fixme : Is Content-Type on HTTP header mandatory? */
			DA_LOG_ERR(Default, "no content type!");
			ret = DA_ERR_MISMATCH_CONTENT_TYPE;
			return ret;
		}

		/* update installed path */
		send_client_update_downloading_info(
		        download_id,
		        GET_DL_REQ_ID(download_id),
		        GET_CONTENT_STORE_CURRENT_FILE_SIZE(GET_STAGE_CONTENT_STORE_INFO(stage)),
		        GET_CONTENT_STORE_ACTUAL_FILE_NAME(GET_STAGE_CONTENT_STORE_INFO(stage))
				);

		if (content_type) {
			free(content_type);
			content_type = DA_NULL;
		}

		return DA_RESULT_OK;
	}
	return ret;
}

static da_result_t __cancel_download_with_download_id(int download_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[download_id]);
	download_state = GET_DL_STATE_ON_ID(download_id);
	DA_LOG(Default, "download_state = %d", GET_DL_STATE_ON_ID(download_id));
	if (download_state == DOWNLOAD_STATE_IDLE) {
	/* FIXME later : It is better to check and exit itself at download thread */
		pthread_cancel(GET_DL_THREAD_ID(download_id));
	} else if (download_state >= DOWNLOAD_STATE_READY_TO_INSTAL) {
		DA_LOG_CRITICAL(Default, "Already download is completed. Do not send cancel request");
		_da_thread_mutex_unlock (&mutex_download_state[download_id]);
		return ret;
	}
	_da_thread_mutex_unlock (&mutex_download_state[download_id]);

	if (ret != DA_RESULT_OK)
		goto ERR;

	stage = GET_DL_CURRENT_STAGE(download_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", download_id);
ERR:
	return ret;
}

da_result_t cancel_download(int dl_req_id)
{
	da_result_t ret = DA_RESULT_OK;

	int download_id = DA_INVALID_ID;

	DA_LOG_FUNC_START(Default);

	if ((dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS) ||
			(dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI)) {
		DA_LOG(Default, "All download items will be cancelled");
		ret = cancel_download_all(dl_req_id);
		return ret;
	}

	ret = get_download_id_for_dl_req_id(dl_req_id, &download_id);
	if (ret != DA_RESULT_OK) {
		DA_LOG_ERR(Default, "dl req ID is not Valid");
		goto ERR;
	}

	if (DA_FALSE == is_valid_dl_ID(download_id)) {
		DA_LOG_ERR(Default, "Download ID is not Valid");
		ret = DA_ERR_INVALID_ARGUMENT;
		goto ERR;
	}

	ret = __cancel_download_with_download_id(download_id);

ERR:
	return ret;

}

da_result_t cancel_download_all(int dl_req_id)
{
	da_result_t ret = DA_RESULT_OK;

	int download_id = DA_INVALID_ID;
	int iter = 0;
	da_bool_t b_any_flag_on = DA_FALSE;

	state_watcher_t *state_watcher = DA_NULL;

	DA_LOG_FUNC_START(Default);

	if (dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI) {
		state_watcher = &(download_mgr.state_watcher);

		_da_thread_mutex_lock(&(state_watcher->mutex));
		state_watcher->type = STATE_WATCHER_TYPE_CANCEL;
		state_watcher->is_progressing_to_all = DA_TRUE;
		_da_thread_mutex_unlock(&(state_watcher->mutex));
	}

	for (iter = 0; iter < DA_MAX_DOWNLOAD_ID; iter++) {
		if (download_mgr.download_info[iter].is_using == DA_TRUE) {
			download_id = iter;

			if (dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI) {
				state_watcher_flag_ON_for_download_id(state_watcher,
					download_id);
			}
			b_any_flag_on = DA_TRUE;

			/* ToDo: retry if fail, check download_state, don't set flag for state_watcher if state is invalid */
			ret = __cancel_download_with_download_id(download_id);
			/*if (ret == DA_RESULT_OK) {
				state_watcher_flag_ON_for_download_id(
				        state_watcher, download_id);
				b_any_flag_on = DA_TRUE;
			}*/
		}

	}

	if (dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI) {
		_da_thread_mutex_lock(&(state_watcher->mutex));
		state_watcher->is_progressing_to_all = DA_FALSE;
		_da_thread_mutex_unlock(&(state_watcher->mutex));
	}

	if (b_any_flag_on == DA_FALSE) {
		DA_LOG_ERR(Default, "There is no item to be able to cancel.");
		ret = DA_ERR_INVALID_DL_REQ_ID;
	}

	return ret;
}

static da_result_t __suspend_download_with_download_id(int download_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[download_id]);
	download_state = GET_DL_STATE_ON_ID(download_id);
	DA_LOG(Default, "download_state = %d", GET_DL_STATE_ON_ID(download_id));
	_da_thread_mutex_unlock (&mutex_download_state[download_id]);

	if (ret != DA_RESULT_OK)
		goto ERR;

	stage = GET_DL_CURRENT_STAGE(download_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", download_id);
ERR:
	return ret;
}

da_result_t suspend_download(int dl_req_id)
{
	da_result_t ret = DA_RESULT_OK;
	int download_id = DA_INVALID_ID;

	DA_LOG_FUNC_START(Default);

	if ((dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS) ||
			(dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI)) {
		DA_LOG(Default, "All download items will be suspended");
		ret = suspend_download_all(dl_req_id);
		return ret;
	}

	ret = get_download_id_for_dl_req_id(dl_req_id, &download_id);
	if (ret != DA_RESULT_OK) {
		DA_LOG_ERR(Default, "dl req ID is not Valid");
		goto ERR;
	}

	if (DA_FALSE == is_valid_dl_ID(download_id)) {
		DA_LOG_ERR(Default, "Download ID is not Valid");
		ret = DA_ERR_INVALID_ARGUMENT;
		goto ERR;
	}

	ret = __suspend_download_with_download_id(download_id);

ERR:
	return ret;

}

da_result_t suspend_download_all(int dl_req_id)
{
	da_result_t ret = DA_RESULT_OK;

	int download_id = DA_INVALID_ID;
	int iter = 0;
	da_bool_t b_any_flag_on = DA_FALSE;

	state_watcher_t *state_watcher = DA_NULL;

	DA_LOG_FUNC_START(Default);

	if (dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI) {
		state_watcher = &(download_mgr.state_watcher);

		_da_thread_mutex_lock(&(state_watcher->mutex));
		state_watcher->type = STATE_WATCHER_TYPE_SUSPEND;
		state_watcher->is_progressing_to_all = DA_TRUE;
		_da_thread_mutex_unlock(&(state_watcher->mutex));
	}

	for (iter = 0; iter < DA_MAX_DOWNLOAD_ID; iter++) {
		if (download_mgr.download_info[iter].is_using == DA_TRUE) {
			download_id = iter;
			/* ToDo: retry if fail, check download_state, don't set flag for state_watcher if state is invalid */
			ret = __suspend_download_with_download_id(download_id);
			if (ret == DA_RESULT_OK) {
				if (dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI) {
					state_watcher_flag_ON_for_download_id(
				        state_watcher, download_id);
				}
				b_any_flag_on = DA_TRUE;
			}
		}

	}

	if (dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI) {
		_da_thread_mutex_lock(&(state_watcher->mutex));
		state_watcher->is_progressing_to_all = DA_FALSE;
		_da_thread_mutex_unlock(&(state_watcher->mutex));
	}

	if (b_any_flag_on == DA_FALSE) {
		DA_LOG_ERR(Default, "There is no item to be able to suspend.");
		ret = DA_ERR_CANNOT_SUSPEND;
	}

	return ret;
}

static da_result_t __resume_download_with_download_id(int download_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[download_id]);
	download_state = GET_DL_STATE_ON_ID(download_id);
	DA_LOG(Default, "download_state = %d", GET_DL_STATE_ON_ID(download_id));
	_da_thread_mutex_unlock (&mutex_download_state[download_id]);

	if (ret != DA_RESULT_OK)
		goto ERR;

	stage = GET_DL_CURRENT_STAGE(download_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", download_id);
ERR:
	return ret;
}

da_result_t resume_download(int dl_req_id)
{
	da_result_t ret = DA_RESULT_OK;
	int download_id = DA_INVALID_ID;

	DA_LOG_FUNC_START(Default);

	if ((dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS) ||
			(dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI)) {
		DA_LOG(Default, "All download items will be resumed");
		ret = resume_download_all(dl_req_id);
		return ret;
	}

	ret = get_download_id_for_dl_req_id(dl_req_id, &download_id);
	if (ret != DA_RESULT_OK)
		goto ERR;

	if (DA_FALSE == is_valid_dl_ID(download_id)) {
		DA_LOG_ERR(Default, "Download ID is not Valid");
		ret = DA_ERR_INVALID_ARGUMENT;
		goto ERR;
	}

	ret = __resume_download_with_download_id(download_id);

ERR:
	return ret;
}

da_result_t resume_download_all(int dl_req_id)
{
	da_result_t ret = DA_RESULT_OK;

	int download_id = DA_INVALID_ID;
	int iter = 0;
	da_bool_t b_any_flag_on = DA_FALSE;

	state_watcher_t *state_watcher = DA_NULL;

	DA_LOG_FUNC_START(Default);

	if (dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI) {
		state_watcher = &(download_mgr.state_watcher);

		_da_thread_mutex_lock(&(state_watcher->mutex));
		state_watcher->type = STATE_WATCHER_TYPE_RESUME;
		state_watcher->is_progressing_to_all = DA_TRUE;
		_da_thread_mutex_unlock(&(state_watcher->mutex));
	}

	for (iter = 0; iter < DA_MAX_DOWNLOAD_ID; iter++) {
		if (download_mgr.download_info[iter].is_using == DA_TRUE) {
			download_id = iter;

			if (dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI) {
				state_watcher_flag_ON_for_download_id(state_watcher,
					download_id);
			}
			b_any_flag_on = DA_TRUE;

			/* ToDo: retry if fail, check download_state, don't set flag for state_watcher if state is invalid */
			ret = __resume_download_with_download_id(download_id);
			/*if (ret == DA_RESULT_OK) {
				state_watcher_flag_ON_for_download_id(
				        state_watcher, download_id);
				b_any_flag_on = DA_TRUE;
			}*/
		}

	}

	if (dl_req_id == DA_DOWNLOAD_REQ_ID_FOR_ALL_ITEMS_WITH_UNIFIED_NOTI) {
		_da_thread_mutex_lock(&(state_watcher->mutex));
		state_watcher->is_progressing_to_all = DA_FALSE;
		_da_thread_mutex_unlock(&(state_watcher->mutex));
	}

	if (b_any_flag_on == DA_FALSE) {
		DA_LOG(Default, "There is no item to be able to cancel.");
		ret = DA_ERR_INVALID_DL_REQ_ID;
	}

	return ret;
}

da_result_t send_user_noti_and_finish_download_flow(int download_id)
{
	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[download_id]);
	download_state = GET_DL_STATE_ON_ID(download_id);
	DA_LOG(Default, "state = %d", download_state);
	_da_thread_mutex_unlock (&mutex_download_state[download_id]);

	switch (download_state) {
	case DOWNLOAD_STATE_FINISH:
		send_client_da_state(download_id, DA_STATE_FINISHED,
				DA_RESULT_OK);
		need_destroy_download_info = DA_TRUE;
		break;
	case DOWNLOAD_STATE_CANCELED:
		send_client_da_state(download_id, DA_STATE_CANCELED,
				DA_RESULT_OK);
		need_destroy_download_info = DA_TRUE;
		break;
	default:
		DA_LOG(Default, "download state = %d", download_state);
		break;
	}

	if (need_destroy_download_info == DA_TRUE) {
		destroy_download_info(download_id);
	} else {
		DA_LOG_CRITICAL(Default, "download info is not destroyed");
	}

	return ret;
}