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> |
11 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/logging.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
17 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
19 #include "content/browser/download/download_net_log_parameters.h" | 20 #include "content/browser/download/download_net_log_parameters.h" |
20 #include "content/browser/download/download_request_handle.h" | 21 #include "content/browser/download/download_request_handle.h" |
21 #include "content/common/content_export.h" | 22 #include "content/common/content_export.h" |
22 #include "content/public/browser/download_destination_observer.h" | 23 #include "content/public/browser/download_destination_observer.h" |
23 #include "content/public/browser/download_interrupt_reasons.h" | 24 #include "content/public/browser/download_interrupt_reasons.h" |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 virtual void MarkAsComplete(); | 211 virtual void MarkAsComplete(); |
211 | 212 |
212 // DownloadDestinationObserver | 213 // DownloadDestinationObserver |
213 void DestinationUpdate(int64_t bytes_so_far, | 214 void DestinationUpdate(int64_t bytes_so_far, |
214 int64_t bytes_per_sec, | 215 int64_t bytes_per_sec, |
215 const std::string& hash_state) override; | 216 const std::string& hash_state) override; |
216 void DestinationError(DownloadInterruptReason reason) override; | 217 void DestinationError(DownloadInterruptReason reason) override; |
217 void DestinationCompleted(const std::string& final_hash) override; | 218 void DestinationCompleted(const std::string& final_hash) override; |
218 | 219 |
219 private: | 220 private: |
220 // Fine grained states of a download. Note that active downloads are created | 221 // Fine grained states of a download. |
221 // in IN_PROGRESS_INTERNAL state. However, downloads creates via history can | 222 // |
222 // be created in COMPLETE_INTERNAL, CANCELLED_INTERNAL and | 223 // New downloads can be created in the following states: |
223 // INTERRUPTED_INTERNAL. | 224 // |
| 225 // INITIAL_INTERNAL: All active new downloads. |
| 226 // |
| 227 // COMPLETE_INTERNAL: Downloads restored from persisted state. |
| 228 // CANCELLED_INTERNAL: - do - |
| 229 // INTERRUPTED_INTERNAL: - do - |
| 230 // |
| 231 // IN_PROGRESS_INTERNAL: SavePackage downloads. |
| 232 // |
| 233 // On debug builds, state transitions can be verified via |
| 234 // IsValidStateTransition() and IsValidSavePackageStateTransition(). Allowed |
| 235 // state transitions are described below, both for normal downloads and |
| 236 // SavePackage downloads. |
| 237 enum DownloadInternalState { |
| 238 // Initial state. Regular downloads are created in this state until the |
| 239 // Start() call is received. |
| 240 // |
| 241 // Transitions to (regular): |
| 242 // TARGET_PENDING_INTERNAL: After a successful Start() call. |
| 243 // INTERRUPTED_INTERNAL: Afater a failed Start() call. |
| 244 // |
| 245 // Transitions to (SavePackage): |
| 246 // <n/a> SavePackage downloads never reach this state. |
| 247 INITIAL_INTERNAL, |
224 | 248 |
225 enum DownloadInternalState { | 249 // Embedder is in the process of determining the target of the download. |
226 // Includes both before and after file name determination, and paused | 250 // Since the embedder is sensitive to state transitions during this time, |
227 // downloads. | 251 // any DestinationError/DestinationCompleted events are deferred until |
228 // TODO(rdsmith): Put in state variable for file name determination. | 252 // TARGET_RESOLVED_INTERNAL. |
229 // Transitions from: | 253 // |
230 // <Initial creation> Active downloads are created in this state. | 254 // Transitions to (regular): |
231 // RESUMING_INTERNAL | 255 // TARGET_RESOLVED_INTERNAL: Once the embedder invokes the callback. |
232 // Transitions to: | 256 // CANCELLED_INTERNAL: Cancelled. |
233 // COMPLETING_INTERNAL On final rename completion. | 257 // |
234 // CANCELLED_INTERNAL On cancel. | 258 // Transitions to (SavePackage): |
235 // INTERRUPTED_INTERNAL On interrupt. | 259 // <n/a> SavePackage downloads never reach this state. |
236 // COMPLETE_INTERNAL On SavePackage download completion. | 260 TARGET_PENDING_INTERNAL, |
| 261 |
| 262 // Embedder has completed target determination. It is now safe to resolve |
| 263 // the download target as well as process deferred DestinationError events. |
| 264 // This state is differs from TARGET_PENDING_INTERNAL due to it being |
| 265 // allowed to transition to INTERRUPTED_INTERNAL, and it's different from |
| 266 // IN_PROGRESS_INTERNAL in that entering this state doesn't require having |
| 267 // a valid target. This state is transient (i.e. DownloadItemImpl will |
| 268 // transition out of it before yielding execution). It's only purpose in |
| 269 // life is to ensure the integrity of state transitions. |
| 270 // |
| 271 // Transitions to (regular): |
| 272 // IN_PROGRESS_INTERNAL: Target successfully determined. The incoming |
| 273 // data stream can now be written to the target. |
| 274 // INTERRUPTED_INTERNAL: Either the target determination or one of the |
| 275 // deferred signals indicated that the download |
| 276 // should be interrupted. |
| 277 // CANCELLED_INTERNAL: User cancelled the download or there was a |
| 278 // deferred Cancel() call. |
| 279 // |
| 280 // Transitions to (SavePackage): |
| 281 // <n/a> SavePackage downloads never reach this state. |
| 282 TARGET_RESOLVED_INTERNAL, |
| 283 |
| 284 // Download target is known and the data can be transferred from our source |
| 285 // to our sink. |
| 286 // |
| 287 // Transitions to (regular): |
| 288 // COMPLETING_INTERNAL: On final rename completion. |
| 289 // CANCELLED_INTERNAL: On cancel. |
| 290 // INTERRUPTED_INTERNAL: On interrupt. |
| 291 // |
| 292 // Transitions to (SavePackage): |
| 293 // COMPLETE_INTERNAL: On completion. |
| 294 // CANCELLED_INTERNAL: On cancel. |
237 IN_PROGRESS_INTERNAL, | 295 IN_PROGRESS_INTERNAL, |
238 | 296 |
239 // Between commit point (dispatch of download file release) and completed. | 297 // Between commit point (dispatch of download file release) and completed. |
240 // Embedder may be opening the file in this state. | 298 // Embedder may be opening the file in this state. |
241 // Transitions from: | 299 // |
242 // IN_PROGRESS_INTERNAL | 300 // Transitions to (regular): |
243 // Transitions to: | 301 // COMPLETE_INTERNAL: On successful completion. |
244 // COMPLETE_INTERNAL On successful completion. | 302 // |
| 303 // Transitions to (SavePackage): |
| 304 // <n/a> SavePackage downloads never reach this state. |
245 COMPLETING_INTERNAL, | 305 COMPLETING_INTERNAL, |
246 | 306 |
247 // After embedder has had a chance to auto-open. User may now open | 307 // After embedder has had a chance to auto-open. User may now open |
248 // or auto-open based on extension. | 308 // or auto-open based on extension. |
249 // Transitions from: | 309 // |
250 // COMPLETING_INTERNAL | 310 // Transitions to (regular): |
251 // IN_PROGRESS_INTERNAL SavePackage only. | 311 // <none> Terminal state. |
252 // <Initial creation> Completed persisted downloads. | 312 // |
253 // Transitions to: | 313 // Transitions to (SavePackage): |
254 // <none> Terminal state. | 314 // <none> Terminal state. |
255 COMPLETE_INTERNAL, | 315 COMPLETE_INTERNAL, |
256 | 316 |
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. | 317 // An error has interrupted the download. |
268 // Transitions from: | 318 // |
269 // IN_PROGRESS_INTERNAL | 319 // Transitions to (regular): |
270 // RESUMING_INTERNAL | 320 // RESUMING_INTERNAL: On resumption. |
271 // <Initial creation> Interrupted persisted downloads. | 321 // CANCELLED_INTERNAL: On cancel. |
272 // Transitions to: | 322 // |
273 // RESUMING_INTERNAL On resumption. | 323 // Transitions to (SavePackage): |
| 324 // <n/a> SavePackage downloads never reach this state. |
274 INTERRUPTED_INTERNAL, | 325 INTERRUPTED_INTERNAL, |
275 | 326 |
276 // A request to resume this interrupted download is in progress. | 327 // A request to resume this interrupted download is in progress. |
277 // Transitions from: | 328 // |
278 // INTERRUPTED_INTERNAL | 329 // Transitions to (regular): |
279 // Transitions to: | 330 // TARGET_PENDING_INTERNAL: Once a server response is received from a |
280 // IN_PROGRESS_INTERNAL Once a server response is received from a | 331 // resumption. |
281 // resumption. | 332 // CANCELLED_INTERNAL: On cancel. |
282 // INTERRUPTED_INTERNAL If the resumption request fails. | 333 // |
283 // CANCELLED_INTERNAL On cancel. | 334 // Transitions to (SavePackage): |
| 335 // <n/a> SavePackage downloads never reach this state. |
284 RESUMING_INTERNAL, | 336 RESUMING_INTERNAL, |
285 | 337 |
| 338 // User has cancelled the download. |
| 339 // TODO(asanka): Merge interrupted and cancelled states. |
| 340 // |
| 341 // Transitions to (regular): |
| 342 // <none> Terminal state. |
| 343 // |
| 344 // Transitions to (SavePackage): |
| 345 // <none> Terminal state. |
| 346 CANCELLED_INTERNAL, |
| 347 |
286 MAX_DOWNLOAD_INTERNAL_STATE, | 348 MAX_DOWNLOAD_INTERNAL_STATE, |
287 }; | 349 }; |
288 | 350 |
289 // Used with TransitionTo() to indicate whether or not to call | 351 // Used with TransitionTo() to indicate whether or not to call |
290 // UpdateObservers() after the state transition. | 352 // UpdateObservers() after the state transition. |
291 enum ShouldUpdateObservers { | 353 enum ShouldUpdateObservers { |
292 UPDATE_OBSERVERS, | 354 UPDATE_OBSERVERS, |
293 DONT_UPDATE_OBSERVERS | 355 DONT_UPDATE_OBSERVERS |
294 }; | 356 }; |
295 | 357 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 void ResumeInterruptedDownload(); | 441 void ResumeInterruptedDownload(); |
380 | 442 |
381 static DownloadState InternalToExternalState( | 443 static DownloadState InternalToExternalState( |
382 DownloadInternalState internal_state); | 444 DownloadInternalState internal_state); |
383 static DownloadInternalState ExternalToInternalState( | 445 static DownloadInternalState ExternalToInternalState( |
384 DownloadState external_state); | 446 DownloadState external_state); |
385 | 447 |
386 // Debugging routines -------------------------------------------------------- | 448 // Debugging routines -------------------------------------------------------- |
387 static const char* DebugDownloadStateString(DownloadInternalState state); | 449 static const char* DebugDownloadStateString(DownloadInternalState state); |
388 static const char* DebugResumeModeString(ResumeMode mode); | 450 static const char* DebugResumeModeString(ResumeMode mode); |
| 451 #if DCHECK_IS_ON() |
| 452 static bool IsValidSavePackageStateTransition(DownloadInternalState from, |
| 453 DownloadInternalState to); |
| 454 static bool IsValidStateTransition(DownloadInternalState from, |
| 455 DownloadInternalState to); |
| 456 #endif |
389 | 457 |
390 // Will be false for save package downloads retrieved from the history. | 458 // Will be false for save package downloads retrieved from the history. |
391 // TODO(rdsmith): Replace with a generalized enum for "download source". | 459 // TODO(rdsmith): Replace with a generalized enum for "download source". |
392 const bool is_save_package_download_; | 460 const bool is_save_package_download_; |
393 | 461 |
394 // The handle to the request information. Used for operations outside the | 462 // The handle to the request information. Used for operations outside the |
395 // download system. | 463 // download system. |
396 scoped_ptr<DownloadRequestHandleInterface> request_handle_; | 464 scoped_ptr<DownloadRequestHandleInterface> request_handle_; |
397 | 465 |
398 uint32_t download_id_; | 466 uint32_t download_id_; |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 const net::BoundNetLog bound_net_log_; | 621 const net::BoundNetLog bound_net_log_; |
554 | 622 |
555 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; | 623 base::WeakPtrFactory<DownloadItemImpl> weak_ptr_factory_; |
556 | 624 |
557 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); | 625 DISALLOW_COPY_AND_ASSIGN(DownloadItemImpl); |
558 }; | 626 }; |
559 | 627 |
560 } // namespace content | 628 } // namespace content |
561 | 629 |
562 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ | 630 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_ITEM_IMPL_H_ |
OLD | NEW |