Skip to content

Commit aba0458

Browse files
author
Ognjen Sobajic
committed
Update projects to use the latest WebView2 SDK 1.0.824-prerelease
1 parent 6cc8b6b commit aba0458

File tree

17 files changed

+261
-125
lines changed

17 files changed

+261
-125
lines changed

SampleApps/WebView2APISample/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ packages/
2828

2929
# Make sure script debug config is checked in for vendor testing
3030
!.vscode
31+
32+
# Nuget.exe is downloaded by UseNuget.ps1
33+
nuget.exe

SampleApps/WebView2APISample/AppStartPage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ std::wstring GetRuntimePath(AppWindow* appWindow)
100100

101101
std::wstring GetUri(AppWindow* appWindow)
102102
{
103-
std::wstring uri = appWindow->GetLocalUri(L"AppStartPage.html");
103+
std::wstring uri = appWindow->GetLocalUri(L"AppStartPage.html", true);
104104

105105
uri += L"?sdkBuild=";
106106
uri += GetSdkBuild();

SampleApps/WebView2APISample/AppWindow.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,30 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
398398
case IDM_SCENARIO_JAVA_SCRIPT:
399399
{
400400
WCHAR c_scriptPath[] = L"ScenarioJavaScriptDebugIndex.html";
401-
std::wstring m_scriptUri = GetLocalUri(c_scriptPath);
401+
std::wstring m_scriptUri = GetLocalUri(c_scriptPath, false);
402402
CHECK_FAILURE(m_webView->Navigate(m_scriptUri.c_str()));
403403
return true;
404404
}
405405
case IDM_SCENARIO_TYPE_SCRIPT:
406406
{
407407
WCHAR c_scriptPath[] = L"ScenarioTypeScriptDebugIndex.html";
408-
std::wstring m_scriptUri = GetLocalUri(c_scriptPath);
408+
std::wstring m_scriptUri = GetLocalUri(c_scriptPath, false);
409409
CHECK_FAILURE(m_webView->Navigate(m_scriptUri.c_str()));
410+
return true;
411+
}
412+
case IDM_SCENARIO_JAVA_SCRIPT_VIRTUAL:
413+
{
414+
WCHAR c_scriptPath[] = L"ScenarioJavaScriptDebugIndex.html";
415+
std::wstring m_scriptUri = GetLocalUri(c_scriptPath, true);
416+
CHECK_FAILURE(m_webView->Navigate(m_scriptUri.c_str()));
417+
return true;
418+
}
419+
case IDM_SCENARIO_TYPE_SCRIPT_VIRTUAL:
420+
{
421+
WCHAR c_scriptPath[] = L"ScenarioTypeScriptDebugIndex.html";
422+
std::wstring m_scriptUri = GetLocalUri(c_scriptPath, true);
423+
CHECK_FAILURE(m_webView->Navigate(m_scriptUri.c_str()));
424+
return true;
410425
}
411426
case IDM_SCENARIO_AUTHENTICATION:
412427
{
@@ -429,6 +444,13 @@ bool AppWindow::ExecuteWebViewCommands(WPARAM wParam, LPARAM lParam)
429444
NewComponent<ScenarioNavigateWithWebResourceRequest>(this);
430445
return true;
431446
}
447+
case IDM_SCENARIO_TESTING_FOCUS:
448+
{
449+
WCHAR testingFocusPath[] = L"ScenarioTestingFocus.html";
450+
std::wstring testingFocusUri = GetLocalUri(testingFocusPath, false);
451+
CHECK_FAILURE(m_webView->Navigate(testingFocusUri.c_str()));
452+
return true;
453+
}
432454
}
433455
return false;
434456
}
@@ -996,6 +1018,7 @@ void AppWindow::CloseWebView(bool cleanupUserDataFolder)
9961018
m_controller->Close();
9971019
m_controller = nullptr;
9981020
m_webView = nullptr;
1021+
m_webView3 = nullptr;
9991022
}
10001023
m_webViewEnvironment = nullptr;
10011024
if (cleanupUserDataFolder)
@@ -1120,9 +1143,11 @@ std::wstring AppWindow::GetLocalPath(std::wstring relativePath, bool keep_exe_pa
11201143
}
11211144
return path;
11221145
}
1123-
std::wstring AppWindow::GetLocalUri(std::wstring relativePath)
1146+
1147+
std::wstring AppWindow::GetLocalUri(
1148+
std::wstring relativePath, bool useVirtualHostName /*= true*/)
11241149
{
1125-
if (m_webView3)
1150+
if (useVirtualHostName && m_webView3)
11261151
{
11271152
//! [LocalUrlUsage]
11281153
const std::wstring localFileRootUrl = L"https://appassets.example/";

SampleApps/WebView2APISample/AppWindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class AppWindow
5555
}
5656
void SetTitleText(PCWSTR titleText);
5757
RECT GetWindowBounds();
58-
std::wstring GetLocalUri(std::wstring path);
58+
std::wstring GetLocalUri(std::wstring path, bool useVirtualHostName = true);
5959
std::function<void()> GetAcceleratorKeyFunction(UINT key);
6060
double GetDpiScale();
6161
double GetTextScale();

SampleApps/WebView2APISample/ProcessComponent.cpp

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "ProcessComponent.h"
88

9+
#include <sstream>
10+
911
#include "CheckFailure.h"
1012

1113
using namespace Microsoft::WRL;
@@ -15,54 +17,56 @@ ProcessComponent::ProcessComponent(AppWindow* appWindow)
1517
{
1618
//! [ProcessFailed]
1719
// Register a handler for the ProcessFailed event.
18-
// This handler checks if the browser process failed, and asks the user if
19-
// they want to recreate the webview.
20+
// This handler checks the failure kind and tries to:
21+
// * Recreate the webview for browser failure and render unresponsive.
22+
// * Reload the webview for render failure.
2023
CHECK_FAILURE(m_webView->add_ProcessFailed(
2124
Callback<ICoreWebView2ProcessFailedEventHandler>(
22-
[this](ICoreWebView2* sender,
23-
ICoreWebView2ProcessFailedEventArgs* args) -> HRESULT
24-
{
25-
COREWEBVIEW2_PROCESS_FAILED_KIND failureType;
26-
CHECK_FAILURE(args->get_ProcessFailedKind(&failureType));
27-
if (failureType == COREWEBVIEW2_PROCESS_FAILED_KIND_BROWSER_PROCESS_EXITED)
28-
{
29-
int button = MessageBox(
30-
m_appWindow->GetMainWindow(),
31-
L"Browser process exited unexpectedly. Recreate webview?",
32-
L"Browser process exited",
33-
MB_YESNO);
34-
if (button == IDYES)
35-
{
36-
m_appWindow->ReinitializeWebView();
37-
}
38-
}
39-
else if (failureType == COREWEBVIEW2_PROCESS_FAILED_KIND_RENDER_PROCESS_UNRESPONSIVE)
40-
{
41-
int button = MessageBox(
42-
m_appWindow->GetMainWindow(),
43-
L"Browser render process has stopped responding. Recreate webview?",
44-
L"Web page unresponsive", MB_YESNO);
45-
if (button == IDYES)
46-
{
47-
m_appWindow->ReinitializeWebView();
48-
}
49-
}
50-
else if (failureType == COREWEBVIEW2_PROCESS_FAILED_KIND_RENDER_PROCESS_EXITED)
51-
{
52-
int button = MessageBox(
53-
m_appWindow->GetMainWindow(),
54-
L"Browser render process exited unexpectedly. Reload page?",
55-
L"Web page unresponsive", MB_YESNO);
56-
if (button == IDYES)
57-
{
58-
CHECK_FAILURE(m_webView->Reload());
59-
}
60-
}
61-
return S_OK;
62-
}).Get(), &m_processFailedToken));
25+
[this](ICoreWebView2* sender, ICoreWebView2ProcessFailedEventArgs* argsRaw)
26+
-> HRESULT {
27+
wil::com_ptr<ICoreWebView2ProcessFailedEventArgs> args = argsRaw;
28+
COREWEBVIEW2_PROCESS_FAILED_KIND failureKind;
29+
CHECK_FAILURE(args->get_ProcessFailedKind(&failureKind));
30+
if (failureKind == COREWEBVIEW2_PROCESS_FAILED_KIND_BROWSER_PROCESS_EXITED)
31+
{
32+
int button = MessageBox(
33+
m_appWindow->GetMainWindow(),
34+
L"Browser process exited unexpectedly. Recreate webview?",
35+
L"Browser process exited", MB_YESNO);
36+
if (button == IDYES)
37+
{
38+
m_appWindow->ReinitializeWebView();
39+
}
40+
}
41+
else if (
42+
failureKind == COREWEBVIEW2_PROCESS_FAILED_KIND_RENDER_PROCESS_UNRESPONSIVE)
43+
{
44+
int button = MessageBox(
45+
m_appWindow->GetMainWindow(),
46+
L"Browser render process has stopped responding. Recreate webview?",
47+
L"Web page unresponsive", MB_YESNO);
48+
if (button == IDYES)
49+
{
50+
m_appWindow->ReinitializeWebView();
51+
}
52+
}
53+
else if (failureKind == COREWEBVIEW2_PROCESS_FAILED_KIND_RENDER_PROCESS_EXITED)
54+
{
55+
int button = MessageBox(
56+
m_appWindow->GetMainWindow(),
57+
L"Browser render process exited unexpectedly. Reload page?",
58+
L"Web page unresponsive", MB_YESNO);
59+
if (button == IDYES)
60+
{
61+
CHECK_FAILURE(m_webView->Reload());
62+
}
63+
}
64+
return S_OK;
65+
})
66+
.Get(),
67+
&m_processFailedToken));
6368
//! [ProcessFailed]
6469
}
65-
6670
bool ProcessComponent::HandleWindowMessage(
6771
HWND hWnd,
6872
UINT message,
@@ -97,7 +101,6 @@ void ProcessComponent::ShowBrowserProcessInfo() {
97101
StringCchPrintf(buffer, ARRAYSIZE(buffer), L"Process ID: %u\n", processId);
98102
MessageBox(m_appWindow->GetMainWindow(), buffer, L"Process Info", MB_OK);
99103
}
100-
101104
// Crash the browser's process on command, to test crash handlers.
102105
void ProcessComponent::CrashBrowserProcess()
103106
{

SampleApps/WebView2APISample/ProcessComponent.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class ProcessComponent : public ComponentBase
2626
void ShowBrowserProcessInfo();
2727
void CrashBrowserProcess();
2828
void CrashRenderProcess();
29-
3029
~ProcessComponent() override;
3130

3231
// Wait for process to exit for timeoutMs, then force quit it if it hasn't.
@@ -39,4 +38,3 @@ class ProcessComponent : public ComponentBase
3938
UINT m_browserProcessId = 0;
4039
EventRegistrationToken m_processFailedToken = {};
4140
};
42-

SampleApps/WebView2APISample/SettingsComponent.cpp

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ SettingsComponent::SettingsComponent(
2323
{
2424
CHECK_FAILURE(m_webView->get_Settings(&m_settings));
2525

26+
m_settings2 = m_settings.try_query<ICoreWebView2Settings2>();
2627
// Copy old settings if desired
2728
if (old)
2829
{
@@ -37,16 +38,11 @@ SettingsComponent::SettingsComponent(
3738
CHECK_FAILURE(m_settings->put_IsStatusBarEnabled(setting));
3839
CHECK_FAILURE(old->m_settings->get_AreDevToolsEnabled(&setting));
3940
CHECK_FAILURE(m_settings->put_AreDevToolsEnabled(setting));
40-
wil::com_ptr<ICoreWebView2ExperimentalSettings> experimental_settings_old;
41-
experimental_settings_old =
42-
old->m_settings.try_query<ICoreWebView2ExperimentalSettings>();
43-
wil::com_ptr<ICoreWebView2ExperimentalSettings> experimental_settings_new;
44-
experimental_settings_new = m_settings.try_query<ICoreWebView2ExperimentalSettings>();
45-
if (experimental_settings_old && experimental_settings_new)
41+
if (old->m_settings2 && m_settings2)
4642
{
4743
LPWSTR user_agent;
48-
CHECK_FAILURE(experimental_settings_old->get_UserAgent(&user_agent));
49-
CHECK_FAILURE(experimental_settings_new->put_UserAgent(user_agent));
44+
CHECK_FAILURE(old->m_settings2->get_UserAgent(&user_agent));
45+
CHECK_FAILURE(m_settings2->put_UserAgent(user_agent));
5046
}
5147
SetBlockImages(old->m_blockImages);
5248
SetReplaceImages(old->m_replaceImages);
@@ -55,7 +51,6 @@ SettingsComponent::SettingsComponent(
5551
m_blockedSitesSet = old->m_blockedSitesSet;
5652
m_blockedSites = std::move(old->m_blockedSites);
5753
}
58-
5954
//! [NavigationStarting]
6055
// Register a handler for the NavigationStarting event.
6156
// This handler will check the domain being navigated to, and if the domain
@@ -98,16 +93,18 @@ SettingsComponent::SettingsComponent(
9893
}
9994
//! [IsScriptEnabled]
10095
//! [UserAgent]
101-
static const PCWSTR url_compare_example = L"fourthcoffee.com";
102-
wil::unique_bstr domain = GetDomainOfUri(uri.get());
103-
const wchar_t* domains = domain.get();
104-
105-
if (wcscmp(url_compare_example, domains) == 0)
96+
if (m_settings2)
10697
{
107-
SetUserAgent(L"example_navigation_ua");
98+
static const PCWSTR url_compare_example = L"fourthcoffee.com";
99+
wil::unique_bstr domain = GetDomainOfUri(uri.get());
100+
const wchar_t* domains = domain.get();
101+
102+
if (wcscmp(url_compare_example, domains) == 0)
103+
{
104+
SetUserAgent(L"example_navigation_ua");
105+
}
108106
}
109107
//! [UserAgent]
110-
111108
return S_OK;
112109
})
113110
.Get(),
@@ -244,6 +241,7 @@ bool SettingsComponent::HandleWindowMessage(
244241
}
245242
case ID_SETTINGS_SETUSERAGENT:
246243
{
244+
CHECK_FEATURE_RETURN(m_settings2);
247245
ChangeUserAgent();
248246
return true;
249247
}
@@ -478,20 +476,10 @@ bool SettingsComponent::HandleWindowMessage(
478476
//! [BuiltInErrorPageEnabled]
479477
return true;
480478
}
481-
case ID_SETTINGS_USER_AGENT_STRING:
482-
{
483-
LPWSTR user_agent;
484-
wil::com_ptr<ICoreWebView2ExperimentalSettings> settings;
485-
settings = m_settings.try_query<ICoreWebView2ExperimentalSettings>();
486-
CHECK_FAILURE(settings->get_UserAgent(&user_agent));
487-
CHECK_FAILURE(settings->put_UserAgent(L"example_string"));
488-
return true;
489-
}
490479
}
491480
}
492481
return false;
493482
}
494-
495483
// Prompt the user for a list of blocked domains
496484
void SettingsComponent::ChangeBlockedSites()
497485
{
@@ -661,36 +649,38 @@ void SettingsComponent::SetReplaceImages(bool replaceImages)
661649
// Prompt the user for a new User Agent string
662650
void SettingsComponent::ChangeUserAgent()
663651
{
664-
wil::com_ptr<ICoreWebView2ExperimentalSettings> experimental_settings;
665-
experimental_settings = m_settings.try_query<ICoreWebView2ExperimentalSettings>();
666-
LPWSTR user_agent;
667-
CHECK_FAILURE(experimental_settings->get_UserAgent(&user_agent));
668-
TextInputDialog dialog(
669-
m_appWindow->GetMainWindow(), L"User Agent", L"User agent:",
670-
L"Enter user agent, or leave blank to restore default.",
671-
m_changeUserAgent ? m_overridingUserAgent.c_str() : user_agent);
672-
if (dialog.confirmed)
652+
if (m_settings2)
673653
{
674-
SetUserAgent(dialog.input);
654+
LPWSTR user_agent;
655+
CHECK_FAILURE(m_settings2->get_UserAgent(&user_agent));
656+
TextInputDialog dialog(
657+
m_appWindow->GetMainWindow(), L"User Agent", L"User agent:",
658+
L"Enter user agent, or leave blank to restore default.",
659+
m_changeUserAgent ? m_overridingUserAgent.c_str() : user_agent);
660+
if (dialog.confirmed)
661+
{
662+
SetUserAgent(dialog.input);
663+
}
675664
}
676665
}
677666

678667
// Register a WebResourceRequested handler which adds a custom User-Agent
679668
// HTTP header to all requests.
680669
void SettingsComponent::SetUserAgent(const std::wstring& userAgent)
681670
{
682-
m_overridingUserAgent = userAgent;
683-
if (m_overridingUserAgent.empty())
684-
{
685-
m_changeUserAgent = false;
686-
}
687-
else
671+
if (m_settings2)
688672
{
689-
m_changeUserAgent = true;
690-
wil::com_ptr<ICoreWebView2ExperimentalSettings> experimental_settings;
691-
experimental_settings = m_settings.try_query<ICoreWebView2ExperimentalSettings>();
692-
experimental_settings->put_UserAgent(m_overridingUserAgent.c_str());
693-
}
673+
m_overridingUserAgent = userAgent;
674+
if (m_overridingUserAgent.empty())
675+
{
676+
m_changeUserAgent = false;
677+
}
678+
else
679+
{
680+
m_changeUserAgent = true;
681+
CHECK_FAILURE(m_settings2->put_UserAgent(m_overridingUserAgent.c_str()));
682+
}
683+
}
694684
}
695685

696686
void SettingsComponent::CompleteScriptDialogDeferral()

SampleApps/WebView2APISample/SettingsComponent.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class SettingsComponent : public ComponentBase
4646
wil::com_ptr<ICoreWebView2Environment> m_webViewEnvironment;
4747
wil::com_ptr<ICoreWebView2> m_webView;
4848
wil::com_ptr<ICoreWebView2Settings> m_settings;
49+
wil::com_ptr<ICoreWebView2Settings2> m_settings2;
4950

5051
bool m_blockImages = false;
5152
bool m_replaceImages = false;

0 commit comments

Comments
 (0)