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 <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 int64_t received_bytes, | 66 int64_t received_bytes, |
67 int64_t total_bytes, | 67 int64_t total_bytes, |
68 DownloadItem::DownloadState state, | 68 DownloadItem::DownloadState state, |
69 DownloadDangerType danger_type, | 69 DownloadDangerType danger_type, |
70 DownloadInterruptReason interrupt_reason, | 70 DownloadInterruptReason interrupt_reason, |
71 bool opened, | 71 bool opened, |
72 const net::BoundNetLog& bound_net_log); | 72 const net::BoundNetLog& bound_net_log); |
73 | 73 |
74 // Constructing for a regular download. | 74 // Constructing for a regular download. |
75 // |bound_net_log| is constructed externally for our use. | 75 // |bound_net_log| is constructed externally for our use. |
| 76 // TODO(asanka): Get rid of the DownloadCreateInfo parameter since active |
| 77 // downloads end up at Start() immediately after creation. |
76 DownloadItemImpl(DownloadItemImplDelegate* delegate, | 78 DownloadItemImpl(DownloadItemImplDelegate* delegate, |
77 uint32_t id, | 79 uint32_t id, |
78 const DownloadCreateInfo& info, | 80 const DownloadCreateInfo& info, |
79 const net::BoundNetLog& bound_net_log); | 81 const net::BoundNetLog& bound_net_log); |
80 | 82 |
81 // Constructing for the "Save Page As..." feature: | 83 // Constructing for the "Save Page As..." feature: |
82 // |bound_net_log| is constructed externally for our use. | 84 // |bound_net_log| is constructed externally for our use. |
83 DownloadItemImpl(DownloadItemImplDelegate* delegate, | 85 DownloadItemImpl(DownloadItemImplDelegate* delegate, |
84 uint32_t id, | 86 uint32_t id, |
85 const base::FilePath& path, | 87 const base::FilePath& path, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 std::string DebugString(bool verbose) const override; | 162 std::string DebugString(bool verbose) const override; |
161 | 163 |
162 // All remaining public interfaces virtual to allow for DownloadItemImpl | 164 // All remaining public interfaces virtual to allow for DownloadItemImpl |
163 // mocks. | 165 // mocks. |
164 | 166 |
165 // Determines the resume mode for an interrupted download. Requires | 167 // Determines the resume mode for an interrupted download. Requires |
166 // last_reason_ to be set, but doesn't require the download to be in | 168 // last_reason_ to be set, but doesn't require the download to be in |
167 // INTERRUPTED state. | 169 // INTERRUPTED state. |
168 virtual ResumeMode GetResumeMode() const; | 170 virtual ResumeMode GetResumeMode() const; |
169 | 171 |
170 // Notify the download item that new origin information is available due to a | |
171 // resumption request receiving a response. | |
172 virtual void MergeOriginInfoOnResume( | |
173 const DownloadCreateInfo& new_create_info); | |
174 | |
175 // State transition operations on regular downloads -------------------------- | 172 // State transition operations on regular downloads -------------------------- |
176 | 173 |
177 // Start the download. | 174 // Start the download. |
178 // |download_file| is the associated file on the storage medium. | 175 // |download_file| is the associated file on the storage medium. |
179 // |req_handle| is the new request handle associated with the download. | 176 // |req_handle| is the new request handle associated with the download. |
| 177 // |new_create_info| is a DownloadCreateInfo containing the new response |
| 178 // parameters. It may be different from the DownloadCreateInfo used to create |
| 179 // the DownloadItem if Start() is being called in response for a download |
| 180 // resumption request. |
180 virtual void Start(scoped_ptr<DownloadFile> download_file, | 181 virtual void Start(scoped_ptr<DownloadFile> download_file, |
181 scoped_ptr<DownloadRequestHandleInterface> req_handle); | 182 scoped_ptr<DownloadRequestHandleInterface> req_handle, |
| 183 const DownloadCreateInfo& new_create_info); |
182 | 184 |
183 // Needed because of intertwining with DownloadManagerImpl ------------------- | 185 // Needed because of intertwining with DownloadManagerImpl ------------------- |
184 | 186 |
185 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, | 187 // TODO(rdsmith): Unwind DownloadManagerImpl and DownloadItemImpl, |
186 // removing these from the public interface. | 188 // removing these from the public interface. |
187 | 189 |
188 // Notify observers that this item is being removed by the user. | 190 // Notify observers that this item is being removed by the user. |
189 virtual void NotifyRemoved(); | 191 virtual void NotifyRemoved(); |
190 | 192 |
191 virtual void OnDownloadedFileRemoved(); | 193 virtual void OnDownloadedFileRemoved(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 // On debug builds, state transitions can be verified via | 234 // On debug builds, state transitions can be verified via |
233 // IsValidStateTransition() and IsValidSavePackageStateTransition(). Allowed | 235 // IsValidStateTransition() and IsValidSavePackageStateTransition(). Allowed |
234 // state transitions are described below, both for normal downloads and | 236 // state transitions are described below, both for normal downloads and |
235 // SavePackage downloads. | 237 // SavePackage downloads. |
236 enum DownloadInternalState { | 238 enum DownloadInternalState { |
237 // Initial state. Regular downloads are created in this state until the | 239 // Initial state. Regular downloads are created in this state until the |
238 // Start() call is received. | 240 // Start() call is received. |
239 // | 241 // |
240 // Transitions to (regular): | 242 // Transitions to (regular): |
241 // TARGET_PENDING_INTERNAL: After a successful Start() call. | 243 // TARGET_PENDING_INTERNAL: After a successful Start() call. |
242 // INTERRUPTED_INTERNAL: Afater a failed Start() call. | 244 // INTERRUPTED_TARGET_PENDING_INTERNAL: After a failed Start() call. |
243 // | 245 // |
244 // Transitions to (SavePackage): | 246 // Transitions to (SavePackage): |
245 // <n/a> SavePackage downloads never reach this state. | 247 // <n/a> SavePackage downloads never reach this state. |
246 INITIAL_INTERNAL, | 248 INITIAL_INTERNAL, |
247 | 249 |
248 // Embedder is in the process of determining the target of the download. | 250 // Embedder is in the process of determining the target of the download. |
249 // Since the embedder is sensitive to state transitions during this time, | 251 // Since the embedder is sensitive to state transitions during this time, |
250 // any DestinationError/DestinationCompleted events are deferred until | 252 // any DestinationError/DestinationCompleted events are deferred until |
251 // TARGET_RESOLVED_INTERNAL. | 253 // TARGET_RESOLVED_INTERNAL. |
252 // | 254 // |
253 // Transitions to (regular): | 255 // Transitions to (regular): |
254 // TARGET_RESOLVED_INTERNAL: Once the embedder invokes the callback. | 256 // TARGET_RESOLVED_INTERNAL: Once the embedder invokes the callback. |
| 257 // INTERRUPTED_TARGET_PENDING_INTERNAL: An error occurred prior to target |
| 258 // determination. |
255 // CANCELLED_INTERNAL: Cancelled. | 259 // CANCELLED_INTERNAL: Cancelled. |
256 // | 260 // |
257 // Transitions to (SavePackage): | 261 // Transitions to (SavePackage): |
258 // <n/a> SavePackage downloads never reach this state. | 262 // <n/a> SavePackage downloads never reach this state. |
259 TARGET_PENDING_INTERNAL, | 263 TARGET_PENDING_INTERNAL, |
260 | 264 |
| 265 // Embedder is in the process of determining the target of the download, and |
| 266 // the download is in an interrupted state. The interrupted state is not |
| 267 // exposed to the emedder until target determination is complete. |
| 268 // |
| 269 // Transitions to (regular): |
| 270 // INTERRUPTED_INTERNAL: Once the target is determined, the download |
| 271 // is marked as interrupted. |
| 272 // CANCELLED_INTERNAL: Cancelled. |
| 273 // |
| 274 // Transitions to (SavePackage): |
| 275 // <n/a> SavePackage downloads never reach this state. |
| 276 INTERRUPTED_TARGET_PENDING_INTERNAL, |
| 277 |
261 // Embedder has completed target determination. It is now safe to resolve | 278 // Embedder has completed target determination. It is now safe to resolve |
262 // the download target as well as process deferred DestinationError events. | 279 // the download target as well as process deferred DestinationError events. |
263 // This state is differs from TARGET_PENDING_INTERNAL due to it being | 280 // This state is differs from TARGET_PENDING_INTERNAL due to it being |
264 // allowed to transition to INTERRUPTED_INTERNAL, and it's different from | 281 // allowed to transition to INTERRUPTED_INTERNAL, and it's different from |
265 // IN_PROGRESS_INTERNAL in that entering this state doesn't require having | 282 // IN_PROGRESS_INTERNAL in that entering this state doesn't require having |
266 // a valid target. This state is transient (i.e. DownloadItemImpl will | 283 // a valid target. This state is transient (i.e. DownloadItemImpl will |
267 // transition out of it before yielding execution). It's only purpose in | 284 // transition out of it before yielding execution). It's only purpose in |
268 // life is to ensure the integrity of state transitions. | 285 // life is to ensure the integrity of state transitions. |
269 // | 286 // |
270 // Transitions to (regular): | 287 // Transitions to (regular): |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 // | 338 // |
322 // Transitions to (SavePackage): | 339 // Transitions to (SavePackage): |
323 // <n/a> SavePackage downloads never reach this state. | 340 // <n/a> SavePackage downloads never reach this state. |
324 INTERRUPTED_INTERNAL, | 341 INTERRUPTED_INTERNAL, |
325 | 342 |
326 // A request to resume this interrupted download is in progress. | 343 // A request to resume this interrupted download is in progress. |
327 // | 344 // |
328 // Transitions to (regular): | 345 // Transitions to (regular): |
329 // TARGET_PENDING_INTERNAL: Once a server response is received from a | 346 // TARGET_PENDING_INTERNAL: Once a server response is received from a |
330 // resumption. | 347 // resumption. |
| 348 // INTERRUPTED_TARGET_PENDING_INTERNAL: A server response was received, |
| 349 // but it indicated an error, and the download |
| 350 // needs to go through target determination. |
| 351 // TARGET_RESOLVED_INTERNAL: A resumption attempt received an error |
| 352 // but it was not necessary to go through target |
| 353 // determination. |
331 // CANCELLED_INTERNAL: On cancel. | 354 // CANCELLED_INTERNAL: On cancel. |
332 // | 355 // |
333 // Transitions to (SavePackage): | 356 // Transitions to (SavePackage): |
334 // <n/a> SavePackage downloads never reach this state. | 357 // <n/a> SavePackage downloads never reach this state. |
335 RESUMING_INTERNAL, | 358 RESUMING_INTERNAL, |
336 | 359 |
337 // User has cancelled the download. | 360 // User has cancelled the download. |
338 // TODO(asanka): Merge interrupted and cancelled states. | 361 // TODO(asanka): Merge interrupted and cancelled states. |
339 // | 362 // |
340 // Transitions to (regular): | 363 // Transitions to (regular): |
341 // <none> Terminal state. | 364 // <none> Terminal state. |
342 // | 365 // |
343 // Transitions to (SavePackage): | 366 // Transitions to (SavePackage): |
344 // <none> Terminal state. | 367 // <none> Terminal state. |
345 CANCELLED_INTERNAL, | 368 CANCELLED_INTERNAL, |
346 | 369 |
347 MAX_DOWNLOAD_INTERNAL_STATE, | 370 MAX_DOWNLOAD_INTERNAL_STATE, |
348 }; | 371 }; |
349 | 372 |
350 // Used with TransitionTo() to indicate whether or not to call | |
351 // UpdateObservers() after the state transition. | |
352 enum ShouldUpdateObservers { | |
353 UPDATE_OBSERVERS, | |
354 DONT_UPDATE_OBSERVERS | |
355 }; | |
356 | |
357 // Normal progression of a download ------------------------------------------ | 373 // Normal progression of a download ------------------------------------------ |
358 | 374 |
359 // These are listed in approximately chronological order. There are also | 375 // These are listed in approximately chronological order. There are also |
360 // public methods involved in normal download progression; see | 376 // public methods involved in normal download progression; see |
361 // the implementation ordering in download_item_impl.cc. | 377 // the implementation ordering in download_item_impl.cc. |
362 | 378 |
363 // Construction common to all constructors. |active| should be true for new | 379 // Construction common to all constructors. |active| should be true for new |
364 // downloads and false for downloads from the history. | 380 // downloads and false for downloads from the history. |
365 // |download_type| indicates to the net log system what kind of download | 381 // |download_type| indicates to the net log system what kind of download |
366 // this is. | 382 // this is. |
367 void Init(bool active, DownloadType download_type); | 383 void Init(bool active, DownloadType download_type); |
368 | 384 |
| 385 // Callback from file thread when we initialize the DownloadFile. |
| 386 void OnDownloadFileInitialized(DownloadInterruptReason result); |
| 387 |
| 388 // Called to determine the target path. Will cause OnDownloadTargetDetermined |
| 389 // to be called when the target information is available. |
| 390 void DetermineDownloadTarget(); |
| 391 |
369 // Called when the target path has been determined. |target_path| is the | 392 // Called when the target path has been determined. |target_path| is the |
370 // suggested target path. |disposition| indicates how the target path should | 393 // suggested target path. |disposition| indicates how the target path should |
371 // be used (see TargetDisposition). |danger_type| is the danger level of | 394 // be used (see TargetDisposition). |danger_type| is the danger level of |
372 // |target_path| as determined by the caller. |intermediate_path| is the path | 395 // |target_path| as determined by the caller. |intermediate_path| is the path |
373 // to use to store the download until OnDownloadCompleting() is called. | 396 // to use to store the download until OnDownloadCompleting() is called. |
374 virtual void OnDownloadTargetDetermined( | 397 virtual void OnDownloadTargetDetermined( |
375 const base::FilePath& target_path, | 398 const base::FilePath& target_path, |
376 TargetDisposition disposition, | 399 TargetDisposition disposition, |
377 DownloadDangerType danger_type, | 400 DownloadDangerType danger_type, |
378 const base::FilePath& intermediate_path); | 401 const base::FilePath& intermediate_path); |
379 | 402 |
380 // Callback from file thread when we initialize the DownloadFile. | |
381 void OnDownloadFileInitialized(DownloadInterruptReason result); | |
382 | |
383 void OnDownloadRenamedToIntermediateName( | 403 void OnDownloadRenamedToIntermediateName( |
384 DownloadInterruptReason reason, const base::FilePath& full_path); | 404 DownloadInterruptReason reason, const base::FilePath& full_path); |
385 | 405 |
386 // If all pre-requisites have been met, complete download processing, i.e. do | 406 // If all pre-requisites have been met, complete download processing, i.e. do |
387 // internal cleanup, file rename, and potentially auto-open. (Dangerous | 407 // internal cleanup, file rename, and potentially auto-open. (Dangerous |
388 // downloads still may block on user acceptance after this point.) | 408 // downloads still may block on user acceptance after this point.) |
389 void MaybeCompleteDownload(); | 409 void MaybeCompleteDownload(); |
390 | 410 |
391 // Called when the download is ready to complete. | 411 // Called when the download is ready to complete. |
392 // This may perform final rename if necessary and will eventually call | 412 // This may perform final rename if necessary and will eventually call |
393 // DownloadItem::Completed(). | 413 // DownloadItem::Completed(). |
394 void OnDownloadCompleting(); | 414 void OnDownloadCompleting(); |
395 | 415 |
396 void OnDownloadRenamedToFinalName(DownloadInterruptReason reason, | 416 void OnDownloadRenamedToFinalName(DownloadInterruptReason reason, |
397 const base::FilePath& full_path); | 417 const base::FilePath& full_path); |
398 | 418 |
399 // Called if the embedder took over opening a download, to indicate that | 419 // Called if the embedder took over opening a download, to indicate that |
400 // the download has been opened. | 420 // the download has been opened. |
401 void DelayedDownloadOpened(bool auto_opened); | 421 void DelayedDownloadOpened(bool auto_opened); |
402 | 422 |
403 // Called when the entire download operation (including renaming etc.) | 423 // Called when the entire download operation (including renaming etc.) |
404 // is completed. | 424 // is completed. |
405 void Completed(); | 425 void Completed(); |
406 | 426 |
407 // Callback invoked when the URLRequest for a download resumption has started. | |
408 void OnResumeRequestStarted(DownloadItem* item, | |
409 DownloadInterruptReason interrupt_reason); | |
410 | |
411 // Helper routines ----------------------------------------------------------- | 427 // Helper routines ----------------------------------------------------------- |
412 | 428 |
413 // Indicate that an error has occurred on the download. | 429 // Indicate that an error has occurred on the download. |
414 void Interrupt(DownloadInterruptReason reason); | 430 void Interrupt(DownloadInterruptReason reason); |
415 | 431 |
416 // Destroy the DownloadFile object. If |destroy_file| is true, the file is | 432 // Destroy the DownloadFile object. If |destroy_file| is true, the file is |
417 // destroyed with it. Otherwise, DownloadFile::Detach() is called before | 433 // destroyed with it. Otherwise, DownloadFile::Detach() is called before |
418 // object destruction to prevent file destruction. Destroying the file also | 434 // object destruction to prevent file destruction. Destroying the file also |
419 // resets |current_path_|. | 435 // resets |current_path_|. |
420 void ReleaseDownloadFile(bool destroy_file); | 436 void ReleaseDownloadFile(bool destroy_file); |
421 | 437 |
422 // Check if a download is ready for completion. The callback provided | 438 // Check if a download is ready for completion. The callback provided |
423 // may be called at some point in the future if an external entity | 439 // may be called at some point in the future if an external entity |
424 // state has change s.t. this routine should be checked again. | 440 // state has change s.t. this routine should be checked again. |
425 bool IsDownloadReadyForCompletion(const base::Closure& state_change_notify); | 441 bool IsDownloadReadyForCompletion(const base::Closure& state_change_notify); |
426 | 442 |
427 // Call to transition state; all state transitions should go through this. | 443 // Call to transition state; all state transitions should go through this. |
428 // |notify_action| specifies whether or not to call UpdateObservers() after | 444 // |notify_action| specifies whether or not to call UpdateObservers() after |
429 // the state transition. | 445 // the state transition. |
430 void TransitionTo(DownloadInternalState new_state, | 446 void TransitionTo(DownloadInternalState new_state); |
431 ShouldUpdateObservers notify_action); | |
432 | 447 |
433 // Set the |danger_type_| and invoke observers if necessary. | 448 // Set the |danger_type_| and invoke observers if necessary. |
434 void SetDangerType(DownloadDangerType danger_type); | 449 void SetDangerType(DownloadDangerType danger_type); |
435 | 450 |
436 void SetFullPath(const base::FilePath& new_path); | 451 void SetFullPath(const base::FilePath& new_path); |
437 | 452 |
438 void AutoResumeIfValid(); | 453 void AutoResumeIfValid(); |
439 | 454 |
440 void ResumeInterruptedDownload(); | 455 void ResumeInterruptedDownload(); |
441 | 456 |
| 457 // Update origin information based on the response to a download resumption |
| 458 // request. Should only be called if the resumption request was successful. |
| 459 virtual void UpdateValidatorsOnResumption( |
| 460 const DownloadCreateInfo& new_create_info); |
| 461 |
442 static DownloadState InternalToExternalState( | 462 static DownloadState InternalToExternalState( |
443 DownloadInternalState internal_state); | 463 DownloadInternalState internal_state); |
444 static DownloadInternalState ExternalToInternalState( | 464 static DownloadInternalState ExternalToInternalState( |
445 DownloadState external_state); | 465 DownloadState external_state); |
446 | 466 |
447 // Debugging routines -------------------------------------------------------- | 467 // Debugging routines -------------------------------------------------------- |
448 static const char* DebugDownloadStateString(DownloadInternalState state); | 468 static const char* DebugDownloadStateString(DownloadInternalState state); |
449 static const char* DebugResumeModeString(ResumeMode mode); | 469 static const char* DebugResumeModeString(ResumeMode mode); |
450 static bool IsValidSavePackageStateTransition(DownloadInternalState from, | 470 static bool IsValidSavePackageStateTransition(DownloadInternalState from, |
451 DownloadInternalState to); | 471 DownloadInternalState to); |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
618 const net::BoundNetLog bound_net_log_; | 638 const net::BoundNetLog bound_net_log_; |
619 | 639 |
620 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 640 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
621 | 641 |
622 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 642 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
623 }; | 643 }; |
624 | 644 |
625 } // namespace content | 645 } // namespace content |
626 | 646 |
627 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 647 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |