StorageTask.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. *
  25. * Callbacks are always fired on the developer-specified callback queue.
  26. * If no queue is specified, it defaults to the main queue.
  27. * This class is thread-safe.
  28. */
  29. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  30. @objc(FIRStorageTask) open class StorageTask: NSObject {
  31. /**
  32. * An immutable view of the task and associated metadata, progress, error, etc.
  33. */
  34. @objc public var snapshot: StorageTaskSnapshot {
  35. objc_sync_enter(StorageTask.self)
  36. defer { objc_sync_exit(StorageTask.self) }
  37. let progress = Progress(totalUnitCount: self.progress.totalUnitCount)
  38. progress.completedUnitCount = self.progress.completedUnitCount
  39. return StorageTaskSnapshot(
  40. task: self,
  41. state: state,
  42. reference: reference,
  43. progress: progress,
  44. metadata: metadata,
  45. error: error
  46. )
  47. }
  48. // MARK: - Internal Implementations
  49. /**
  50. * State for the current task in progress.
  51. */
  52. var state: StorageTaskState
  53. /**
  54. * StorageMetadata for the task in progress, or nil if none present.
  55. */
  56. var metadata: StorageMetadata?
  57. /**
  58. * Error which occurred during task execution, or nil if no error occurred.
  59. */
  60. var error: NSError?
  61. /**
  62. * NSProgress object which tracks the progress of an observable task.
  63. */
  64. var progress: Progress
  65. /**
  66. * Reference pointing to the location the task is being performed against.
  67. */
  68. let reference: StorageReference
  69. /**
  70. * A serial queue for all storage operations.
  71. */
  72. let dispatchQueue: DispatchQueue
  73. let baseRequest: URLRequest
  74. init(reference: StorageReference,
  75. queue: DispatchQueue) {
  76. self.reference = reference
  77. dispatchQueue = queue
  78. state = .unknown
  79. progress = Progress(totalUnitCount: 0)
  80. baseRequest = StorageUtils.defaultRequestForReference(reference: reference)
  81. }
  82. }
  83. /**
  84. * Defines task operations such as pause, resume, cancel, and enqueue for all tasks.
  85. *
  86. * All tasks are required to implement enqueue, which begins the task, and may optionally
  87. * implement pause, resume, and cancel, which operate on the task to pause, resume, and cancel
  88. * operations.
  89. */
  90. @objc(FIRStorageTaskManagement) public protocol StorageTaskManagement: NSObjectProtocol {
  91. /**
  92. * Prepares a task and begins execution.
  93. */
  94. @objc func enqueue() -> Void
  95. /**
  96. * Pauses a task currently in progress.
  97. */
  98. @objc optional func pause() -> Void
  99. /**
  100. * Cancels a task.
  101. */
  102. @objc optional func cancel() -> Void
  103. /**
  104. * Resumes a paused task.
  105. */
  106. @objc optional func resume() -> Void
  107. }