Skip to content

Commit c5e2b73

Browse files
committed
Move projects to use latest WebView2 SDK 1.0.672-prerelease
1 parent dfb908e commit c5e2b73

22 files changed

+810
-31
lines changed

SampleApps/WebView2APISample/AppWindow.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include "Resource.h"
2424
#include "ScenarioAddHostObject.h"
2525
#include "ScenarioAuthentication.h"
26+
#include "ScenarioCookieManagement.h"
27+
#include "ScenarioDOMContentLoaded.h"
28+
#include "ScenarioNavigateWithWebResourceRequest.h"
2629
#include "ScenarioWebMessage.h"
2730
#include "ScenarioWebViewEventMonitor.h"
2831
#include "ScriptComponent.h"
@@ -308,9 +311,31 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
308311
std::wstring m_scriptUri = GetLocalUri(c_scriptPath);
309312
CHECK_FAILURE(m_webView->Navigate(m_scriptUri.c_str()));
310313
}
314+
case IDM_SCENARIO_AUTHENTICATION:
315+
{
316+
NewComponent<ScenarioAuthentication>(this);
317+
318+
return true;
319+
}
320+
case IDM_SCENARIO_COOKIE_MANAGEMENT:
321+
{
322+
NewComponent<ScenarioCookieManagement>(this);
323+
return true;
324+
}
325+
case IDM_SCENARIO_DOM_CONTENT_LOADED:
326+
{
327+
NewComponent<ScenarioDOMContentLoaded>(this);
328+
return true;
329+
}
330+
case IDM_SCENARIO_NAVIGATEWITHWEBRESOURCEREQUEST:
331+
{
332+
NewComponent<ScenarioNavigateWithWebResourceRequest>(this);
333+
return true;
334+
}
311335
}
312336
return false;
313337
}
338+
314339
// Handle commands not related to the WebView, which will work even if the WebView
315340
// is not currently initialized.
316341
bool AppWindow::ExecuteAppCommands(WPARAM wParam, LPARAM lParam)
@@ -730,8 +755,8 @@ void AppWindow::RegisterEventHandlers()
730755

731756
BOOL hasPosition = FALSE;
732757
BOOL hasSize = FALSE;
733-
CHECK_FAILURE(windowFeatures->HasPosition(&hasPosition));
734-
CHECK_FAILURE(windowFeatures->HasSize(&hasSize));
758+
CHECK_FAILURE(windowFeatures->get_HasPosition(&hasPosition));
759+
CHECK_FAILURE(windowFeatures->get_HasSize(&hasSize));
735760

736761
bool useDefaultWindow = true;
737762

@@ -743,7 +768,7 @@ void AppWindow::RegisterEventHandlers()
743768
CHECK_FAILURE(windowFeatures->get_Width(&width));
744769
useDefaultWindow = false;
745770
}
746-
CHECK_FAILURE(windowFeatures->get_Toolbar(&shouldHaveToolbar));
771+
CHECK_FAILURE(windowFeatures->get_ShouldDisplayToolbar(&shouldHaveToolbar));
747772

