You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.7 KiB

6 years ago
  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 "FIRMessagingRegistrar.h"
  17. #import "FIRMessagingDefines.h"
  18. #import "FIRMessagingLogger.h"
  19. #import "FIRMessagingPubSubRegistrar.h"
  20. #import "FIRMessagingUtilities.h"
  21. #import "NSError+FIRMessaging.h"
  22. @interface FIRMessagingRegistrar ()
  23. @property(nonatomic, readwrite, assign) BOOL stopAllSubscriptions;
  24. @property(nonatomic, readwrite, strong) FIRMessagingCheckinService *checkinService;
  25. @property(nonatomic, readwrite, strong) FIRMessagingPubSubRegistrar *pubsubRegistrar;
  26. @end
  27. @implementation FIRMessagingRegistrar
  28. - (NSString *)deviceAuthID {
  29. return self.checkinService.deviceAuthID;
  30. }
  31. - (NSString *)secretToken {
  32. return self.checkinService.secretToken;
  33. }
  34. - (instancetype)init {
  35. self = [super init];
  36. if (self) {
  37. _checkinService = [[FIRMessagingCheckinService alloc] init];
  38. // TODO(chliangGoogle): Merge pubsubRegistrar with Registrar as it is hard to track how many
  39. // checkinService instances by separating classes too often.
  40. _pubsubRegistrar = [[FIRMessagingPubSubRegistrar alloc] initWithCheckinService:_checkinService];
  41. }
  42. return self;
  43. }
  44. #pragma mark - Checkin
  45. - (BOOL)tryToLoadValidCheckinInfo {
  46. [self.checkinService tryToLoadPrefetchedCheckinPreferences];
  47. return [self.checkinService hasValidCheckinInfo];
  48. }
  49. - (BOOL)hasValidCheckinInfo {
  50. return [self.checkinService hasValidCheckinInfo];
  51. }
  52. #pragma mark - Subscribe/Unsubscribe
  53. - (void)updateSubscriptionToTopic:(NSString *)topic
  54. withToken:(NSString *)token
  55. options:(NSDictionary *)options
  56. shouldDelete:(BOOL)shouldDelete
  57. handler:(FIRMessagingTopicOperationCompletion)handler {
  58. _FIRMessagingDevAssert(handler, @"Invalid nil handler");
  59. if ([self tryToLoadValidCheckinInfo]) {
  60. [self doUpdateSubscriptionForTopic:topic
  61. token:token
  62. options:options
  63. shouldDelete:shouldDelete
  64. completion:handler];
  65. } else {
  66. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeRegistrar000,
  67. @"Device check in error, no auth credentials found");
  68. NSError *error = [NSError errorWithFCMErrorCode:kFIRMessagingErrorCodeMissingDeviceID];
  69. handler(error);
  70. }
  71. }
  72. - (void)cancelAllRequests {
  73. self.stopAllSubscriptions = YES;
  74. [self.pubsubRegistrar stopAllSubscriptionRequests];
  75. }
  76. #pragma mark - Private
  77. - (void)doUpdateSubscriptionForTopic:(NSString *)topic
  78. token:(NSString *)token
  79. options:(NSDictionary *)options
  80. shouldDelete:(BOOL)shouldDelete
  81. completion:(FIRMessagingTopicOperationCompletion)completion {
  82. _FIRMessagingDevAssert([self.checkinService hasValidCheckinInfo],
  83. @"No valid checkin info found before subscribe");
  84. [self.pubsubRegistrar updateSubscriptionToTopic:topic
  85. withToken:token
  86. options:options
  87. shouldDelete:shouldDelete
  88. handler:completion];
  89. }
  90. @end