Skip to content

Commit 246c65d

Browse files
author
Diana Qu
committed
api design on expose environment
1 parent 2f7820e commit 246c65d

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

specs/GetEnvironment.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 it was not intutive to create the WebView2 Environment. Thus it was not straightforward to use the CreateWebResourceResponse off the WebView2 Environment.
3+
WebView2 team plans to introduce an easier way to get the Environment variable and CreateWebResourceResponse.
4+
5+
# Description
6+
One can get WebView2 Environment from `Environment`.
7+
8+
9+
# Examples
10+
11+
The following code snippet demonstrates how the environment APIs can be use:
12+
13+
## Win32 C++
14+
15+
```cpp
16+
// Turn on or off image blocking by adding or removing a WebResourceRequested handler
17+
// which selectively intercepts requests for images.
18+
void SettingsComponent::SetBlockImages(bool blockImages)
19+
{
20+
if (blockImages != m_blockImages)
21+
{
22+
m_blockImages = blockImages;
23+
if (m_blockImages)
24+
{
25+
m_webView->AddWebResourceRequestedFilter(L"*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE);
26+
CHECK_FAILURE(m_webView->add_WebResourceRequested(
27+
Callback<ICoreWebView2WebResourceRequestedEventHandler>(
28+
[this](
29+
ICoreWebView2* sender,
30+
ICoreWebView2WebResourceRequestedEventArgs* args) {
31+
COREWEBVIEW2_WEB_RESOURCE_CONTEXT resourceContext;
32+
CHECK_FAILURE(
33+
args->get_ResourceContext(&resourceContext));
34+
// Ensure that the type is image
35+
if (resourceContext != COREWEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE)
36+
{
37+
return E_INVALIDARG;
38+
}
39+
// Override the response with an empty one to block the image.
40+
// If put_Response is not called, the request will continue as normal.
41+
wil::com_ptr<ICoreWebView2WebResourceResponse> response;
42+
// Environment Usage
43+
wil::com_ptr<ICoreWebView2Staging> m_webViewstaging =
44+
m_webView.try_query<ICoreWebView2Staging>();
45+
wil::com_ptr<ICoreWebView2Environment> environment;
46+
CHECK_FAILURE(m_webViewstaging->get_Environment(&environment));
47+
CHECK_FAILURE(environment->CreateWebResourceResponse(
48+
nullptr, 403 /*NoContent*/, L"Blocked", L"", &response));
49+
CHECK_FAILURE(args->put_Response(response.get()));
50+
return S_OK;
51+
})
52+
.Get(),
53+
&m_webResourceRequestedTokenForImageBlocking));
54+
}
55+
else
56+
{
57+
CHECK_FAILURE(m_webView->remove_WebResourceRequested(
58+
m_webResourceRequestedTokenForImageBlocking));
59+
}
60+
}
61+
}
62+
```
63+
64+
## .NET and WinRT
65+
66+
```c#
67+
private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
68+
{
69+
CoreWebView2Environment environment = webView2Control.CoreWebView2.Environment;
70+
// Create response object for custom response and set it
71+
var environment = webView2Control.CoreWebView2.Environment;
72+
CoreWebView2WebResourceResponse response = environment.CreateWebResourceResponse(null, 403, "Blocked", "");
73+
e.Response = response;
74+
75+
// statusCode will now be accessible and equal to 403
76+
var code = e.Response.StatusCode;
77+
}
78+
```
79+
80+
# API Notes
81+
82+
See [API Details](#api-details) section below for API reference.
83+
84+
# API Details
85+
86+
## Win32 C++
87+
88+
```IDL
89+
interface ICoreWebView2;
90+
91+
[uuid(76eceacb-0462-4d94-ac83-423a6793775e), object, pointer_default(unique)]
92+
interface ICoreWebView2 : IUnknown {
93+
[propget] HRESULT Environment([out, retval] ICoreWebView2Environment** environment);
94+
}
95+
```
96+
97+
## .NET and WinRT
98+
99+
```c#
100+
namespace Microsoft.Web.WebView2.Core
101+
{
102+
public partial class CoreWebView2
103+
{
104+
public CoreWebView2Environment Environment { get; };
105+
}
106+
}
107+
```

0 commit comments

Comments
 (0)