Ingen beskrivning

RNCWebViewManager.m 9.1KB

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