Нет описания

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. RegisterEvents();
  26. }
  27. winrt::WebView ReactWebView::GetView() {
  28. return m_webView;
  29. }
  30. void ReactWebView::RegisterEvents() {
  31. m_navigationStartingRevoker = m_webView.NavigationStarting(
  32. winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
  33. if (auto self = ref.get()) {
  34. self->OnNavigationStarting(sender, args);
  35. }
  36. });
  37. m_navigationCompletedRevoker = m_webView.NavigationCompleted(
  38. winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
  39. if (auto self = ref.get()) {
  40. self->OnNavigationCompleted(sender, args);
  41. }
  42. });
  43. m_navigationFailedRevoker = m_webView.NavigationFailed(
  44. winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
  45. if (auto self = ref.get()) {
  46. self->OnNavigationFailed(sender, args);
  47. }
  48. });
  49. m_scriptNotifyRevoker = m_webView.ScriptNotify(
  50. winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
  51. if (auto self = ref.get()) {
  52. self->OnScriptNotify(sender, args);
  53. }
  54. });
  55. }
  56. void ReactWebView::WriteWebViewNavigationEventArg(winrt::IJSValueWriter const& eventDataWriter) {
  57. auto tag = m_webView.GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
  58. WriteProperty(eventDataWriter, L"canGoBack", m_webView.CanGoBack());
  59. WriteProperty(eventDataWriter, L"canGoForward", m_webView.CanGoForward());
  60. WriteProperty(eventDataWriter, L"loading", !m_webView.IsLoaded());
  61. WriteProperty(eventDataWriter, L"target", tag);
  62. WriteProperty(eventDataWriter, L"title", m_webView.DocumentTitle());
  63. if (auto uri = m_webView.Source()) {
  64. WriteProperty(eventDataWriter, L"url", uri.AbsoluteCanonicalUri());
  65. }
  66. }
  67. void ReactWebView::OnNavigationStarting(winrt::WebView const& webView, winrt::WebViewNavigationStartingEventArgs const& /*args*/) {
  68. m_reactContext.DispatchEvent(
  69. webView,
  70. L"topLoadingStart",
  71. [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
  72. eventDataWriter.WriteObjectBegin();
  73. WriteWebViewNavigationEventArg(eventDataWriter);
  74. eventDataWriter.WriteObjectEnd();
  75. });
  76. }
  77. void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {
  78. m_reactContext.DispatchEvent(
  79. webView,
  80. L"topLoadingFinish",
  81. [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
  82. eventDataWriter.WriteObjectBegin();
  83. WriteWebViewNavigationEventArg(eventDataWriter);
  84. eventDataWriter.WriteObjectEnd();
  85. });
  86. winrt::hstring windowAlert = L"window.alert = function (msg) {window.external.notify(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
  87. winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
  88. m_webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
  89. }
  90. void ReactWebView::OnNavigationFailed(winrt::IInspectable const& /*sender*/, winrt::WebViewNavigationFailedEventArgs const& args) {
  91. m_reactContext.DispatchEvent(
  92. m_webView,
  93. L"topLoadingError",
  94. [&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
  95. auto httpCode = static_cast<int32_t>(args.WebErrorStatus());
  96. eventDataWriter.WriteObjectBegin();
  97. {
  98. WriteProperty(eventDataWriter, L"code", httpCode);
  99. WriteWebViewNavigationEventArg(eventDataWriter);
  100. }
  101. eventDataWriter.WriteObjectEnd();
  102. });
  103. }
  104. void ReactWebView::OnScriptNotify(winrt::IInspectable const& /*sender*/, winrt::Windows::UI::Xaml::Controls::NotifyEventArgs const& args) {
  105. winrt::JsonObject jsonObject;
  106. if (winrt::JsonObject::TryParse(args.Value(), jsonObject) && jsonObject.HasKey(L"type")) {
  107. auto type = jsonObject.GetNamedString(L"type");
  108. if (type == L"__alert") {
  109. auto dialog = winrt::MessageDialog(jsonObject.GetNamedString(L"message"));
  110. dialog.Commands().Append(winrt::UICommand(L"OK"));
  111. dialog.ShowAsync();
  112. return;
  113. }
  114. }
  115. PostMessage(winrt::hstring(args.Value()));
  116. }
  117. void ReactWebView::PostMessage(winrt::hstring const& message) {
  118. m_reactContext.DispatchEvent(
  119. m_webView,
  120. L"topMessage",
  121. [&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
  122. eventDataWriter.WriteObjectBegin();
  123. {
  124. WriteProperty(eventDataWriter, L"data", message);
  125. }
  126. eventDataWriter.WriteObjectEnd();
  127. });
  128. }
  129. } // namespace winrt::ReactNativeWebView::implementation