FIRStorageObservableTask.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. handler = nil;
  47. // Note: self.snapshot is synchronized
  48. FIRStorageTaskSnapshot *snapshot = self.snapshot;
  49. // TODO: use an increasing counter instead of a random UUID
  50. NSString *UUIDString = [[NSUUID UUID] UUIDString];
  51. switch (status) {
  52. case FIRStorageTaskStatusPause:
  53. @synchronized(self) {
  54. [_pauseHandlers setValue:callback forKey:UUIDString];
  55. } // @synchronized(self)
  56. if (_state == FIRStorageTaskStatePausing || _state == FIRStorageTaskStatePaused) {
  57. [self fireHandlers:_pauseHandlers snapshot:snapshot];
  58. }
  59. break;
  60. case FIRStorageTaskStatusResume:
  61. @synchronized(self) {
  62. [_resumeHandlers setValue:callback forKey:UUIDString];
  63. } // @synchronized(self)
  64. if (_state == FIRStorageTaskStateResuming || _state == FIRStorageTaskStateRunning) {
  65. [self fireHandlers:_resumeHandlers snapshot:snapshot];
  66. }
  67. break;
  68. case FIRStorageTaskStatusProgress:
  69. @synchronized(self) {
  70. [_progressHandlers setValue:callback forKey:UUIDString];
  71. } // @synchronized(self)
  72. if (_state == FIRStorageTaskStateRunning || _state == FIRStorageTaskStateProgress) {
  73. [self fireHandlers:_progressHandlers snapshot:snapshot];
  74. }
  75. break;
  76. case FIRStorageTaskStatusSuccess:
  77. @synchronized(self) {
  78. [_successHandlers setValue:callback forKey:UUIDString];
  79. } // @synchronized(self)
  80. if (_state == FIRStorageTaskStateSuccess) {
  81. [self fireHandlers:_successHandlers snapshot:snapshot];
  82. }
  83. break;
  84. case FIRStorageTaskStatusFailure:
  85. @synchronized(self) {
  86. [_failureHandlers setValue:callback forKey:UUIDString];
  87. } // @synchronized(self)
  88. if (_state == FIRStorageTaskStateFailing || _state == FIRStorageTaskStateFailed) {
  89. [self fireHandlers:_failureHandlers snapshot:snapshot];
  90. }
  91. break;
  92. case FIRStorageTaskStatusUnknown:
  93. // Fall through to exception case if an unknown status is passed
  94. default:
  95. [NSException raise:NSInternalInconsistencyException
  96. format:kFIRStorageInvalidObserverStatus, nil];
  97. break;
  98. }
  99. @synchronized(self) {
  100. _handleToStatusMap[UUIDString] = @(status);
  101. } // @synchronized(self)
  102. return UUIDString;
  103. }
  104. - (void)removeObserverWithHandle:(FIRStorageHandle)handle {
  105. FIRStorageTaskStatus status = [_handleToStatusMap[handle] intValue];
  106. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *observerDictionary =
  107. [self handlerDictionaryForStatus:status];
  108. @synchronized(self) {
  109. [observerDictionary removeObjectForKey:handle];
  110. [_handleToStatusMap removeObjectForKey:handle];
  111. } // @synchronized(self)
  112. }
  113. - (void)removeAllObserversForStatus:(FIRStorageTaskStatus)status {
  114. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *observerDictionary =
  115. [self handlerDictionaryForStatus:status];
  116. [self removeHandlersFromStatusMapForDictionary:observerDictionary];
  117. @synchronized(self) {
  118. [observerDictionary removeAllObjects];
  119. } // @synchronized(self)
  120. }
  121. - (void)removeAllObservers {
  122. @synchronized(self) {
  123. [_pauseHandlers removeAllObjects];
  124. [_resumeHandlers removeAllObjects];
  125. [_progressHandlers removeAllObjects];
  126. [_successHandlers removeAllObjects];
  127. [_failureHandlers removeAllObjects];
  128. [_handleToStatusMap removeAllObjects];
  129. } // @synchronized(self)
  130. }
  131. - (NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *)handlerDictionaryForStatus:
  132. (FIRStorageTaskStatus)status {
  133. switch (status) {
  134. case FIRStorageTaskStatusPause:
  135. return _pauseHandlers;
  136. case FIRStorageTaskStatusResume:
  137. return _resumeHandlers;
  138. case FIRStorageTaskStatusProgress:
  139. return _progressHandlers;
  140. case FIRStorageTaskStatusSuccess:
  141. return _successHandlers;
  142. case FIRStorageTaskStatusFailure:
  143. return _failureHandlers;
  144. case FIRStorageTaskStatusUnknown:
  145. return [NSMutableDictionary dictionary];
  146. default:
  147. [NSException raise:NSInternalInconsistencyException
  148. format:kFIRStorageInvalidObserverStatus, nil];
  149. return nil;
  150. }
  151. }
  152. - (void)removeHandlersFromStatusMapForDictionary:
  153. (NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *)dict {
  154. @synchronized(self) {
  155. [_handleToStatusMap removeObjectsForKeys:dict.allKeys];
  156. } // @synchronized(self)
  157. }
  158. - (void)fireHandlersForStatus:(FIRStorageTaskStatus)status
  159. snapshot:(FIRStorageTaskSnapshot *)snapshot {
  160. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *observerDictionary =
  161. [self handlerDictionaryForStatus:status];
  162. [self fireHandlers:observerDictionary snapshot:snapshot];
  163. }
  164. - (void)fireHandlers:(NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *)handlers
  165. snapshot:(FIRStorageTaskSnapshot *)snapshot {
  166. dispatch_queue_t callbackQueue = self.fetcherService.callbackQueue;
  167. if (!callbackQueue) {
  168. callbackQueue = dispatch_get_main_queue();
  169. }
  170. // TODO: iterate over this list in a consistent order
  171. NSMutableDictionary<NSString *, FIRStorageVoidSnapshot> *handlersCopy;
  172. @synchronized(self) {
  173. handlersCopy = [handlers copy];
  174. } // @synchronized(self)
  175. [handlersCopy enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull key,
  176. FIRStorageVoidSnapshot _Nonnull handler,
  177. BOOL *_Nonnull stop) {
  178. dispatch_async(callbackQueue, ^{
  179. handler(snapshot);
  180. });
  181. }];
  182. }
  183. @end