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 Pause() OVERRIDE; | 89 virtual void Pause() OVERRIDE; |
80 virtual void Resume() OVERRIDE; | 90 virtual void Resume() OVERRIDE; |
| 91 virtual void ResumeInterruptedDownload() OVERRIDE; |
81 virtual void Cancel(bool user_cancel) OVERRIDE; | 92 virtual void Cancel(bool user_cancel) OVERRIDE; |
82 virtual void Delete(DeleteReason reason) OVERRIDE; | 93 virtual void Delete(DeleteReason reason) OVERRIDE; |
83 virtual void Remove() OVERRIDE; | 94 virtual void Remove() OVERRIDE; |
84 virtual void OpenDownload() OVERRIDE; | 95 virtual void OpenDownload() OVERRIDE; |
85 virtual void ShowDownloadInShell() OVERRIDE; | 96 virtual void ShowDownloadInShell() OVERRIDE; |
86 virtual int32 GetId() const OVERRIDE; | 97 virtual int32 GetId() const OVERRIDE; |
87 virtual DownloadId GetGlobalId() const OVERRIDE; | 98 virtual DownloadId GetGlobalId() const OVERRIDE; |
88 virtual DownloadState GetState() const OVERRIDE; | 99 virtual DownloadState GetState() const OVERRIDE; |
89 virtual DownloadInterruptReason GetLastReason() const OVERRIDE; | 100 virtual DownloadInterruptReason GetLastReason() const OVERRIDE; |
90 virtual bool IsPaused() const OVERRIDE; | 101 virtual bool IsPaused() const OVERRIDE; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 virtual void SetOpenWhenComplete(bool open) OVERRIDE; | 151 virtual void SetOpenWhenComplete(bool open) OVERRIDE; |
141 virtual void SetIsTemporary(bool temporary) OVERRIDE; | 152 virtual void SetIsTemporary(bool temporary) OVERRIDE; |
142 virtual void SetOpened(bool opened) OVERRIDE; | 153 virtual void SetOpened(bool opened) OVERRIDE; |
143 virtual void SetDisplayName(const FilePath& name) OVERRIDE; | 154 virtual void SetDisplayName(const FilePath& name) OVERRIDE; |
144 virtual std::string DebugString(bool verbose) const OVERRIDE; | 155 virtual std::string DebugString(bool verbose) const OVERRIDE; |
145 virtual void MockDownloadOpenForTesting() OVERRIDE; | 156 virtual void MockDownloadOpenForTesting() OVERRIDE; |
146 | 157 |
147 // All remaining public interfaces virtual to allow for DownloadItemImpl | 158 // All remaining public interfaces virtual to allow for DownloadItemImpl |
148 // mocks. | 159 // mocks. |
149 | 160 |
150 // Main entry points for regular downloads, in order ------------------------- | 161 virtual ResumeMode GetResumeMode() const; |
151 | 162 |
152 // TODO(rdsmith): Fold the process that uses these fully into | 163 // State transition operations on regular downloads -------------------------- |
153 // DownloadItemImpl and pass callbacks to the delegate so that all of | |
154 // these other than Start() can be made private. | |
155 | 164 |
156 // Start the download | 165 // Start the download. |
157 virtual void Start(scoped_ptr<DownloadFile> download_file); | 166 // |download_file| is the associated file on the storage medium. |
| 167 // |req_handle| is the new request handle associated with the download. |
| 168 virtual void Start(scoped_ptr<DownloadFile> download_file, |
| 169 scoped_ptr<DownloadRequestHandleInterface> req_handle); |
158 | 170 |
159 // Needed because of interwining with DownloadManagerImpl -------------------- | 171 // Needed because of intertwining with DownloadManagerImpl ------------------- |
160 | 172 |
161 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, | 173 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, |
162 // removing these from the public interface. | 174 // removing these from the public interface. |
163 | 175 |
164 // Notify observers that this item is being removed by the user. | 176 // Notify observers that this item is being removed by the user. |
165 virtual void NotifyRemoved(); | 177 virtual void NotifyRemoved(); |
166 | 178 |
167 virtual void OnDownloadedFileRemoved(); | 179 virtual void OnDownloadedFileRemoved(); |
168 | 180 |
169 // Provide a weak pointer reference to a DownloadDestinationObserver | 181 // Provide a weak pointer reference to a DownloadDestinationObserver |
170 // for use by download destinations. | 182 // for use by download destinations. |
171 base::WeakPtr<DownloadDestinationObserver> DestinationObserverAsWeakPtr(); | 183 virtual base::WeakPtr<DownloadDestinationObserver> |
| 184 DestinationObserverAsWeakPtr(); |
172 | 185 |
173 // For dispatching on whether we're dealing with a SavePackage download. | 186 // Get the download's BoundNetLog. |
| 187 virtual const net::BoundNetLog& GetBoundNetLog() const; |
174 | 188 |
175 // DownloadItemImpl routines only needed by SavePackage ---------------------- | 189 // DownloadItemImpl routines only needed by SavePackage ---------------------- |
176 | 190 |
177 // Called by SavePackage to set the total number of bytes on the item. | 191 // Called by SavePackage to set the total number of bytes on the item. |
178 virtual void SetTotalBytes(int64 total_bytes); | 192 virtual void SetTotalBytes(int64 total_bytes); |
179 | 193 |
180 // Indicate progress in saving data to its destination. | 194 // Indicate progress in saving data to its destination. |
181 // |bytes_so_far| is the number of bytes received so far. | 195 // |bytes_so_far| is the number of bytes received so far. |
182 // |hash_state| is the current hash state. | 196 // |hash_state| is the current hash state. |
183 virtual void UpdateProgress(int64 bytes_so_far, | 197 virtual void UpdateProgress(int64 bytes_so_far, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 bool IsDownloadReadyForCompletion(const base::Closure& state_change_notify); | 306 bool IsDownloadReadyForCompletion(const base::Closure& state_change_notify); |
293 | 307 |
294 // Call to transition state; all state transitions should go through this. | 308 // Call to transition state; all state transitions should go through this. |
295 void TransitionTo(DownloadInternalState new_state); | 309 void TransitionTo(DownloadInternalState new_state); |
296 | 310 |
297 // Set the |danger_type_| and invoke obserers if necessary. | 311 // Set the |danger_type_| and invoke obserers if necessary. |
298 void SetDangerType(DownloadDangerType danger_type); | 312 void SetDangerType(DownloadDangerType danger_type); |
299 | 313 |
300 void SetFullPath(const FilePath& new_path); | 314 void SetFullPath(const FilePath& new_path); |
301 | 315 |
302 // Mapping between internal and external states. | 316 void AutoResumeIfValid(); |
| 317 |
303 static DownloadState InternalToExternalState( | 318 static DownloadState InternalToExternalState( |
304 DownloadInternalState internal_state); | 319 DownloadInternalState internal_state); |
305 static DownloadInternalState ExternalToInternalState( | 320 static DownloadInternalState ExternalToInternalState( |
306 DownloadState external_state); | 321 DownloadState external_state); |
307 | 322 |
308 // Debugging routines -------------------------------------------------------- | 323 // Debugging routines -------------------------------------------------------- |
309 static const char* DebugDownloadStateString(DownloadInternalState state); | 324 static const char* DebugDownloadStateString(DownloadInternalState state); |
| 325 static const char* DebugResumeModeString(ResumeMode mode); |
310 | 326 |
311 // Will be false for save package downloads retrieved from the history. | 327 // Will be false for save package downloads retrieved from the history. |
312 // TODO(rdsmith): Replace with a generalized enum for "download source". | 328 // TODO(rdsmith): Replace with a generalized enum for "download source". |
313 const bool is_save_package_download_; | 329 const bool is_save_package_download_; |
314 | 330 |
315 // The handle to the request information. Used for operations outside the | 331 // The handle to the request information. Used for operations outside the |
316 // download system. | 332 // download system. |
317 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | 333 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
318 | 334 |
319 // Download ID assigned by DownloadResourceHandler. | 335 // Download ID assigned by DownloadResourceHandler. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 | 434 |
419 // Time the download completed. | 435 // Time the download completed. |
420 base::Time end_time_; | 436 base::Time end_time_; |
421 | 437 |
422 // Our delegate. | 438 // Our delegate. |
423 DownloadItemImplDelegate* delegate_; | 439 DownloadItemImplDelegate* delegate_; |
424 | 440 |
425 // In progress downloads may be paused by the user, we note it here. | 441 // In progress downloads may be paused by the user, we note it here. |
426 bool is_paused_; | 442 bool is_paused_; |
427 | 443 |
| 444 // The number of times this download has been resumed automatically. |
| 445 int auto_resume_count_; |
| 446 |
428 // A flag for indicating if the download should be opened at completion. | 447 // A flag for indicating if the download should be opened at completion. |
429 bool open_when_complete_; | 448 bool open_when_complete_; |
430 | 449 |
431 // A flag for indicating if the downloaded file is externally removed. | 450 // A flag for indicating if the downloaded file is externally removed. |
432 bool file_externally_removed_; | 451 bool file_externally_removed_; |
433 | 452 |
434 // Indicates if the download is considered potentially safe or dangerous | 453 // Indicates if the download is considered potentially safe or dangerous |
435 // (executable files are typically considered dangerous). | 454 // (executable files are typically considered dangerous). |
436 SafetyState safety_state_; | 455 SafetyState safety_state_; |
437 | 456 |
(...skipping 30 matching lines...) Expand all Loading... |
468 const net::BoundNetLog bound_net_log_; | 487 const net::BoundNetLog bound_net_log_; |
469 | 488 |
470 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 489 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
471 | 490 |
472 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 491 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
473 }; | 492 }; |
474 | 493 |
475 } // namespace content | 494 } // namespace content |
476 | 495 |
477 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 496 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |