FUNContext.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #import "Functions/FirebaseFunctions/FUNContext.h"
  16. #import "FirebaseMessaging/Sources/Interop/FIRMessagingInterop.h"
  17. #import "Interop/Auth/Public/FIRAuthInterop.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. @interface FUNContext ()
  20. - (instancetype)initWithAuthToken:(NSString *_Nullable)authToken
  21. FCMToken:(NSString *_Nullable)FCMToken NS_DESIGNATED_INITIALIZER;
  22. @end
  23. @implementation FUNContext
  24. - (instancetype)initWithAuthToken:(NSString *_Nullable)authToken
  25. FCMToken:(NSString *_Nullable)FCMToken {
  26. self = [super init];
  27. if (self) {
  28. _authToken = [authToken copy];
  29. _FCMToken = [FCMToken copy];
  30. }
  31. return self;
  32. }
  33. @end
  34. @interface FUNContextProvider () {
  35. id<FIRAuthInterop> _auth;
  36. id<FIRMessagingInterop> _messaging;
  37. }
  38. @end
  39. @implementation FUNContextProvider
  40. - (instancetype)initWithAuth:(nullable id<FIRAuthInterop>)auth
  41. messaging:(nullable id<FIRMessagingInterop>)messaging {
  42. self = [super init];
  43. if (self) {
  44. _auth = auth;
  45. _messaging = messaging;
  46. }
  47. return self;
  48. }
  49. // This is broken out so it can be mocked for tests.
  50. - (NSString *)FCMToken {
  51. return _messaging.FCMToken;
  52. }
  53. - (void)getContext:(void (^)(FUNContext *_Nullable context, NSError *_Nullable error))completion {
  54. // If auth isn't included, call the completion handler and return.
  55. if (_auth == nil) {
  56. // With no auth, just populate FCMToken and call the completion handler.
  57. NSString *FCMToken = [self FCMToken];
  58. FUNContext *context = [[FUNContext alloc] initWithAuthToken:nil FCMToken:FCMToken];
  59. completion(context, nil);
  60. return;
  61. }
  62. // Auth exists, get the auth token.
  63. [_auth getTokenForcingRefresh:NO
  64. withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
  65. if (error) {
  66. completion(nil, error);
  67. return;
  68. }
  69. // Get the instance id token.
  70. NSString *_Nullable FCMToken = [self FCMToken];
  71. FUNContext *context = [[FUNContext alloc] initWithAuthToken:token
  72. FCMToken:FCMToken];
  73. completion(context, nil);
  74. }];
  75. }
  76. @end
  77. NS_ASSUME_NONNULL_END