FIRAppCheckBackoffWrapper.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2021 Google LLC
  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 <Foundation/Foundation.h>
  17. @class FBLPromise<ValueType>;
  18. NS_ASSUME_NONNULL_BEGIN
  19. /// Backoff type. Backoff interval calculation depends on the type.
  20. typedef NS_ENUM(NSUInteger, FIRAppCheckBackoffType) {
  21. /// No backoff. Another retry is allowed straight away.
  22. FIRAppCheckBackoffTypeNone,
  23. /// Next retry will be allowed in 1 day (24 hours) after the failure.
  24. FIRAppCheckBackoffType1Day,
  25. /// A small backoff interval that exponentially increases after each consequent failure.
  26. FIRAppCheckBackoffTypeExponential
  27. };
  28. /// Creates a promise for an operation to apply the backoff to.
  29. typedef FBLPromise *_Nonnull (^FIRAppCheckBackoffOperationProvider)(void);
  30. /// Converts an error to a backoff type.
  31. typedef FIRAppCheckBackoffType (^FIRAppCheckBackoffErrorHandler)(NSError *error);
  32. /// A block returning a date. Is used instead of `+[NSDate date]` for better testability of logic
  33. /// dependent on the current time.
  34. typedef NSDate *_Nonnull (^FIRAppCheckDateProvider)(void);
  35. /// Defines API for an object that conditionally applies backoff to a given operation based on the
  36. /// history of previous operation failures.
  37. @protocol FIRAppCheckBackoffWrapperProtocol <NSObject>
  38. /// Conditionally applies backoff to the given operation.
  39. /// @param operationProvider A block that returns a new promise. The block will be called only when
  40. /// the operation is allowed.
  41. /// NOTE: We cannot accept just a promise because the operation will be started once the
  42. /// promise has been instantiated, so we need to have a way to instantiate the promise only
  43. /// when the operation is good to go. The provider block is the way we use.
  44. /// @param errorHandler A block that receives an operation error as an input and returns the
  45. /// appropriate backoff type. `defaultErrorHandler` provides a default implementation for Firebase
  46. /// services.
  47. /// @return A promise that is either:
  48. /// - a promise returned by the promise provider if no backoff is required
  49. /// - rejected if the backoff is needed
  50. - (FBLPromise *)applyBackoffToOperation:(FIRAppCheckBackoffOperationProvider)operationProvider
  51. errorHandler:(FIRAppCheckBackoffErrorHandler)errorHandler;
  52. /// The default Firebase services error handler. It keeps track of network errors and
  53. /// `FIRAppCheckHTTPError.HTTPResponse.statusCode.statusCode` value to return the appropriate
  54. /// backoff type for the standard Firebase App Check backend response codes.
  55. - (FIRAppCheckBackoffErrorHandler)defaultAppCheckProviderErrorHandler;
  56. @end
  57. /// Provides a backoff implementation. Keeps track of the operation successes and failures to either
  58. /// create and perform the operation promise or fails with a backoff error when the backoff is
  59. /// needed.
  60. @interface FIRAppCheckBackoffWrapper : NSObject <FIRAppCheckBackoffWrapperProtocol>
  61. /// Initializes the wrapper with `+[FIRAppCheckBackoffWrapper currentDateProvider]`.
  62. - (instancetype)init;
  63. - (instancetype)initWithDateProvider:(FIRAppCheckDateProvider)dateProvider
  64. NS_DESIGNATED_INITIALIZER;
  65. /// A date provider that returns `+[NSDate date]`.
  66. + (FIRAppCheckDateProvider)currentDateProvider;
  67. @end
  68. NS_ASSUME_NONNULL_END