StorageTask.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. #if COCOAPODS
  16. import GTMSessionFetcher
  17. #else
  18. import GTMSessionFetcherCore
  19. #endif
  20. /**
  21. * A superclass to all Storage tasks, including `StorageUploadTask`
  22. * and `StorageDownloadTask`, to provide state transitions, event raising, and common storage
  23. * for metadata and errors.
  24. * Callbacks are always fired on the developer-specified callback queue.
  25. * If no queue is specified, it defaults to the main queue.
  26. * This class is not thread safe, so only call methods on the main thread.
  27. */
  28. @objc(FIRStorageTask) open class StorageTask: NSObject {
  29. /**
  30. * An immutable view of the task and associated metadata, progress, error, etc.
  31. */
  32. @objc public var snapshot: StorageTaskSnapshot {
  33. objc_sync_enter(StorageTask.self)
  34. defer { objc_sync_exit(StorageTask.self) }
  35. let progress = Progress(totalUnitCount: self.progress.totalUnitCount)
  36. progress.completedUnitCount = self.progress.completedUnitCount
  37. return StorageTaskSnapshot(
  38. task: self,
  39. state: state,
  40. reference: reference,
  41. progress: progress,
  42. metadata: metadata,
  43. error: error
  44. )
  45. }
  46. // MARK: - Internal Implementations
  47. /**
  48. * State for the current task in progress.
  49. */
  50. internal var state: StorageTaskState
  51. /**
  52. * StorageMetadata for the task in progress, or nil if none present.
  53. */
  54. internal var metadata: StorageMetadata?
  55. /**
  56. * Error which occurred during task execution, or nil if no error occurred.
  57. */
  58. internal var error: NSError?
  59. /**
  60. * NSProgress object which tracks the progress of an observable task.
  61. */
  62. internal var progress: Progress
  63. /**
  64. * Reference pointing to the location the task is being performed against.
  65. */
  66. internal let reference: StorageReference
  67. /**
  68. * A serial queue for all storage operations.
  69. */
  70. internal let dispatchQueue: DispatchQueue
  71. internal let fetcherService: GTMSessionFetcherService
  72. internal let baseRequest: URLRequest
  73. internal init(reference: StorageReference,
  74. service: GTMSessionFetcherService,
  75. queue: DispatchQueue) {
  76. self.reference = reference
  77. fetcherService = service
  78. fetcherService.maxRetryInterval = reference.storage.maxOperationRetryInterval
  79. dispatchQueue = queue
  80. state = .unknown
  81. progress = Progress(totalUnitCount: 0)
  82. baseRequest = StorageUtils.defaultRequestForReference(reference: reference)
  83. }
  84. }
  85. /**
  86. * Defines task operations such as pause, resume, cancel, and enqueue for all tasks.
  87. * All tasks are required to implement enqueue, which begins the task, and may optionally
  88. * implement pause, resume, and cancel, which operate on the task to pause, resume, and cancel
  89. * operations.
  90. */
  91. @objc(FIRStorageTaskManagement) public protocol StorageTaskManagement: NSObjectProtocol {
  92. /**
  93. * Prepares a task and begins execution.
  94. */
  95. @objc func enqueue() -> Void
  96. /**
  97. * Pauses a task currently in progress.
  98. */
  99. @objc optional func pause() -> Void
  100. /**
  101. * Cancels a task.
  102. */
  103. @objc optional func cancel() -> Void
  104. /**
  105. * Resumes a paused task.
  106. */
  107. @objc optional func resume() -> Void
  108. }