2 * Copyright 2017 Google
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #import "FIRMessagingRegistrar.h"
19 #import "FIRMessagingDefines.h"
20 #import "FIRMessagingLogger.h"
21 #import "FIRMessagingPubSubRegistrar.h"
22 #import "FIRMessagingUtilities.h"
23 #import "NSError+FIRMessaging.h"
25 @interface FIRMessagingRegistrar ()
27 @property(nonatomic, readwrite, assign) BOOL stopAllSubscriptions;
29 @property(nonatomic, readwrite, strong) FIRMessagingCheckinService *checkinService;
30 @property(nonatomic, readwrite, strong) FIRMessagingPubSubRegistrar *pubsubRegistrar;
34 @implementation FIRMessagingRegistrar
36 - (NSString *)deviceAuthID {
37 return self.checkinService.deviceAuthID;
40 - (NSString *)secretToken {
41 return self.checkinService.secretToken;
44 - (instancetype)init {
47 _checkinService = [[FIRMessagingCheckinService alloc] init];
48 // TODO(chliangGoogle): Merge pubsubRegistrar with Registrar as it is hard to track how many
49 // checkinService instances by separating classes too often.
50 _pubsubRegistrar = [[FIRMessagingPubSubRegistrar alloc] initWithCheckinService:_checkinService];
55 #pragma mark - Checkin
57 - (BOOL)tryToLoadValidCheckinInfo {
58 [self.checkinService tryToLoadPrefetchedCheckinPreferences];
59 return [self.checkinService hasValidCheckinInfo];
62 #pragma mark - Subscribe/Unsubscribe
64 - (void)updateSubscriptionToTopic:(NSString *)topic
65 withToken:(NSString *)token
66 options:(NSDictionary *)options
67 shouldDelete:(BOOL)shouldDelete
68 handler:(FIRMessagingTopicOperationCompletion)handler {
69 _FIRMessagingDevAssert(handler, @"Invalid nil handler");
71 if ([self tryToLoadValidCheckinInfo]) {
72 [self doUpdateSubscriptionForTopic:topic
75 shouldDelete:shouldDelete
79 FIRMessagingLoggerDebug(kFIRMessagingMessageCodeRegistrar000,
80 @"Device check in error, no auth credentials found");
81 NSError *error = [NSError errorWithFCMErrorCode:kFIRMessagingErrorCodeMissingDeviceID];
86 - (void)cancelAllRequests {
87 self.stopAllSubscriptions = YES;
88 [self.pubsubRegistrar stopAllSubscriptionRequests];
91 #pragma mark - Private
93 - (void)doUpdateSubscriptionForTopic:(NSString *)topic
94 token:(NSString *)token
95 options:(NSDictionary *)options
96 shouldDelete:(BOOL)shouldDelete
97 completion:(FIRMessagingTopicOperationCompletion)completion {
98 _FIRMessagingDevAssert([self.checkinService hasValidCheckinInfo],
99 @"No valid checkin info found before subscribe");
101 [self.pubsubRegistrar updateSubscriptionToTopic:topic
104 shouldDelete:shouldDelete