FIRAppCheckTimer.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2021 Google LLC
  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 "FirebaseAppCheck/Sources/Core/TokenRefresh/FIRAppCheckTimer.h"
  17. NS_ASSUME_NONNULL_BEGIN
  18. @interface FIRAppCheckTimer ()
  19. @property(nonatomic, readonly) dispatch_queue_t dispatchQueue;
  20. @property(atomic, readonly) dispatch_source_t timer;
  21. @end
  22. @implementation FIRAppCheckTimer
  23. + (FIRTimerProvider)timerProvider {
  24. return ^id<FIRAppCheckTimerProtocol> _Nullable(NSDate *fireDate, dispatch_queue_t queue,
  25. dispatch_block_t handler) {
  26. return [[FIRAppCheckTimer alloc] initWithFireDate:fireDate dispatchQueue:queue block:handler];
  27. };
  28. }
  29. + (nullable instancetype)timerFireDate:(NSDate *)fireDate
  30. dispatchQueue:(dispatch_queue_t)dispatchQueue
  31. block:(dispatch_block_t)block {
  32. return [[FIRAppCheckTimer alloc] initWithFireDate:fireDate
  33. dispatchQueue:dispatchQueue
  34. block:block];
  35. }
  36. - (nullable instancetype)initWithFireDate:(NSDate *)date
  37. dispatchQueue:(dispatch_queue_t)dispatchQueue
  38. block:(dispatch_block_t)block {
  39. self = [super init];
  40. if (self == nil) {
  41. return nil;
  42. }
  43. if (block == nil) {
  44. return nil;
  45. }
  46. NSTimeInterval scheduleInSec = [date timeIntervalSinceNow];
  47. if (scheduleInSec <= 0) {
  48. return nil;
  49. }
  50. dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, scheduleInSec * NSEC_PER_SEC);
  51. _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.dispatchQueue);
  52. dispatch_source_set_timer(_timer, startTime, UINT64_MAX * NSEC_PER_SEC, 0);
  53. __auto_type __weak weakSelf = self;
  54. dispatch_source_set_event_handler(_timer, ^{
  55. __auto_type strongSelf = weakSelf;
  56. // The initializer returns a one-off timer, so we need to invalidate the dispatch timer to
  57. // prevent firing again.
  58. [strongSelf invalidate];
  59. block();
  60. });
  61. dispatch_resume(_timer);
  62. return self;
  63. }
  64. - (void)dealloc {
  65. [self invalidate];
  66. }
  67. - (void)invalidate {
  68. if (self.timer != nil) {
  69. dispatch_source_cancel(self.timer);
  70. }
  71. }
  72. @end
  73. NS_ASSUME_NONNULL_END