Aucune description

RNCWebViewManager.m 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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(hideKeyboardAccessoryView, BOOL)
  62. RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL)
  63. RCT_EXPORT_VIEW_PROPERTY(incognito, BOOL)
  64. RCT_EXPORT_VIEW_PROPERTY(pagingEnabled, BOOL)
  65. RCT_EXPORT_VIEW_PROPERTY(userAgent, NSString)
  66. RCT_EXPORT_VIEW_PROPERTY(applicationNameForUserAgent, NSString)
  67. RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
  68. RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
  69. RCT_EXPORT_VIEW_PROPERTY(allowingReadAccessToURL, NSString)
  70. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 /* __IPHONE_11_0 */
  71. RCT_EXPORT_VIEW_PROPERTY(contentInsetAdjustmentBehavior, UIScrollViewContentInsetAdjustmentBehavior)
  72. #endif
  73. #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 /* iOS 13 */
  74. RCT_EXPORT_VIEW_PROPERTY(contentMode, WKContentMode)
  75. #endif
  76. /**
  77. * Expose methods to enable messaging the webview.
  78. */
  79. RCT_EXPORT_VIEW_PROPERTY(messagingEnabled, BOOL)
  80. RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock)
  81. RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock)
  82. RCT_EXPORT_METHOD(postMessage:(nonnull NSNumber *)reactTag message:(NSString *)message)
  83. {
  84. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  85. RNCWebView *view = viewRegistry[reactTag];
  86. if (![view isKindOfClass:[RNCWebView class]]) {
  87. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  88. } else {
  89. [view postMessage:message];
  90. }
  91. }];
  92. }
  93. RCT_CUSTOM_VIEW_PROPERTY(pullToRefreshEnabled, BOOL, RNCWebView) {
  94. view.pullToRefreshEnabled = json == nil ? false : [RCTConvert BOOL: json];
  95. }
  96. RCT_CUSTOM_VIEW_PROPERTY(bounces, BOOL, RNCWebView) {
  97. view.bounces = json == nil ? true : [RCTConvert BOOL: json];
  98. }
  99. RCT_CUSTOM_VIEW_PROPERTY(useSharedProcessPool, BOOL, RNCWebView) {
  100. view.useSharedProcessPool = json == nil ? true : [RCTConvert BOOL: json];
  101. }
  102. RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, RNCWebView) {
  103. view.scrollEnabled = json == nil ? true : [RCTConvert BOOL: json];
  104. }
  105. RCT_CUSTOM_VIEW_PROPERTY(sharedCookiesEnabled, BOOL, RNCWebView) {
  106. view.sharedCookiesEnabled = json == nil ? false : [RCTConvert BOOL: json];
  107. }
  108. #if !TARGET_OS_OSX
  109. RCT_CUSTOM_VIEW_PROPERTY(decelerationRate, CGFloat, RNCWebView) {
  110. view.decelerationRate = json == nil ? UIScrollViewDecelerationRateNormal : [RCTConvert CGFloat: json];
  111. }
  112. #endif // !TARGET_OS_OSX
  113. RCT_CUSTOM_VIEW_PROPERTY(directionalLockEnabled, BOOL, RNCWebView) {
  114. view.directionalLockEnabled = json == nil ? true : [RCTConvert BOOL: json];
  115. }
  116. RCT_CUSTOM_VIEW_PROPERTY(showsHorizontalScrollIndicator, BOOL, RNCWebView) {
  117. view.showsHorizontalScrollIndicator = json == nil ? true : [RCTConvert BOOL: json];
  118. }
  119. RCT_CUSTOM_VIEW_PROPERTY(showsVerticalScrollIndicator, BOOL, RNCWebView) {
  120. view.showsVerticalScrollIndicator = json == nil ? true : [RCTConvert BOOL: json];
  121. }
  122. RCT_CUSTOM_VIEW_PROPERTY(keyboardDisplayRequiresUserAction, BOOL, RNCWebView) {
  123. view.keyboardDisplayRequiresUserAction = json == nil ? true : [RCTConvert BOOL: json];
  124. }
  125. RCT_EXPORT_METHOD(injectJavaScript:(nonnull NSNumber *)reactTag script:(NSString *)script)
  126. {
  127. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  128. RNCWebView *view = viewRegistry[reactTag];
  129. if (![view isKindOfClass:[RNCWebView class]]) {
  130. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  131. } else {
  132. [view injectJavaScript:script];
  133. }
  134. }];
  135. }
  136. RCT_EXPORT_METHOD(goBack:(nonnull NSNumber *)reactTag)
  137. {
  138. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  139. RNCWebView *view = viewRegistry[reactTag];
  140. if (![view isKindOfClass:[RNCWebView class]]) {
  141. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  142. } else {
  143. [view goBack];
  144. }
  145. }];
  146. }
  147. RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
  148. {
  149. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  150. RNCWebView *view = viewRegistry[reactTag];
  151. if (![view isKindOfClass:[RNCWebView class]]) {
  152. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  153. } else {
  154. [view goForward];
  155. }
  156. }];
  157. }
  158. RCT_EXPORT_METHOD(reload:(nonnull NSNumber *)reactTag)
  159. {
  160. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  161. RNCWebView *view = viewRegistry[reactTag];
  162. if (![view isKindOfClass:[RNCWebView class]]) {
  163. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  164. } else {
  165. [view reload];
  166. }
  167. }];
  168. }
  169. RCT_EXPORT_METHOD(stopLoading:(nonnull NSNumber *)reactTag)
  170. {
  171. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCWebView *> *viewRegistry) {
  172. RNCWebView *view = viewRegistry[reactTag];
  173. if (![view isKindOfClass:[RNCWebView class]]) {
  174. RCTLogError(@"Invalid view returned from registry, expecting RNCWebView, got: %@", view);
  175. } else {
  176. [view stopLoading];
  177. }
  178. }];
  179. }
  180. #pragma mark - Exported synchronous methods
  181. - (BOOL) webView:(RNCWebView *)webView
  182. shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
  183. withCallback:(RCTDirectEventBlock)callback
  184. {
  185. _shouldStartLoadLock = [[NSConditionLock alloc] initWithCondition:arc4random()];
  186. _shouldStartLoad = YES;
  187. request[@"lockIdentifier"] = @(_shouldStartLoadLock.condition);
  188. callback(request);
  189. // Block the main thread for a maximum of 250ms until the JS thread returns
  190. if ([_shouldStartLoadLock lockWhenCondition:0 beforeDate:[NSDate dateWithTimeIntervalSinceNow:.25]]) {
  191. BOOL returnValue = _shouldStartLoad;
  192. [_shouldStartLoadLock unlock];
  193. _shouldStartLoadLock = nil;
  194. return returnValue;
  195. } else {
  196. RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to YES");
  197. return YES;
  198. }
  199. }
  200. RCT_EXPORT_METHOD(startLoadWithResult:(BOOL)result lockIdentifier:(NSInteger)lockIdentifier)
  201. {
  202. if ([_shouldStartLoadLock tryLockWhenCondition:lockIdentifier]) {
  203. _shouldStartLoad = result;
  204. [_shouldStartLoadLock unlockWithCondition:0];
  205. } else {
  206. RCTLogWarn(@"startLoadWithResult invoked with invalid lockIdentifier: "
  207. "got %lld, expected %lld", (long long)lockIdentifier, (long long)_shouldStartLoadLock.condition);
  208. }
  209. }
  210. @end