Skip to content

Commit b73646a

Browse files
committed
WebResourceResponseReceived API doc Rev 2
1 parent fe03e64 commit b73646a

1 file changed

Lines changed: 43 additions & 27 deletions

File tree

specs/WebResourceResponseReceived.md

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
# Background
22
The WebView2 team has been asked for an API to get the response for a web
33
resource as it was received and to provide request headers not available when
4-
`WebResourceRequested` event fires (such as Authentication headers). The
5-
`WebResourceResponseReceived` event provides such HTTP response representation
6-
and exposes the request as sent over the wire.
4+
`WebResourceRequested` event is raised (such as HTTP Authentication headers).
5+
The `WebResourceResponseReceived` event provides such response representation
6+
and exposes the request as committed.
77

88
In this document we describe the new API. We'd appreciate your feedback.
99

1010
# Description
11-
The `WebResourceResponseReceived` event fires when the WebView receives the
12-
response for a request for a web resource. It provides access to both the
13-
response as it was received and the request as it was sent over the wire,
14-
including modifications made by the network stack (such as adding of
15-
Authorization headers). The app can use this event to view the actual request
16-
and response for a web resource, but modifications made to these objects are
17-
ignored.
11+
The `WebResourceResponseReceived` event allows developers to inspect the
12+
response object from URL requests (such as HTTP/HTTPS, file and data). A key
13+
scenario is to allow developers to get Auth headers from an HTTP response to
14+
authenticate other tools they're using, since the Auth headers are not exposed
15+
in the `WebResourceRequested` event.
16+
17+
This event is raised when the WebView receives the response for a request for a
18+
web resource. It provides access to both the response as it was received and the
19+
request as it was committed, including modifications made by the network stack
20+
(such as the adding of HTTP Authorization headers). The app can use this event
21+
to view the actual request and response for a web resource, but modifications
22+
made to these objects are ignored.
1823

1924
The host app registers for this event by providing a
2025
`WebResourceResponseReceivedEventHandler` (or delegate) to WebView's
2126
`add_WebResourceResponseReceived`/`WebResourceResponseReceived`. When invoking
2227
the handler, the WebView will pass a `WebResourceResponseReceivedEventArgs`,
2328
which lets the app view the request and response. The additional
2429
`PopulateResponseContent` API is exposed from the event arguments so the app
25-
can get the response's body (if it has one).
30+
can get the response's body (if it has one). If the app tries to get the
31+
response content before calling `PopulateResponseContent`, the stream object
32+
returned will be null.
2633

2734
# Examples
28-
The following code snippets demonstrates how the `WebResourceResponseReceived`
35+
The following code snippets demonstrate how the `WebResourceResponseReceived`
2936
event can be used:
3037

