Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: content/browser/download/download_item_impl.h

Issue 1691543002: [Downloads] Enforce state transition integrity and state invariants. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 virtual void MarkAsComplete(); 210 virtual void MarkAsComplete();
211 211
212 // DownloadDestinationObserver 212 // DownloadDestinationObserver
213 void DestinationUpdate(int64_t bytes_so_far, 213 void DestinationUpdate(int64_t bytes_so_far,
214 int64_t bytes_per_sec, 214 int64_t bytes_per_sec,
215 const std::string& hash_state) override; 215 const std::string& hash_state) override;
216 void DestinationError(DownloadInterruptReason reason) override; 216 void DestinationError(DownloadInterruptReason reason) override;
217 void DestinationCompleted(const std::string& final_hash) override; 217 void DestinationCompleted(const std::string& final_hash) override;
218 218
219 private: 219 private:
220 // Fine grained states of a download. Note that active downloads are created 220 // Fine grained states of a download.
221 // in IN_PROGRESS_INTERNAL state. However, downloads creates via history can 221 //
222 // be created in COMPLETE_INTERNAL, CANCELLED_INTERNAL and 222 // New downloads can be created in the following states:
223 // INTERRUPTED_INTERNAL. 223 //
224 // INITIAL_INTERNAL: All active new downloads.
225 //
226 // COMPLETE_INTERNAL: Downloads restored from persisted state.
227 // CANCELLED_INTERNAL: - do -
228 // INTERRUPTED_INTERNAL: - do -
229 //
230 // IN_PROGRESS_INTERNAL: SavePackage downloads.
231 //
232 // On debug builds, state transitions can be verified via
233 // IsValidStateTransition() and IsValidSavePackageStateTransition(). Allowed
234 // state transitions are described below, both for normal downloads and
235 // SavePackage downloads.
236 enum DownloadInternalState {
237 // Initial state. Regular downloads are created in this state until the
238 // Start() call is received.
239 //
240 // Transitions to (regular):
241 // TARGET_PENDING_INTERNAL: After a successful Start() call.
242 // INTERRUPTED_INTERNAL: Afater a failed Start() call.
243 //
244 // Transitions to (SavePackage):
245 // <n/a> SavePackage downloads never reach this state.
246 INITIAL_INTERNAL,
224 247
225 enum DownloadInternalState { 248 // Embedder is in the process of determining the target of the download.
226 // Includes both before and after file name determination, and paused 249 // Since the embedder is sensitive to state transitions during this time,
227 // downloads. 250 // any DestinationError/DestinationCompleted events are deferred until
228 // TODO(rdsmith): Put in state variable for file name determination. 251 // TARGET_RESOLVED_INTERNAL.
229 // Transitions from: 252 //
230 // <Initial creation> Active downloads are created in this state. 253 // Transitions to (regular):
231 // RESUMING_INTERNAL 254 // TARGET_RESOLVED_INTERNAL: Once the embedder invokes the callback.
232 // Transitions to: 255 // CANCELLED_INTERNAL: Cancelled.
233 // COMPLETING_INTERNAL On final rename completion. 256 //
234 // CANCELLED_INTERNAL On cancel. 257 // Transitions to (SavePackage):
235 // INTERRUPTED_INTERNAL On interrupt. 258 // <n/a> SavePackage downloads never reach this state.
236 // COMPLETE_INTERNAL On SavePackage download completion. 259 TARGET_PENDING_INTERNAL,
260
261 // Embedder has completed target determination. It is now safe to resolve
262 // the download target as well as process deferred DestinationError events.
263 // This state is differs from TARGET_PENDING_INTERNAL due to it being
264 // allowed to transition to INTERRUPTED_INTERNAL, and it's different from
265 // IN_PROGRESS_INTERNAL in that entering this state doesn't require having
266 // a valid target. This state is transient (i.e. DownloadItemImpl will
267 // transition out of it before yielding execution). It's only purpose in
268 // life is to ensure the integrity of state transitions.
269 //
270 // Transitions to (regular):
271 // IN_PROGRESS_INTERNAL: Target successfully determined. The incoming
272 // data stream can now be written to the target.
273 // INTERRUPTED_INTERNAL: Either the target determination or one of the
274 // deferred signals indicated that the download
275 // should be interrupted.
276 // CANCELLED_INTERNAL: User cancelled the download or there was a
277 // deferred Cancel() call.
278 //
279 // Transitions to (SavePackage):
280 // <n/a> SavePackage downloads never reach this state.
281 TARGET_RESOLVED_INTERNAL,
282
283 // Download target is known and the data can be transferred from our source
284 // to our sink.
285 //
286 // Transitions to (regular):
287 // COMPLETING_INTERNAL: On final rename completion.
288 // CANCELLED_INTERNAL: On cancel.
289 // INTERRUPTED_INTERNAL: On interrupt.
290 //
291 // Transitions to (SavePackage):
292 // COMPLETE_INTERNAL: On completion.
293 // CANCELLED_INTERNAL: On cancel.
237 IN_PROGRESS_INTERNAL, 294 IN_PROGRESS_INTERNAL,
238 295
239 // Between commit point (dispatch of download file release) and completed. 296 // Between commit point (dispatch of download file release) and completed.
240 // Embedder may be opening the file in this state. 297 // Embedder may be opening the file in this state.
241 // Transitions from: 298 //
242 // IN_PROGRESS_INTERNAL 299 // Transitions to (regular):
243 // Transitions to: 300 // COMPLETE_INTERNAL: On successful completion.
244 // COMPLETE_INTERNAL On successful completion. 301 //
302 // Transitions to (SavePackage):
303 // <n/a> SavePackage downloads never reach this state.
245 COMPLETING_INTERNAL, 304 COMPLETING_INTERNAL,
246 305
247 // After embedder has had a chance to auto-open. User may now open 306 // After embedder has had a chance to auto-open. User may now open
248 // or auto-open based on extension. 307 // or auto-open based on extension.
249 // Transitions from: 308 //
250 // COMPLETING_INTERNAL 309 // Transitions to (regular):
251 // IN_PROGRESS_INTERNAL SavePackage only. 310 // <none> Terminal state.
252 // <Initial creation> Completed persisted downloads. 311 //
253 // Transitions to: 312 // Transitions to (SavePackage):
254 // <none> Terminal state. 313 // <none> Terminal state.
255 COMPLETE_INTERNAL, 314 COMPLETE_INTERNAL,
256 315
257 // User has cancelled the download.
258 // Transitions from:
259 // IN_PROGRESS_INTERNAL
260 // INTERRUPTED_INTERNAL
261 // RESUMING_INTERNAL
262 // <Initial creation> Canceleld persisted downloads.
263 // Transitions to:
264 // <none> Terminal state.
265 CANCELLED_INTERNAL,
266
267 // An error has interrupted the download. 316 // An error has interrupted the download.
268 // Transitions from: 317 //
269 // IN_PROGRESS_INTERNAL 318 // Transitions to (regular):
270 // RESUMING_INTERNAL 319 // RESUMING_INTERNAL: On resumption.
271 // <Initial creation> Interrupted persisted downloads. 320 // CANCELLED_INTERNAL: On cancel.
272 // Transitions to: 321 //
273 // RESUMING_INTERNAL On resumption. 322 // Transitions to (SavePackage):
323 // <n/a> SavePackage downloads never reach this state.
274 INTERRUPTED_INTERNAL, 324 INTERRUPTED_INTERNAL,
275 325
276 // A request to resume this interrupted download is in progress. 326 // A request to resume this interrupted download is in progress.
277 // Transitions from: 327 //
278 // INTERRUPTED_INTERNAL 328 // Transitions to (regular):
279 // Transitions to: 329 // TARGET_PENDING_INTERNAL: Once a server response is received from a
280 // IN_PROGRESS_INTERNAL Once a server response is received from a 330 // resumption.
281 // resumption. 331 // CANCELLED_INTERNAL: On cancel.
282 // INTERRUPTED_INTERNAL If the resumption request fails. 332 //
283 // CANCELLED_INTERNAL On cancel. 333 // Transitions to (SavePackage):
334 // <n/a> SavePackage downloads never reach this state.
284 RESUMING_INTERNAL, 335 RESUMING_INTERNAL,
285 336
337 // User has cancelled the download.
338 // TODO(asanka): Merge interrupted and cancelled states.
339 //
340 // Transitions to (regular):
341 // <none> Terminal state.
342 //
343 // Transitions to (SavePackage):
344 // <none> Terminal state.
345 CANCELLED_INTERNAL,
346
286 MAX_DOWNLOAD_INTERNAL_STATE, 347 MAX_DOWNLOAD_INTERNAL_STATE,
287 }; 348 };
288 349
289 // Used with TransitionTo() to indicate whether or not to call 350 // Used with TransitionTo() to indicate whether or not to call
290 // UpdateObservers() after the state transition. 351 // UpdateObservers() after the state transition.
291 enum ShouldUpdateObservers { 352 enum ShouldUpdateObservers {
292 UPDATE_OBSERVERS, 353 UPDATE_OBSERVERS,
293 DONT_UPDATE_OBSERVERS 354 DONT_UPDATE_OBSERVERS
294 }; 355 };
295 356
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 // DownloadItem::Completed(). 393 // DownloadItem::Completed().
333 void OnDownloadCompleting(); 394 void OnDownloadCompleting();
334 395
335 void OnDownloadRenamedToFinalName(DownloadInterruptReason reason, 396 void OnDownloadRenamedToFinalName(DownloadInterruptReason reason,
336 const base::FilePath& full_path); 397 const base::FilePath& full_path);
337 398
338 // Called if the embedder took over opening a download, to indicate that 399 // Called if the embedder took over opening a download, to indicate that
339 // the download has been opened. 400 // the download has been opened.
340 void DelayedDownloadOpened(bool auto_opened); 401 void DelayedDownloadOpened(bool auto_opened);
341 402
342 // Called when the entire download operation (including renaming etc) 403 // Called when the entire download operation (including renaming etc.)
343 // is completed. 404 // is completed.
344 void Completed(); 405 void Completed();
345 406
346 // Callback invoked when the URLRequest for a download resumption has started. 407 // Callback invoked when the URLRequest for a download resumption has started.
347 void OnResumeRequestStarted(DownloadItem* item, 408 void OnResumeRequestStarted(DownloadItem* item,
348 DownloadInterruptReason interrupt_reason); 409 DownloadInterruptReason interrupt_reason);
349 410
350 // Helper routines ----------------------------------------------------------- 411 // Helper routines -----------------------------------------------------------
351 412
352 // Indicate that an error has occurred on the download. 413 // Indicate that an error has occurred on the download.
353 void Interrupt(DownloadInterruptReason reason); 414 void Interrupt(DownloadInterruptReason reason);
354 415
355 // Destroy the DownloadFile object. If |destroy_file| is true, the file is 416 // Destroy the DownloadFile object. If |destroy_file| is true, the file is
356 // destroyed with it. Otherwise, DownloadFile::Detach() is called before 417 // destroyed with it. Otherwise, DownloadFile::Detach() is called before
357 // object destruction to prevent file destruction. Destroying the file also 418 // object destruction to prevent file destruction. Destroying the file also
358 // resets |current_path_|. 419 // resets |current_path_|.
359 void ReleaseDownloadFile(bool destroy_file); 420 void ReleaseDownloadFile(bool destroy_file);
360 421
361 // Check if a download is ready for completion. The callback provided 422 // Check if a download is ready for completion. The callback provided
362 // may be called at some point in the future if an external entity 423 // may be called at some point in the future if an external entity
363 // state has change s.t. this routine should be checked again. 424 // state has change s.t. this routine should be checked again.
364 bool IsDownloadReadyForCompletion(const base::Closure& state_change_notify); 425 bool IsDownloadReadyForCompletion(const base::Closure& state_change_notify);
365 426
366 // Call to transition state; all state transitions should go through this. 427 // Call to transition state; all state transitions should go through this.
367 // |notify_action| specifies whether or not to call UpdateObservers() after 428 // |notify_action| specifies whether or not to call UpdateObservers() after
368 // the state transition. 429 // the state transition.
369 void TransitionTo(DownloadInternalState new_state, 430 void TransitionTo(DownloadInternalState new_state,
370 ShouldUpdateObservers notify_action); 431 ShouldUpdateObservers notify_action);
371 432
372 // Set the |danger_type_| and invoke obserers if necessary. 433 // Set the |danger_type_| and invoke observers if necessary.
373 void SetDangerType(DownloadDangerType danger_type); 434 void SetDangerType(DownloadDangerType danger_type);
374 435
375 void SetFullPath(const base::FilePath& new_path); 436 void SetFullPath(const base::FilePath& new_path);
376 437
377 void AutoResumeIfValid(); 438 void AutoResumeIfValid();
378 439
379 void ResumeInterruptedDownload(); 440 void ResumeInterruptedDownload();
380 441
381 static DownloadState InternalToExternalState( 442 static DownloadState InternalToExternalState(
382 DownloadInternalState internal_state); 443 DownloadInternalState internal_state);
383 static DownloadInternalState ExternalToInternalState( 444 static DownloadInternalState ExternalToInternalState(
384 DownloadState external_state); 445 DownloadState external_state);
385 446
386 // Debugging routines -------------------------------------------------------- 447 // Debugging routines --------------------------------------------------------
387 static const char* DebugDownloadStateString(DownloadInternalState state); 448 static const char* DebugDownloadStateString(DownloadInternalState state);
388 static const char* DebugResumeModeString(ResumeMode mode); 449 static const char* DebugResumeModeString(ResumeMode mode);
450 static bool IsValidSavePackageStateTransition(DownloadInternalState from,
451 DownloadInternalState to);
452 static bool IsValidStateTransition(DownloadInternalState from,
453 DownloadInternalState to);
389 454
390 // Will be false for save package downloads retrieved from the history. 455 // Will be false for save package downloads retrieved from the history.
391 // TODO(rdsmith): Replace with a generalized enum for "download source". 456 // TODO(rdsmith): Replace with a generalized enum for "download source".
392 const bool is_save_package_download_; 457 const bool is_save_package_download_;
393 458
394 // The handle to the request information. Used for operations outside the 459 // The handle to the request information. Used for operations outside the
395 // download system. 460 // download system.
396 scoped_ptr<DownloadRequestHandleInterface> request_handle_; 461 scoped_ptr<DownloadRequestHandleInterface> request_handle_;
397 462
398 uint32_t download_id_; 463 uint32_t download_id_;
(...skipping 30 matching lines...) Expand all
429 494
430 // Filename suggestion from DownloadSaveInfo. It could, among others, be the 495 // Filename suggestion from DownloadSaveInfo. It could, among others, be the
431 // suggested filename in 'download' attribute of an anchor. Details: 496 // suggested filename in 'download' attribute of an anchor. Details:
432 // http://www.whatwg.org/specs/web-apps/current-work/#downloading-hyperlinks 497 // http://www.whatwg.org/specs/web-apps/current-work/#downloading-hyperlinks
433 std::string suggested_filename_; 498 std::string suggested_filename_;
434 499
435 // If non-empty, contains an externally supplied path that should be used as 500 // If non-empty, contains an externally supplied path that should be used as
436 // the target path. 501 // the target path.
437 base::FilePath forced_file_path_; 502 base::FilePath forced_file_path_;
438 503
439 // Page transition that triggerred the download. 504 // Page transition that triggered the download.
440 ui::PageTransition transition_type_; 505 ui::PageTransition transition_type_;
441 506
442 // Whether the download was triggered with a user gesture. 507 // Whether the download was triggered with a user gesture.
443 bool has_user_gesture_; 508 bool has_user_gesture_;
444 509
445 // Information from the request. 510 // Information from the request.
446 // Content-disposition field from the header. 511 // Content-disposition field from the header.
447 std::string content_disposition_; 512 std::string content_disposition_;
448 513
449 // Mime-type from the header. Subject to change. 514 // Mime-type from the header. Subject to change.
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 const net::BoundNetLog bound_net_log_; 618 const net::BoundNetLog bound_net_log_;
554 619
555 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; 620 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_;
556 621
557 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); 622 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl);
558 }; 623 };
559 624
560 } // namespace content 625 } // namespace content
561 626
562 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ 627 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/downloads/downloads_api_browsertest.cc ('k') | content/browser/download/download_item_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698