react-native-webview.git

RNCWebViewManager.m 8.0KB

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