3138
## COM
@@ -36,7 +43,7 @@ m_webview->add_WebResourceResponseReceived(
3643
Callback<ICoreWebView2WebResourceResponseReceivedEventHandler>(
3744
[this](ICoreWebView2* webview, ICoreWebView2WebResourceResponseReceivedEventArgs* args)
3845
-> HRESULT {
39-
// The request object as sent by the wire
46+
// The request object as committed
4047
wil::com_ptr<ICoreWebView2WebResourceRequest> webResourceRequest;
4148
CHECK_FAILURE(args->get_Request(&webResourceRequest));
4249
// The response object as received
@@ -102,19 +109,19 @@ private async void WebView_WebResourceResponseReceived(object sender, CoreWebVie
102109
{
103110
await e.PopulateResponseContentAsync();
104111
DoSomethingWithResponseBody(e.Response.Content);
105-
} catch (Exception ex)
112+
}
113+
catch (COMException ex)
106114
{
107-
// An exception will be thrown if the request has no body.
115+
// A COMException will be thrown if the request has no body.
108116
}
109117
}
110118
}
111119
```
112120

113121

114122
# Remarks
115-
Calling `PopulateResponseContent` will fail/throw an exception if the response
116-
has no body. An exception will also be thrown for redirect responses, for which
117-
the body (if any) is ignored.
123+
Calling `PopulateResponseContent` will fail/throw a COMException if the response
124+
has no body. Note the body for redirect responses is ignored.
118125

119126

120127
# API Notes
@@ -131,9 +138,9 @@ library WebView2
131138
// ...
132139

133140
/// Add an event handler for the WebResourceResponseReceived event.
134-
/// WebResourceResponseReceived event fires after the WebView has received
141+
/// WebResourceResponseReceived event is raised after the WebView has received
135142
/// and processed the response for a WebResource request. The event args
136-
/// include the WebResourceRequest as sent by the wire and WebResourceResponse
143+
/// include the WebResourceRequest as committed and the WebResourceResponse
137144
/// received, including any additional headers added by the network stack that
138145
/// were not be included as part of the associated WebResourceRequested event,
139146
/// such as Authentication headers.
@@ -146,12 +153,12 @@ library WebView2
146153
[in] EventRegistrationToken token);
147154
}
148155

149-
/// Fires when a response for a request is received for a Web resource in the webview.
156+
/// Raised when a response for a request is received for a Web resource in the webview.
150157
/// Host can use this event to view the actual request and response for a Web resource.
151158
/// This includes any request or response modifications made by the network stack (such as
152-
/// adding of Authorization headers) after the WebResourceRequested event for
153-
/// the associated request has fired. Modifications made to the request or response
154-
/// objects are ignored.
159+
/// the adding of Authorization headers) after the WebResourceRequested event for
160+
/// the associated request has been raised. Modifications made to the request or
161+
/// response objects are ignored.
155162
interface ICoreWebView2WebResourceResponseReceivedEventHandler : IUnknown
156163
{
157164
/// Called to provide the implementer with the event args for the
@@ -184,7 +191,9 @@ library WebView2
184191
/// will be ignored.
185192
[propget] HRESULT Response([out, retval] ICoreWebView2WebResourceResponse** response);
186193

187-
/// Async method to request the Content stream of the response.
194+
/// Async method to ensure that the Content property of the response contains the actual response body content.
195+
/// If this method is being called again before a first call has completed, all handlers are invoked at the same time.
196+
/// If this method is being called after a first call has completed, the handler is invoked immediately.
188197
HRESULT PopulateResponseContent(ICoreWebView2WebResourceResponseReceivedEventArgsPopulateResponseContentCompletedHandler* handler);
189198
}
190199
}
@@ -198,11 +207,16 @@ namespace Microsoft.Web.WebView2.Core
198207
{
199208
// ...
200209
201-
/// WebResourceResponseReceived event fires after the WebView has received and processed the response for a WebResource request.
210+
/// WebResourceResponseReceived event is raised after the WebView has received and processed the response for a WebResource request.
211+
/// The event args include the WebResourceRequest as committed and the WebResourceResponse received,
212+
/// including any additional headers added by the network stack that were not be included as part of
213+
/// the associated WebResourceRequested event, such as Authentication headers.
202214
event Windows.Foundation.TypedEventHandler<CoreWebView2, CoreWebView2WebResourceResponseReceivedEventArgs> WebResourceResponseReceived;
203215
}
204216

205217
/// Event args for the WebResourceResponseReceived event.
218+
/// Note: To get the response content stream, first call PopulateResponseContentAsync and
219+
/// wait for the call to complete, otherwise the content stream object returned will be null.
206220
runtimeclass CoreWebView2WebResourceResponseReceivedEventArgs
207221
{
208222
/// Web resource request object.
@@ -212,7 +226,9 @@ namespace Microsoft.Web.WebView2.Core
212226
/// Any modifications to this object will be ignored.
213227
CoreWebView2WebResourceResponse Response { get; };
214228

215-
/// Async method to request the Content stream of the response.
229+
/// Async method to ensure that the Content property of the response contains the actual response body content.
230+
/// If this method is being called again before a first call has completed, it will complete at the same time all prior calls do.
231+
/// If this method is being called after a first call has completed, it will return immediately (asynchronously).
216232
Windows.Foundation.IAsyncAction PopulateResponseContentAsync();
217233
}
218234
}

0 commit comments

Comments
 (0)