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 15 matching lines...) Expand all Loading... |
26 | 26 |
27 namespace content { | 27 namespace content { |
28 class DownloadFile; | 28 class DownloadFile; |
29 class DownloadItemImplDelegate; | 29 class DownloadItemImplDelegate; |
30 | 30 |
31 // See download_item.h for usage. | 31 // See download_item.h for usage. |
32 class CONTENT_EXPORT DownloadItemImpl | 32 class CONTENT_EXPORT DownloadItemImpl |
33 : public DownloadItem, | 33 : public DownloadItem, |
34 public DownloadDestinationObserver { | 34 public DownloadDestinationObserver { |
35 public: | 35 public: |
| 36 enum ResumeMode { |
| 37 RESUME_MODE_INVALID = 0, |
| 38 RESUME_MODE_IMMEDIATE_CONTINUE, |
| 39 RESUME_MODE_IMMEDIATE_RESTART, |
| 40 RESUME_MODE_USER_CONTINUE, |
| 41 RESUME_MODE_USER_RESTART |
| 42 }; |
| 43 |
| 44 // The maximum number of attempts we will make to resume automatically. |
| 45 static const int kMaxAutoResumeAttempts; |
| 46 |
36 // Note that it is the responsibility of the caller to ensure that a | 47 // Note that it is the responsibility of the caller to ensure that a |
37 // DownloadItemImplDelegate passed to a DownloadItemImpl constructor | 48 // DownloadItemImplDelegate passed to a DownloadItemImpl constructor |
38 // outlives the DownloadItemImpl. | 49 // outlives the DownloadItemImpl. |
39 | 50 |
40 // Constructing from persistent store: | 51 // Constructing from persistent store: |
41 // |bound_net_log| is constructed externally for our use. | 52 // |bound_net_log| is constructed externally for our use. |
42 DownloadItemImpl(DownloadItemImplDelegate* delegate, | 53 DownloadItemImpl(DownloadItemImplDelegate* delegate, |
43 DownloadId download_id, | 54 DownloadId download_id, |
44 const FilePath& path, | 55 const FilePath& path, |
45 const GURL& url, | 56 const GURL& url, |
46 const GURL& referrer_url, | 57 const GURL& referrer_url, |
47 const base::Time& start_time, | 58 const base::Time& start_time, |
48 const base::Time& end_time, | 59 const base::Time& end_time, |
49 int64 received_bytes, | 60 int64 received_bytes, |
50 int64 total_bytes, | 61 int64 total_bytes, |
51 DownloadItem::DownloadState state, | 62 DownloadItem::DownloadState state, |
52 bool opened, | 63 bool opened, |
53 const net::BoundNetLog& bound_net_log); | 64 const net::BoundNetLog& bound_net_log); |
54 | 65 |
55 // Constructing for a regular download. | 66 // Constructing for a regular download. |
56 // |bound_net_log| is constructed externally for our use. | 67 // |bound_net_log| is constructed externally for our use. |
57 DownloadItemImpl(DownloadItemImplDelegate* delegate, | 68 DownloadItemImpl(DownloadItemImplDelegate* delegate, |
58 const DownloadCreateInfo& info, | 69 const DownloadCreateInfo& info, |
59 scoped_ptr<DownloadRequestHandleInterface> request_handle, | |
60 const net::BoundNetLog& bound_net_log); | 70 const net::BoundNetLog& bound_net_log); |
61 | 71 |
62 // Constructing for the "Save Page As..." feature: | 72 // Constructing for the "Save Page As..." feature: |
63 // |bound_net_log| is constructed externally for our use. | 73 // |bound_net_log| is constructed externally for our use. |
64 DownloadItemImpl(DownloadItemImplDelegate* delegate, | 74 DownloadItemImpl(DownloadItemImplDelegate* delegate, |
65 const FilePath& path, | 75 const FilePath& path, |
66 const GURL& url, | 76 const GURL& url, |
67 DownloadId download_id, | 77 DownloadId download_id, |
68 const std::string& mime_type, | 78 const std::string& mime_type, |
69 const net::BoundNetLog& bound_net_log); | 79 const net::BoundNetLog& bound_net_log); |
70 | 80 |
71 virtual ~DownloadItemImpl(); | 81 virtual ~DownloadItemImpl(); |
72 | 82 |
73 // DownloadItem | 83 // DownloadItem |
74 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; | 84 virtual void AddObserver(DownloadItem::Observer* observer) OVERRIDE; |
75 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; | 85 virtual void RemoveObserver(DownloadItem::Observer* observer) OVERRIDE; |
76 virtual void UpdateObservers() OVERRIDE; | 86 virtual void UpdateObservers() OVERRIDE; |
77 virtual void DangerousDownloadValidated() OVERRIDE; | 87 virtual void DangerousDownloadValidated() OVERRIDE; |
78 virtual void TogglePause() OVERRIDE; | 88 virtual void TogglePause() OVERRIDE; |
79 virtual void Cancel(bool user_cancel) OVERRIDE; | 89 virtual void Cancel(bool user_cancel) OVERRIDE; |
80 virtual void Delete(DeleteReason reason) OVERRIDE; | 90 virtual void Delete(DeleteReason reason) OVERRIDE; |
81 virtual void Remove() OVERRIDE; | 91 virtual void Remove() OVERRIDE; |
82 virtual void OpenDownload() OVERRIDE; | 92 virtual void OpenDownload() OVERRIDE; |
83 virtual void ShowDownloadInShell() OVERRIDE; | 93 virtual void ShowDownloadInShell() OVERRIDE; |
84 virtual int32 GetId() const OVERRIDE; | 94 virtual int32 GetId() const OVERRIDE; |
85 virtual DownloadId GetGlobalId() const OVERRIDE; | 95 virtual DownloadId GetGlobalId() const OVERRIDE; |
86 virtual DownloadState GetState() const OVERRIDE; | 96 virtual DownloadState GetState() const OVERRIDE; |
87 virtual DownloadInterruptReason GetLastReason() const OVERRIDE; | 97 virtual DownloadInterruptReason GetLastReason() const OVERRIDE; |
88 virtual bool IsPaused() const OVERRIDE; | 98 virtual bool IsPaused() const OVERRIDE; |
| 99 virtual bool CanResumeInterrupted() const OVERRIDE; |
89 virtual bool IsTemporary() const OVERRIDE; | 100 virtual bool IsTemporary() const OVERRIDE; |
90 virtual bool IsPartialDownload() const OVERRIDE; | 101 virtual bool IsPartialDownload() const OVERRIDE; |
91 virtual bool IsInProgress() const OVERRIDE; | 102 virtual bool IsInProgress() const OVERRIDE; |
92 virtual bool IsCancelled() const OVERRIDE; | 103 virtual bool IsCancelled() const OVERRIDE; |
93 virtual bool IsInterrupted() const OVERRIDE; | 104 virtual bool IsInterrupted() const OVERRIDE; |
94 virtual bool IsComplete() const OVERRIDE; | 105 virtual bool IsComplete() const OVERRIDE; |
95 virtual const GURL& GetURL() const OVERRIDE; | 106 virtual const GURL& GetURL() const OVERRIDE; |
96 virtual const std::vector<GURL>& GetUrlChain() const OVERRIDE; | 107 virtual const std::vector<GURL>& GetUrlChain() const OVERRIDE; |
97 virtual const GURL& GetOriginalUrl() const OVERRIDE; | 108 virtual const GURL& GetOriginalUrl() const OVERRIDE; |
98 virtual const GURL& GetReferrerUrl() const OVERRIDE; | 109 virtual const GURL& GetReferrerUrl() const OVERRIDE; |
(...skipping 22 matching lines...) Expand all Loading... |
121 virtual bool TimeRemaining(base::TimeDelta* remaining) const OVERRIDE; | 132 virtual bool TimeRemaining(base::TimeDelta* remaining) const OVERRIDE; |
122 virtual int64 CurrentSpeed() const OVERRIDE; | 133 virtual int64 CurrentSpeed() const OVERRIDE; |
123 virtual int PercentComplete() const OVERRIDE; | 134 virtual int PercentComplete() const OVERRIDE; |
124 virtual bool AllDataSaved() const OVERRIDE; | 135 virtual bool AllDataSaved() const OVERRIDE; |
125 virtual int64 GetTotalBytes() const OVERRIDE; | 136 virtual int64 GetTotalBytes() const OVERRIDE; |
126 virtual int64 GetReceivedBytes() const OVERRIDE; | 137 virtual int64 GetReceivedBytes() const OVERRIDE; |
127 virtual base::Time GetStartTime() const OVERRIDE; | 138 virtual base::Time GetStartTime() const OVERRIDE; |
128 virtual base::Time GetEndTime() const OVERRIDE; | 139 virtual base::Time GetEndTime() const OVERRIDE; |
129 virtual bool CanShowInFolder() OVERRIDE; | 140 virtual bool CanShowInFolder() OVERRIDE; |
130 virtual bool CanOpenDownload() OVERRIDE; | 141 virtual bool CanOpenDownload() OVERRIDE; |
| 142 virtual bool CanResumeDownload() const OVERRIDE; |
131 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; | 143 virtual bool ShouldOpenFileBasedOnExtension() OVERRIDE; |
132 virtual bool GetOpenWhenComplete() const OVERRIDE; | 144 virtual bool GetOpenWhenComplete() const OVERRIDE; |
133 virtual bool GetAutoOpened() OVERRIDE; | 145 virtual bool GetAutoOpened() OVERRIDE; |
134 virtual bool GetOpened() const OVERRIDE; | 146 virtual bool GetOpened() const OVERRIDE; |
135 virtual BrowserContext* GetBrowserContext() const OVERRIDE; | 147 virtual BrowserContext* GetBrowserContext() const OVERRIDE; |
136 virtual WebContents* GetWebContents() const OVERRIDE; | 148 virtual WebContents* GetWebContents() const OVERRIDE; |
137 virtual void OnContentCheckCompleted(DownloadDangerType danger_type) OVERRIDE; | 149 virtual void OnContentCheckCompleted(DownloadDangerType danger_type) OVERRIDE; |
138 virtual void SetOpenWhenComplete(bool open) OVERRIDE; | 150 virtual void SetOpenWhenComplete(bool open) OVERRIDE; |
139 virtual void SetIsTemporary(bool temporary) OVERRIDE; | 151 virtual void SetIsTemporary(bool temporary) OVERRIDE; |
140 virtual void SetOpened(bool opened) OVERRIDE; | 152 virtual void SetOpened(bool opened) OVERRIDE; |
141 virtual void SetDisplayName(const FilePath& name) OVERRIDE; | 153 virtual void SetDisplayName(const FilePath& name) OVERRIDE; |
142 virtual std::string DebugString(bool verbose) const OVERRIDE; | 154 virtual std::string DebugString(bool verbose) const OVERRIDE; |
143 virtual void MockDownloadOpenForTesting() OVERRIDE; | 155 virtual void MockDownloadOpenForTesting() OVERRIDE; |
| 156 virtual const net::BoundNetLog& GetBoundNetLog() const OVERRIDE; |
144 | 157 |
145 // All remaining public interfaces virtual to allow for DownloadItemImpl | 158 // All remaining public interfaces virtual to allow for DownloadItemImpl |
146 // mocks. | 159 // mocks. |
147 | 160 |
| 161 virtual bool IsResuming() const; |
| 162 virtual ResumeMode GetResumeMode() const; |
| 163 |
148 // Main entry points for regular downloads, in order ------------------------- | 164 // Main entry points for regular downloads, in order ------------------------- |
149 | 165 |
150 // TODO(rdsmith): Fold the process that uses these fully into | 166 // TODO(rdsmith): Fold the process that uses these fully into |
151 // DownloadItemImpl and pass callbacks to the delegate so that all of | 167 // DownloadItemImpl and pass callbacks to the delegate so that all of |
152 // these other than Start() can be made private. | 168 // these other than Start() can be made private. |
153 | 169 |
154 // Start the download | 170 // Start the download. |
155 virtual void Start(scoped_ptr<DownloadFile> download_file); | 171 // |download_file| is the associated file on the storage medium. |
| 172 // |req_handle| is the new request handle associated with the download. |
| 173 virtual void Start(scoped_ptr<DownloadFile> download_file, |
| 174 scoped_ptr<DownloadRequestHandleInterface> req_handle); |
156 | 175 |
157 // If all pre-requisites have been met, complete download processing, i.e. do | 176 // If all pre-requisites have been met, complete download processing, i.e. do |
158 // internal cleanup, file rename, and potentially auto-open. (Dangerous | 177 // internal cleanup, file rename, and potentially auto-open. (Dangerous |
159 // downloads still may block on user acceptance after this point.) | 178 // downloads still may block on user acceptance after this point.) |
160 virtual void MaybeCompleteDownload(); | 179 virtual void MaybeCompleteDownload(); |
161 | 180 |
162 // Needed because of interwining with DownloadManagerImpl -------------------- | 181 // Needed because of intertwining with DownloadManagerImpl ------------------- |
163 | 182 |
164 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, | 183 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, |
165 // removing these from the public interface. | 184 // removing these from the public interface. |
166 | 185 |
167 // Notify observers that this item is being removed by the user. | 186 // Notify observers that this item is being removed by the user. |
168 virtual void NotifyRemoved(); | 187 virtual void NotifyRemoved(); |
169 | 188 |
170 virtual void OnDownloadedFileRemoved(); | 189 virtual void OnDownloadedFileRemoved(); |
171 | 190 |
172 // Provide a weak pointer reference to a DownloadDestinationObserver | 191 // Provide a weak pointer reference to a DownloadDestinationObserver |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 const FilePath& full_path); | 292 const FilePath& full_path); |
274 | 293 |
275 // Called if the embedder took over opening a download, to indicate that | 294 // Called if the embedder took over opening a download, to indicate that |
276 // the download has been opened. | 295 // the download has been opened. |
277 virtual void DelayedDownloadOpened(bool auto_opened); | 296 virtual void DelayedDownloadOpened(bool auto_opened); |
278 | 297 |
279 // Called when the entire download operation (including renaming etc) | 298 // Called when the entire download operation (including renaming etc) |
280 // is completed. | 299 // is completed. |
281 void Completed(); | 300 void Completed(); |
282 | 301 |
| 302 // Initiates a resume operation on an interrupted download. |
| 303 void ResumeInterruptedDownload(); |
| 304 |
283 // Helper routines ----------------------------------------------------------- | 305 // Helper routines ----------------------------------------------------------- |
284 | 306 |
285 // Indicate that an error has occurred on the download. | 307 // Indicate that an error has occurred on the download. |
286 virtual void Interrupt(DownloadInterruptReason reason); | 308 virtual void Interrupt(DownloadInterruptReason reason); |
287 | 309 |
288 // Cancel the DownloadFile if we have it. | 310 // Cancel the DownloadFile if we have it. |
289 void CancelDownloadFile(); | 311 void CancelDownloadFile(); |
290 | 312 |
291 // Check if a download is ready for completion. | 313 // Check if a download is ready for completion. |
292 bool IsDownloadReadyForCompletion(); | 314 bool IsDownloadReadyForCompletion(); |
293 | 315 |
294 // Call to transition state; all state transitions should go through this. | 316 // Call to transition state; all state transitions should go through this. |
295 void TransitionTo(DownloadInternalState new_state); | 317 void TransitionTo(DownloadInternalState new_state); |
296 | 318 |
297 // Set the |danger_type_| and invoke obserers if necessary. | 319 // Set the |danger_type_| and invoke obserers if necessary. |
298 void SetDangerType(DownloadDangerType danger_type); | 320 void SetDangerType(DownloadDangerType danger_type); |
299 | 321 |
300 void SetFullPath(const FilePath& new_path); | 322 void SetFullPath(const FilePath& new_path); |
301 | 323 |
302 // Mapping between internal and external states. | 324 void AutoResumeIfValid(); |
| 325 |
303 static DownloadState InternalToExternalState( | 326 static DownloadState InternalToExternalState( |
304 DownloadInternalState internal_state); | 327 DownloadInternalState internal_state); |
305 static DownloadInternalState ExternalToInternalState( | 328 static DownloadInternalState ExternalToInternalState( |
306 DownloadState external_state); | 329 DownloadState external_state); |
307 | 330 |
308 // Debugging routines -------------------------------------------------------- | 331 // Debugging routines -------------------------------------------------------- |
309 static const char* DebugDownloadStateString(DownloadInternalState state); | 332 static const char* DebugDownloadStateString(DownloadInternalState state); |
| 333 static const char* DebugResumeModeString(ResumeMode mode); |
310 | 334 |
311 // Will be false for save package downloads retrieved from the history. | 335 // Will be false for save package downloads retrieved from the history. |
312 // TODO(rdsmith): Replace with a generalized enum for "download source". | 336 // TODO(rdsmith): Replace with a generalized enum for "download source". |
313 const bool is_save_package_download_; | 337 const bool is_save_package_download_; |
314 | 338 |
315 // The handle to the request information. Used for operations outside the | 339 // The handle to the request information. Used for operations outside the |
316 // download system. | 340 // download system. |
317 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | 341 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
318 | 342 |
319 // Download ID assigned by DownloadResourceHandler. | 343 // Download ID assigned by DownloadResourceHandler. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 | 442 |
419 // Time the download completed. | 443 // Time the download completed. |
420 base::Time end_time_; | 444 base::Time end_time_; |
421 | 445 |
422 // Our delegate. | 446 // Our delegate. |
423 DownloadItemImplDelegate* delegate_; | 447 DownloadItemImplDelegate* delegate_; |
424 | 448 |
425 // In progress downloads may be paused by the user, we note it here. | 449 // In progress downloads may be paused by the user, we note it here. |
426 bool is_paused_; | 450 bool is_paused_; |
427 | 451 |
| 452 // A download has been resumed. |
| 453 bool is_resuming_; |
| 454 |
| 455 // The number of times this download has been resumed automatically. |
| 456 int auto_resume_count_; |
| 457 |
428 // A flag for indicating if the download should be opened at completion. | 458 // A flag for indicating if the download should be opened at completion. |
429 bool open_when_complete_; | 459 bool open_when_complete_; |
430 | 460 |
431 // A flag for indicating if the downloaded file is externally removed. | 461 // A flag for indicating if the downloaded file is externally removed. |
432 bool file_externally_removed_; | 462 bool file_externally_removed_; |
433 | 463 |
434 // Indicates if the download is considered potentially safe or dangerous | 464 // Indicates if the download is considered potentially safe or dangerous |
435 // (executable files are typically considered dangerous). | 465 // (executable files are typically considered dangerous). |
436 SafetyState safety_state_; | 466 SafetyState safety_state_; |
437 | 467 |
(...skipping 30 matching lines...) Expand all Loading... |
468 const net::BoundNetLog bound_net_log_; | 498 const net::BoundNetLog bound_net_log_; |
469 | 499 |
470 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 500 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
471 | 501 |
472 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 502 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
473 }; | 503 }; |
474 | 504 |
475 } // namespace content | 505 } // namespace content |
476 | 506 |
477 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 507 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |