FIRStorageObservableTask.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2017 Google
  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 "FIRStorageObservableTask.h"
  15. #import "FIRStorageObservableTask_Private.h"
  16. #import "FIRStorageTask_Private.h"
  17. @implementation FIRStorageObservableTask {
  18. @private
  19. // Handlers for pause, resume, progress, success, and failure callbacks
  20. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *_resumeHandlers;
  21. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *_pauseHandlers;
  22. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *_progressHandlers;
  23. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *_successHandlers;
  24. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *_failureHandlers;
  25. // Reverse map of fetcher handles to status types
  26. NSMutableDictionary<NSString *, NSNumber *> *_handleToStatusMap;
  27. }
  28. @synthesize state = _state;
  29. - (instancetype)initWithReference:(FIRStorageReference *)reference
  30. fetcherService:(GTMSessionFetcherService *)service {
  31. self = [super initWithReference:reference fetcherService:service];
  32. if (self) {
  33. _pauseHandlers = [[NSMutableDictionary alloc] init];
  34. _resumeHandlers = [[NSMutableDictionary alloc] init];
  35. _progressHandlers = [[NSMutableDictionary alloc] init];
  36. _successHandlers = [[NSMutableDictionary alloc] init];
  37. _failureHandlers = [[NSMutableDictionary alloc] init];
  38. _handleToStatusMap = [[NSMutableDictionary alloc] init];
  39. }
  40. return self;
  41. }
  42. #pragma mark - Observers
  43. - (FIRStorageHandle)observeStatus:(FIRStorageTaskStatus)status
  44. handler:(FIRStorageVoidSnapshot)handler {
  45. FIRStorageVoidSnapshot callback = handler;
  46. // Note: self.snapshot is synchronized
  47. FIRStorageTaskSnapshot *snapshot = self.snapshot;
  48. // TODO: use an increasing counter instead of a random UUID
  49. NSString *UUIDString = [[NSUUID UUID] UUIDString];
  50. switch (status) {
  51. case FIRStorageTaskStatusPause:
  52. @synchronized(self) {
  53. [_pauseHandlers setValue:callback forKey:UUIDString];
  54. } // @synchronized(self)
  55. if (_state == FIRStorageTaskStatePausing || _state == FIRStorageTaskStatePaused) {
  56. [self fireHandlers:_pauseHandlers snapshot:snapshot];
  57. }
  58. break;
  59. case FIRStorageTaskStatusResume:
  60. @synchronized(self) {
  61. [_resumeHandlers setValue:callback forKey:UUIDString];
  62. } // @synchronized(self)
  63. if (_state == FIRStorageTaskStateResuming || _state == FIRStorageTaskStateRunning) {
  64. [self fireHandlers:_resumeHandlers snapshot:snapshot];
  65. }
  66. break;
  67. case FIRStorageTaskStatusProgress:
  68. @synchronized(self) {
  69. [_progressHandlers setValue:callback forKey:UUIDString];
  70. } // @synchronized(self)
  71. if (_state == FIRStorageTaskStateRunning || _state == FIRStorageTaskStateProgress) {
  72. [self fireHandlers:_progressHandlers snapshot:snapshot];
  73. }
  74. break;
  75. case FIRStorageTaskStatusSuccess:
  76. @synchronized(self) {
  77. [_successHandlers setValue:callback forKey:UUIDString];
  78. } // @synchronized(self)
  79. if (_state == FIRStorageTaskStateSuccess) {
  80. [self fireHandlers:_successHandlers snapshot:snapshot];
  81. }
  82. break;
  83. case FIRStorageTaskStatusFailure:
  84. @synchronized(self) {
  85. [_failureHandlers setValue:callback forKey:UUIDString];
  86. } // @synchronized(self)
  87. if (_state == FIRStorageTaskStateFailing || _state == FIRStorageTaskStateFailed) {
  88. [self fireHandlers:_failureHandlers snapshot:snapshot];
  89. }
  90. break;
  91. case FIRStorageTaskStatusUnknown:
  92. // Fall through to exception case if an unknown status is passed
  93. default:
  94. [NSException raise:NSInternalInconsistencyException
  95. format:kFIRStorageInvalidObserverStatus, nil];
  96. break;
  97. }
  98. @synchronized(self) {
  99. _handleToStatusMap[UUIDString] = @(status);
  100. } // @synchronized(self)
  101. return UUIDString;
  102. }
  103. - (void)removeObserverWithHandle:(FIRStorageHandle)handle {
  104. FIRStorageTaskStatus status = [_handleToStatusMap[handle] intValue];
  105. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *observerDictionary =
  106. [self handlerDictionaryForStatus:status];
  107. @synchronized(self) {
  108. [observerDictionary removeObjectForKey:handle];
  109. [_handleToStatusMap removeObjectForKey:handle];
  110. } // @synchronized(self)
  111. }
  112. - (void)removeAllObserversForStatus:(FIRStorageTaskStatus)status {
  113. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *observerDictionary =
  114. [self handlerDictionaryForStatus:status];
  115. [self removeHandlersFromStatusMapForDictionary:observerDictionary];
  116. @synchronized(self) {
  117. [observerDictionary removeAllObjects];
  118. } // @synchronized(self)
  119. }
  120. - (void)removeAllObservers {
  121. @synchronized(self) {
  122. [_pauseHandlers removeAllObjects];
  123. [_resumeHandlers removeAllObjects];
  124. [_progressHandlers removeAllObjects];
  125. [_successHandlers removeAllObjects];
  126. [_failureHandlers removeAllObjects];
  127. [_handleToStatusMap removeAllObjects];
  128. } // @synchronized(self)
  129. }
  130. - (NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *)handlerDictionaryForStatus:
  131. (FIRStorageTaskStatus)status {
  132. switch (status) {
  133. case FIRStorageTaskStatusPause:
  134. return _pauseHandlers;
  135. case FIRStorageTaskStatusResume:
  136. return _resumeHandlers;
  137. case FIRStorageTaskStatusProgress:
  138. return _progressHandlers;
  139. case FIRStorageTaskStatusSuccess:
  140. return _successHandlers;
  141. case FIRStorageTaskStatusFailure:
  142. return _failureHandlers;
  143. case FIRStorageTaskStatusUnknown:
  144. return [NSMutableDictionary dictionary];
  145. default:
  146. [NSException raise:NSInternalInconsistencyException
  147. format:kFIRStorageInvalidObserverStatus, nil];
  148. return nil;
  149. }
  150. }
  151. - (void)removeHandlersFromStatusMapForDictionary:
  152. (NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *)dict {
  153. @synchronized(self) {
  154. [_handleToStatusMap removeObjectsForKeys:dict.allKeys];
  155. } // @synchronized(self)
  156. }
  157. - (void)fireHandlersForStatus:(FIRStorageTaskStatus)status
  158. snapshot:(FIRStorageTaskSnapshot *)snapshot {
  159. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *observerDictionary =
  160. [self handlerDictionaryForStatus:status];
  161. [self fireHandlers:observerDictionary snapshot:snapshot];
  162. }
  163. - (void)fireHandlers:(NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *)handlers
  164. snapshot:(FIRStorageTaskSnapshot *)snapshot {
  165. dispatch_queue_t callbackQueue = self.fetcherService.callbackQueue;
  166. if (!callbackQueue) {
  167. callbackQueue = dispatch_get_main_queue();
  168. }
  169. // TODO: iterate over this list in a consistent order
  170. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *handlersCopy;
  171. @synchronized(self) {
  172. handlersCopy = [handlers copy];
  173. } // @synchronized(self)
  174. [handlersCopy
  175. enumerateKeysAndObjectsUsingBlock:^(
  176. NSString *_Nonnull key, FIRStorageVoidSnapshot _Nonnull handler, BOOL *_Nonnull stop) {
  177. dispatch_async(callbackQueue, ^{
  178. handler(snapshot);
  179. });
  180. }];
  181. }
  182. @end