added iOS source code
[wl-app.git] / iOS / Pods / FirebaseMessaging / Firebase / Messaging / FIRMessagingRegistrar.m
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
17 #import "FIRMessagingRegistrar.h"
18
19 #import "FIRMessagingDefines.h"
20 #import "FIRMessagingLogger.h"
21 #import "FIRMessagingPubSubRegistrar.h"
22 #import "FIRMessagingUtilities.h"
23 #import "NSError+FIRMessaging.h"
24
25 @interface FIRMessagingRegistrar ()
26
27 @property(nonatomic, readwrite, assign) BOOL stopAllSubscriptions;
28
29 @property(nonatomic, readwrite, strong) FIRMessagingCheckinService *checkinService;
30 @property(nonatomic, readwrite, strong) FIRMessagingPubSubRegistrar *pubsubRegistrar;
31
32 @end
33
34 @implementation FIRMessagingRegistrar
35
36 - (NSString *)deviceAuthID {
37   return self.checkinService.deviceAuthID;
38 }
39
40 - (NSString *)secretToken {
41   return self.checkinService.secretToken;
42 }
43
44 - (instancetype)init {
45   self = [super init];
46   if (self) {
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];
51   }
52   return self;
53 }
54
55 #pragma mark - Checkin
56
57 - (BOOL)tryToLoadValidCheckinInfo {
58   [self.checkinService tryToLoadPrefetchedCheckinPreferences];
59   return [self.checkinService hasValidCheckinInfo];
60 }
61
62 #pragma mark - Subscribe/Unsubscribe
63
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");
70
71   if ([self tryToLoadValidCheckinInfo]) {
72     [self doUpdateSubscriptionForTopic:topic
73                                  token:token
74                                options:options
75                           shouldDelete:shouldDelete
76                             completion:handler];
77
78   } else {
79     FIRMessagingLoggerDebug(kFIRMessagingMessageCodeRegistrar000,
80                             @"Device check in error, no auth credentials found");
81     NSError *error = [NSError errorWithFCMErrorCode:kFIRMessagingErrorCodeMissingDeviceID];
82     handler(error);
83   }
84 }
85
86 - (void)cancelAllRequests {
87   self.stopAllSubscriptions = YES;
88   [self.pubsubRegistrar stopAllSubscriptionRequests];
89 }
90
91 #pragma mark - Private
92
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");
100
101   [self.pubsubRegistrar updateSubscriptionToTopic:topic
102                                         withToken:token
103                                           options:options
104                                      shouldDelete:shouldDelete
105                                           handler:completion];
106 }
107
108 @end