Нема описа

RNCWebViewManager.m 5.6KB

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