Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Unable to receive a ScriptNotify event from: ms-local-stream:// #1600

Closed
tero-paananen opened this issue Aug 28, 2020 · 4 comments
Closed

Comments

@tero-paananen
Copy link
Contributor

tero-paananen commented Aug 28, 2020

Bug description:

When opening web page from the application local folder ms-appdata:///local/some.html, is messaging from the web to the react native app impossible.
Read more from social.msdn.microsoft.com

DOM7010: Unable to receive a ScriptNotify event from: ‘ms-local-stream://xyz.xyz_local_7573657273/some.html’. 
The website attempted to send a ScriptNotify event to the app from a WebView URI that is not included in the ApplicationContentUriRules 
for this app. To permit this event, add the URI to the ApplicationContentUriRules section of the package manifest. 
(In Visual Studio, add this URI to the Content URIs tab of the Manifest Designer.)

Reason is that window.external.notify = window.ReactNativeWebView.postMessage is not allowed from local file ms-appdata:///local/some.html.

ApplicationContentUriRules is not possible to set ms-appdata scheme. See more from interacting-with-web-view-content that says The manifest requirement does not apply to content that originates from the app package, uses an ms-local-stream:// URI, or is loaded using NavigateToString

Solution:
Solution seems to be use WebView.NavigateToLocalStreamUri

Am I right?

Code
I tried to implement IUriToStreamResolver using C++

// UriToStreamResolver.h
#pragma once

#include <winrt/Windows.Web.h>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Streams.h>

namespace winrt::ReactNativeWebView::implementation {
  class UriToStreamResolver : public winrt::implements<UriToStreamResolver, winrt::Windows::Web::IUriToStreamResolver>
  {
    public:
      UriToStreamResolver();
      winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::Streams::IInputStream> UriToStreamAsync(const winrt::Windows::Foundation::Uri& uri);
  };
}
// UriToStreamResolver.cpp
#include "pch.h"
#include "UriToStreamResolver.h"

using namespace winrt::Windows::Storage;

namespace winrt::ReactNativeWebView::implementation {

  UriToStreamResolver::UriToStreamResolver()
  {}

  winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Storage::Streams::IInputStream> UriToStreamResolver::UriToStreamAsync(const winrt::Windows::Foundation::Uri& uri)
  {
    auto safe_uri = uri;
    auto file = co_await StorageFile::GetFileFromApplicationUriAsync(safe_uri);
    auto stream = co_await file.OpenAsync(FileAccessMode::Read);
    auto inputStream = stream.GetInputStreamAt(0);
    co_return inputStream;
  }
}

And using UriToStreamResolver in ReactWebViewManager::UpdateProperties() function

...
if (uriString.find("ms-appdata:///local/") == 0) {
    auto streamResolver = UriToStreamResolver();
    webView.NavigateToLocalStreamUri(winrt::Uri(to_hstring(uriString)), streamResolver);
}
...

but i did not manage to implement it right. I got error Microsoft C++ exception: winrt::hresult_invalid_argument at memory location 0x00000052B94FC158. and did not manage to find solution for that or right implementation.

Is someone found solution for that?

Environment:

  • OS: Windows 10
  • react-native version: 0.62.2
  • react-native-windows version: 0.62.5
  • react-native-webview version: 10.8.2
@tero-paananen
Copy link
Contributor Author

Another solution might be implement bridge object that was in implemented in older C# react-native-webview https://github.com/microsoft/react-native-windows/blob/0.59-legacy/current/ReactWindows/ReactNativeWebViewBridge/WebViewBridge.cs

@tero-paananen
Copy link
Contributor Author

I will try to implement WebView.messagingEnabled feature into Windows WebView that is currently missing

@tero-paananen
Copy link
Contributor Author

I have implementation that has Bridge and window.external.notify is changed to use bridge __REACT_WEB_VIEW_BRIDGE.postMessage like old C# implementatio has. I will make PR later

@tero-paananen
Copy link
Contributor Author

See the PR #1617

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant