No Description

RNCWebViewManager.m 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RNCWebViewManager.h"
  8. #import <React/RCTUIManager.h>
  9. #import <React/RCTDefines.h>
  10. #import "RNCWebView.h"
  11. @interface RNCWebViewManager () <RNCWebViewDelegate>
  12. @end
  13. @implementation RNCWebViewManager
  14. {
  15. NSConditionLock *_shouldStartLoadLock;
  16. BOOL _shouldStartLoad;
  17. }
  18. RCT_EXPORT_MODULE()
  19. #if !TARGET_OS_OSX
  20. - (UIView *)view
  21. #else
  22. - (RCTUIView *)view
  23. #endif // !TARGET_OS_OSX
  24. {
  25. RNCWebView *webView = [RNCWebView new];
  26. webView.delegate = self;
  27. return webView;
  28. }
  29. RCT_EXPORT_VIEW_PROPERTY(source, NSDictionary)
  30. RCT_EXPORT_VIEW_PROPERTY(onFileDownload, RCTDirectEventBlock)
  31. RCT_EXPORT_VIEW_PROPERTY(onLoadingStart, RCTDirectEventBlock)
  32. RCT_EXPORT_VIEW_PROPERTY(onLoadingFinish, RCTDirectEventBlock)
  33. RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
  34. RCT_EXPORT_VIEW_PROPERTY(onLoadingProgress, RCTDirectEventBlock)
  35. RCT_EXPORT_VIEW_PROPERTY(onHttpError, RCTDirectEventBlock)
  36. RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
  37. RCT_EXPORT_VIEW_PROPERTY(onContentProcessDidTerminate, RCTDirectEventBlock)
  38. RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
  39. RCT_EXPORT_VIEW_PROPERTY(injectedJavaScriptBeforeContentLoaded, NSString)
  40. RCT_EXPORT_VIEW_PROPERTY(injectedJavaScriptForMainFrameOnly, BOOL)
  41. RCT_EXPORT_VIEW_PROPERTY(injectedJavaScriptBeforeContentLoadedForMainFrameOnly, BOOL)
  42. RCT_EXPORT_VIEW_PROPERTY(javaScriptEnabled, BOOL)
  43. RCT_EXPORT_VIEW_PROPERTY(javaScriptCanOpenWindowsAutomatically, BOOL)
  44. RCT_EXPORT_VIEW_PROPERTY(allowFileAccessFromFileURLs, BOOL)
  45. RCT_EXPORT_VIEW_PROPERTY(allowsInlineMediaPlayback, BOOL)
  46. RCT_EXPORT_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, BOOL)
  47. #if WEBKIT_IOS_10_APIS_AVAILABLE
  48. RCT_EXPORT_VIEW_PROPERTY(dataDetectorTypes, WKDataDetectorTypes)
  49. #endif
  50. RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
  51. RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
  52. RCT_EXPORT_VIEW_PROPERTY(autoManageStatusBarEnabled, BOOL)
  53. RCT_EXPORT_VIEW_PROPERTY(hideKeyboardAccessoryView, BOOL)
  54. RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL)
  55. RCT_EXPORT_VIEW_PROPERTY(incognito, BOOL)
  56. RCT_EXPORT_VIEW_PROPERTY(pagingEnabled, BOOL)
  57. RCT_EXPORT_VIEW_PROPERTY(userAgent, NSString)
  58. RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
  59. RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
  60. RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
  61. RCT_EXPORT_VIEW_PROPERTY(allowingReadAccessToURL, NSString)
  62. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
  63. RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
  64. #endif
  65. /**
  66. * Expose methods to enable messaging the webview.
  67. */
  68. RCT_EXPORT_VIEW_PROPERTY(messagingEnabled, BOOL)
  69. RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock)
  70. RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock)
  71. RCT_EXPORT_METHOD(postMessage:(nonnull NSNumber *)reactTag message:(NSString *)message)
  72. {
  73. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  74. RNCWebView *view = viewRegistry[reactTag];
  75. if (![view isKindOfClass:[RNCWebView class]]) {
  76. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  77. } else {
  78. [view postMessage:message];
  79. }
  80. }];
  81. }
  82. RCT_CUSTOM_VIEW_PROPERTY(bounces, BOOL, RNCWebView) {
  83. view.bounces = json == nil ? true : [RCTConvert BOOL: json];
  84. }
  85. RCT_CUSTOM_VIEW_PROPERTY(useSharedProcessPool, BOOL, RNCWebView) {
  86. view.useSharedProcessPool = json == nil ? true : [RCTConvert BOOL: json];
  87. }
  88. RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, RNCWebView) {
  89. view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json];
  90. }
  91. RCT_CUSTOM_VIEW_PROPERTY(sharedCookiesEnabled, BOOL, RNCWebView) {
  92. view.sharedCookiesEnabled = json == nil ? false : [RCTConvert BOOL: json];
  93. }
  94. #if !TARGET_OS_OSX
  95. RCT_CUSTOM_VIEW_PROPERTY(decelerationRate, CGFloat, RNCWebView) {
  96. view.decelerationRate = json == nil ? UIScrollViewDecelerationRateNormal : [RCTConvert CGFloat: json];
  97. }
  98. #endif // !TARGET_OS_OSX
  99. RCT_CUSTOM_VIEW_PROPERTY(directionalLockEnabled, BOOL, RNCWebView) {
  100. view.directionalLockEnabled = json == nil ? true : [RCTConvert BOOL: json];
  101. }
  102. RCT_CUSTOM_VIEW_PROPERTY(showsHorizontalScrollIndicator, BOOL, RNCWebView) {
  103. view.showsHorizontalScrollIndicator = json == nil ? true : [RCTConvert BOOL: json];
  104. }
  105. RCT_CUSTOM_VIEW_PROPERTY(showsVerticalScrollIndicator, BOOL, RNCWebView) {
  106. view.showsVerticalScrollIndicator = json == nil ? true : [RCTConvert BOOL: json];
  107. }
  108. RCT_CUSTOM_VIEW_PROPERTY(keyboardDisplayRequiresUserAction, BOOL, RNCWebView) {
  109. view.keyboardDisplayRequiresUserAction = json == nil ? true : [RCTConvert BOOL: json];
  110. }
  111. RCT_EXPORT_METHOD(injectJavaScript:(nonnull NSNumber *)reactTag script:(NSString *)script)
  112. {
  113. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  114. RNCWebView *view = viewRegistry[reactTag];
  115. if (![view isKindOfClass:[RNCWebView class]]) {
  116. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  117. } else {
  118. [view injectJavaScript:script];
  119. }
  120. }];
  121. }
  122. RCT_EXPORT_METHOD(goBack:(nonnull NSNumber *)reactTag)
  123. {
  124. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  125. RNCWebView *view = viewRegistry[reactTag];
  126. if (![view isKindOfClass:[RNCWebView class]]) {
  127. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  128. } else {
  129. [view goBack];
  130. }
  131. }];
  132. }
  133. RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
  134. {
  135. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  136. RNCWebView *view = viewRegistry[reactTag];
  137. if (![view isKindOfClass:[RNCWebView class]]) {
  138. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  139. } else {
  140. [view goForward];
  141. }
  142. }];
  143. }
  144. RCT_EXPORT_METHOD(reload:(nonnull NSNumber *)reactTag)
  145. {
  146. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  147. RNCWebView *view = viewRegistry[reactTag];
  148. if (![view isKindOfClass:[RNCWebView class]]) {
  149. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  150. } else {
  151. [view reload];
  152. }
  153. }];
  154. }
  155. RCT_EXPORT_METHOD(stopLoading:(nonnull NSNumber *)reactTag)
  156. {
  157. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  158. RNCWebView *view = viewRegistry[reactTag];
  159. if (![view isKindOfClass:[RNCWebView class]]) {
  160. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  161. } else {
  162. [view stopLoading];
  163. }
  164. }];
  165. }
  166. #pragma mark - Exported synchronous methods
  167. - (BOOL) webView:(RNCWebView *)webView
  168. shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
  169. withCallback:(RCTDirectEventBlock)callback
  170. {
  171. _shouldStartLoadLock = [[NSConditionLock alloc] initWithCondition:arc4random()];
  172. _shouldStartLoad = YES;
  173. request[@"lockIdentifier"] = @(_shouldStartLoadLock.condition);
  174. callback(request);
  175. // Block the main thread for a maximum of 250ms until the JS thread returns
  176. if ([_shouldStartLoadLock lockWhenCondition:0 beforeDate:[NSDate dateWithTimeIntervalSinceNow:.25]]) {
  177. BOOL returnValue = _shouldStartLoad;
  178. [_shouldStartLoadLock unlock];
  179. _shouldStartLoadLock = nil;
  180. return returnValue;
  181. } else {
  182. RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to YES");
  183. return YES;
  184. }
  185. }
  186. RCT_EXPORT_METHOD(startLoadWithResult:(BOOL)result lockIdentifier:(NSInteger)lockIdentifier)
  187. {
  188. if ([_shouldStartLoadLock tryLockWhenCondition:lockIdentifier]) {
  189. _shouldStartLoad = result;
  190. [_shouldStartLoadLock unlockWithCondition:0];
  191. } else {
  192. RCTLogWarn(@"startLoadWithResult invoked with invalid lockIdentifier: "
  193. "got %lld, expected %lld", (long long)lockIdentifier, (long long)_shouldStartLoadLock.condition);
  194. }
  195. }
  196. @end