FIRDLJavaScriptExecutor.m 4.3 KB

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