Skip to content

Commit 68f2e34

Browse files
author
Maura Winstanley
committed
update doc to standardize capitilization, add details, change api reference, update sample code
1 parent b893902 commit 68f2e34

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

specs/APIReview_UAString.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
# Background
22

33
Currently, a developer can pass the --user-agent browser args to the CreateWebView2EnvironmentWithDetails function.
4-
Ex. CreateWebView2EnvironmentWithDetails(nullptr, nullptr, L"--user-agent=\"myUA\"", ...);
5-
For more info about the ‘user - agent’ flag visit: https://peter.sh/experiments/chromium-command-line-switches/#user-agent.
4+
Ex. CreateWebView2EnvironmentWithDetails(nullptr, nullptr, L"--user-agent=\\"myUA\\"", ...);
5+
For more info about the ‘--user-agent’ flag visit: https://peter.sh/experiments/chromium-command-line-switches/#user-agent.
66

7-
However, there are a couple limitations to this workaround-- you cannot modify a command line switch at runtime, nor can you change the user agent per webview. In this document we describe the new API. We'd appreciate your feedback.
7+
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.
88

99
# Description
1010

11-
The User Agent (UA) is a client-side piece of information that the browser/webcontrol sends to the server or website a user visits. It contains information about user’s system and is modifiable by the user.
11+
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 and can be modified by the user.
1212

13-
The User Agent String API allows developers to modify WebView's user agent string via Chrome Developer Protocol (CDP). The user agent can be changed based on different events such as navigation to a specific website.
13+
The User Agent API allows developers to modify WebView2's User Agent and can be changed based on different events.
1414

15-
The Settings component will change the UA per WebView2 via Chrome Developer Protocol command (CDP). A key scenario is to allow end developers to get the current user agent from the webview and modify it based on some sort of event, such as navigating to a specific website and setting the user agent to emulate a different browser version.
15+
The Settings component will change the User Agent per WebView via Chrome Developer Protocol command (CDP). A key scenario is to allow end developers to get the current User Agent from WebView and modify it based on some sort of event, such as navigating to a specific website and setting the user agent to emulate a different browser version.
1616

1717
# Examples
1818

19-
The following code snippet demonstrates how the environment APIs can be used
20-
:
19+
The following code snippet demonstrates how the User Agent API can be used:
2120

2221
## Win32 C++
2322

@@ -29,19 +28,23 @@ m_webView->add_NavigationStarting(
2928
static const PCWSTR url_compare_example = L"foo.org";
3029
wil::unique_bstr domain = GetDomainOfUri(uri.get());
3130
const wchar_t *domains = domain.get();
31+
wil::com_ptr<ICoreWebView2Settings> settings;
32+
CHECK_FAILURE(m_webView->get_Settings(&m_settings));
3233
if (wcscmp(url_compare_example, domains) == 0) {
3334
// Upon navigation to a specified url
34-
// Change the user agent to emulate a mobile device
35-
wil::com_ptr<ICoreWebView2Settings> settings;
36-
CHECK_FAILURE(m_webView->get_Settings(&m_settings));
35+
// Change the user agent to emulate a mobile device
3736
LPCWSTR mobile_ua =
3837
L"Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) "
3938
"AppleWebKit/537.36 (KHTML, like Gecko) "
4039
"Chrome/62.0.3202.84 Mobile Safari/537.36";
41-
CHECK_FAILURE(settings->put_UserAgent(mobile_ua));
42-
LPWSTR received_ua;
43-
CHECK_FAILURE(settings->get_UserAgent(&received_ua));
44-
EXPECT_STREQ(received_ua, mobile_ua);
40+
CHECK_FAILURE(settings->put_UserAgent(mobile_ua));
41+
} else {
42+
// Change the user agent back to desktop
43+
LPCWSTR desktop_ua =
44+
L"Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) "
45+
"AppleWebKit/537.36 (KHTML, like Gecko) "
46+
"Chrome/62.0.3202.84 Safari/537.36";
47+
CHECK_FAILURE(settings->put_UserAgent(desktop_ua));
4548
}
4649
return S_OK;
4750
})
@@ -53,8 +56,9 @@ m_webView->add_NavigationStarting(
5356
5457
```c #
5558
private void SetUserAgent(CoreWebView2 sender, CoreWebView2UserAgentArgs e) {
59+
static const std::string updatedUA;
5660
var settings = webView2Control.CoreWebView2.Settings;
57-
settings.UserAgent = "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36";
61+
settings.UserAgent = updatedUA;
5862
}
5963
```
6064

@@ -71,22 +75,22 @@ See [API Details](#api-details) section below for API reference.
7175
[uuid(c79ba37e-9bd6-4b9e-b460-2ced163f231f), object, pointer_default(unique)]
7276
interface ICoreWebView2StagingSettings : IUnknown {
7377
/// `UserAgent` . Returns the User Agent. The default value is the
74-
/// default User Agent.
78+
/// default User Agent of the Edge browser.
7579
[propget] HRESULT UserAgent([ out, retval ] LPWSTR * userAgent);
7680
/// Sets the `UserAgentString` property. This property may be overriden if
77-
/// the User-Agent header is set in a request.
81+
/// the User-Agent header is set in a request. If the parameter is empty
82+
/// the User Agent will not be updated and the current User Agent will remain.
7883
[propput] HRESULT UserAgent([in] LPCWSTR userAgent);
7984
}
8085
```
8186
## .NET and WinRT
8287

8388
```c#
84-
namespace Microsoft.Web.WebView2.Core {
85-
public
86-
partial class CoreWebView2 {
87-
// There are other API in this interface that we are not showing
88-
public
89-
CoreWebView2Settings UserAgent {get ; set; };
89+
namespace Microsoft.Web.WebView2.Core
90+
{
91+
public partial class CoreWebView2Settings
92+
{
93+
public string UserAgent { get; set; };
9094
}
9195
}
9296
```

0 commit comments

Comments
 (0)