|
| 1 | +# Background |
| 2 | +When the WebView2 team was making design changes to the [WebResourceRequested API](https://github.com/MicrosoftEdge/WebView2Feedback/wiki/WebResourceRequested-API-Review-Spec) for .NET, we realized several caveats. It was not ideal to force all end developers to keep references back to the CoreWebView2Environment from their CoreWebView2 event handlers, and also in the case of WPF, WinForms and WinUI3.0 the UI framework can create the CoreWebView2Environment internally with no easy way for the end developers to obtain a reference to it. Thus providing a reference to the CoreWebView2Environment off of the CoreWebView2 solves both of those problems. |
| 3 | + |
| 4 | +# Description |
| 5 | +Get the `CoreWebView2Environment` used to create the `CoreWebView2` from that `CoreWebView2`'s `Environment` property |
| 6 | + |
| 7 | + |
| 8 | +# Examples |
| 9 | + |
| 10 | +The following code snippet demonstrates how the environment APIs can be use: |
| 11 | + |
| 12 | +## Win32 C++ |
| 13 | + |
| 14 | +```cpp |
| 15 | +// Turn on or off image blocking by adding or removing a WebResourceRequested handler |
| 16 | +// which selectively intercepts requests for images. |
| 17 | +void SettingsComponent::SetBlockImages(bool blockImages) |
| 18 | +{ |
| 19 | + if (blockImages != m_blockImages) |
| 20 | + { |
| 21 | + m_blockImages = blockImages; |
| 22 | + if (m_blockImages) |
| 23 | + { |
| 24 | + m_webView->AddWebResourceRequestedFilter(L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE); |
| 25 | + CHECK_FAILURE(m_webView->add_WebResourceRequested( |
| 26 | + Callback<ICoreWebView2WebResourceRequestedEventHandler>( |
| 27 | + [this]( |
| 28 | + ICoreWebView2* sender, |
| 29 | + ICoreWebView2WebResourceRequestedEventArgs* args) { |
| 30 | + COREWEBVIEW2_WEB_RESOURCE_CONTEXT resourceContext; |
| 31 | + CHECK_FAILURE( |
| 32 | + args->get_ResourceContext(&resourceContext)); |
| 33 | + // Ensure that the type is image |
| 34 | + if (resourceContext != COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE) |
| 35 | + { |
| 36 | + return E_INVALIDARG; |
| 37 | + } |
| 38 | + // Override the response with an empty one to block the image. |
| 39 | + // If put_Response is not called, the request will continue as normal. |
| 40 | + wil::com_ptr<ICoreWebView2WebResourceResponse> response; |
| 41 | + // Environment Usage |
| 42 | + wil::com_ptr<ICoreWebView2Environment> environment; |
| 43 | + CHECK_FAILURE(m_webView->get_Environment(&environment)); |
| 44 | + CHECK_FAILURE(environment->CreateWebResourceResponse( |
| 45 | + nullptr, 403 /*NoContent*/, L"Blocked", L"", &response)); |
| 46 | + CHECK_FAILURE(args->put_Response(response.get())); |
| 47 | + return S_OK; |
| 48 | + }) |
| 49 | + .Get(), |
| 50 | + &m_webResourceRequestedTokenForImageBlocking)); |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + CHECK_FAILURE(m_webView->remove_WebResourceRequested( |
| 55 | + m_webResourceRequestedTokenForImageBlocking)); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | +
|
| 61 | +## .NET and WinRT |
| 62 | +
|
| 63 | +```c# |
| 64 | + private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e) |
| 65 | + { |
| 66 | + // Create response object for custom response and set it |
| 67 | + var environment = webView2Control.CoreWebView2.Environment; |
| 68 | + CoreWebView2WebResourceResponse response = environment.CreateWebResourceResponse(null, 403, "Blocked", ""); |
| 69 | + e.Response = response; |
| 70 | +
|
| 71 | + // statusCode will now be accessible and equal to 403 |
| 72 | + var code = e.Response.StatusCode; |
| 73 | + } |
| 74 | +``` |
| 75 | + |
| 76 | +# API Notes |
| 77 | + |
| 78 | +See [API Details](#api-details) section below for API reference. |
| 79 | + |
| 80 | +# API Details |
| 81 | + |
| 82 | +## Win32 C++ |
| 83 | + |
| 84 | +```IDL |
| 85 | +interface ICoreWebView2; |
| 86 | +interface ICoreWebView2_2; |
| 87 | +
|
| 88 | +[uuid(76eceacb-0462-4d94-ac83-423a6793775e), object, pointer_default(unique)] |
| 89 | +interface ICoreWebView2_2 : ICoreWebView2 { |
| 90 | + /// Exposes the CoreWebView2Environment used to create this CoreWebView2. |
| 91 | + [propget] HRESULT Environment([out, retval] ICoreWebView2Environment** environment); |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## .NET and WinRT |
| 96 | + |
| 97 | +```c# |
| 98 | +namespace Microsoft.Web.WebView2.Core |
| 99 | +{ |
| 100 | + public partial class CoreWebView2 |
| 101 | + { |
| 102 | + // There are other API in this interface that we are not showing |
| 103 | + public CoreWebView2Environment Environment { get; }; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
0 commit comments