748773
windowRect.left = left;
749774
windowRect.right = left + (width < s_minNewWindowSize ? s_minNewWindowSize : width);
@@ -861,7 +886,7 @@ void AppWindow::CloseWebView(bool cleanupUserDataFolder)
861886
// developers specify userDataFolder during WebView environment
862887
// creation, they would need to pass in that explicit value here.
863888
// For more information about userDataFolder:
864-
// https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/0-9-622/webview2-idl#createcorewebview2environmentwithoptions
889+
// https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/webview2-idl#createcorewebview2environmentwithoptions
865890
WCHAR userDataFolder[MAX_PATH] = L"";
866891
// Obtain the absolute path for relative paths that include "./" or "../"
867892
_wfullpath(
@@ -978,6 +1003,12 @@ std::wstring AppWindow::GetLocalPath(std::wstring relativePath, bool keep_exe_pa
9781003
}
9791004
std::wstring AppWindow::GetLocalUri(std::wstring relativePath)
9801005
{
1006+
#if 0 // To be enabled after AddHostMappingForLocalFolder fully works.
1007+
//! [LocalUrlUsage]
1008+
const std::wstring localFileRootUrl = L"https://app-file.invalid/";
1009+
return localFileRootUrl + regex_replace(relativePath, std::wregex(L"\\"), L"/");
1010+
//! [LocalUrlUsage]
1011+
#else
9811012
std::wstring path = GetLocalPath(relativePath, false);
9821013

9831014
wil::com_ptr<IUri> uri;
@@ -986,6 +1017,7 @@ std::wstring AppWindow::GetLocalUri(std::wstring relativePath)
9861017
wil::unique_bstr uriBstr;
9871018
CHECK_FAILURE(uri->GetAbsoluteUri(&uriBstr));
9881019
return std::wstring(uriBstr.get());
1020+
#endif
9891021
}
9901022

9911023
void AppWindow::RunAsync(std::function<void()> callback)

SampleApps/WebView2APISample/AppWindow.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class AppWindow
4545
{
4646
return m_webView.get();
4747
}
48+
ICoreWebView2Environment* GetWebViewEnvironment()
49+
{
50+
return m_webViewEnvironment.get();
51+
}
4852
HWND GetMainWindow()
4953
{
5054
return m_mainWindow;

SampleApps/WebView2APISample/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ The section below briefly explains some of the key functions in the Sample App.
113113

114114
#### InitializeWebView()
115115

116-
In the AppWindow file, we use the InitializeWebView() function to create the WebView2 environment by using [CreateCoreWebView2EnvironmentWithOptions](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/0-9-622/webview2-idl#createcorewebview2environmentwithoptions).
116+
In the AppWindow file, we use the InitializeWebView() function to create the WebView2 environment by using [CreateCoreWebView2EnvironmentWithOptions](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/webview2-idl#createcorewebview2environmentwithoptions).
117117

118118
Once we've created the environment, we create the WebView by using `CreateCoreWebView2Controller`.
119119

@@ -171,7 +171,7 @@ This callback function is passed to `CreateCoreWebView2Controller` in `Initializ
171171
172172
This function is called within `CreateCoreWebView2Controller`. It sets up some of the event handlers used by the application, and adds them to the WebView.
173173
174-
To read more about event handlers in WebView2, you can refer to this [documentation](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/0-9-622/icorewebview2).
174+
To read more about event handlers in WebView2, you can refer to this [documentation](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/icorewebview2).
175175
176176
Below is a code snippet from `RegisterEventHandlers()`, where we set up an event handler for the `NewWindowRequested` event. This event is fired when JavaScript in the webpage calls `window.open()`, and our handler makes a new `AppWindow` and passes the new window's WebView back to the browser so it can return it from the `window.open()` call. Unlike our calls to `CreateCoreWebView2EnvironmentWithOptions` and `CreateCoreWebView2Controller`, instead of providing a method for the callback, we just provide a C++ lambda right then and there.
177177
@@ -231,7 +231,7 @@ The text under Posting Messages should now be blue.
231231

232232
Here's how it works:
233233

234-
1. In `ScriptComponent.cpp`, we use [PostWebMessageAsJson](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/0-9-622/icorewebview2#postwebmessageasjson) to post user input to the `ScenarioMessage.html` web application.
234+
1. In `ScriptComponent.cpp`, we use [PostWebMessageAsJson](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/icorewebview2#postwebmessageasjson) to post user input to the `ScenarioMessage.html` web application.
235235

236236
```cpp
237237
// Prompt the user for some JSON and then post it as a web message.
@@ -281,7 +281,7 @@ function SetTitleText() {
281281
}
282282
```
283283

284-
2. Within `ScenarioWebMessage.cpp`, we use [add_WebMessageReceived](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/0-9-622/icorewebview2#add_webmessagereceived) to register the event handler. When we receive the event, after validating the input, we change the title of the App Window.
284+
2. Within `ScenarioWebMessage.cpp`, we use [add_WebMessageReceived](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/icorewebview2#add_webmessagereceived) to register the event handler. When we receive the event, after validating the input, we change the title of the App Window.
285285

286286
```cpp
287287
// Setup the web message received event handler before navigating to
@@ -328,7 +328,7 @@ function GetWindowBounds() {
328328
}
329329
```
330330

331-
2. Within `ScenarioWebMessage.cpp`, we use [add_WebMessageReceived](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/0-9-622/icorewebview2#add_webmessagereceived) to register the received event handler. After validating the input, the event handler gets window bounds from the App Window. [PostWebMessageAsJson](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/0-9-622/icorewebview2#postwebmessageasjson) sends the bounds to the web application.
331+
2. Within `ScenarioWebMessage.cpp`, we use [add_WebMessageReceived](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/icorewebview2#add_webmessagereceived) to register the received event handler. After validating the input, the event handler gets window bounds from the App Window. [PostWebMessageAsJson](https://docs.microsoft.com/microsoft-edge/webview2/reference/win32/icorewebview2#postwebmessageasjson) sends the bounds to the web application.
332332

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

0 commit comments

Comments
 (0)