react-native-webview.git

RNCWebViewManager.m 8.8KB

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