|
| 1 | +// Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | +#include "stdafx.h" |
| 5 | + |
| 6 | +#include "ScenarioCustomScheme.h" |
| 7 | + |
| 8 | +#include "AppWindow.h" |
| 9 | +#include "CheckFailure.h" |
| 10 | + |
| 11 | +#include <Shlwapi.h> |
| 12 | + |
| 13 | +using namespace Microsoft::WRL; |
| 14 | + |
| 15 | +ScenarioCustomScheme::ScenarioCustomScheme(AppWindow* appWindow) : m_appWindow(appWindow) |
| 16 | +{ |
| 17 | + CHECK_FAILURE(m_appWindow->GetWebView()->AddWebResourceRequestedFilter( |
| 18 | + L"custom-scheme*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL)); |
| 19 | + CHECK_FAILURE(m_appWindow->GetWebView()->add_WebResourceRequested( |
| 20 | + Callback<ICoreWebView2WebResourceRequestedEventHandler>( |
| 21 | + [this](ICoreWebView2* sender, ICoreWebView2WebResourceRequestedEventArgs* args) |
| 22 | + { |
| 23 | + wil::com_ptr<ICoreWebView2WebResourceRequest> request; |
| 24 | + wil::com_ptr<ICoreWebView2WebResourceResponse> response; |
| 25 | + CHECK_FAILURE(args->get_Request(&request)); |
| 26 | + wil::unique_cotaskmem_string uri; |
| 27 | + CHECK_FAILURE(request->get_Uri(&uri)); |
| 28 | + if (wcsncmp(uri.get(), L"custom-scheme", ARRAYSIZE(L"custom-scheme") - 1) == 0) |
| 29 | + { |
| 30 | + std::wstring assetsFilePath = L"assets/"; |
| 31 | + assetsFilePath += wcsstr(uri.get(), L":") + 1; |
| 32 | + wil::com_ptr<IStream> stream; |
| 33 | + SHCreateStreamOnFileEx( |
| 34 | + assetsFilePath.c_str(), STGM_READ, FILE_ATTRIBUTE_NORMAL, FALSE, |
| 35 | + nullptr, &stream); |
| 36 | + if (stream) |
| 37 | + { |
| 38 | + CHECK_FAILURE( |
| 39 | + m_appWindow->GetWebViewEnvironment()->CreateWebResourceResponse( |
| 40 | + stream.get(), 200, L"OK", |
| 41 | + L"Content-Type: application/json\nAccess-Control-Allow-Origin: " |
| 42 | + L"*", |
| 43 | + &response)); |
| 44 | + CHECK_FAILURE(args->put_Response(response.get())); |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + CHECK_FAILURE( |
| 49 | + m_appWindow->GetWebViewEnvironment()->CreateWebResourceResponse( |
| 50 | + nullptr, 404, L"Not Found", L"", &response)); |
| 51 | + CHECK_FAILURE(args->put_Response(response.get())); |
| 52 | + } |
| 53 | + return S_OK; |
| 54 | + } |
| 55 | + |
| 56 | + return S_OK; |
| 57 | + }) |
| 58 | + .Get(), |
| 59 | + &m_webResourceRequestedToken)); |
| 60 | + |
| 61 | + m_appWindow->GetWebView()->add_NavigationCompleted( |
| 62 | + Callback<ICoreWebView2NavigationCompletedEventHandler>( |
| 63 | + [this](ICoreWebView2* sender, ICoreWebView2NavigationCompletedEventArgs* args) |
| 64 | + { |
| 65 | + // The following XHR will execute in the context of https://www.example.com page |
| 66 | + // and will succeed. WebResourceRequested event will be raised for this request |
| 67 | + // as *.example.com is in the allowed origin list of custom-scheme. Since the |
| 68 | + // response header provided in WebResourceRequested handler allows all origins |
| 69 | + // for CORS the XHR succeeds. |
| 70 | + CHECK_FAILURE(m_appWindow->GetWebView()->ExecuteScript( |
| 71 | + L"function reqListener(e) { console.log(e.data) };" |
| 72 | + L"function errListener(e) { console.log(e.error) };" |
| 73 | + L"var oReq = new XMLHttpRequest();" |
| 74 | + L"oReq.addEventListener(\"load\", reqListener);" |
| 75 | + L"oReq.addEventListener(\"error\", errListener);" |
| 76 | + L"oReq.open(\"GET\", \"custom-scheme:ScenarioCustomScheme.json\");" |
| 77 | + L"oReq.send();", |
| 78 | + Callback<ICoreWebView2ExecuteScriptCompletedHandler>( |
| 79 | + [](HRESULT error, PCWSTR result) -> HRESULT { return S_OK; }) |
| 80 | + .Get())); |
| 81 | + // The following XHR will fail because *.example.com is not in the allowed |
| 82 | + // origin list of custom-scheme2. The WebResourceRequested event will not be |
| 83 | + // raised for this request. |
| 84 | + CHECK_FAILURE(m_appWindow->GetWebView()->ExecuteScript( |
| 85 | + L"var oReq = new XMLHttpRequest();" |
| 86 | + L"oReq.addEventListener(\"load\", reqListener);" |
| 87 | + L"oReq.addEventListener(\"error\", errListener);" |
| 88 | + L"oReq.open(\"GET\", " |
| 89 | + L"\"custom-scheme-not-in-allowed-origins://" |
| 90 | + L"ScenarioCustomScheme.json\");" |
| 91 | + L"oReq.send();", |
| 92 | + Callback<ICoreWebView2ExecuteScriptCompletedHandler>( |
| 93 | + [](HRESULT error, PCWSTR result) -> HRESULT { return S_OK; }) |
| 94 | + .Get())); |
| 95 | + CHECK_FAILURE(m_appWindow->GetWebView()->remove_NavigationCompleted( |
| 96 | + m_navigationCompletedToken)); |
| 97 | + m_navigationCompletedToken = {0}; |
| 98 | + return S_OK; |
| 99 | + }) |
| 100 | + .Get(), |
| 101 | + &m_navigationCompletedToken); |
| 102 | + m_appWindow->GetWebView()->Navigate(L"https://www.example.com"); |
| 103 | +} |
| 104 | + |
| 105 | +ScenarioCustomScheme::~ScenarioCustomScheme() |
| 106 | +{ |
| 107 | + CHECK_FAILURE( |
| 108 | + m_appWindow->GetWebView()->remove_WebResourceRequested(m_webResourceRequestedToken)); |
| 109 | +} |
0 commit comments