FIRTestAuthTokenProvider.m 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRTestAuthTokenProvider.h"
  17. #import "FIRDatabaseQuery_Private.h"
  18. @interface FIRTestAuthTokenProvider ()
  19. @property(nonatomic, strong) NSMutableArray *listeners;
  20. @end
  21. @implementation FIRTestAuthTokenProvider
  22. - (instancetype)initWithToken:(NSString *)token {
  23. self = [super init];
  24. if (self != nil) {
  25. self.listeners = [NSMutableArray array];
  26. self.token = token;
  27. }
  28. return self;
  29. }
  30. - (void)setToken:(NSString *)token {
  31. self->_token = token;
  32. dispatch_async([FIRDatabaseQuery sharedQueue], ^{
  33. [self.listeners enumerateObjectsUsingBlock:^(fbt_void_nsstring _Nonnull listener,
  34. NSUInteger idx, BOOL *_Nonnull stop) {
  35. listener(token);
  36. }];
  37. });
  38. }
  39. - (void)fetchTokenForcingRefresh:(BOOL)forceRefresh
  40. withCallback:(fbt_void_nsstring_nserror)callback {
  41. if (forceRefresh) {
  42. self.token = self.nextToken;
  43. }
  44. // Simulate delay
  45. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_MSEC)),
  46. [FIRDatabaseQuery sharedQueue], ^{
  47. callback(self.token, nil);
  48. });
  49. }
  50. - (void)listenForTokenChanges:(fbt_void_nsstring)listener {
  51. [self.listeners addObject:[listener copy]];
  52. }
  53. @end