StorageTask.swift 3.5 KB

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