Apply tizen coding rule
[platform/framework/web/download-provider.git] / agent / include / download-agent-dl-info.h
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 #ifndef _DOWNLOAD_AGENT_INFO_H
18 #define _DOWNLOAD_AGENT_INFO_H
19
20 #include "download-agent-type.h"
21 #include "download-agent-interface.h"
22 #include "download-agent-pthread.h"
23
24 #include "curl/curl.h"
25
26 typedef struct {
27         CURL *curl;
28         pthread_mutex_t mutex;
29         da_bool_t is_paused;
30         da_bool_t is_cancel_reqeusted;
31 } http_msg_t;
32
33 typedef enum {
34         HTTP_STATE_READY_TO_DOWNLOAD = 0,
35         HTTP_STATE_REDIRECTED = 1,
36         HTTP_STATE_DOWNLOAD_REQUESTED = 2,
37         HTTP_STATE_DOWNLOAD_STARTED = 3,
38         HTTP_STATE_DOWNLOADING = 4,
39         HTTP_STATE_DOWNLOAD_FINISH = 5,
40         HTTP_STATE_REQUEST_CANCEL = 6,
41         HTTP_STATE_REQUEST_PAUSE = 7,
42         HTTP_STATE_REQUEST_RESUME = 8,
43         HTTP_STATE_CANCELED = 9,
44         HTTP_STATE_FAILED = 10,
45         HTTP_STATE_PAUSED = 11,
46         HTTP_STATE_RESUMED = 12,
47         HTTP_STATE_ABORTED = 13,
48         HTTP_STATE_WAIT_FOR_NET_ERR = 14,
49 } http_state_t;
50
51 typedef enum {
52         HTTP_METHOD_GET = 1,
53         HTTP_METHOD_HEAD,
54         HTTP_METHOD_POST
55 } http_method_t;
56
57 typedef struct {
58         char *url;
59         char **req_header;
60         int req_header_count;
61         char *install_path;
62         char *file_name;
63         char *etag;
64         char *temp_file_path;
65         char *pkg_name;
66         int network_bonding;
67         void *user_req_data;
68         void *user_client_data;
69 } req_info_t;
70
71 typedef enum {
72         HTTP_EVENT_GOT_HEADER = 0,
73         HTTP_EVENT_GOT_PACKET,
74         HTTP_EVENT_FINAL,
75 //      HTTP_EVENT_ABORT
76 } http_event_type_t;
77
78 typedef struct _http_header_options_t http_header_options_t;
79 struct _http_header_options_t {
80         char *field;
81         char *value;
82         http_header_options_t *next;
83 };
84
85 typedef struct _http_header_t http_header_t;
86 typedef http_header_t *http_msg_iter_t;
87 struct _http_header_t {
88         char *field;
89         char *value;
90         http_header_options_t *options;
91         char *raw_value; // raw string including options
92         http_header_t *next;
93 };
94
95 typedef struct {
96         char *http_method;
97         char *url;
98         http_header_t *head;
99         char *http_body;
100 } http_msg_request_t;
101
102
103 typedef struct {
104         int status_code;
105         http_header_t *head;
106 } http_msg_response_t;
107
108 typedef struct {
109         http_event_type_t type;
110         char *body;
111         int body_len;
112 #ifdef _RAF_SUPPORT
113         da_size_t received_len;
114 #endif
115         int status_code;
116         int error;
117 } http_raw_data_t;
118
119 typedef void (*http_update_cb) (http_raw_data_t *data, void *user_param);
120
121 typedef struct {
122         char *location_url;
123         http_state_t state;
124         pthread_mutex_t mutex_state;
125         pthread_mutex_t mutex_http;
126         pthread_cond_t cond_http;
127         http_msg_request_t *http_msg_request;
128         http_msg_response_t *http_msg_response;
129         http_method_t http_method;
130         http_msg_t *http_msg;
131         char *proxy_addr;
132         char *content_type_from_header;
133         char *file_name_from_header;
134         da_size_t content_len_from_header;
135         char *etag_from_header;
136         int error_code; // for error value for http abort.
137         da_size_t total_size;
138 #ifdef _RAF_SUPPORT
139         da_bool_t is_raf_mode_confirmed;
140 #endif
141         http_update_cb update_cb;
142 } http_info_t;
143
144 typedef struct {
145         void *file_handle;
146         char *pure_file_name;
147         char *extension;
148         char *file_path; /* malloced in set_file_path_for_final_saving */
149         char *mime_type;// For drm converting
150         char *buffer;
151         da_size_t buffer_len;
152         da_size_t file_size; /* http header's Content-Length has higher priority than DD's <size> */
153         da_size_t bytes_written_to_file; /* The file size to be written at actual file */
154 #ifdef _RAF_SUPPORT
155         da_size_t file_size_of_temp_file; /* If the temporary file is existed, the file size of it */
156 #endif
157         da_bool_t is_updated; /* The flag for updating progress event only if the data is wrriten to file not buffer */
158 } file_info_t;
159
160 typedef struct {
161         int da_id;
162         int tid;
163         pthread_t thread_id;
164         http_info_t *http_info;
165         file_info_t *file_info;
166         req_info_t *req_info;
167         da_cb_t cb_info;
168         da_bool_t is_cb_update;
169         int update_time;
170 } da_info_t;
171
172 extern da_info_t *da_info_list[DA_MAX_ID];
173
174 #define GET_STATE_MUTEX(INFO) (INFO->mutex_state)
175 #define GET_STATE(INFO) (INFO->state)
176 #define CHANGE_STATE(STATE, INFO) {\
177         DA_MUTEX_LOCK(&GET_STATE_MUTEX(INFO));\
178         GET_STATE(INFO) = STATE;\
179         DA_LOGV("Changed state[%d]", GET_STATE(INFO));\
180         DA_MUTEX_UNLOCK(&GET_STATE_MUTEX(INFO));\
181         }
182
183 da_ret_t init_openssl_locks(void);
184 da_ret_t deinit_openssl_locks(void);
185 da_ret_t get_available_da_id(int *available_id);
186 da_ret_t copy_user_input_data(da_info_t *da_info, const char *url,
187                 req_data_t *ext_data, da_cb_t *da_cb_data);
188 da_bool_t is_valid_download_id(int id);
189 void destroy_da_info(int id);
190 void destroy_da_info_list(void);
191 da_ret_t get_da_info_with_da_id(int id, da_info_t **out_info);
192 da_ret_t init_http_msg_t(http_msg_t **http_msg);
193 void destroy_http_msg_t(http_msg_t *http_msg);
194 void reset_http_info(http_info_t *http_info);
195 void reset_http_info_for_resume(http_info_t *http_info);
196 void destroy_http_info(http_info_t *http_info);
197 void destroy_file_info(file_info_t *file_info);
198
199 #endif  /* _DOWNLOAD_AGENT_INFO_H */