react-native-webview.git

RNCWebViewManager.m 7.8KB

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