Tizen 2.1 base
[platform/framework/web/download-provider.git] / src / agent / include / download-agent-dl-info-util.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_Dl_Info_Util_H
18 #define _Download_Agent_Dl_Info_Util_H
19
20 #include "download-agent-type.h"
21 #include "download-agent-http-queue.h"
22 #include "download-agent-utils-dl-id-history.h"
23
24 #define DA_MAX_DOWNLOAD_ID      DA_MAX_DOWNLOAD_REQ_AT_ONCE
25 #define DA_MAX_TYPE_COUNT       10
26
27 #define DOWNLOAD_NOTIFY_LIMIT (1024*32) //bytes
28 extern pthread_mutex_t mutex_download_state[];
29
30 typedef enum {
31         DOWNLOAD_STATE_IDLE = 0,
32         DOWNLOAD_STATE_NEW_DOWNLOAD = 10, /* stage */
33         DOWNLOAD_STATE_FINISH = 50, /* stage */
34         DOWNLOAD_STATE_PAUSED = 60, /* http */
35         DOWNLOAD_STATE_CANCELED = 70, /* http */
36         DOWNLOAD_STATE_ABORTED = 80 /* satge */
37 } download_state_t;
38
39 typedef enum {
40         HTTP_STATE_READY_TO_DOWNLOAD = 0,
41         HTTP_STATE_REDIRECTED = 1,
42         HTTP_STATE_DOWNLOAD_REQUESTED = 2,
43         HTTP_STATE_DOWNLOAD_STARTED = 3,
44         HTTP_STATE_DOWNLOADING = 4,
45         HTTP_STATE_DOWNLOAD_FINISH = 5,
46         HTTP_STATE_REQUEST_CANCEL = 6,
47         HTTP_STATE_REQUEST_PAUSE = 7,
48         HTTP_STATE_REQUEST_RESUME = 8,
49         HTTP_STATE_CANCELED = 9,
50         HTTP_STATE_PAUSED = 10,
51         HTTP_STATE_RESUMED = 11,
52         HTTP_STATE_ABORTED = 12,
53 } http_state_t;
54
55 typedef struct _client_input_basic_t {
56         char *req_url;
57         char **user_request_header;
58         int user_request_header_count;
59 } client_input_basic_t;
60
61 typedef struct _client_input_t {
62         void *user_data;
63         char *install_path;
64         char *file_name;
65         char *etag;
66         char *temp_file_path;
67         client_input_basic_t client_input_basic;
68 } client_input_t;
69
70 typedef struct _download_thread_input {
71         int slot_id;
72         client_input_t *client_input;
73 } download_thread_input;
74
75 typedef struct _source_info_basic_t {
76         int dl_id;
77         char *url;
78         char **user_request_header;
79         int user_request_header_count;
80 } source_info_basic_t;
81
82 typedef struct _source_info_t {
83         union _source_info_type {
84                 source_info_basic_t *source_info_basic;
85         } source_info_type;
86 } source_info_t;
87
88 #define GET_SOURCE_TYPE(SOURCE)                 ((SOURCE)->source_type)
89 #define GET_SOURCE_BASIC(SOURCE)                ((SOURCE)->source_info_type.source_info_basic)
90 #define GET_SOURCE_BASIC_URL(SOURCE)            (GET_SOURCE_BASIC(SOURCE)->url)
91
92 typedef struct _req_dl_info {
93         http_info_t http_info;
94
95         /* This is just pointer assignment from stage source info. */
96         char *destination_url;
97         /* The location url is assigned here in case of redirection.
98          * At this time, the pointer should be freed. */
99         char *location_url;
100         char **user_request_header;
101         int user_request_header_count;
102         char *user_request_etag;
103         char *user_request_temp_file_path;
104
105         http_state_t http_state;
106         pthread_mutex_t mutex_http_state;
107
108         da_result_t result;
109         /*************** will be depreciated ***********************/
110         /* ToDo : previous http_info should be saved in case of pause */
111         char *content_type_from_header; /* calloced in set hdr fiels on download info */
112         int content_len_from_header;
113         char *etag_from_header;
114
115         unsigned long int downloaded_data_size;
116
117         int invloved_transaction_id;
118 } req_dl_info;
119
120 #define GET_REQUEST_HTTP_RESULT(REQUEST)                (REQUEST->result)
121 #define GET_REQUEST_HTTP_TRANS_ID(REQUEST)      (REQUEST->invloved_transaction_id)
122 #define GET_REQUEST_HTTP_REQ_URL(REQUEST)               (REQUEST->destination_url)
123 #define GET_REQUEST_HTTP_REQ_LOCATION(REQUEST)                  (REQUEST->location_url)
124 #define GET_REQUEST_HTTP_USER_REQUEST_HEADER(REQUEST)           (REQUEST->user_request_header)
125 #define GET_REQUEST_HTTP_USER_REQUEST_HEADER_COUNT(REQUEST)             (REQUEST->user_request_header_count)
126 #define GET_REQUEST_HTTP_USER_REQUEST_ETAG(REQUEST)             (REQUEST->user_request_etag)
127 #define GET_REQUEST_HTTP_USER_REQUEST_TEMP_FILE_PATH(REQUEST)           (REQUEST->user_request_temp_file_path)
128 #define GET_REQUEST_HTTP_HDR_ETAG(REQUEST)      (REQUEST->etag_from_header)
129 #define GET_REQUEST_HTTP_HDR_CONT_TYPE(REQUEST) (REQUEST->content_type_from_header)
130 #define GET_REQUEST_HTTP_HDR_CONT_LEN(REQUEST)  (REQUEST->content_len_from_header)
131 #define GET_REQUEST_HTTP_CONTENT_OFFSET(REQUEST)(REQUEST->downloaded_data_size)
132 #define GET_REQUEST_HTTP_MUTEX_HTTP_STATE(STAGE)                (GET_STAGE_TRANSACTION_INFO(STAGE)->mutex_http_state)
133 #define GET_HTTP_STATE_ON_STAGE(STAGE)  (GET_STAGE_TRANSACTION_INFO(STAGE)->http_state)
134 #define CHANGE_HTTP_STATE(STATE,STAGE) {\
135         _da_thread_mutex_lock(&(GET_REQUEST_HTTP_MUTEX_HTTP_STATE(STAGE)));\
136         GET_HTTP_STATE_ON_STAGE(STAGE) = STATE;\
137         DA_LOG_CRITICAL(Default, "Changed http_state to - [%d] ", GET_HTTP_STATE_ON_STAGE(STAGE));\
138         _da_thread_mutex_unlock(&(GET_REQUEST_HTTP_MUTEX_HTTP_STATE(STAGE)));\
139 }
140
141 typedef struct _file_info {
142         void *file_handle;
143         char *pure_file_name;
144         char *extension;
145         char *file_name_final; /* malloced in set_file_path_for_final_saving */
146         char *content_type; /* malloced in make file info. */
147         char *add_to_buffer;
148         unsigned int file_size; /* http header's Content-Length has higher priority than DD's <size> */
149         unsigned int total_bytes_written_to_file; /* current written file size */
150         unsigned int bytes_written_to_file;
151         unsigned int current_buffer_len;
152 } file_info;
153
154 #define GET_CONTENT_STORE_PURE_FILE_NAME(FILE_CNTXT) (FILE_CNTXT)->pure_file_name
155 #define GET_CONTENT_STORE_EXTENSION(FILE_CNTXT) (FILE_CNTXT)->extension
156 #define GET_CONTENT_STORE_ACTUAL_FILE_NAME(FILE_CNTXT) (FILE_CNTXT)->file_name_final
157 #define GET_CONTENT_STORE_FILE_HANDLE(FILE_CNTXT) (FILE_CNTXT)->file_handle
158 #define GET_CONTENT_STORE_FILE_SIZE(FILE_CNTXT) (FILE_CNTXT)->file_size
159 #define GET_CONTENT_STORE_CURRENT_FILE_SIZE(FILE_CNTXT) (FILE_CNTXT)->total_bytes_written_to_file
160 #define IS_CONTENT_STORE_FILE_BYTES_WRITTEN_TO_FILE(FILE_CNTXT) (FILE_CNTXT)->bytes_written_to_file
161 #define GET_CONTENT_STORE_FILE_BUFFER(FILE_CNTXT) (FILE_CNTXT)->add_to_buffer
162 #define GET_CONTENT_STORE_FILE_BUFF_LEN(FILE_CNTXT) ((FILE_CNTXT)->current_buffer_len)
163 #define GET_CONTENT_STORE_CONTENT_TYPE(FILE_CNTXT) (FILE_CNTXT)->content_type
164
165 typedef struct _stage_info {
166         int dl_id;
167         source_info_t dl_request;
168         req_dl_info dl_tansaction_context;
169         file_info dl_content_storage;
170         struct _stage_info *next_stage_info;
171 } stage_info;
172
173 #define GET_STAGE_DL_ID(STAGE)                                  ((STAGE)->dl_id)
174 #define GET_STAGE_SOURCE_INFO(STAGE)                    (&((STAGE)->dl_request))
175 #define GET_STAGE_TRANSACTION_INFO(STAGE)               (&((STAGE)->dl_tansaction_context))
176 #define GET_STAGE_CONTENT_STORE_INFO(STAGE)             (&((STAGE)->dl_content_storage))
177 #define GET_STAGE_INSTALLATION_INFO(STAGE)              (&((STAGE)->post_dl_context))
178
179 typedef struct {
180         da_bool_t is_using;
181         int slot_id;
182         int dl_id;
183         pthread_t active_dl_thread_id;
184         download_state_t state;
185         stage_info *download_stage_data;
186         queue_t queue;
187         int http_status;
188         da_bool_t enable_pause_update;
189         // FIXME have client_input itself, not to have each of them
190         char *user_install_path;
191         char *user_file_name;
192         char *user_etag;
193         char *user_temp_file_path;
194         void *user_data;
195 } dl_info_t;
196
197 #define GET_DL_THREAD_ID(ID)            (download_mgr.dl_info[ID].active_dl_thread_id)
198 #define GET_DL_STATE_ON_ID(ID)          (download_mgr.dl_info[ID].state)
199 #define GET_DL_STATE_ON_STAGE(STAGE)    (GET_DL_STATE_ON_ID(GET_STAGE_DL_ID(STAGE)))
200 #define GET_DL_CURRENT_STAGE(ID)        (download_mgr.dl_info[ID].download_stage_data)
201 #define GET_DL_ID(ID)           (download_mgr.dl_info[ID].dl_id)
202 #define GET_DL_QUEUE(ID)                &(download_mgr.dl_info[ID].queue)
203 #define GET_DL_ENABLE_PAUSE_UPDATE(ID)          (download_mgr.dl_info[ID].enable_pause_update)
204 #define GET_DL_USER_INSTALL_PATH(ID)            (download_mgr.dl_info[ID].user_install_path)
205 #define GET_DL_USER_FILE_NAME(ID)               (download_mgr.dl_info[ID].user_file_name)
206 #define GET_DL_USER_ETAG(ID)            (download_mgr.dl_info[ID].user_etag)
207 #define GET_DL_USER_TEMP_FILE_PATH(ID)          (download_mgr.dl_info[ID].user_temp_file_path)
208 #define GET_DL_USER_DATA(ID)            (download_mgr.dl_info[ID].user_data)
209 #define IS_THIS_DL_ID_USING(ID) (download_mgr.dl_info[ID].is_using)
210
211 #define CHANGE_DOWNLOAD_STATE(STATE,STAGE) {\
212         _da_thread_mutex_lock (&mutex_download_state[GET_STAGE_DL_ID(STAGE)]);\
213         GET_DL_STATE_ON_STAGE(STAGE) = STATE;\
214         DA_LOG_CRITICAL(Default, "Changed download_state to - [%d] ", GET_DL_STATE_ON_STAGE(STAGE));\
215         _da_thread_mutex_unlock (&mutex_download_state[GET_STAGE_DL_ID(STAGE)]);\
216         }
217
218 typedef struct _download_mgr_t {
219         da_bool_t is_init;
220         dl_info_t dl_info[DA_MAX_DOWNLOAD_ID];
221         dl_id_history_t dl_id_history;
222         /* FIXME: This is temporary solution to prevent crash on following case;
223          *    1) OMA download(that is, DA's libsoup is using) is on progressing on Browser
224          *    2) User push END hard key
225          *    3) da_deinit() is called. - on UI thread
226          *    4) cancel_download(all) is called.
227          *    5) plugin-libsoup.c calls soup_session_cancel_message().
228          *    6) da_deinit() is finished and process is over.
229          *    7) soup's callback for soup_session_cancel_message() is trying to be called - on UI thread
230          *    8) Browser crashed because the callback address is no longer exist.
231          *
232          * Here is a temporary solution;
233          *    If cancel is from da_deinit(), plugin-libsoup.c will not call soup_session_cancel_message().
234          *    So, append following variable to recognize this.
235          **/
236         //da_bool_t is_progressing_deinit;
237 } download_mgr_t;
238
239 extern download_mgr_t download_mgr;
240
241 da_result_t init_download_mgr();
242 da_result_t deinit_download_mgr(void);
243 void init_download_info(int slot_id);
244 void destroy_download_info(int slot_id);
245 void *Add_new_download_stage(int slot_id);
246 void remove_download_stage(int slot_id, stage_info *in_stage);
247 void empty_stage_info(stage_info *in_stage);
248 void clean_up_client_input_info(client_input_t *client_input);
249 da_result_t  get_available_slot_id(int *available_id);
250 da_result_t  get_slot_id_for_dl_id(int dl_id , int* slot_id);
251 da_bool_t is_valid_slot_id(int slot_id);
252 void store_http_status(int dl_id, int status);
253 int get_http_status(int dl_id);
254 #endif  /* _Download_Agent_Dl_Info_Util_H */