| 1 | 
           efrain | 
           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 <Foundation/Foundation.h>
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           #import "FirebaseMessaging/Sources/FIRMessagingTopicsCommon.h"
  | 
        
        
            | 
            | 
           22 | 
              | 
        
        
            | 
            | 
           23 | 
           NS_ASSUME_NONNULL_BEGIN
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           /**
  | 
        
        
            | 
            | 
           26 | 
            *  Represents a single batch of topics, with the same action.
  | 
        
        
            | 
            | 
           27 | 
            *
  | 
        
        
            | 
            | 
           28 | 
            *  Topic operations which have the same action (subscribe or unsubscribe) can be executed
  | 
        
        
            | 
            | 
           29 | 
            *  simultaneously, as the order of operations do not matter with the same action. The set of
  | 
        
        
            | 
            | 
           30 | 
            *  topics is unique, as it doesn't make sense to apply the same action to the same topic
  | 
        
        
            | 
            | 
           31 | 
            *  repeatedly; the result would be the same as the first time.
  | 
        
        
            | 
            | 
           32 | 
            */
  | 
        
        
            | 
            | 
           33 | 
           @interface FIRMessagingTopicBatch : NSObject <NSSecureCoding>
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
           @property(nonatomic, readonly, assign) FIRMessagingTopicAction action;
  | 
        
        
            | 
            | 
           36 | 
           @property(nonatomic, readonly, copy) NSMutableSet<NSString *> *topics;
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
           - (instancetype)init NS_UNAVAILABLE;
  | 
        
        
            | 
            | 
           39 | 
           - (instancetype)initWithAction:(FIRMessagingTopicAction)action NS_DESIGNATED_INITIALIZER;
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
           @end
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
           @class FIRMessagingPendingTopicsList;
  | 
        
        
            | 
            | 
           44 | 
           /**
  | 
        
        
            | 
            | 
           45 | 
            *  This delegate must be supplied to the instance of FIRMessagingPendingTopicsList, via the
  | 
        
        
            | 
            | 
           46 | 
            *  @cdelegate property. It lets the
  | 
        
        
            | 
            | 
           47 | 
            *  pending topics list know whether or not it can begin making requests via
  | 
        
        
            | 
            | 
           48 | 
            *  @c-pendingTopicsListCanRequestTopicUpdates:, and handles the request to actually
  | 
        
        
            | 
            | 
           49 | 
            *  perform the topic operation. The delegate also handles when the pending topics list is updated,
  | 
        
        
            | 
            | 
           50 | 
            *  so that it can be archived or persisted.
  | 
        
        
            | 
            | 
           51 | 
            *
  | 
        
        
            | 
            | 
           52 | 
            *  @see FIRMessagingPendingTopicsList
  | 
        
        
            | 
            | 
           53 | 
            */
  | 
        
        
            | 
            | 
           54 | 
           @protocol FIRMessagingPendingTopicsListDelegate <NSObject>
  | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
           - (void)pendingTopicsList:(FIRMessagingPendingTopicsList *)list
  | 
        
        
            | 
            | 
           57 | 
               requestedUpdateForTopic:(NSString *)topic
  | 
        
        
            | 
            | 
           58 | 
                                action:(FIRMessagingTopicAction)action
  | 
        
        
            | 
            | 
           59 | 
                            completion:(FIRMessagingTopicOperationCompletion)completion;
  | 
        
        
            | 
            | 
           60 | 
           - (void)pendingTopicsListDidUpdate:(FIRMessagingPendingTopicsList *)list;
  | 
        
        
            | 
            | 
           61 | 
           - (BOOL)pendingTopicsListCanRequestTopicUpdates:(FIRMessagingPendingTopicsList *)list;
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
           @end
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
           /**
  | 
        
        
            | 
            | 
           66 | 
            *  FIRMessagingPendingTopicsList manages a list of topic subscription updates, batched by the same
  | 
        
        
            | 
            | 
           67 | 
            *  action (subscribe or unsubscribe). The list roughly maintains the order of the topic operations,
  | 
        
        
            | 
            | 
           68 | 
            *  batched together whenever the topic action (subscribe or unsubscribe) changes.
  | 
        
        
            | 
            | 
           69 | 
            *
  | 
        
        
            | 
            | 
           70 | 
            *  Topics operations are batched by action because it is safe to perform the same topic action
  | 
        
        
            | 
            | 
           71 | 
            *  (subscribe or unsubscribe) on many topics simultaneously. After each batch is successfully
  | 
        
        
            | 
            | 
           72 | 
            *  completed, the next batch operations can begin.
  | 
        
        
            | 
            | 
           73 | 
            *
  | 
        
        
            | 
            | 
           74 | 
            *  When asked to resume its operations, FIRMessagingPendingTopicsList will begin performing updates
  | 
        
        
            | 
            | 
           75 | 
            *  of its current batch of topics. For example, it may begin subscription operations for topics
  | 
        
        
            | 
            | 
           76 | 
            *  [A, B, C] simultaneously.
  | 
        
        
            | 
            | 
           77 | 
            *
  | 
        
        
            | 
            | 
           78 | 
            *  When the current batch is completed, the next batch of operations will be started. For example
  | 
        
        
            | 
            | 
           79 | 
            *  the list may begin unsubscribe operations for [D, A, E]. Note that because A is in both batches,
  | 
        
        
            | 
            | 
           80 | 
            *  A will be correctly subscribed in the first batch, then unsubscribed as part of the second batch
  | 
        
        
            | 
            | 
           81 | 
            *  of operations. Without batching, it would be ambiguous whether A's subscription operation or the
  | 
        
        
            | 
            | 
           82 | 
            *  unsubscription operation would be completed first.
  | 
        
        
            | 
            | 
           83 | 
            *
  | 
        
        
            | 
            | 
           84 | 
            *  An app can subscribe and unsubscribe from many topics, and this class helps persist the pending
  | 
        
        
            | 
            | 
           85 | 
            *  topics and perform the operation safely and correctly.
  | 
        
        
            | 
            | 
           86 | 
            *
  | 
        
        
            | 
            | 
           87 | 
            *  When a topic fails to subscribe or unsubscribe due to a network error, it is considered a
  | 
        
        
            | 
            | 
           88 | 
            *  recoverable error, and so it remains in the current batch until it is succesfully completed.
  | 
        
        
            | 
            | 
           89 | 
            *  Topic updates are completed when they either (a) succeed, (b) are cancelled, or (c) result in an
  | 
        
        
            | 
            | 
           90 | 
            *  unrecoverable error. Any error outside of `NSURLErrorDomain` is considered an unrecoverable
  | 
        
        
            | 
            | 
           91 | 
            *  error.
  | 
        
        
            | 
            | 
           92 | 
            *
  | 
        
        
            | 
            | 
           93 | 
            *  In addition to maintaining the list of pending topic updates, FIRMessagingPendingTopicsList also
  | 
        
        
            | 
            | 
           94 | 
            *  can track completion handlers for topic operations.
  | 
        
        
            | 
            | 
           95 | 
            *
  | 
        
        
            | 
            | 
           96 | 
            *  @discussion Completion handlers for topic updates are not maintained if it was restored from a
  | 
        
        
            | 
            | 
           97 | 
            *  keyed archive. They are only called if the topic operation finished within the same app session.
  | 
        
        
            | 
            | 
           98 | 
            *
  | 
        
        
            | 
            | 
           99 | 
            *  You must supply an object conforming to FIRMessagingPendingTopicsListDelegate in order for the
  | 
        
        
            | 
            | 
           100 | 
            *  topic operations to execute.
  | 
        
        
            | 
            | 
           101 | 
            *
  | 
        
        
            | 
            | 
           102 | 
            *  @see FIRMessagingPendingTopicsListDelegate
  | 
        
        
            | 
            | 
           103 | 
            */
  | 
        
        
            | 
            | 
           104 | 
           @interface FIRMessagingPendingTopicsList : NSObject <NSSecureCoding>
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
           @property(nonatomic, weak) NSObject<FIRMessagingPendingTopicsListDelegate> *delegate;
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
           @property(nonatomic, readonly, strong, nullable) NSDate *archiveDate;
  | 
        
        
            | 
            | 
           109 | 
           @property(nonatomic, readonly) NSUInteger numberOfBatches;
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
           - (instancetype)init NS_DESIGNATED_INITIALIZER;
  | 
        
        
            | 
            | 
           112 | 
           - (void)addOperationForTopic:(NSString *)topic
  | 
        
        
            | 
            | 
           113 | 
                             withAction:(FIRMessagingTopicAction)action
  | 
        
        
            | 
            | 
           114 | 
                             completion:(nullable FIRMessagingTopicOperationCompletion)completion;
  | 
        
        
            | 
            | 
           115 | 
           - (void)resumeOperationsIfNeeded;
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
           @end
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
           NS_ASSUME_NONNULL_END
  |