Нет описания

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #import "RNCUIWebViewManager.h"
  2. #import <React/RCTBridge.h>
  3. #import <React/RCTUIManager.h>
  4. #import <React/UIView+React.h>
  5. #import "RNCUIWebView.h"
  6. @interface RNCUIWebViewManager () <RNCUIWebViewDelegate>
  7. @end
  8. @implementation RNCUIWebViewManager
  9. {
  10. NSConditionLock *_shouldStartLoadLock;
  11. BOOL _shouldStartLoad;
  12. }
  13. RCT_EXPORT_MODULE()
  14. - (UIView *)view
  15. {
  16. RNCUIWebView *webView = [RNCUIWebView new];
  17. webView.delegate = self;
  18. return webView;
  19. }
  20. RCT_EXPORT_VIEW_PROPERTY(source, NSDictionary)
  21. RCT_REMAP_VIEW_PROPERTY(bounces, _webView.scrollView.bounces, BOOL)
  22. RCT_REMAP_VIEW_PROPERTY(scrollEnabled, _webView.scrollView.scrollEnabled, BOOL)
  23. RCT_REMAP_VIEW_PROPERTY(decelerationRate, _webView.scrollView.decelerationRate, CGFloat)
  24. RCT_EXPORT_VIEW_PROPERTY(scalesPageToFit, BOOL)
  25. RCT_EXPORT_VIEW_PROPERTY(messagingEnabled, BOOL)
  26. RCT_EXPORT_VIEW_PROPERTY(injectedJavaScript, NSString)
  27. RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
  28. RCT_EXPORT_VIEW_PROPERTY(automaticallyAdjustContentInsets, BOOL)
  29. RCT_EXPORT_VIEW_PROPERTY(onLoadingStart, RCTDirectEventBlock)
  30. RCT_EXPORT_VIEW_PROPERTY(onLoadingFinish, RCTDirectEventBlock)
  31. RCT_EXPORT_VIEW_PROPERTY(onLoadingError, RCTDirectEventBlock)
  32. RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock)
  33. RCT_EXPORT_VIEW_PROPERTY(onShouldStartLoadWithRequest, RCTDirectEventBlock)
  34. RCT_REMAP_VIEW_PROPERTY(allowsInlineMediaPlayback, _webView.allowsInlineMediaPlayback, BOOL)
  35. RCT_REMAP_VIEW_PROPERTY(mediaPlaybackRequiresUserAction, _webView.mediaPlaybackRequiresUserAction, BOOL)
  36. RCT_REMAP_VIEW_PROPERTY(dataDetectorTypes, _webView.dataDetectorTypes, UIDataDetectorTypes)
  37. RCT_REMAP_VIEW_PROPERTY(showsHorizontalScrollIndicator, _webView.scrollView.showsHorizontalScrollIndicator, BOOL)
  38. RCT_REMAP_VIEW_PROPERTY(showsVerticalScrollIndicator, _webView.scrollView.showsVerticalScrollIndicator, BOOL)
  39. RCT_EXPORT_METHOD(goBack:(nonnull NSNumber *)reactTag)
  40. {
  41. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCUIWebView *> *viewRegistry) {
  42. RNCUIWebView *view = viewRegistry[reactTag];
  43. if (![view isKindOfClass:[RNCUIWebView class]]) {
  44. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  45. } else {
  46. [view goBack];
  47. }
  48. }];
  49. }
  50. RCT_EXPORT_METHOD(goForward:(nonnull NSNumber *)reactTag)
  51. {
  52. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
  53. RNCUIWebView *view = viewRegistry[reactTag];
  54. if (![view isKindOfClass:[RNCUIWebView class]]) {
  55. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  56. } else {
  57. [view goForward];
  58. }
  59. }];
  60. }
  61. RCT_EXPORT_METHOD(reload:(nonnull NSNumber *)reactTag)
  62. {
  63. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCUIWebView *> *viewRegistry) {
  64. RNCUIWebView *view = viewRegistry[reactTag];
  65. if (![view isKindOfClass:[RNCUIWebView class]]) {
  66. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  67. } else {
  68. [view reload];
  69. }
  70. }];
  71. }
  72. RCT_EXPORT_METHOD(stopLoading:(nonnull NSNumber *)reactTag)
  73. {
  74. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCUIWebView *> *viewRegistry) {
  75. RNCUIWebView *view = viewRegistry[reactTag];
  76. if (![view isKindOfClass:[RNCUIWebView class]]) {
  77. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  78. } else {
  79. [view stopLoading];
  80. }
  81. }];
  82. }
  83. RCT_EXPORT_METHOD(postMessage:(nonnull NSNumber *)reactTag message:(NSString *)message)
  84. {
  85. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCUIWebView *> *viewRegistry) {
  86. RNCUIWebView *view = viewRegistry[reactTag];
  87. if (![view isKindOfClass:[RNCUIWebView class]]) {
  88. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  89. } else {
  90. [view postMessage:message];
  91. }
  92. }];
  93. }
  94. RCT_EXPORT_METHOD(injectJavaScript:(nonnull NSNumber *)reactTag script:(NSString *)script)
  95. {
  96. [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RNCUIWebView *> *viewRegistry) {
  97. RNCUIWebView *view = viewRegistry[reactTag];
  98. if (![view isKindOfClass:[RNCUIWebView class]]) {
  99. RCTLogError(@"Invalid view returned from registry, expecting RNCUIWebView, got: %@", view);
  100. } else {
  101. [view injectJavaScript:script];
  102. }
  103. }];
  104. }
  105. #pragma mark - Exported synchronous methods
  106. - (BOOL)webView:(__unused RNCUIWebView *)webView
  107. shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
  108. withCallback:(RCTDirectEventBlock)callback
  109. {
  110. _shouldStartLoadLock = [[NSConditionLock alloc] initWithCondition:arc4random()];
  111. _shouldStartLoad = YES;
  112. request[@"lockIdentifier"] = @(_shouldStartLoadLock.condition);
  113. callback(request);
  114. // Block the main thread for a maximum of 250ms until the JS thread returns
  115. if ([_shouldStartLoadLock lockWhenCondition:0 beforeDate:[NSDate dateWithTimeIntervalSinceNow:.25]]) {
  116. BOOL returnValue = _shouldStartLoad;
  117. [_shouldStartLoadLock unlock];
  118. _shouldStartLoadLock = nil;
  119. return returnValue;
  120. } else {
  121. RCTLogWarn(@"Did not receive response to shouldStartLoad in time, defaulting to YES");
  122. return YES;
  123. }
  124. }
  125. RCT_EXPORT_METHOD(startLoadWithResult:(BOOL)result lockIdentifier:(NSInteger)lockIdentifier)
  126. {
  127. if ([_shouldStartLoadLock tryLockWhenCondition:lockIdentifier]) {
  128. _shouldStartLoad = result;
  129. [_shouldStartLoadLock unlockWithCondition:0];
  130. } else {
  131. RCTLogWarn(@"startLoadWithResult invoked with invalid lockIdentifier: "
  132. "got %lld, expected %lld", (long long)lockIdentifier, (long long)_shouldStartLoadLock.condition);
  133. }
  134. }
  135. @end