OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Each download is represented by a DownloadItem, and all DownloadItems | 5 // Each download is represented by a DownloadItem, and all DownloadItems |
6 // are owned by the DownloadManager which maintains a global list of all | 6 // are owned by the DownloadManager which maintains a global list of all |
7 // downloads. DownloadItems are created when a user initiates a download, | 7 // downloads. DownloadItems are created when a user initiates a download, |
8 // and exist for the duration of the browser life time. | 8 // and exist for the duration of the browser life time. |
9 // | 9 // |
10 // Download observers: | 10 // Download observers: |
11 // DownloadItem::Observer: | 11 // DownloadItem::Observer: |
12 // - allows observers to receive notifications about one download from start | 12 // - allows observers to receive notifications about one download from start |
13 // to completion | 13 // to completion |
14 // Use AddObserver() / RemoveObserver() on the appropriate download object to | 14 // Use AddObserver() / RemoveObserver() on the appropriate download object to |
15 // receive state updates. | 15 // receive state updates. |
16 | 16 |
17 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ | 17 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ |
18 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ | 18 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ |
19 #pragma once | 19 #pragma once |
20 | 20 |
21 #include <string> | 21 #include <string> |
22 | 22 |
23 #include "base/basictypes.h" | 23 #include "base/basictypes.h" |
24 #include "base/file_path.h" | 24 #include "base/file_path.h" |
25 #include "base/observer_list.h" | 25 #include "base/observer_list.h" |
26 #include "base/time.h" | 26 #include "base/time.h" |
27 #include "base/timer.h" | 27 #include "base/timer.h" |
28 #include "chrome/browser/download/download_process_handle.h" | 28 #include "chrome/browser/download/download_process_handle.h" |
| 29 #include "chrome/browser/download/download_state_info.h" |
29 #include "googleurl/src/gurl.h" | 30 #include "googleurl/src/gurl.h" |
30 | 31 |
31 class DownloadFileManager; | 32 class DownloadFileManager; |
32 class DownloadManager; | 33 class DownloadManager; |
33 struct DownloadCreateInfo; | 34 struct DownloadCreateInfo; |
| 35 struct DownloadHistoryInfo; |
34 | 36 |
35 // One DownloadItem per download. This is the model class that stores all the | 37 // One DownloadItem per download. This is the model class that stores all the |
36 // state for a download. Multiple views, such as a tab's download shelf and the | 38 // state for a download. Multiple views, such as a tab's download shelf and the |
37 // Destination tab's download view, may refer to a given DownloadItem. | 39 // Destination tab's download view, may refer to a given DownloadItem. |
38 // | 40 // |
39 // This is intended to be used only on the UI thread. | 41 // This is intended to be used only on the UI thread. |
40 class DownloadItem { | 42 class DownloadItem { |
41 public: | 43 public: |
42 enum DownloadState { | 44 enum DownloadState { |
43 // Download is actively progressing. | 45 // Download is actively progressing. |
44 IN_PROGRESS, | 46 IN_PROGRESS = 0, |
45 | 47 |
46 // Download is completely finished. | 48 // Download is completely finished. |
47 COMPLETE, | 49 COMPLETE, |
48 | 50 |
49 // Download has been cancelled. | 51 // Download has been cancelled. |
50 CANCELLED, | 52 CANCELLED, |
51 | 53 |
52 // This state indicates that the download item is about to be destroyed, | 54 // This state indicates that the download item is about to be destroyed, |
53 // and observers seeing this state should release all references. | 55 // and observers seeing this state should release all references. |
54 REMOVING, | 56 REMOVING, |
55 | 57 |
56 // This state indicates that the download has been interrupted. | 58 // This state indicates that the download has been interrupted. |
57 INTERRUPTED | 59 INTERRUPTED, |
| 60 |
| 61 // Maximum value. |
| 62 MAX_DOWNLOAD_STATE |
58 }; | 63 }; |
59 | 64 |
60 enum SafetyState { | 65 enum SafetyState { |
61 SAFE = 0, | 66 SAFE = 0, |
62 DANGEROUS, | 67 DANGEROUS, |
63 DANGEROUS_BUT_VALIDATED // Dangerous but the user confirmed the download. | 68 DANGEROUS_BUT_VALIDATED // Dangerous but the user confirmed the download. |
64 }; | 69 }; |
65 | 70 |
66 // This enum is used by histograms. Do not change the ordering or remove | 71 // This enum is used by histograms. Do not change the ordering or remove |
67 // items. | 72 // items. |
(...skipping 26 matching lines...) Expand all Loading... |
94 | 99 |
95 // Called when a downloaded file has been opened. | 100 // Called when a downloaded file has been opened. |
96 virtual void OnDownloadOpened(DownloadItem* download) = 0; | 101 virtual void OnDownloadOpened(DownloadItem* download) = 0; |
97 | 102 |
98 protected: | 103 protected: |
99 virtual ~Observer() {} | 104 virtual ~Observer() {} |
100 }; | 105 }; |
101 | 106 |
102 // Constructing from persistent store: | 107 // Constructing from persistent store: |
103 DownloadItem(DownloadManager* download_manager, | 108 DownloadItem(DownloadManager* download_manager, |
104 const DownloadCreateInfo& info); | 109 const DownloadHistoryInfo& info); |
105 | 110 |
106 // Constructing for a regular download: | 111 // Constructing for a regular download: |
107 DownloadItem(DownloadManager* download_manager, | 112 DownloadItem(DownloadManager* download_manager, |
108 const DownloadCreateInfo& info, | 113 const DownloadCreateInfo& info, |
109 bool is_otr); | 114 bool is_otr); |
110 | 115 |
111 // Constructing for the "Save Page As..." feature: | 116 // Constructing for the "Save Page As..." feature: |
112 DownloadItem(DownloadManager* download_manager, | 117 DownloadItem(DownloadManager* download_manager, |
113 const FilePath& path, | 118 const FilePath& path, |
114 const GURL& url, | 119 const GURL& url, |
115 bool is_otr); | 120 bool is_otr); |
116 | 121 |
117 ~DownloadItem(); | 122 ~DownloadItem(); |
118 | 123 |
119 void AddObserver(Observer* observer); | 124 void AddObserver(Observer* observer); |
120 void RemoveObserver(Observer* observer); | 125 void RemoveObserver(Observer* observer); |
121 | 126 |
122 // Notifies our observers periodically. | 127 // Notifies our observers periodically. |
123 void UpdateObservers(); | 128 void UpdateObservers(); |
124 | 129 |
125 // Whether it is OK to open this download. | 130 // Returns true if it is OK to open this download. |
126 bool CanOpenDownload(); | 131 bool CanOpenDownload(); |
127 | 132 |
128 // Tests if a file type should be opened automatically. | 133 // Tests if a file type should be opened automatically. |
129 bool ShouldOpenFileBasedOnExtension(); | 134 bool ShouldOpenFileBasedOnExtension(); |
130 | 135 |
131 // Registers this file extension for automatic opening upon download | 136 // Registers this file extension for automatic opening upon download |
132 // completion if 'open' is true, or prevents the extension from automatic | 137 // completion if 'open' is true, or prevents the extension from automatic |
133 // opening if 'open' is false. | 138 // opening if 'open' is false. |
134 void OpenFilesBasedOnExtension(bool open); | 139 void OpenFilesBasedOnExtension(bool open); |
135 | 140 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 // not estimate. | 189 // not estimate. |
185 bool TimeRemaining(base::TimeDelta* remaining) const; | 190 bool TimeRemaining(base::TimeDelta* remaining) const; |
186 | 191 |
187 // Simple speed estimate in bytes/s | 192 // Simple speed estimate in bytes/s |
188 int64 CurrentSpeed() const; | 193 int64 CurrentSpeed() const; |
189 | 194 |
190 // Rough percent complete, -1 means we don't know (since we didn't receive a | 195 // Rough percent complete, -1 means we don't know (since we didn't receive a |
191 // total size). | 196 // total size). |
192 int PercentComplete() const; | 197 int PercentComplete() const; |
193 | 198 |
194 // Whether or not this download has saved all of its data. | 199 // Called when the final path has been determined. |
| 200 void OnPathDetermined(const FilePath& path) { full_path_ = path; } |
| 201 |
| 202 // Returns true if this download has saved all of its data. |
195 bool all_data_saved() const { return all_data_saved_; } | 203 bool all_data_saved() const { return all_data_saved_; } |
196 | 204 |
197 // Update the fields that may have changed in DownloadCreateInfo as a | 205 // Update the fields that may have changed in DownloadStateInfo as a |
198 // result of analyzing the file and figuring out its type, location, etc. | 206 // result of analyzing the file and figuring out its type, location, etc. |
199 // May only be called once. | 207 // May only be called once. |
200 void SetFileCheckResults(const FilePath& path, | 208 void SetFileCheckResults(const DownloadStateInfo& state); |
201 bool is_dangerous_file, | 209 |
202 bool is_dangerous_url, | 210 // Updates the target file. |
203 int path_uniquifier, | 211 void UpdateTarget(); |
204 bool prompt, | |
205 bool is_extension_install, | |
206 const FilePath& original_name); | |
207 | 212 |
208 // Update the download's path, the actual file is renamed on the download | 213 // Update the download's path, the actual file is renamed on the download |
209 // thread. | 214 // thread. |
210 void Rename(const FilePath& full_path); | 215 void Rename(const FilePath& full_path); |
211 | 216 |
212 // Allow the user to temporarily pause a download or resume a paused download. | 217 // Allow the user to temporarily pause a download or resume a paused download. |
213 void TogglePause(); | 218 void TogglePause(); |
214 | 219 |
215 // Called when the download is ready to complete. | 220 // Called when the download is ready to complete. |
216 // This may perform final rename if necessary and will eventually call | 221 // This may perform final rename if necessary and will eventually call |
(...skipping 17 matching lines...) Expand all Loading... |
234 | 239 |
235 // Returns true if the download was interrupted. | 240 // Returns true if the download was interrupted. |
236 bool IsInterrupted() const; | 241 bool IsInterrupted() const; |
237 | 242 |
238 // Returns true if we have all the data and know the final file name. | 243 // Returns true if we have all the data and know the final file name. |
239 bool IsComplete() const; | 244 bool IsComplete() const; |
240 | 245 |
241 // Accessors | 246 // Accessors |
242 DownloadState state() const { return state_; } | 247 DownloadState state() const { return state_; } |
243 FilePath full_path() const { return full_path_; } | 248 FilePath full_path() const { return full_path_; } |
244 void set_path_uniquifier(int uniquifier) { path_uniquifier_ = uniquifier; } | 249 void set_path_uniquifier(int uniquifier) { |
245 const GURL& url() const { return url_chain_.back(); } | 250 state_info_.path_uniquifier = uniquifier; |
| 251 } |
| 252 const GURL& GetURL() const; |
| 253 |
246 const std::vector<GURL>& url_chain() const { return url_chain_; } | 254 const std::vector<GURL>& url_chain() const { return url_chain_; } |
247 const GURL& original_url() const { return url_chain_.front(); } | 255 const GURL& original_url() const { return url_chain_.front(); } |
248 const GURL& referrer_url() const { return referrer_url_; } | 256 const GURL& referrer_url() const { return referrer_url_; } |
| 257 std::string content_disposition() const { return content_disposition_; } |
249 std::string mime_type() const { return mime_type_; } | 258 std::string mime_type() const { return mime_type_; } |
250 std::string original_mime_type() const { return original_mime_type_; } | 259 std::string original_mime_type() const { return original_mime_type_; } |
| 260 std::string referrer_charset() const { return referrer_charset_; } |
251 int64 total_bytes() const { return total_bytes_; } | 261 int64 total_bytes() const { return total_bytes_; } |
252 void set_total_bytes(int64 total_bytes) { total_bytes_ = total_bytes; } | 262 void set_total_bytes(int64 total_bytes) { |
| 263 total_bytes_ = total_bytes; |
| 264 } |
253 int64 received_bytes() const { return received_bytes_; } | 265 int64 received_bytes() const { return received_bytes_; } |
254 int32 id() const { return id_; } | 266 int32 id() const { return download_id_; } |
255 base::Time start_time() const { return start_time_; } | 267 base::Time start_time() const { return start_time_; } |
256 void set_db_handle(int64 handle) { db_handle_ = handle; } | 268 void set_db_handle(int64 handle) { db_handle_ = handle; } |
257 int64 db_handle() const { return db_handle_; } | 269 int64 db_handle() const { return db_handle_; } |
258 bool is_paused() const { return is_paused_; } | 270 bool is_paused() const { return is_paused_; } |
259 bool open_when_complete() const { return open_when_complete_; } | 271 bool open_when_complete() const { return open_when_complete_; } |
260 void set_open_when_complete(bool open) { open_when_complete_ = open; } | 272 void set_open_when_complete(bool open) { open_when_complete_ = open; } |
261 SafetyState safety_state() const { return safety_state_; } | 273 SafetyState safety_state() const { return safety_state_; } |
262 void set_safety_state(SafetyState safety_state) { | 274 void set_safety_state(SafetyState safety_state) { |
263 safety_state_ = safety_state; | 275 safety_state_ = safety_state; |
264 } | 276 } |
265 DangerType danger_type() { return danger_type_;} | 277 // Why |safety_state_| is not SAFE. |
| 278 DangerType GetDangerType() const; |
| 279 bool IsDangerous() const; |
| 280 void MarkUrlDangerous(); |
| 281 |
266 bool auto_opened() { return auto_opened_; } | 282 bool auto_opened() { return auto_opened_; } |
267 FilePath target_name() const { return target_name_; } | 283 FilePath target_name() const { return state_info_.target_name; } |
268 bool save_as() const { return save_as_; } | 284 bool save_as() const { return state_info_.prompt_user_for_save_location; } |
269 bool is_otr() const { return is_otr_; } | 285 bool is_otr() const { return is_otr_; } |
270 bool is_extension_install() const { return is_extension_install_; } | 286 bool is_extension_install() const { |
| 287 return state_info_.is_extension_install; |
| 288 } |
| 289 FilePath suggested_path() const { return state_info_.suggested_path; } |
271 bool is_temporary() const { return is_temporary_; } | 290 bool is_temporary() const { return is_temporary_; } |
272 void set_opened(bool opened) { opened_ = opened; } | 291 void set_opened(bool opened) { opened_ = opened; } |
273 bool opened() const { return opened_; } | 292 bool opened() const { return opened_; } |
274 | 293 |
| 294 DownloadHistoryInfo GetHistoryInfo() const; |
| 295 DownloadStateInfo state_info() const { return state_info_; } |
275 const DownloadProcessHandle& process_handle() const { | 296 const DownloadProcessHandle& process_handle() const { |
276 return process_handle_; | 297 return process_handle_; |
277 } | 298 } |
278 | 299 |
279 // Returns the final target file path for the download. | 300 // Returns the final target file path for the download. |
280 FilePath GetTargetFilePath() const; | 301 FilePath GetTargetFilePath() const; |
281 | 302 |
282 // Returns the file-name that should be reported to the user, which is | 303 // Returns the file-name that should be reported to the user, which is |
283 // target_name_ possibly with the uniquifier number. | 304 // target_name possibly with the uniquifier number. |
284 FilePath GetFileNameToReportUser() const; | 305 FilePath GetFileNameToReportUser() const; |
285 | 306 |
286 // Returns the user-verified target file path for the download. | 307 // Returns the user-verified target file path for the download. |
287 // This returns the same path as GetTargetFilePath() for safe downloads | 308 // This returns the same path as GetTargetFilePath() for safe downloads |
288 // but does not for dangerous downloads until the name is verified. | 309 // but does not for dangerous downloads until the name is verified. |
289 FilePath GetUserVerifiedFilePath() const; | 310 FilePath GetUserVerifiedFilePath() const; |
290 | 311 |
291 // Returns true if the current file name is not the final target name yet. | 312 // Returns true if the current file name is not the final target name yet. |
292 bool NeedsRename() const { | 313 bool NeedsRename() const { |
293 return target_name_ != full_path_.BaseName(); | 314 return state_info_.target_name != full_path_.BaseName(); |
294 } | 315 } |
295 | 316 |
296 std::string DebugString(bool verbose) const; | 317 std::string DebugString(bool verbose) const; |
297 | 318 |
298 private: | 319 private: |
299 void Init(bool start_timer); | 320 void Init(bool start_timer); |
300 | 321 |
301 // Internal helper for maintaining consistent received and total sizes. | 322 // Internal helper for maintaining consistent received and total sizes. |
302 void UpdateSize(int64 size); | 323 void UpdateSize(int64 size); |
303 | 324 |
304 // Called when the entire download operation (including renaming etc) | 325 // Called when the entire download operation (including renaming etc) |
305 // is completed. | 326 // is completed. |
306 void Completed(); | 327 void Completed(); |
307 | 328 |
308 // Start/stop sending periodic updates to our observers | 329 // Start/stop sending periodic updates to our observers |
309 void StartProgressTimer(); | 330 void StartProgressTimer(); |
310 void StopProgressTimer(); | 331 void StopProgressTimer(); |
311 | 332 |
312 // Request ID assigned by the ResourceDispatcherHost. | 333 // State information used by the download manager. |
313 int32 id_; | 334 DownloadStateInfo state_info_; |
| 335 |
| 336 // The handle to the process information. Used for operations outside the |
| 337 // download system. |
| 338 DownloadProcessHandle process_handle_; |
| 339 |
| 340 // Download ID assigned by DownloadResourceHandler. |
| 341 int32 download_id_; |
314 | 342 |
315 // Full path to the downloaded or downloading file. | 343 // Full path to the downloaded or downloading file. |
316 FilePath full_path_; | 344 FilePath full_path_; |
317 | 345 |
318 // A number that should be appended to the path to make it unique, or 0 if the | 346 // A number that should be appended to the path to make it unique, or 0 if the |
319 // path should be used as is. | 347 // path should be used as is. |
320 int path_uniquifier_; | 348 int path_uniquifier_; |
321 | 349 |
322 // The chain of redirects that leading up to and including the final URL. | 350 // The chain of redirects that leading up to and including the final URL. |
323 std::vector<GURL> url_chain_; | 351 std::vector<GURL> url_chain_; |
324 | 352 |
325 // The URL of the page that initiated the download. | 353 // The URL of the page that initiated the download. |
326 GURL referrer_url_; | 354 GURL referrer_url_; |
327 | 355 |
328 // The mimetype of the download | 356 // Information from the request. |
| 357 // Content-disposition field from the header. |
| 358 std::string content_disposition_; |
| 359 |
| 360 // Mime-type from the header. Subject to change. |
329 std::string mime_type_; | 361 std::string mime_type_; |
330 | 362 |
331 // The value of the content type header received when downloading | 363 // The value of the content type header sent with the downloaded item. It |
332 // this item. |mime_type_| may be different because of type sniffing. | 364 // may be different from |mime_type_|, which may be set based on heuristics |
| 365 // which may look at the file extension and first few bytes of the file. |
333 std::string original_mime_type_; | 366 std::string original_mime_type_; |
334 | 367 |
| 368 // The charset of the referring page where the download request comes from. |
| 369 // It's used to construct a suggested filename. |
| 370 std::string referrer_charset_; |
| 371 |
335 // Total bytes expected | 372 // Total bytes expected |
336 int64 total_bytes_; | 373 int64 total_bytes_; |
337 | 374 |
338 // Current received bytes | 375 // Current received bytes |
339 int64 received_bytes_; | 376 int64 received_bytes_; |
340 | 377 |
341 // Last error. | 378 // Last error. |
342 int last_os_error_; | 379 int last_os_error_; |
343 | 380 |
344 // Start time for calculating remaining time | 381 // Start time for calculating remaining time |
(...skipping 16 matching lines...) Expand all Loading... |
361 | 398 |
362 // Our owning object | 399 // Our owning object |
363 DownloadManager* download_manager_; | 400 DownloadManager* download_manager_; |
364 | 401 |
365 // In progress downloads may be paused by the user, we note it here | 402 // In progress downloads may be paused by the user, we note it here |
366 bool is_paused_; | 403 bool is_paused_; |
367 | 404 |
368 // A flag for indicating if the download should be opened at completion. | 405 // A flag for indicating if the download should be opened at completion. |
369 bool open_when_complete_; | 406 bool open_when_complete_; |
370 | 407 |
371 // Whether the download is considered potentially safe or dangerous | 408 // Indicates if the download is considered potentially safe or dangerous |
372 // (executable files are typically considered dangerous). | 409 // (executable files are typically considered dangerous). |
373 SafetyState safety_state_; | 410 SafetyState safety_state_; |
374 | 411 |
375 // Why |safety_state_| is not SAFE. | 412 // True if the download was auto-opened. We set this rather than using |
376 DangerType danger_type_; | |
377 | |
378 // Whether the download was auto-opened. We set this rather than using | |
379 // an observer as it's frequently possible for the download to be auto opened | 413 // an observer as it's frequently possible for the download to be auto opened |
380 // before the observer is added. | 414 // before the observer is added. |
381 bool auto_opened_; | 415 bool auto_opened_; |
382 | 416 |
383 // Dangerous downloads or ongoing downloads are given temporary names until | |
384 // the user approves them or the downloads finish. | |
385 // This stores their final target name. | |
386 FilePath target_name_; | |
387 | |
388 // The handle to the process information. Used for operations outside the | |
389 // download system. | |
390 DownloadProcessHandle process_handle_; | |
391 | |
392 // True if the item was downloaded as a result of 'save as...' | |
393 bool save_as_; | |
394 | |
395 // True if the download was initiated in an incognito window. | 417 // True if the download was initiated in an incognito window. |
396 bool is_otr_; | 418 bool is_otr_; |
397 | 419 |
398 // True if the item was downloaded for an extension installation. | |
399 bool is_extension_install_; | |
400 | |
401 // True if the item was downloaded temporarily. | 420 // True if the item was downloaded temporarily. |
402 bool is_temporary_; | 421 bool is_temporary_; |
403 | 422 |
404 // True if we've saved all the data for the download. | 423 // True if we've saved all the data for the download. |
405 bool all_data_saved_; | 424 bool all_data_saved_; |
406 | 425 |
407 // Did the user open the item either directly or indirectly (such as by | 426 // Did the user open the item either directly or indirectly (such as by |
408 // setting always open files of this type)? The shelf also sets this field | 427 // setting always open files of this type)? The shelf also sets this field |
409 // when the user closes the shelf before the item has been opened but should | 428 // when the user closes the shelf before the item has been opened but should |
410 // be treated as though the user opened it. | 429 // be treated as though the user opened it. |
411 bool opened_; | 430 bool opened_; |
412 | 431 |
413 DISALLOW_COPY_AND_ASSIGN(DownloadItem); | 432 DISALLOW_COPY_AND_ASSIGN(DownloadItem); |
414 }; | 433 }; |
415 | 434 |
416 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ | 435 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_H_ |
OLD | NEW |