forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScenarioCookieManagement.cpp
More file actions
228 lines (204 loc) · 8.07 KB
/
ScenarioCookieManagement.cpp
File metadata and controls
228 lines (204 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include "ScenarioCookieManagement.h"
#include "AppWindow.h"
#include "CheckFailure.h"
#include <ctime>
#include <regex>
using namespace Microsoft::WRL;
static constexpr WCHAR c_samplePath[] = L"ScenarioCookieManagement.html";
ScenarioCookieManagement::ScenarioCookieManagement(AppWindow* appWindow)
: m_appWindow(appWindow), m_webView(appWindow->GetWebView())
{
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
ComPtr<ICoreWebView2Settings> settings;
CHECK_FAILURE(m_webView->get_Settings(&settings));
CHECK_FAILURE(settings->put_IsWebMessageEnabled(TRUE));
//! [CookieManager]
wil::com_ptr<ICoreWebView2_2> m_webview2;
CHECK_FAILURE(m_appWindow->GetWebView()->QueryInterface(IID_PPV_ARGS(&m_webview2)));
CHECK_FAILURE(m_webview2->get_CookieManager(&m_cookieManager));
//! [CookieManager]
// Setup the web message received event handler before navigating to
// ensure we don't miss any messages.
CHECK_FAILURE(m_webView->add_WebMessageReceived(
Microsoft::WRL::Callback<ICoreWebView2WebMessageReceivedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args) {
wil::unique_cotaskmem_string uri;
CHECK_FAILURE(args->get_Source(&uri));
// Always validate that the origin of the message is what you expect.
if (uri.get() != m_sampleUri)
{
return S_OK;
}
wil::unique_cotaskmem_string messageRaw;
CHECK_FAILURE(args->TryGetWebMessageAsString(&messageRaw));
std::wstring message = messageRaw.get();
std::wstring reply;
if (message.compare(0, 11, L"GetCookies ") == 0)
{
std::wstring uri;
if (message.length() != 11)
{
uri = message.substr(11);
}
GetCookiesHelper(uri.c_str());
}
else if (message.compare(0, 17, L"AddOrUpdateCookie") == 0)
{
//! [AddOrUpdateCookie]
wil::com_ptr<ICoreWebView2Cookie> cookie;
CHECK_FAILURE(m_cookieManager->CreateCookie(
L"CookieName", L"CookieValue", L".bing.com", L"/", &cookie));
CHECK_FAILURE(m_cookieManager->AddOrUpdateCookie(cookie.get()));
//! [AddOrUpdateCookie]
}
else if (message.compare(0, 16, L"DeleteAllCookies") == 0)
{
CHECK_FAILURE(m_cookieManager->DeleteAllCookies());
}
return S_OK;
})
.Get(),
&m_webMessageReceivedToken));
// Turn off this scenario if we navigate away from the sample page
CHECK_FAILURE(m_webView->add_ContentLoading(
Callback<ICoreWebView2ContentLoadingEventHandler>(
[this](
ICoreWebView2* sender, ICoreWebView2ContentLoadingEventArgs* args) -> HRESULT {
wil::unique_cotaskmem_string uri;
sender->get_Source(&uri);
if (uri.get() != m_sampleUri)
{
m_appWindow->DeleteComponent(this);
}
return S_OK;
})
.Get(),
&m_contentLoadingToken));
CHECK_FAILURE(m_webView->Navigate(m_sampleUri.c_str()));
}
ScenarioCookieManagement::~ScenarioCookieManagement()
{
m_webView->remove_WebMessageReceived(m_webMessageReceivedToken);
m_webView->remove_ContentLoading(m_contentLoadingToken);
}
static std::wstring BoolToString(BOOL value)
{
return value ? L"true" : L"false";
}
static std::wstring EncodeQuote(std::wstring raw)
{
return L"\"" + regex_replace(raw, std::wregex(L"\""), L"\\\"") + L"\"";
}
static std::wstring SecondsToString(UINT32 time)
{
WCHAR rawResult[26];
time_t rawTime;
rawTime = (const time_t)time;
struct tm timeStruct;
gmtime_s(&timeStruct, &rawTime);
_wasctime_s(rawResult, 26, &timeStruct);
std::wstring result(rawResult);
return result;
}
static std::wstring CookieToString(ICoreWebView2Cookie* cookie)
{
//! [CookieObject]
wil::unique_cotaskmem_string name;
CHECK_FAILURE(cookie->get_Name(&name));
wil::unique_cotaskmem_string value;
CHECK_FAILURE(cookie->get_Value(&value));
wil::unique_cotaskmem_string domain;
CHECK_FAILURE(cookie->get_Domain(&domain));
wil::unique_cotaskmem_string path;
CHECK_FAILURE(cookie->get_Path(&path));
double expires;
CHECK_FAILURE(cookie->get_Expires(&expires));
BOOL isHttpOnly = FALSE;
CHECK_FAILURE(cookie->get_IsHttpOnly(&isHttpOnly));
COREWEBVIEW2_COOKIE_SAME_SITE_KIND same_site;
std::wstring same_site_as_string;
CHECK_FAILURE(cookie->get_SameSite(&same_site));
switch (same_site)
{
case COREWEBVIEW2_COOKIE_SAME_SITE_KIND_NONE:
same_site_as_string = L"None";
break;
case COREWEBVIEW2_COOKIE_SAME_SITE_KIND_LAX:
same_site_as_string = L"Lax";
break;
case COREWEBVIEW2_COOKIE_SAME_SITE_KIND_STRICT:
same_site_as_string = L"Strict";
break;
}
BOOL isSecure = FALSE;
CHECK_FAILURE(cookie->get_IsSecure(&isSecure));
BOOL isSession = FALSE;
CHECK_FAILURE(cookie->get_IsSession(&isSession));
std::wstring result = L"{";
result += L"\"Name\": " + EncodeQuote(name.get()) + L", " + L"\"Value\": " +
EncodeQuote(value.get()) + L", " + L"\"Domain\": " + EncodeQuote(domain.get()) +
L", " + L"\"Path\": " + EncodeQuote(path.get()) + L", " + L"\"HttpOnly\": " +
BoolToString(isHttpOnly) + L", " + L"\"Secure\": " + BoolToString(isSecure) + L", " +
L"\"SameSite\": " + EncodeQuote(same_site_as_string) + L", " + L"\"Expires\": ";
if (!!isSession)
{
result += L"This is a session cookie.";
}
else
{
result += std::to_wstring(expires);
}
return result + L"\"}";
//! [CookieObject]
}
void ScenarioCookieManagement::GetCookiesHelper(std::wstring uri)
{
//! [GetCookies]
if (m_cookieManager)
{
CHECK_FAILURE(m_cookieManager->GetCookies(
uri.c_str(),
Callback<ICoreWebView2GetCookiesCompletedHandler>(
[this, uri](HRESULT error_code, ICoreWebView2CookieList* list) -> HRESULT {
CHECK_FAILURE(error_code);
std::wstring result;
UINT cookie_list_size;
CHECK_FAILURE(list->get_Count(&cookie_list_size));
if (cookie_list_size == 0)
{
result += L"No cookies found.";
}
else
{
result += std::to_wstring(cookie_list_size) + L" cookie(s) found";
if (!uri.empty())
{
result += L" on " + uri;
}
result += L"\n\n[";
for (int i = 0; i < cookie_list_size; ++i)
{
wil::com_ptr<ICoreWebView2Cookie> cookie;
CHECK_FAILURE(list->GetValueAtIndex(i, &cookie));
if (cookie.get())
{
result += CookieToString(cookie.get());
if (i != cookie_list_size - 1)
{
result += L",\n";
}
}
}
result += L"]";
}
MessageBox(nullptr, result.c_str(), L"GetCookies Result", MB_OK);
return S_OK;
})
.Get()));
}
//! [GetCookies]
}