Added Android code
[wl-app.git] / iOS / Pods / FirebaseMessaging / Firebase / Messaging / FIRMessagingPubSubRegistrar.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 "FIRMessagingPubSubRegistrar.h"
18
19 #import "FIRMessagingCheckinService.h"
20 #import "FIRMessagingDefines.h"
21 #import "FIRMessagingPubSubRegistrar.h"
22 #import "FIRMessagingTopicsCommon.h"
23 #import "NSError+FIRMessaging.h"
24
25 @interface FIRMessagingPubSubRegistrar ()
26
27 @property(nonatomic, readwrite, strong) FIRMessagingCheckinService *checkinService;
28
29 @property(nonatomic, readonly, strong) NSOperationQueue *topicOperations;
30 // Common errors, instantiated, to avoid generating multiple copies
31 @property(nonatomic, readwrite, strong) NSError *operationInProgressError;
32
33 @end
34
35 @implementation FIRMessagingPubSubRegistrar
36
37 - (instancetype)init {
38   FIRMessagingInvalidateInitializer();
39 }
40
41 - (instancetype)initWithCheckinService:(FIRMessagingCheckinService *)checkinService {
42   self = [super init];
43   if (self) {
44     _checkinService = checkinService;
45     _topicOperations = [[NSOperationQueue alloc] init];
46     // Do 10 topic operations at a time; it's enough to keep the TCP connection to the host alive,
47     // saving hundreds of milliseconds on each request (compared to a serial queue).
48     _topicOperations.maxConcurrentOperationCount = 10;
49   }
50   return self;
51 }
52
53 - (void)stopAllSubscriptionRequests {
54   [self.topicOperations cancelAllOperations];
55 }
56
57 - (void)updateSubscriptionToTopic:(NSString *)topic
58                         withToken:(NSString *)token
59                           options:(NSDictionary *)options
60                      shouldDelete:(BOOL)shouldDelete
61                           handler:(FIRMessagingTopicOperationCompletion)handler {
62
63   FIRMessagingTopicAction action = FIRMessagingTopicActionSubscribe;
64   if (shouldDelete) {
65     action = FIRMessagingTopicActionUnsubscribe;
66   }
67   FIRMessagingTopicOperation *operation =
68       [[FIRMessagingTopicOperation alloc] initWithTopic:topic
69                                                  action:action
70                                                   token:token
71                                                 options:options
72                                          checkinService:self.checkinService
73                                              completion:handler];
74   [self.topicOperations addOperation:operation];
75
76 }
77
78 @end