react-native-webview.git

ReactWebViewManager.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "pch.h"
  2. #include "ReactWebViewManager.h"
  3. #include "NativeModules.h"
  4. namespace winrt {
  5. using namespace Microsoft::ReactNative;
  6. using namespace Windows::Foundation;
  7. using namespace Windows::Foundation::Collections;
  8. using namespace Windows::UI::Xaml;
  9. using namespace Windows::UI::Xaml::Controls;
  10. }
  11. namespace winrt::ReactNativeWebView::implementation {
  12. ReactWebViewManager::ReactWebViewManager() {}
  13. // IViewManager
  14. winrt::hstring ReactWebViewManager::Name() noexcept {
  15. return L"RCTWebView";
  16. }
  17. winrt::FrameworkElement ReactWebViewManager::CreateView() noexcept {
  18. return winrt::WebView();
  19. }
  20. // IViewManagerWithReactContext
  21. winrt::IReactContext ReactWebViewManager::ReactContext() noexcept {
  22. return m_reactContext;
  23. }
  24. void ReactWebViewManager::ReactContext(IReactContext reactContext) noexcept {
  25. m_reactContext = reactContext;
  26. }
  27. // IViewManagerWithNativeProperties
  28. IMapView<hstring, ViewManagerPropertyType> ReactWebViewManager::NativeProps() noexcept {
  29. auto nativeProps = winrt::single_threaded_map<hstring, ViewManagerPropertyType>();
  30. nativeProps.Insert(L"source", ViewManagerPropertyType::Map);
  31. return nativeProps.GetView();
  32. }
  33. void ReactWebViewManager::UpdateProperties(
  34. FrameworkElement const& view,
  35. IJSValueReader const& propertyMapReader) noexcept {
  36. if (auto webView = view.try_as<winrt::WebView>()) {
  37. const JSValueObject& propertyMap = JSValue::ReadObjectFrom(propertyMapReader);
  38. for (auto const& pair : propertyMap) {
  39. auto const& propertyName = pair.first;
  40. auto const& propertyValue = pair.second;
  41. if (propertyName == "source") {
  42. if (!propertyValue.IsNull()) {
  43. auto const& srcMap = propertyValue.Object();
  44. auto uriString = srcMap.at("uri").String();
  45. auto const isPackagerAsset = srcMap.at("__packager_asset").Boolean();
  46. // non-uri sources not yet supported
  47. if (uriString.length() == 0) {
  48. continue;
  49. }
  50. if (isPackagerAsset && uriString.find("assets") == 0) {
  51. uriString.replace(0, 6, "ms-appx://");
  52. }
  53. auto const uri = winrt::Uri(to_hstring(uriString));
  54. SetSource(webView, uri);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. void ReactWebViewManager::SetSource(winrt::WebView const& webView, winrt::Uri const& uri) {
  61. auto const tag = webView.GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
  62. m_reactContext.DispatchEvent(
  63. webView,
  64. L"topLoadStart",
  65. [tag](winrt::IJSValueWriter const& eventDataWriter) noexcept {
  66. eventDataWriter.WriteObjectBegin();
  67. {
  68. WriteProperty(eventDataWriter, L"target", tag);
  69. }
  70. eventDataWriter.WriteObjectEnd();
  71. });
  72. webView.Navigate(uri);
  73. }
  74. // IViewManagerWithExportedEventTypeConstants
  75. ConstantProvider ReactWebViewManager::ExportedCustomBubblingEventTypeConstants() noexcept {
  76. return nullptr;
  77. }
  78. ConstantProvider ReactWebViewManager::ExportedCustomDirectEventTypeConstants() noexcept {
  79. return [](winrt::Microsoft::ReactNative::IJSValueWriter const& constantWriter) {
  80. WriteCustomDirectEventTypeConstant(constantWriter, "onLoadStart");
  81. WriteCustomDirectEventTypeConstant(constantWriter, "onLoad");
  82. WriteCustomDirectEventTypeConstant(constantWriter, "onLoadEnd");
  83. };
  84. }
  85. } // namespace winrt::ReactWebView::implementation