Skip to content

Commit dd70f22

Browse files
author
Maura Winstanley
committed
md file
1 parent 65f5caf commit dd70f22

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

specs/APIReview_UAString.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#Background
2+
The User Agent is a client-side piece of information that the browser/webcontrol sends to the server/website a user visits.
3+
It contains information about user’s system and is modifiable by the user.
4+
5+
Currently, a developer can pass the --user-agent browser args to the CreateWebView2EnvironmentWithDetails function.
6+
Ex. CreateWebView2EnvironmentWithDetails(nullptr, nullptr, L"--user-agent=\"myUA\"", ...);
7+
For more info about the ‘—user - agent’ flag visit : https
8+
: // peter.sh/experiments/chromium-command-line-switches/#user-agent.
9+
10+
However,
11+
there are a couple limitations to this workaround-- you
12+
cannot modify a command line switch at runtime,
13+
nor can you change the user agent per webview
14+
.
15+
16+
In this document we describe the new API
17+
.We'd appreciate your feedback.
18+
19+
#Description
20+
The Settings component will change the UA per WebView2 via Chrome Developer
21+
Protocol command.(CDP)
22+
A key scenario is to allow end developers to get the current user
23+
agent from the webview and modify it based on some sort of event,
24+
such as navigating to a specific website and setting the user agent to
25+
emulate a different browser version
26+
.
27+
28+
#Examples
29+
30+
The following code snippet demonstrates how the environment APIs can be used
31+
:
32+
33+
##Win32 C++
34+
35+
```cpp m_webView->add_NavigationStarting(
36+
Callback<ICoreWebView2NavigationStartingEventHandler>(
37+
[this](ICoreWebView2 *sender,
38+
ICoreWebView2NavigationStartingEventArgs *args) -> HRESULT {
39+
static const PCWSTR url_compare_example = L"foo.org";
40+
wil::unique_bstr domain = GetDomainOfUri(uri.get());
41+
const wchar_t *domains = domain.get();
42+
if (wcscmp(url_compare_example, domains) == 0) {
43+
wil::com_ptr<ICoreWebView2Settings> settings;
44+
CHECK_FAILURE(m_webView->get_Settings(&m_settings));
45+
LPCWSTR mobile_ua =
46+
"Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) "
47+
"AppleWebKit/537.36 (KHTML, like Gecko) "
48+
"Chrome/62.0.3202.84 Mobile Safari/537.36";
49+
CHECK_FAILURE(settings->put_UserAgent(mobile_ua));
50+
LPCWSTR received_ua;
51+
CHECK_FAILURE(settings->get_UserAgent(&received_ua));
52+
EXPECT_EQ(base::Value(received_ua), base::Value(mobile_ua))
53+
}
54+
return S_OK;
55+
})
56+
.Get(),
57+
&m_navigationStartingToken);
58+
``` ##.NET and WinRT
59+
```c #private void SetUserAgent(CoreWebView2 sender,
60+
CoreWebView2UserAgentArgs e) {
61+
var settings = webView2Control.CoreWebView2.Settings;
62+
settings.UserAgent = "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F "
63+
"Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) "
64+
"Chrome/62.0.3202.84 Mobile Safari/537.36";
65+
}
66+
```
67+
68+
#API Notes
69+
70+
See [API Details](#api-details) section below for API reference.
71+
72+
#API Details
73+
74+
## Win32 C++
75+
76+
```IDL
77+
// This is the ICoreWebView2Settings Staging interface.
78+
[uuid(c79ba37e-9bd6-4b9e-b460-2ced163f231f), object, pointer_default(unique)]
79+
interface ICoreWebView2StagingSettings : IUnknown {
80+
/// `UserAgent` . Returns the User Agent. The default value is the
81+
/// default User Agent.
82+
[propget] HRESULT UserAgent([ out, retval ] LPCWSTR * userAgent);
83+
/// Sets the `UserAgentString` property. This property may be overriden if
84+
/// the User-Agent header is set in a request.
85+
[propput] HRESULT UserAgent([in] LPCWSTR userAgent);
86+
}
87+
```
88+
## .NET and WinRT
89+
90+
```c #namespace Microsoft.Web.WebView2.Core {
91+
public
92+
partial class CoreWebView2 {
93+
// There are other API in this interface that we are not showing
94+
public
95+
CoreWebView2Settings UserAgent {
96+
get;
97+
set;
98+
};
99+
}
100+
}
101+
```

0 commit comments

Comments
 (0)