Bez popisu

ReactWebView.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. #include "pch.h"
  4. #include "JSValueXaml.h"
  5. #include "ReactWebView.h"
  6. #include "ReactWebView.g.cpp"
  7. namespace winrt {
  8. using namespace Microsoft::ReactNative;
  9. using namespace Windows::Data::Json;
  10. using namespace Windows::Foundation;
  11. using namespace Windows::UI;
  12. using namespace Windows::UI::Popups;
  13. using namespace Windows::UI::Xaml;
  14. using namespace Windows::UI::Xaml::Controls;
  15. using namespace Windows::UI::Xaml::Input;
  16. using namespace Windows::UI::Xaml::Media;
  17. } // namespace winrt
  18. namespace winrt::ReactNativeWebView::implementation {
  19. ReactWebView::ReactWebView(winrt::IReactContext const& reactContext) : m_reactContext(reactContext) {
  20. #ifdef CHAKRACORE_UWP
  21. m_webView = winrt::WebView(winrt::WebViewExecutionMode::SeparateProcess);
  22. #else
  23. m_webView = winrt::WebView();
  24. #endif
  25. this->Content(m_webView);
  26. RegisterEvents();
  27. }
  28. ReactWebView::~ReactWebView()
  29. {
  30. if (m_messagingEnabled) {
  31. m_webBridge.MessagePostEvent(m_message_token);
  32. }
  33. }
  34. void ReactWebView::RegisterEvents() {
  35. m_navigationStartingRevoker = m_webView.NavigationStarting(
  36. winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
  37. if (auto self = ref.get()) {
  38. self->OnNavigationStarting(sender, args);
  39. }
  40. });
  41. m_navigationCompletedRevoker = m_webView.NavigationCompleted(
  42. winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
  43. if (auto self = ref.get()) {
  44. self->OnNavigationCompleted(sender, args);
  45. }
  46. });
  47. m_navigationFailedRevoker = m_webView.NavigationFailed(
  48. winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
  49. if (auto self = ref.get()) {
  50. self->OnNavigationFailed(sender, args);
  51. }
  52. });
  53. }
  54. void ReactWebView::WriteWebViewNavigationEventArg(winrt::WebView const& sender, winrt::IJSValueWriter const& eventDataWriter) {
  55. auto tag = this->GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
  56. WriteProperty(eventDataWriter, L"canGoBack", sender.CanGoBack());
  57. WriteProperty(eventDataWriter, L"canGoForward", sender.CanGoForward());
  58. WriteProperty(eventDataWriter, L"loading", !sender.IsLoaded());
  59. WriteProperty(eventDataWriter, L"target", tag);
  60. WriteProperty(eventDataWriter, L"title", sender.DocumentTitle());
  61. if (auto uri = sender.Source()) {
  62. WriteProperty(eventDataWriter, L"url", uri.AbsoluteCanonicalUri());
  63. }
  64. }
  65. void ReactWebView::OnNavigationStarting(winrt::WebView const& webView, winrt::WebViewNavigationStartingEventArgs const& /*args*/) {
  66. m_reactContext.DispatchEvent(
  67. *this,
  68. L"topLoadingStart",
  69. [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
  70. eventDataWriter.WriteObjectBegin();
  71. WriteWebViewNavigationEventArg(webView, eventDataWriter);
  72. eventDataWriter.WriteObjectEnd();
  73. });
  74. if (m_messagingEnabled) {
  75. m_webBridge = WebBridge();
  76. m_message_token = m_webBridge.MessagePostEvent([this](const auto&, hstring const& message)
  77. {
  78. this->OnMessagePosted(message);
  79. });
  80. webView.AddWebAllowedObject(L"__REACT_WEB_VIEW_BRIDGE", m_webBridge);
  81. }
  82. }
  83. void ReactWebView::OnMessagePosted(hstring const& message)
  84. {
  85. PostMessage(message);
  86. }
  87. void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {
  88. m_reactContext.DispatchEvent(
  89. *this,
  90. L"topLoadingFinish",
  91. [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
  92. eventDataWriter.WriteObjectBegin();
  93. WriteWebViewNavigationEventArg(webView, eventDataWriter);
  94. eventDataWriter.WriteObjectEnd();
  95. });
  96. if (m_messagingEnabled) {
  97. winrt::hstring windowAlert = L"window.alert = function (msg) {__REACT_WEB_VIEW_BRIDGE.postMessage(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
  98. winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {__REACT_WEB_VIEW_BRIDGE.postMessage(String(data))}};";
  99. webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
  100. }
  101. }
  102. void ReactWebView::OnNavigationFailed(winrt::IInspectable const& /*sender*/, winrt::WebViewNavigationFailedEventArgs const& args) {
  103. m_reactContext.DispatchEvent(
  104. *this,
  105. L"topLoadingError",
  106. [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
  107. auto httpCode = static_cast<int32_t>(args.WebErrorStatus());
  108. eventDataWriter.WriteObjectBegin();
  109. {
  110. WriteProperty(eventDataWriter, L"code", httpCode);
  111. WriteWebViewNavigationEventArg(m_webView, eventDataWriter);
  112. }
  113. eventDataWriter.WriteObjectEnd();
  114. });
  115. }
  116. void ReactWebView::PostMessage(winrt::hstring const& message) {
  117. winrt::JsonObject jsonObject;
  118. if (winrt::JsonObject::TryParse(message, jsonObject) && jsonObject.HasKey(L"type")) {
  119. auto type = jsonObject.GetNamedString(L"type");
  120. if (type == L"__alert") {
  121. auto dialog = winrt::MessageDialog(jsonObject.GetNamedString(L"message"));
  122. dialog.Commands().Append(winrt::UICommand(L"OK"));
  123. dialog.ShowAsync();
  124. return;
  125. }
  126. }
  127. m_reactContext.DispatchEvent(
  128. *this,
  129. L"topMessage",
  130. [&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
  131. eventDataWriter.WriteObjectBegin();
  132. {
  133. WriteProperty(eventDataWriter, L"data", message);
  134. }
  135. eventDataWriter.WriteObjectEnd();
  136. });
  137. }
  138. void ReactWebView::SetMessagingEnabled(bool enabled) {
  139. m_messagingEnabled = enabled;
  140. }
  141. } // namespace winrt::ReactNativeWebView::implementation