Skip to content

Commit 183097b

Browse files
committed
Added COM sample code
1 parent e87845a commit 183097b

1 file changed

Lines changed: 163 additions & 0 deletions

File tree

specs/CookieManagement.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,169 @@ One can create a `CookieManager` off a WebView to `GetCookies` via a `CookieList
1010

1111
The following code snippet demonstrates how the cookie management APIs can be use:
1212

13+
## Win32 C++
14+
15+
```cpp
16+
ScenarioCookieManagement::ScenarioCookieManagement(AppWindow* appWindow)
17+
: m_appWindow(appWindow), m_webView(appWindow->GetWebView())
18+
{
19+
//! [CreateCookieManager]
20+
CHECK_FAILURE(m_webView->CreateCookieManager(&m_cookieManager));
21+
//! [CreateCookieManager]
22+
23+
CHECK_FAILURE(m_webView->add_WebMessageReceived(
24+
Microsoft::WRL::Callback<ICoreWebView2WebMessageReceivedEventHandler>(
25+
[this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args) {
26+
wil::unique_cotaskmem_string uri;
27+
CHECK_FAILURE(args->get_Source(&uri));
28+
29+
// Always validate that the origin of the message is what you expect.
30+
if (uri.get() != m_sampleUri)
31+
{
32+
return S_OK;
33+
}
34+
wil::unique_cotaskmem_string messageRaw;
35+
CHECK_FAILURE(args->TryGetWebMessageAsString(&messageRaw));
36+
std::wstring message = messageRaw.get();
37+
std::wstring reply;
38+
39+
if (message.compare(0, 11, L"GetCookies ") == 0)
40+
{
41+
GetCookiesHelper(message.substr(11));
42+
reply =
43+
L"{\"CookiesGot\":\"" + GetCookiesHelper(message.substr(11)) + L"\"}";
44+
CHECK_FAILURE(sender->PostWebMessageAsJson(reply.c_str()));
45+
}
46+
else if (message.compare(0, 10, L"SetCookie ") == 0)
47+
{
48+
message = message.substr(10);
49+
std::wstring name = message.substr(0, message.find(' '));
50+
std::wstring value = message.substr(message.find(' ') + 1);
51+
52+
//! [SetCookie]
53+
wil::com_ptr<ICoreWebView2Cookie> cookie;
54+
CHECK_FAILURE(m_cookieManager->CreateCookie(
55+
name.c_str(), value.c_str(), L".bing.com", L"/", &cookie));
56+
CHECK_FAILURE(m_cookieManager->SetCookie(cookie.get()));
57+
reply = L"{\"CookieSet\":\"Cookie set successfully.\"}";
58+
CHECK_FAILURE(sender->PostWebMessageAsJson(reply.c_str()));
59+
//! [SetCookie]
60+
}
61+
else if (message.compare(0, 16, L"ClearAllCookies ") == 0)
62+
{
63+
CHECK_FAILURE(m_cookieManager->ClearAllCookies());
64+
reply = L"{\"CookiesCleared\":\"Cookies cleared.\"}";
65+
CHECK_FAILURE(sender->PostWebMessageAsJson(reply.c_str()));
66+
}
67+
return S_OK;
68+
})
69+
.Get(),
70+
&m_webMessageReceivedToken));
71+
}
72+
73+
static std::wstring CookieToJsonString(ICoreWebView2Cookie* cookie)
74+
{
75+
//! [CookieObject]
76+
wil::unique_cotaskmem_string name;
77+
CHECK_FAILURE(cookie->get_Name(&name));
78+
wil::unique_cotaskmem_string value;
79+
CHECK_FAILURE(cookie->get_Value(&value));
80+
wil::unique_cotaskmem_string domain;
81+
CHECK_FAILURE(cookie->get_Domain(&domain));
82+
wil::unique_cotaskmem_string path;
83+
CHECK_FAILURE(cookie->get_Path(&path));
84+
double expires;
85+
CHECK_FAILURE(cookie->get_Expires(&expires));
86+
BOOL httpOnly;
87+
CHECK_FAILURE(cookie->get_HttpOnly(&httpOnly));
88+
COREWEBVIEW2_COOKIE_SAME_SITE_KIND same_site;
89+
std::wstring same_site_as_string;
90+
CHECK_FAILURE(cookie->get_SameSite(&same_site));
91+
switch (same_site)
92+
{
93+
case COREWEBVIEW2_COOKIE_SAME_SITE_KIND_NONE:
94+
same_site_as_string = L"None";
95+
break;
96+
case COREWEBVIEW2_COOKIE_SAME_SITE_KIND_LAX:
97+
same_site_as_string = L"Lax";
98+
break;
99+
case COREWEBVIEW2_COOKIE_SAME_SITE_KIND_STRICT:
100+
same_site_as_string = L"Strict";
101+
break;
102+
}
103+
BOOL secure;
104+
CHECK_FAILURE(cookie->get_Secure(&secure));
105+
106+
std::wstring result = L"{";
107+
result += L"\"Name\": " + EncodeQuote(name.get()) + L", " + L"\"Value\": " +
108+
EncodeQuote(value.get()) + L", " + L"\"Domain\": " + EncodeQuote(domain.get()) +
109+
L", " + L"\"Path\": " + EncodeQuote(path.get()) + L", " + L"\"HttpOnly\": " +
110+
BoolToString(httpOnly) + L", " + L"\"Secure\": " + BoolToString(secure) + L", " +
111+
L"\"SameSite\": " + EncodeQuote(same_site_as_string) + L", " + L"\"Expires\": ";
112+
if (expires == -1)
113+
{
114+
result += L"This is a session cookie.";
115+
}
116+
else
117+
{
118+
result += std::to_wstring(expires);
119+
}
120+
121+
return result + L"\"}";
122+
//! [CookieObject]
123+
}
124+
125+
void ScenarioCookieManagement::GetCookiesHelper(std::wstring uri)
126+
{
127+
//! [GetCookies]
128+
if (m_cookieManager)
129+
{
130+
CHECK_FAILURE(m_cookieManager->GetCookies(
131+
uri.c_str(),
132+
Callback<ICoreWebView2GetCookiesCompletedHandler>(
133+
[this, uri](HRESULT error_code, ICoreWebView2CookieList* list) -> HRESULT {
134+
CHECK_FAILURE(error_code);
135+
136+
std::wstring result;
137+
UINT cookie_list_size;
138+
CHECK_FAILURE(list->get_Size(&cookie_list_size));
139+
140+
if (cookie_list_size == 0)
141+
{
142+
result += L"No cookies found.";
143+
}
144+
else
145+
{
146+
result += std::to_wstring(cookie_list_size) + L" cookie(s) found on " +
147+
uri + L".";
148+
result += L"\n\n[";
149+
for (int i = 0; i < cookie_list_size; ++i)
150+
{
151+
wil::com_ptr<ICoreWebView2Cookie> cookie;
152+
CHECK_FAILURE(list->GetValueAtIndex(i, &cookie));
153+
154+
if (cookie.get())
155+
{
156+
result += CookieToJsonString(cookie.get());
157+
if (i != cookie_list_size - 1)
158+
{
159+
result += L",\n";
160+
}
161+
}
162+
}
163+
result += L"]";
164+
}
165+
MessageBox(nullptr, result.c_str(), L"GetCookies Result", MB_OK);
166+
return S_OK;
167+
})
168+
.Get()));
169+
}
170+
//! [GetCookies]
171+
}
172+
```
173+
174+
## .NET and WinRT
175+
13176
```c#
14177
CoreWebView2CookieManager _cookieManager;
15178

0 commit comments

Comments
 (0)