SteveSandersonMS / WebWindow

.NET Core library to open native OS windows containing web UI on Windows, Mac, and Linux. Experimental.
Apache License 2.0
1.99k stars 215 forks source link

testassets/HelloWorldApp: Scheme Handlers.Add do not work correct #52

Open ITAgnesmeyer opened 4 years ago

ITAgnesmeyer commented 4 years ago

The application works. Except adding SchemeHandlers. The application opens but no message box is displayed. But that should be the case. Because:

When I open the developer tools and look in the console, I see the following: something.js/:1 Failed to load resource: net::ERR_UNKNOWN_URL_SCHEME

I USE: Microsoft Edge Microsoft Edge is up to date. Version 81.0.389.2 (Official build) dev (64-bit)

Microsoft Visual Studio Professional 2019 Preview Version 16.5.0 Preview 1.0

ITAgnesmeyer commented 4 years ago

OK Found Solution: IN WebWindow.h add:

wil::com_ptr<IWebView2WebView5> _webviewWindow5;

In WebWindow.Windows.cpp add:

...
// Create a WebView, whose parent is the main window hWnd
                env->CreateWebView(_hWnd, Callback<IWebView2CreateWebViewCompletedHandler>(
                    [&, this](HRESULT result, IWebView2WebView* webview) -> HRESULT {
                        if (webview != nullptr) {
                            _webviewWindow = webview;
                            _webviewWindow5 =(IWebView2WebView5*) webview;
                        }

And later change:

_webviewWindow5->AddWebResourceRequestedFilter(L"*", WEBVIEW2_WEB_RESOURCE_CONTEXT_ALL);

_webviewWindow5->add_WebResourceRequested( Callback<IWebView2WebResourceRequestedEventHandler>(
    [this](IWebView2WebView* sender, IWebView2WebResourceRequestedEventArgs* args)
    {
        IWebView2WebResourceRequest* req;
        args->get_Request(&req);

        wil::unique_cotaskmem_string uri;
        req->get_Uri(&uri);
        std::wstring uriString = uri.get();
        size_t colonPos = uriString.find(L':', 0);
        if (colonPos > 0)
        {
            std::wstring scheme = uriString.substr(0, colonPos);
            WebResourceRequestedCallback handler = _schemeToRequestHandler[scheme];
            if (handler != NULL)
            {
                int numBytes;
                AutoString contentType;
                wil::unique_cotaskmem dotNetResponse(handler(uriString.c_str(), &numBytes, &contentType));

                if (dotNetResponse != nullptr && contentType != nullptr)
                {
                    std::wstring contentTypeWS = contentType;

                    IStream* dataStream = SHCreateMemStream((BYTE*)dotNetResponse.get(), numBytes);
                    wil::com_ptr<IWebView2WebResourceResponse> response;
                    _webviewEnvironment->CreateWebResourceResponse(
                        dataStream, 200, L"OK", (L"Content-Type: " + contentTypeWS).c_str(),
                        &response);
                    args->put_Response(response.get());
                }
            }
        }

        return S_OK;
    }
).Get(), &webResourceRequestedToken);