OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 namespace content { | 28 namespace content { |
29 class DownloadFile; | 29 class DownloadFile; |
30 class DownloadItemImplDelegate; | 30 class DownloadItemImplDelegate; |
31 | 31 |
32 // See download_item.h for usage. | 32 // See download_item.h for usage. |
33 class CONTENT_EXPORT DownloadItemImpl | 33 class CONTENT_EXPORT DownloadItemImpl |
34 : public DownloadItem, | 34 : public DownloadItem, |
35 public DownloadDestinationObserver { | 35 public DownloadDestinationObserver { |
36 public: | 36 public: |
| 37 enum ResumeMode { |
| 38 RESUME_MODE_INVALID = 0, |
| 39 RESUME_MODE_IMMEDIATE_CONTINUE, |
| 40 RESUME_MODE_IMMEDIATE_RESTART, |
| 41 RESUME_MODE_USER_CONTINUE, |
| 42 RESUME_MODE_USER_RESTART |
| 43 }; |
| 44 |
| 45 // The maximum number of attempts we will make to resume automatically. |
| 46 static const int kMaxAutoResumeAttempts; |
| 47 |
37 // Note that it is the responsibility of the caller to ensure that a | 48 // Note that it is the responsibility of the caller to ensure that a |
38 // DownloadItemImplDelegate passed to a DownloadItemImpl constructor | 49 // DownloadItemImplDelegate passed to a DownloadItemImpl constructor |
39 // outlives the DownloadItemImpl. | 50 // outlives the DownloadItemImpl. |
40 | 51 |
41 // Constructing from persistent store: | 52 // Constructing from persistent store: |
42 // |bound_net_log| is constructed externally for our use. | 53 // |bound_net_log| is constructed externally for our use. |
43 DownloadItemImpl(DownloadItemImplDelegate* delegate, | 54 DownloadItemImpl(DownloadItemImplDelegate* delegate, |
44 DownloadId download_id, | 55 DownloadId download_id, |
45 const FilePath& path, | 56 const FilePath& path, |
46 const GURL& url, | 57 const GURL& url, |
47 const GURL& referrer_url, | 58 const GURL& referrer_url, |
48 const base::Time& start_time, | 59 const base::Time& start_time, |
49 const base::Time& end_time, | 60 const base::Time& end_time, |
50 int64 received_bytes, | 61 int64 received_bytes, |
51 int64 total_bytes, | 62 int64 total_bytes, |
52 DownloadItem::DownloadState state, | 63 DownloadItem::DownloadState state, |
53 bool opened, | 64 bool opened, |
54 const net::BoundNetLog& bound_net_log); | 65 const net::BoundNetLog& bound_net_log); |
55 | 66 |
56 // Constructing for a regular download. | 67 // Constructing for a regular download. |
57 // |bound_net_log| is constructed externally for our use. | 68 // |bound_net_log| is constructed externally for our use. |
58 DownloadItemImpl(DownloadItemImplDelegate* delegate, | 69 DownloadItemImpl(DownloadItemImplDelegate* delegate, |
59 const DownloadCreateInfo& info, | 70 const DownloadCreateInfo& info, |
60 scoped_ptr<DownloadRequestHandleInterface> request_handle, | |
61 const net::BoundNetLog& bound_net_log); | 71 const net::BoundNetLog& bound_net_log); |
62 | 72 |
63 // Constructing for the "Save Page As..." feature: | 73 // Constructing for the "Save Page As..." feature: |
64 // |bound_net_log| is constructed externally for our use. | 74 // |bound_net_log| is constructed externally for our use. |
65 DownloadItemImpl(DownloadItemImplDelegate* delegate, | 75 DownloadItemImpl(DownloadItemImplDelegate* delegate, |
66 const FilePath& path, | 76 const FilePath& path, |
67 const GURL& url, | 77 const GURL& url, |
68 DownloadId download_id, | 78 DownloadId download_id, |
69 const std::string& mime_type, | 79 const std::string& mime_type, |
70 const net::BoundNetLog& bound_net_log); | 80 const net::BoundNetLog& bound_net_log); |
71 | 81 |
72 virtual ~DownloadItemImpl(); | 82 virtual ~DownloadItemImpl(); |
73 | 83 |
74 // DownloadItem | 84 // DownloadItem |
75 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; | 85 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; |
76 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; | 86 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; |
77 virtual void UpdateObservers() OVERRIDE; | 87 virtual void UpdateObservers() OVERRIDE; |
78 virtual void DangerousDownloadValidated() OVERRIDE; | 88 virtual void DangerousDownloadValidated() OVERRIDE; |
79 virtual void TogglePause() OVERRIDE; | 89 virtual void TogglePause() OVERRIDE; |
| 90 virtual void ResumeInterruptedDownload() OVERRIDE; |
80 virtual void Cancel(bool user_cancel) OVERRIDE; | 91 virtual void Cancel(bool user_cancel) OVERRIDE; |
81 virtual void Delete(DeleteReason reason) OVERRIDE; | 92 virtual void Delete(DeleteReason reason) OVERRIDE; |
82 virtual void Remove() OVERRIDE; | 93 virtual void Remove() OVERRIDE; |
83 virtual void OpenDownload() OVERRIDE; | 94 virtual void OpenDownload() OVERRIDE; |
84 virtual void ShowDownloadInShell() OVERRIDE; | 95 virtual void ShowDownloadInShell() OVERRIDE; |
85 virtual int32 GetId() const OVERRIDE; | 96 virtual int32 GetId() const OVERRIDE; |
86 virtual DownloadId GetGlobalId() const OVERRIDE; | 97 virtual DownloadId GetGlobalId() const OVERRIDE; |
87 virtual DownloadState GetState() const OVERRIDE; | 98 virtual DownloadState GetState() const OVERRIDE; |
88 virtual DownloadInterruptReason GetLastReason() const OVERRIDE; | 99 virtual DownloadInterruptReason GetLastReason() const OVERRIDE; |
89 virtual bool IsPaused() const OVERRIDE; | 100 virtual bool IsPaused() const OVERRIDE; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 virtual void SetOpenWhenComplete(bool open) OVERRIDE; | 150 virtual void SetOpenWhenComplete(bool open) OVERRIDE; |
140 virtual void SetIsTemporary(bool temporary) OVERRIDE; | 151 virtual void SetIsTemporary(bool temporary) OVERRIDE; |
141 virtual void SetOpened(bool opened) OVERRIDE; | 152 virtual void SetOpened(bool opened) OVERRIDE; |
142 virtual void SetDisplayName(const FilePath& name) OVERRIDE; | 153 virtual void SetDisplayName(const FilePath& name) OVERRIDE; |
143 virtual std::string DebugString(bool verbose) const OVERRIDE; | 154 virtual std::string DebugString(bool verbose) const OVERRIDE; |
144 virtual void MockDownloadOpenForTesting() OVERRIDE; | 155 virtual void MockDownloadOpenForTesting() OVERRIDE; |
145 | 156 |
146 // All remaining public interfaces virtual to allow for DownloadItemImpl | 157 // All remaining public interfaces virtual to allow for DownloadItemImpl |
147 // mocks. | 158 // mocks. |
148 | 159 |
149 // Main entry points for regular downloads, in order ------------------------- | 160 virtual ResumeMode GetResumeMode() const; |
150 | 161 |
151 // TODO(rdsmith): Fold the process that uses these fully into | 162 // State transition operations on regular downloads -------------------------- |
152 // DownloadItemImpl and pass callbacks to the delegate so that all of | |
153 // these other than Start() can be made private. | |
154 | 163 |
155 // Start the download | 164 // Start the download. |
156 virtual void Start(scoped_ptr<DownloadFile> download_file); | 165 // |download_file| is the associated file on the storage medium. |
| 166 // |req_handle| is the new request handle associated with the download. |
| 167 virtual void Start(scoped_ptr<DownloadFile> download_file, |
| 168 scoped_ptr<DownloadRequestHandleInterface> req_handle); |
157 | 169 |
158 // Needed because of interwining with DownloadManagerImpl -------------------- | 170 // Needed because of intertwining with DownloadManagerImpl ------------------- |
159 | 171 |
160 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, | 172 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, |
161 // removing these from the public interface. | 173 // removing these from the public interface. |
162 | 174 |
163 // Notify observers that this item is being removed by the user. | 175 // Notify observers that this item is being removed by the user. |
164 virtual void NotifyRemoved(); | 176 virtual void NotifyRemoved(); |
165 | 177 |
166 virtual void OnDownloadedFileRemoved(); | 178 virtual void OnDownloadedFileRemoved(); |
167 | 179 |
168 // Provide a weak pointer reference to a DownloadDestinationObserver | 180 // Provide a weak pointer reference to a DownloadDestinationObserver |
169 // for use by download destinations. | 181 // for use by download destinations. |
170 base::WeakPtr<DownloadDestinationObserver> DestinationObserverAsWeakPtr(); | 182 virtual base::WeakPtr<DownloadDestinationObserver> |
| 183 DestinationObserverAsWeakPtr(); |
171 | 184 |
172 // For dispatching on whether we're dealing with a SavePackage download. | 185 // Get the download's BoundNetLog. |
| 186 virtual const net::BoundNetLog& GetBoundNetLog() const; |
173 | 187 |
174 // DownloadItemImpl routines only needed by SavePackage ---------------------- | 188 // DownloadItemImpl routines only needed by SavePackage ---------------------- |
175 | 189 |
176 // Called by SavePackage to set the total number of bytes on the item. | 190 // Called by SavePackage to set the total number of bytes on the item. |
177 virtual void SetTotalBytes(int64 total_bytes); | 191 virtual void SetTotalBytes(int64 total_bytes); |
178 | 192 |
179 // Indicate progress in saving data to its destination. | 193 // Indicate progress in saving data to its destination. |
180 // |bytes_so_far| is the number of bytes received so far. | 194 // |bytes_so_far| is the number of bytes received so far. |
181 // |hash_state| is the current hash state. | 195 // |hash_state| is the current hash state. |
182 virtual void UpdateProgress(int64 bytes_so_far, | 196 virtual void UpdateProgress(int64 bytes_so_far, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 bool IsDownloadReadyForCompletion(const base::Closure& state_change_notify); | 305 bool IsDownloadReadyForCompletion(const base::Closure& state_change_notify); |
292 | 306 |
293 // Call to transition state; all state transitions should go through this. | 307 // Call to transition state; all state transitions should go through this. |
294 void TransitionTo(DownloadInternalState new_state); | 308 void TransitionTo(DownloadInternalState new_state); |
295 | 309 |
296 // Set the |danger_type_| and invoke obserers if necessary. | 310 // Set the |danger_type_| and invoke obserers if necessary. |
297 void SetDangerType(DownloadDangerType danger_type); | 311 void SetDangerType(DownloadDangerType danger_type); |
298 | 312 |
299 void SetFullPath(const FilePath& new_path); | 313 void SetFullPath(const FilePath& new_path); |
300 | 314 |
301 // Mapping between internal and external states. | 315 void AutoResumeIfValid(); |
| 316 |
302 static DownloadState InternalToExternalState( | 317 static DownloadState InternalToExternalState( |
303 DownloadInternalState internal_state); | 318 DownloadInternalState internal_state); |
304 static DownloadInternalState ExternalToInternalState( | 319 static DownloadInternalState ExternalToInternalState( |
305 DownloadState external_state); | 320 DownloadState external_state); |
306 | 321 |
307 // Debugging routines -------------------------------------------------------- | 322 // Debugging routines -------------------------------------------------------- |
308 static const char* DebugDownloadStateString(DownloadInternalState state); | 323 static const char* DebugDownloadStateString(DownloadInternalState state); |
| 324 static const char* DebugResumeModeString(ResumeMode mode); |
309 | 325 |
310 // Will be false for save package downloads retrieved from the history. | 326 // Will be false for save package downloads retrieved from the history. |
311 // TODO(rdsmith): Replace with a generalized enum for "download source". | 327 // TODO(rdsmith): Replace with a generalized enum for "download source". |
312 const bool is_save_package_download_; | 328 const bool is_save_package_download_; |
313 | 329 |
314 // The handle to the request information. Used for operations outside the | 330 // The handle to the request information. Used for operations outside the |
315 // download system. | 331 // download system. |
316 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | 332 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
317 | 333 |
318 // Download ID assigned by DownloadResourceHandler. | 334 // Download ID assigned by DownloadResourceHandler. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 | 433 |
418 // Time the download completed. | 434 // Time the download completed. |
419 base::Time end_time_; | 435 base::Time end_time_; |
420 | 436 |
421 // Our delegate. | 437 // Our delegate. |
422 DownloadItemImplDelegate* delegate_; | 438 DownloadItemImplDelegate* delegate_; |
423 | 439 |
424 // In progress downloads may be paused by the user, we note it here. | 440 // In progress downloads may be paused by the user, we note it here. |
425 bool is_paused_; | 441 bool is_paused_; |
426 | 442 |
| 443 // The number of times this download has been resumed automatically. |
| 444 int auto_resume_count_; |
| 445 |
427 // A flag for indicating if the download should be opened at completion. | 446 // A flag for indicating if the download should be opened at completion. |
428 bool open_when_complete_; | 447 bool open_when_complete_; |
429 | 448 |
430 // A flag for indicating if the downloaded file is externally removed. | 449 // A flag for indicating if the downloaded file is externally removed. |
431 bool file_externally_removed_; | 450 bool file_externally_removed_; |
432 | 451 |
433 // Indicates if the download is considered potentially safe or dangerous | 452 // Indicates if the download is considered potentially safe or dangerous |
434 // (executable files are typically considered dangerous). | 453 // (executable files are typically considered dangerous). |
435 SafetyState safety_state_; | 454 SafetyState safety_state_; |
436 | 455 |
(...skipping 30 matching lines...) Expand all Loading... |
467 const net::BoundNetLog bound_net_log_; | 486 const net::BoundNetLog bound_net_log_; |
468 | 487 |
469 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 488 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
470 | 489 |
471 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 490 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
472 }; | 491 }; |
473 | 492 |
474 } // namespace content | 493 } // namespace content |
475 | 494 |
476 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 495 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |