Retry read() when getting EAGAIN
[platform/framework/web/download-provider.git] / agent / download-agent-client-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
19 #include "download-agent-client-mgr.h"
20
21 da_ret_t send_client_paused_info(da_info_t *da_info)
22 {
23         req_info_t *req_info = DA_NULL;
24         NULL_CHECK_RET(da_info);
25         req_info = da_info->req_info;
26         NULL_CHECK_RET(req_info);
27
28         if (da_info->is_cb_update && da_info->cb_info.paused_cb) {
29                 da_info->cb_info.paused_cb(da_info->da_id,
30                                 req_info->user_req_data, req_info->user_client_data);
31                 //DA_LOGV("id[%d]", da_info->da_id);
32         } else {
33                 DA_LOGV("No CB:id[%d]", da_info->da_id);
34         }
35
36         return DA_RESULT_OK;
37 }
38
39 da_ret_t send_client_update_dl_info(da_info_t *da_info)
40 {
41         download_info_t *info = DA_NULL;
42         file_info_t *file_info = DA_NULL;
43         http_info_t *http_info = DA_NULL;
44         req_info_t *req_info = DA_NULL;
45         NULL_CHECK_RET(da_info);
46         file_info = da_info->file_info;
47         NULL_CHECK_RET(file_info);
48         req_info = da_info->req_info;
49         NULL_CHECK_RET(req_info);
50         http_info = da_info->http_info;
51         NULL_CHECK_RET(http_info);
52
53         if (da_info->is_cb_update && da_info->cb_info.download_info_cb) {
54                 info = (download_info_t *)calloc(1, sizeof(download_info_t));
55                 if (!info)
56                         return DA_ERR_FAIL_TO_MEMALLOC;
57                 info->download_id = da_info->da_id;
58                 info->file_size = http_info->content_len_from_header;
59                 if (http_info->content_type_from_header)
60                         info->file_type = strdup(http_info->content_type_from_header);
61                 if (file_info->file_path)
62                         info->tmp_saved_path = strdup(file_info->file_path);
63                 if (file_info->pure_file_name)
64                         info->content_name = strdup(file_info->pure_file_name);
65                 if (http_info->etag_from_header) {
66                         info->etag = strdup(http_info->etag_from_header);
67                         //DA_SECURE_LOGI("etag[%s]", info->etag);
68                 }
69                 da_info->cb_info.download_info_cb(info,
70                                 req_info->user_req_data, req_info->user_client_data);
71                 //DA_LOGD("id[%d]", info->download_id);
72                 //DA_LOGI("id[%d]total_size[%lu]", info->download_id, info->file_size);
73                 //if (http_info->content_type_from_header)
74                         //DA_SECURE_LOGI("mime_type[%s]", http_info->content_type_from_header);
75         } else {
76                 DA_LOGI("No CB:id[%d]", da_info->da_id);
77         }
78         return DA_RESULT_OK;
79 }
80
81 da_ret_t send_client_update_progress_info(da_info_t *da_info)
82 {
83         file_info_t *file_info = DA_NULL;
84         req_info_t *req_info = DA_NULL;
85         NULL_CHECK_RET(da_info);
86         file_info = da_info->file_info;
87         NULL_CHECK_RET(file_info);
88         req_info = da_info->req_info;
89         NULL_CHECK_RET(req_info);
90
91         if (!file_info->is_updated)
92                 return DA_RESULT_OK;
93
94         if (da_info->is_cb_update && da_info->cb_info.progress_cb) {
95                 da_info->cb_info.progress_cb(da_info->da_id,
96                                 file_info->bytes_written_to_file,
97                                 req_info->user_req_data, req_info->user_client_data);
98                 //DA_LOGV("id[%d],size[%llu]", da_info->da_id,
99                 //              file_info->bytes_written_to_file);
100         } else {
101                 DA_LOGI("No CB:id[%d]", da_info->da_id);
102         }
103         file_info->is_updated = DA_FALSE;
104         return DA_RESULT_OK;
105 }
106
107 da_ret_t send_client_finished_info(da_info_t *da_info, int err)
108 {
109         finished_info_t *info = DA_NULL;
110         file_info_t *file_info = DA_NULL;
111         http_info_t *http_info = DA_NULL;
112         req_info_t *req_info = DA_NULL;
113         NULL_CHECK_RET(da_info);
114         file_info = da_info->file_info;
115         NULL_CHECK_RET(file_info);
116         req_info = da_info->req_info;
117         NULL_CHECK_RET(req_info);
118         http_info = da_info->http_info;
119         NULL_CHECK_RET(http_info);
120
121         if (da_info->is_cb_update && da_info->cb_info.finished_cb) {
122                 info = (finished_info_t *)calloc(1, sizeof(finished_info_t));
123                 if (!info)
124                         return DA_ERR_FAIL_TO_MEMALLOC;
125                 info->download_id = da_info->da_id;
126                 if (http_info->http_msg_response)
127                         info->http_status = http_info->http_msg_response->status_code;
128                 else
129                         DA_LOGE("http_msg_response is NULL");
130                 if (file_info->file_path)
131                         info->saved_path = strdup(file_info->file_path);
132                 if (http_info->etag_from_header)
133                         info->etag = strdup(http_info->etag_from_header);
134                 info->err = err;
135                 da_info->cb_info.finished_cb(info,
136                                 req_info->user_req_data, req_info->user_client_data);
137                 //DA_LOGD("id[%d]", info->download_id);
138                 //DA_LOGI("id[%d],err[%d], http_status[%d]", info->download_id,
139                                 //info->err, info->http_status);
140         } else {
141                 DA_LOGI("No CB:id[%d]", da_info->da_id);
142         }
143         return DA_RESULT_OK;
144 }
145