FIRDLJavaScriptExecutor.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2018 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. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import <WebKit/WebKit.h>
  19. #import "FirebaseDynamicLinks/Sources/FIRDLJavaScriptExecutor.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. static NSString *const kJSMethodName = @"generateFingerprint";
  22. /** Creates and returns the FDL JS method name. */
  23. NSString *FIRDLTypeofFingerprintJSMethodNameString(void) {
  24. static NSString *methodName;
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. methodName = [NSString stringWithFormat:@"typeof(%@)", kJSMethodName];
  28. });
  29. return methodName;
  30. }
  31. /** Creates and returns the FDL JS method definition. */
  32. NSString *GINFingerprintJSMethodString(void) {
  33. static NSString *methodString;
  34. static dispatch_once_t onceToken;
  35. dispatch_once(&onceToken, ^{
  36. methodString = [NSString stringWithFormat:@"%@()", kJSMethodName];
  37. });
  38. return methodString;
  39. }
  40. @interface FIRDLJavaScriptExecutor () <WKNavigationDelegate>
  41. @end
  42. @implementation FIRDLJavaScriptExecutor {
  43. __weak id<FIRDLJavaScriptExecutorDelegate> _delegate;
  44. NSString *_script;
  45. // Web view with which to run JavaScript.
  46. WKWebView *_wkWebView;
  47. }
  48. - (instancetype)initWithDelegate:(id<FIRDLJavaScriptExecutorDelegate>)delegate
  49. script:(NSString *)script {
  50. NSParameterAssert(delegate);
  51. NSParameterAssert(script);
  52. NSParameterAssert(script.length > 0);
  53. NSAssert([NSThread isMainThread], @"%@ must be used in main thread",
  54. NSStringFromClass([self class]));
  55. if (self = [super init]) {
  56. _delegate = delegate;
  57. _script = [script copy];
  58. [self start];
  59. }
  60. return self;
  61. }
  62. #pragma mark - Internal methods
  63. - (void)start {
  64. NSString *htmlContent =
  65. [NSString stringWithFormat:@"<html><head><script>%@</script></head></html>", _script];
  66. _wkWebView = [[WKWebView alloc] init];
  67. _wkWebView.navigationDelegate = self;
  68. [_wkWebView loadHTMLString:htmlContent baseURL:nil];
  69. }
  70. - (void)handleExecutionResult:(NSString *)result {
  71. [self cleanup];
  72. [_delegate javaScriptExecutor:self completedExecutionWithResult:result];
  73. }
  74. - (void)handleExecutionError:(nullable NSError *)error {
  75. [self cleanup];
  76. if (!error) {
  77. error = [NSError errorWithDomain:@"com.firebase.durabledeeplink" code:-1 userInfo:nil];
  78. }
  79. [_delegate javaScriptExecutor:self failedWithError:error];
  80. }
  81. - (void)cleanup {
  82. _wkWebView.navigationDelegate = nil;
  83. _wkWebView = nil;
  84. }
  85. #pragma mark - WKNavigationDelegate
  86. - (void)webView:(WKWebView *)webView
  87. didFinishNavigation:(null_unspecified WKNavigation *)navigation {
  88. __weak __typeof__(self) weakSelf = self;
  89. // Make sure that the javascript was loaded successfully before calling the method.
  90. [webView evaluateJavaScript:FIRDLTypeofFingerprintJSMethodNameString()
  91. completionHandler:^(id _Nullable typeofResult, NSError *_Nullable typeError) {
  92. if (typeError) {
  93. [weakSelf handleExecutionError:typeError];
  94. return;
  95. }
  96. if ([typeofResult isEqual:@"function"]) {
  97. [webView
  98. evaluateJavaScript:GINFingerprintJSMethodString()
  99. completionHandler:^(id _Nullable result, NSError *_Nullable functionError) {
  100. __typeof__(self) strongSelf = weakSelf;
  101. if ([result isKindOfClass:[NSString class]]) {
  102. [strongSelf handleExecutionResult:result];
  103. } else {
  104. [strongSelf handleExecutionError:nil];
  105. }
  106. }];
  107. } else {
  108. [weakSelf handleExecutionError:nil];
  109. }
  110. }];
  111. }
  112. - (void)webView:(WKWebView *)webView
  113. didFailNavigation:(null_unspecified WKNavigation *)navigation
  114. withError:(NSError *)error {
  115. [self handleExecutionError:error];
  116. }
  117. @end
  118. NS_ASSUME_NONNULL_END
  119. #endif // TARGET_OS_IOS