2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef _Download_Agent_Dl_Info_Util_H
18 #define _Download_Agent_Dl_Info_Util_H
20 #include "download-agent-type.h"
21 #include "download-agent-http-queue.h"
22 #include "download-agent-utils-dl-id-history.h"
24 #define DA_MAX_DOWNLOAD_ID DA_MAX_DOWNLOAD_REQ_AT_ONCE
25 #define DA_MAX_TYPE_COUNT 10
27 #define DOWNLOAD_NOTIFY_LIMIT (1024*32) //bytes
28 extern pthread_mutex_t mutex_download_state[];
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 */
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,
55 typedef struct _client_input_basic_t {
57 char **user_request_header;
58 int user_request_header_count;
59 } client_input_basic_t;
61 typedef struct _client_input_t {
67 client_input_basic_t client_input_basic;
70 typedef struct _download_thread_input {
72 client_input_t *client_input;
73 } download_thread_input;
75 typedef struct _source_info_basic_t {
78 char **user_request_header;
79 int user_request_header_count;
80 } source_info_basic_t;
82 typedef struct _source_info_t {
83 union _source_info_type {
84 source_info_basic_t *source_info_basic;
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)
92 typedef struct _req_dl_info {
93 http_info_t http_info;
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. */
100 char **user_request_header;
101 int user_request_header_count;
102 char *user_request_etag;
103 char *user_request_temp_file_path;
105 http_state_t http_state;
106 pthread_mutex_t mutex_http_state;
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;
115 unsigned long int downloaded_data_size;
117 int invloved_transaction_id;
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)));\
141 typedef struct _file_info {
143 char *pure_file_name;
145 char *file_name_final; /* malloced in set_file_path_for_final_saving */
146 char *content_type; /* malloced in make file info. */
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;
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
165 typedef struct _stage_info {
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;
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))
183 pthread_t active_dl_thread_id;
184 download_state_t state;
185 stage_info *download_stage_data;
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;
193 char *user_temp_file_path;
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)
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)]);\
218 typedef struct _download_mgr_t {
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.
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.
236 //da_bool_t is_progressing_deinit;
239 extern download_mgr_t download_mgr;
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 */