Tizen 2.1 base
[platform/framework/web/download-provider.git] / src / agent / download-agent-interface.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-interface.h"
18 #include "download-agent-debug.h"
19 #include "download-agent-utils.h"
20 #include "download-agent-http-mgr.h"
21 #include "download-agent-http-misc.h"
22 #include "download-agent-client-mgr.h"
23 #include "download-agent-dl-mgr.h"
24 #include "download-agent-basic.h"
25 #include "download-agent-file.h"
26
27 int da_init(
28         da_client_cb_t *da_client_callback)
29 {
30         da_result_t ret = DA_RESULT_OK;
31
32         DA_LOG_FUNC_START(Default);
33
34         if (!da_client_callback) {
35                 ret = DA_ERR_INVALID_ARGUMENT;
36                 return ret;
37         }
38
39         ret = init_log_mgr();
40         if (ret != DA_RESULT_OK)
41                 goto ERR;
42
43         ret = init_client_app_mgr();
44         if (ret != DA_RESULT_OK)
45                 goto ERR;
46
47         ret = reg_client_app(da_client_callback);
48         if (ret != DA_RESULT_OK)
49                 goto ERR;
50
51         ret = init_http_mgr();
52         if (ret != DA_RESULT_OK)
53                 goto ERR;
54
55         ret = init_download_mgr();
56         if (ret != DA_RESULT_OK)
57                 goto ERR;
58
59         ret = create_saved_dir();
60         if (ret != DA_RESULT_OK)
61                 goto ERR;
62
63 ERR:
64         if (DA_RESULT_OK != ret)
65                 da_deinit();
66
67         DA_LOG_CRITICAL(Default, "Return ret = %d", ret);
68
69         return ret;
70 }
71
72 /* TODO:: deinit should clean up all the clients... */
73 int da_deinit()
74 {
75         da_result_t ret = DA_RESULT_OK;
76
77         DA_LOG_FUNC_START(Default);
78
79         deinit_http_mgr();
80         deinit_download_mgr();
81         /* Do not clean temporary download path
82          * The client can resume or restart download with temporary file in case of failed download.
83          */
84         dereg_client_app();
85         DA_LOG(Default, "====== da_deinit EXIT =====");
86
87         return ret;
88 }
89
90 int da_start_download(
91         const char *url,
92         int *download_id)
93 {
94         da_result_t ret = DA_RESULT_OK;
95
96         DA_LOG_FUNC_START(Default);
97
98         *download_id = DA_INVALID_ID;
99
100         if (DA_FALSE == is_valid_url(url, &ret))
101                 goto ERR;
102
103         DA_LOG(Default, "url = %s", url);
104
105         ret = start_download(url, download_id);
106         if (ret != DA_RESULT_OK)
107                 goto ERR;
108
109 ERR:
110         DA_LOG_CRITICAL(Default, "Return: Dl req id = %d, ret = %d", *download_id, ret);
111         return ret;
112 }
113
114 int da_start_download_with_extension(
115         const char              *url,
116         extension_data_t *extension_data,
117         int     *download_id
118 )
119 {
120         da_result_t ret = DA_RESULT_OK;
121         int req_header_count = 0;
122         int i = 0;
123
124         DA_LOG_FUNC_START(Default);
125
126         *download_id = DA_INVALID_ID;
127
128         if (DA_FALSE == is_valid_url(url, &ret))
129                 goto ERR;
130
131         DA_LOG(Default, "url = %s", url);
132
133         if (ret != DA_RESULT_OK)
134                 goto ERR;
135         if (!extension_data) {
136                 ret = DA_ERR_INVALID_ARGUMENT;
137                 goto ERR;
138         }
139
140         if (extension_data->request_header_count > 0) {
141                 DA_LOG_VERBOSE(Default, "input request_header_count = [%d]",
142                         extension_data->request_header_count);
143                 for (i = 0; i < extension_data->request_header_count; i++) {
144                         if (extension_data->request_header[i]) {
145                                 req_header_count++;
146                                 DA_LOG_VERBOSE(Default, "request_header = [%s]",
147                                         extension_data->request_header[i]);
148                         }
149                 }
150                 DA_LOG(Default, "actual request_header_count = [%d]", req_header_count);
151                 if (extension_data->request_header_count != req_header_count) {
152                         DA_LOG_ERR(Default, "Request header count is not matched with number of request header array");
153                         extension_data->request_header = NULL;
154                         extension_data->request_header_count = 0;
155                         ret = DA_ERR_INVALID_ARGUMENT;
156                         goto ERR;
157                 }
158         }
159
160         if (extension_data->install_path) {
161                 if (!is_dir_exist(extension_data->install_path))
162                         return DA_ERR_INVALID_INSTALL_PATH;
163                 DA_LOG_VERBOSE(Default, "install_path = [%s]", extension_data->install_path);
164         }
165
166         if (extension_data->file_name)
167                 DA_LOG_VERBOSE(Default, "file_name = [%s]", extension_data->file_name);
168         if (extension_data->temp_file_path)
169                 DA_LOG_VERBOSE(Default, "temp_file_path = [%s]", extension_data->temp_file_path);
170         if (extension_data->etag)
171                 DA_LOG_VERBOSE(Default, "etag = [%s]", extension_data->etag);
172
173         if (extension_data->user_data)
174                 DA_LOG_VERBOSE(Default, "user_data = [%p]", extension_data->user_data);
175
176         ret = start_download_with_extension(url, download_id, extension_data);
177
178 ERR:
179         DA_LOG_CRITICAL(Default, "Return: Dl req id = %d, ret = %d", *download_id, ret);
180         return ret;
181 }
182
183 int da_cancel_download(int download_id)
184 {
185         da_result_t ret = DA_RESULT_OK;
186
187         DA_LOG_VERBOSE(Default, "Cancel for dl_id = %d", download_id);
188
189         ret = cancel_download(download_id);
190
191         DA_LOG_CRITICAL(Default, "Return: Cancel id = %d, ret = %d", download_id, ret);
192         return ret;
193 }
194
195 int da_suspend_download(int download_id)
196 {
197         da_result_t ret = DA_RESULT_OK;
198
199         DA_LOG_VERBOSE(Default, "Suspend for dl_id = %d", download_id);
200
201         ret = suspend_download(download_id, DA_TRUE);
202
203         DA_LOG_CRITICAL(Default, "Return: Suspend id = %d, ret = %d", download_id, ret);
204         return ret;
205 }
206
207 int da_suspend_download_without_update(int download_id)
208 {
209         da_result_t ret = DA_RESULT_OK;
210
211         DA_LOG_VERBOSE(Default, "Suspend for dl_id = %d", download_id);
212
213         ret = suspend_download(download_id, DA_FALSE);
214
215         DA_LOG_CRITICAL(Default, "Return: Suspend id = %d, ret = %d", download_id, ret);
216         return ret;
217 }
218
219
220 int da_resume_download(int download_id)
221 {
222         da_result_t ret = DA_RESULT_OK;
223
224         DA_LOG_VERBOSE(Default, "Resume for dl_id = %d", download_id);
225
226         ret = resume_download(download_id);
227
228         DA_LOG_CRITICAL(Default, "Return: Resume id = %d, ret = %d", download_id, ret);
229         return ret;
230 }
231
232 int da_is_valid_download_id(int download_id)
233 {
234         da_bool_t ret = DA_FALSE;
235         ret = is_valid_download_id(download_id);
236         return ret;
237 }