Skip to content

Commit 8128531

Browse files
Merge pull request #617 from MicrosoftEdge/api-uastring
Create APIReview_UAString
2 parents 8dad30f + 157c1cc commit 8128531

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

specs/APIReview_UAString.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Background
2+
3+
Currently, a developer can pass the --user-agent browser args to the CreateWebView2EnvironmentWithDetails function.
4+
5+
Ex. CreateWebView2EnvironmentWithDetails(nullptr, nullptr, L"--user-agent=\\"myUA\\"", ...);
6+
7+
For more info about the ‘--user-agent’ flag visit: https://peter.sh/experiments/chromium-command-line-switches/#user-agent.
8+
9+
However, there are a couple limitations to this workaround-- it is not an API that is easy to use or discover, you cannot modify a command line switch at runtime, and you cannot change the User Agent per WebView. In this document we describe the new API. We'd appreciate your feedback.
10+
11+
# Description
12+
13+
The User Agent (UA) is a client-side piece of information regarding the user's OS, application, and version. The browser/webcontrol sends the User Agent to the HTTP server.
14+
15+
The User Agent property lets developers modify WebView2's User Agent. A key scenario is to allow end developers to get the current User Agent from the WebView and modify it based on an event, such as navigating to a specific website and setting the User Agent to emulate a different browser version.
16+
17+
# Examples
18+
19+
The following code snippet demonstrates how the User Agent property can be used:
20+
21+
## Win32 C++
22+
23+
```cpp
24+
m_webView->add_NavigationStarting(
25+
Callback<ICoreWebView2NavigationStartingEventHandler>(
26+
[this](ICoreWebView2 *sender,
27+
ICoreWebView2NavigationStartingEventArgs *args) -> HRESULT {
28+
static const PCWSTR url_compare_example = L"foo.org";
29+
wil::unique_bstr domain = GetDomainOfUri(uri.get());
30+
const wchar_t *domains = domain.get();
31+
wil::com_ptr<ICoreWebView2Settings> settings;
32+
CHECK_FAILURE(m_webView->get_Settings(&m_settings));
33+
if (wcscmp(url_compare_example, domains) == 0) {
34+
// Upon navigation to a specified url
35+
// Change the user agent to emulate a mobile device
36+
CHECK_FAILURE(settings->put_UserAgent(GetMobileUserAgent()));
37+
} else {
38+
// Change the user agent back to desktop
39+
CHECK_FAILURE(settings->put_UserAgent(GetDesktopUserAgent()));
40+
}
41+
return S_OK;
42+
})
43+
.Get(),
44+
&m_navigationStartingToken);
45+
```
46+
47+
## .NET and WinRT
48+
49+
```c #
50+
private void SetUserAgent(CoreWebView2 sender, CoreWebView2UserAgentArgs e) {
51+
var settings = webView2Control.CoreWebView2.Settings;
52+
settings.UserAgent = GetUserAgent();
53+
}
54+
```
55+
56+
# API Notes
57+
58+
See [API Details](#api-details) section below for API reference.
59+
60+
# API Details
61+
62+
## Win32 C++
63+
64+
```IDL
65+
// This is the ICoreWebView2Settings Staging interface.
66+
[uuid(c79ba37e-9bd6-4b9e-b460-2ced163f231f), object, pointer_default(unique)]
67+
interface ICoreWebView2StagingSettings : IUnknown {
68+
/// `UserAgent` . Returns the User Agent. The default value is the
69+
/// default User Agent of the Edge browser.
70+
[propget] HRESULT UserAgent([ out, retval ] LPWSTR * userAgent);
71+
/// Sets the `UserAgentString` property. This property may be overriden if
72+
/// the User-Agent header is set in a request. If the parameter is empty
73+
/// the User Agent will not be updated and the current User Agent will remain.
74+
[propput] HRESULT UserAgent([in] LPCWSTR userAgent);
75+
}
76+
```
77+
## .NET and WinRT
78+
79+
```c#
80+
namespace Microsoft.Web.WebView2.Core
81+
{
82+
public partial class CoreWebView2Settings
83+
{
84+
public string UserAgent { get; set; };
85+
}
86+
}
87+
```

0 commit comments

Comments
 (0)