FIRDLJavaScriptExecutor.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 <sys/sysctl.h>
  19. #import <WebKit/WebKit.h>
  20. #import "FirebaseDynamicLinks/Sources/FIRDLJavaScriptExecutor.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. static NSString *const kJSMethodName = @"generateFingerprint";
  23. /** Creates and returns the FDL JS method name. */
  24. NSString *FIRDLTypeofFingerprintJSMethodNameString(void) {
  25. static NSString *methodName;
  26. static dispatch_once_t onceToken;
  27. dispatch_once(&onceToken, ^{
  28. methodName = [NSString stringWithFormat:@"typeof(%@)", kJSMethodName];
  29. });
  30. return methodName;
  31. }
  32. /** Creates and returns the FDL JS method definition. */
  33. NSString *GINFingerprintJSMethodString(void) {
  34. static NSString *methodString;
  35. static dispatch_once_t onceToken;
  36. dispatch_once(&onceToken, ^{
  37. methodString = [NSString stringWithFormat:@"%@()", kJSMethodName];
  38. });
  39. return methodString;
  40. }
  41. @interface FIRDLJavaScriptExecutor () <WKNavigationDelegate>
  42. @end
  43. @implementation FIRDLJavaScriptExecutor {
  44. __weak id<FIRDLJavaScriptExecutorDelegate> _delegate;
  45. NSString *_script;
  46. // Web view with which to run JavaScript.
  47. WKWebView *_wkWebView;
  48. }
  49. - (instancetype)initWithDelegate:(id<FIRDLJavaScriptExecutorDelegate>)delegate
  50. script:(NSString *)script {
  51. NSParameterAssert(delegate);
  52. NSParameterAssert(script);
  53. NSParameterAssert(script.length > 0);
  54. NSAssert([NSThread isMainThread], @"%@ must be used in main thread",
  55. NSStringFromClass([self class]));
  56. if (self = [super init]) {
  57. _delegate = delegate;
  58. _script = [script copy];
  59. [self start];
  60. }
  61. return self;
  62. }
  63. #pragma mark - Internal methods
  64. - (void)start {
  65. // Initializing a `WKWebView` causes a memory allocation error when the process
  66. // is running under Rosetta translation on Apple Silicon.
  67. // The issue only occurs on the simulator in apps targeting below iOS 14. (Issue #7618)
  68. #if TARGET_OS_SIMULATOR
  69. BOOL systemVersionAtLeastiOS14 = [NSProcessInfo.processInfo
  70. isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){14, 0, 0}];
  71. // Perform an early exit if the process is running under Rosetta translation and targeting
  72. // under iOS 14.
  73. if (processIsTranslated() && !systemVersionAtLeastiOS14) {
  74. [self handleExecutionError:nil];
  75. return;
  76. }
  77. #endif
  78. NSString *htmlContent =
  79. [NSString stringWithFormat:@"<html><head><script>%@</script></head></html>", _script];
  80. _wkWebView = [[WKWebView alloc] init];
  81. _wkWebView.navigationDelegate = self;
  82. [_wkWebView loadHTMLString:htmlContent baseURL:nil];
  83. }
  84. - (void)handleExecutionResult:(NSString *)result {
  85. [self cleanup];
  86. [_delegate javaScriptExecutor:self completedExecutionWithResult:result];
  87. }
  88. - (void)handleExecutionError:(nullable NSError *)error {
  89. [self cleanup];
  90. if (!error) {
  91. error = [NSError errorWithDomain:@"com.firebase.durabledeeplink" code:-1 userInfo:nil];
  92. }
  93. [_delegate javaScriptExecutor:self failedWithError:error];
  94. }
  95. - (void)cleanup {
  96. _wkWebView.navigationDelegate = nil;
  97. _wkWebView = nil;
  98. }
  99. #pragma mark - WKNavigationDelegate
  100. - (void)webView:(WKWebView *)webView
  101. didFinishNavigation:(null_unspecified WKNavigation *)navigation {
  102. __weak __typeof__(self) weakSelf = self;
  103. // Make sure that the javascript was loaded successfully before calling the method.
  104. [webView evaluateJavaScript:FIRDLTypeofFingerprintJSMethodNameString()
  105. completionHandler:^(id _Nullable typeofResult, NSError *_Nullable typeError) {
  106. if (typeError) {
  107. [weakSelf handleExecutionError:typeError];
  108. return;
  109. }
  110. if ([typeofResult isEqual:@"function"]) {
  111. [webView
  112. evaluateJavaScript:GINFingerprintJSMethodString()
  113. completionHandler:^(id _Nullable result, NSError *_Nullable functionError) {
  114. __typeof__(self) strongSelf = weakSelf;
  115. if ([result isKindOfClass:[NSString class]]) {
  116. [strongSelf handleExecutionResult:result];
  117. } else {
  118. [strongSelf handleExecutionError:nil];
  119. }
  120. }];
  121. } else {
  122. [weakSelf handleExecutionError:nil];
  123. }
  124. }];
  125. }
  126. - (void)webView:(WKWebView *)webView
  127. didFailNavigation:(null_unspecified WKNavigation *)navigation
  128. withError:(NSError *)error {
  129. [self handleExecutionError:error];
  130. }
  131. // Determine whether a process is running under Rosetta translation.
  132. // Returns 0 for a native process, 1 for a translated process,
  133. // and -1 when an error occurs.
  134. // From:
  135. // https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment
  136. #if TARGET_OS_SIMULATOR
  137. static int processIsTranslated() {
  138. int ret = 0;
  139. size_t size = sizeof(ret);
  140. if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1) {
  141. if (errno == ENOENT) return 0;
  142. return -1;
  143. }
  144. return ret;
  145. }
  146. #endif
  147. @end
  148. NS_ASSUME_NONNULL_END
  149. #endif // TARGET_OS_IOS