Skip to content

Commit 4f28fa3

Browse files
Update projects to use latest WebView2 SDK 1.0.2065-prerelease (#206)
* Updates for Win32, WPF and WinForms sample apps from 118.0.2065.0 * Updated package version for Win32, WPF and WinForms sample apps to 1.0.2065-prerelease --------- Co-authored-by: WebView2 Github Bot <[email protected]>
1 parent c8e255f commit 4f28fa3

28 files changed

+560
-179
lines changed

SampleApps/WebView2APISample/App.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmd
4242
std::wstring userDataFolder(L"");
4343
std::wstring initialUri;
4444
DWORD creationModeId = IDM_CREATION_MODE_WINDOWED;
45+
WebViewCreateOption opt;
4546

4647
if (lpCmdLine && lpCmdLine[0])
4748
{
@@ -120,7 +121,7 @@ wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmd
120121

121122
DpiUtil::SetProcessDpiAwarenessContext(dpiAwarenessContext);
122123

123-
new AppWindow(creationModeId, WebViewCreateOption(), initialUri, userDataFolder, true);
124+
new AppWindow(creationModeId, opt, initialUri, userDataFolder, true);
124125

125126
int retVal = RunMessagePump();
126127

SampleApps/WebView2APISample/AppWindow.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,11 @@ void AppWindow::InitializeWebView()
13711371
HRESULT AppWindow::OnCreateEnvironmentCompleted(
13721372
HRESULT result, ICoreWebView2Environment* environment)
13731373
{
1374-
CHECK_FAILURE(result);
1374+
if (result != S_OK)
1375+
{
1376+
ShowFailure(result, L"Failed to create environment object.");
1377+
return S_OK;
1378+
}
13751379
m_webViewEnvironment = environment;
13761380

13771381
if (m_webviewOption.entry == WebViewCreateEntry::EVER_FROM_CREATE_WITH_OPTION_MENU
@@ -1569,7 +1573,6 @@ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(
15691573
}
15701574
NewComponent<ScenarioPermissionManagement>(this);
15711575
NewComponent<ScenarioNotificationReceived>(this);
1572-
15731576
// We have a few of our own event handlers to register here as well
15741577
RegisterEventHandlers();
15751578

@@ -1721,6 +1724,32 @@ void AppWindow::RegisterEventHandlers()
17211724
args->put_Handled(FALSE);
17221725
return S_OK;
17231726
}
1727+
wil::com_ptr<ICoreWebView2ExperimentalNewWindowRequestedEventArgs2>
1728+
experimental_args;
1729+
if (SUCCEEDED(args->QueryInterface(IID_PPV_ARGS(&experimental_args))))
1730+
{
1731+
wil::com_ptr<ICoreWebView2FrameInfo> frame_info;
1732+
CHECK_FAILURE(experimental_args->get_OriginalSourceFrameInfo(&frame_info));
1733+
wil::unique_cotaskmem_string source;
1734+
CHECK_FAILURE(frame_info->get_Source(&source));
1735+
// The host can decide how to open based on source frame info,
1736+
// such as URI.
1737+
static const wchar_t* browser_launching_domain = L"www.example.com";
1738+
wil::unique_bstr source_domain = GetDomainOfUri(source.get());
1739+
const wchar_t* source_domain_as_wchar = source_domain.get();
1740+
if (wcscmp(browser_launching_domain, source_domain_as_wchar) == 0)
1741+
{
1742+
// Open the URI in the default browser.
1743+
wil::unique_cotaskmem_string target_uri;
1744+
CHECK_FAILURE(args->get_Uri(&target_uri));
1745+
ShellExecute(
1746+
nullptr, L"open", target_uri.get(), nullptr, nullptr,
1747+
SW_SHOWNORMAL);
1748+
CHECK_FAILURE(args->put_Handled(TRUE));
1749+
return S_OK;
1750+
}
1751+
}
1752+
17241753
wil::com_ptr<ICoreWebView2Deferral> deferral;
17251754
CHECK_FAILURE(args->GetDeferral(&deferral));
17261755
AppWindow* newAppWindow;
@@ -2176,7 +2205,6 @@ std::wstring AppWindow::GetLocalUri(
21762205
else
21772206
{
21782207
std::wstring path = GetLocalPath(L"assets\\" + relativePath, false);
2179-
21802208
wil::com_ptr<IUri> uri;
21812209
CHECK_FAILURE(CreateUri(path.c_str(), Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, &uri));
21822210

SampleApps/WebView2APISample/AppWindow.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ struct WebViewCreateOption
4141
WebViewCreateOption()
4242
{
4343
}
44-
4544
WebViewCreateOption(
4645
const std::wstring& profile_, bool inPrivate, const std::wstring& downloadPath,
4746
const std::wstring& scriptLocale_, WebViewCreateEntry entry_, bool useOSRegion_)
@@ -59,7 +58,6 @@ struct WebViewCreateOption
5958
entry = opt.entry;
6059
useOSRegion = opt.useOSRegion;
6160
}
62-
6361
void PopupDialog(AppWindow* app);
6462
};
6563

SampleApps/WebView2APISample/ProcessComponent.cpp

Lines changed: 63 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -215,63 +215,65 @@ std::wstring ProcessComponent::FrameKindToString(const COREWEBVIEW2_FRAME_KIND k
215215
}
216216

217217
void ProcessComponent::AppendFrameInfo(
218-
wil::com_ptr<ICoreWebView2FrameInfo> frameInfo, std::wstring& result)
218+
wil::com_ptr<ICoreWebView2FrameInfo> frameInfo, std::wstringstream& result)
219219
{
220-
if (!frameInfo)
221-
{
222-
return;
223-
}
224-
220+
UINT32 frameId = 0;
221+
UINT32 parentFrameId = 0;
222+
UINT32 mainFrameId = 0;
223+
UINT32 firstLevelFrameId = 0;
224+
std::wstring type = L"other child frame";
225225
wil::unique_cotaskmem_string nameRaw;
226+
wil::unique_cotaskmem_string sourceRaw;
227+
COREWEBVIEW2_FRAME_KIND frameKind = COREWEBVIEW2_FRAME_KIND_OTHER;
228+
226229
CHECK_FAILURE(frameInfo->get_Name(&nameRaw));
227-
result.append(L"{frame name:");
228-
result.append(nameRaw.get());
230+
std::wstring name = ((std::wstring)(nameRaw.get())).empty() ? L"none" : nameRaw.get();
231+
CHECK_FAILURE(frameInfo->get_Source(&sourceRaw));
232+
std::wstring source = ((std::wstring)(sourceRaw.get())).empty() ? L"none" : sourceRaw.get();
229233

230234
wil::com_ptr<ICoreWebView2ExperimentalFrameInfo> frameInfoExperimental;
231235
CHECK_FAILURE(frameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental)));
232-
UINT32 frameId = 0;
233236
frameInfoExperimental->get_FrameId(&frameId);
234-
result.append(L" | frame Id:" + std::to_wstring(frameId));
237+
frameInfoExperimental->get_FrameKind(&frameKind);
238+
239+
wil::com_ptr<ICoreWebView2FrameInfo> parentFrameInfo;
240+
CHECK_FAILURE(frameInfoExperimental->get_ParentFrameInfo(&parentFrameInfo));
241+
if (parentFrameInfo)
242+
{
243+
CHECK_FAILURE(parentFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental)));
244+
CHECK_FAILURE(frameInfoExperimental->get_FrameId(&parentFrameId));
245+
}
235246

236-
BOOL isMainFrameOrFirstLevelframeInfo = false;
237247
wil::com_ptr<ICoreWebView2FrameInfo> mainFrameInfo = GetAncestorMainFrameInfo(frameInfo);
238-
wil::com_ptr<ICoreWebView2FrameInfo> firstLevelFrameInfo =
239-
GetAncestorFirstLevelFrameInfo(frameInfo);
240-
// check if a frame is a main frame.
241248
if (mainFrameInfo == frameInfo)
242249
{
243-
result.append(L" | frame type: main frame");
244-
isMainFrameOrFirstLevelframeInfo = true;
250+
type = L"main frame";
245251
}
246-
// check if a frame is a first level frame.
252+
CHECK_FAILURE(mainFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental)));
253+
CHECK_FAILURE(frameInfoExperimental->get_FrameId(&mainFrameId));
254+
255+
wil::com_ptr<ICoreWebView2FrameInfo> firstLevelFrameInfo =
256+
GetAncestorFirstLevelFrameInfo(frameInfo);
247257
if (firstLevelFrameInfo == frameInfo)
248258
{
249-
result.append(L" | frame type: first level frame");
250-
isMainFrameOrFirstLevelframeInfo = true;
251-
}
252-
if (!isMainFrameOrFirstLevelframeInfo)
253-
{
254-
result.append(L" | frame type: other child frame");
259+
type = L"first level frame";
255260
}
256-
257-
COREWEBVIEW2_FRAME_KIND frameKind = COREWEBVIEW2_FRAME_KIND_OTHER;
258-
frameInfoExperimental->get_FrameKind(&frameKind);
259-
result.append(L"\n | frame kind:" + FrameKindToString(frameKind));
260-
261-
wil::com_ptr<ICoreWebView2FrameInfo> parentFrameInfo;
262-
CHECK_FAILURE(frameInfoExperimental->get_ParentFrameInfo(&parentFrameInfo));
263-
if (parentFrameInfo)
261+
if (firstLevelFrameInfo)
264262
{
265-
CHECK_FAILURE(parentFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental)));
266-
CHECK_FAILURE(frameInfoExperimental->get_FrameId(&frameId));
267-
result.append(L" | parent frame Id:" + std::to_wstring(frameId));
263+
CHECK_FAILURE(
264+
firstLevelFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental)));
265+
CHECK_FAILURE(frameInfoExperimental->get_FrameId(&firstLevelFrameId));
268266
}
269267

270-
wil::unique_cotaskmem_string sourceRaw;
271-
CHECK_FAILURE(frameInfo->get_Source(&sourceRaw));
272-
result.append(L"\n | frame source:\n\"");
273-
result.append(sourceRaw.get());
274-
result.append(L"\"");
268+
result << L"{frame name:" << name << L" | frame Id:" << std::to_wstring(frameId)
269+
<< L" | parent frame Id:"
270+
<< ((parentFrameId == 0) ? L"none" : std::to_wstring(parentFrameId))
271+
<< L" | frame type:" << type << L"\n"
272+
<< L" | ancestor main frame Id:" << std::to_wstring(mainFrameId)
273+
<< L" | ancestor first level frame Id:"
274+
<< ((firstLevelFrameId == 0) ? L"none" : std::to_wstring(firstLevelFrameId)) << L"\n"
275+
<< L" | frame kind:" << FrameKindToString(frameKind) << L"\n"
276+
<< L" | frame source:" << source << L"}," << std::endl;
275277
}
276278

277279
// Get the ancestor main frameInfo.
@@ -308,36 +310,6 @@ wil::com_ptr<ICoreWebView2FrameInfo> ProcessComponent::GetAncestorFirstLevelFram
308310
return firstLevelFrameInfo;
309311
}
310312

311-
// Append the frame Id of the ancestor first level frame and ancestor main frame.
312-
void ProcessComponent::AppendAncestorFrameInfo(
313-
wil::com_ptr<ICoreWebView2FrameInfo> frameInfo, std::wstring& result)
314-
{
315-
if (!frameInfo)
316-
{
317-
return;
318-
}
319-
320-
wil::com_ptr<ICoreWebView2FrameInfo> mainFrameInfo = GetAncestorMainFrameInfo(frameInfo);
321-
wil::com_ptr<ICoreWebView2FrameInfo> firstLevelFrameInfo =
322-
GetAncestorFirstLevelFrameInfo(frameInfo);
323-
wil::com_ptr<ICoreWebView2ExperimentalFrameInfo> frameInfoExperimental;
324-
UINT32 frameId = 0;
325-
if (firstLevelFrameInfo)
326-
{
327-
CHECK_FAILURE(
328-
firstLevelFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental)));
329-
CHECK_FAILURE(frameInfoExperimental->get_FrameId(&frameId));
330-
result.append(L"\n | ancestor first level frame Id:" + std::to_wstring(frameId));
331-
}
332-
if (mainFrameInfo)
333-
{
334-
CHECK_FAILURE(mainFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfoExperimental)));
335-
CHECK_FAILURE(frameInfoExperimental->get_FrameId(&frameId));
336-
result.append(L"\n | ancestor main frame Id:" + std::to_wstring(frameId));
337-
}
338-
result.append(L"},\n");
339-
}
340-
341313
void ProcessComponent::ShowProcessFrameInfo()
342314
{
343315
auto environmentExperimental11 =
@@ -353,8 +325,8 @@ void ProcessComponent::ShowProcessFrameInfo()
353325
UINT32 processCount = 0;
354326
UINT32 rendererProcessCount = 0;
355327
CHECK_FAILURE(processCollection->get_Count(&processCount));
356-
std::wstring result;
357-
std::wstring otherProcessResult;
328+
std::wstringstream otherProcessInfos;
329+
std::wstringstream rendererProcessInfos;
358330
for (UINT32 i = 0; i < processCount; i++)
359331
{
360332
Microsoft::WRL::ComPtr<ICoreWebView2ProcessInfo> processInfo;
@@ -366,7 +338,7 @@ void ProcessComponent::ShowProcessFrameInfo()
366338
if (kind == COREWEBVIEW2_PROCESS_KIND_RENDERER)
367339
{
368340
//! [AssociatedFrameInfos]
369-
std::wstring rendererProcessResult;
341+
std::wstringstream rendererProcess;
370342
wil::com_ptr<ICoreWebView2ExperimentalProcessInfo>
371343
processInfoExperimental;
372344
CHECK_FAILURE(processInfo->QueryInterface(
@@ -384,39 +356,37 @@ void ProcessComponent::ShowProcessFrameInfo()
384356
wil::com_ptr<ICoreWebView2FrameInfo> frameInfo;
385357
CHECK_FAILURE(iterator->GetCurrent(&frameInfo));
386358

387-
AppendFrameInfo(frameInfo, rendererProcessResult);
388-
AppendAncestorFrameInfo(frameInfo, rendererProcessResult);
359+
AppendFrameInfo(frameInfo, rendererProcess);
389360

390361
BOOL hasNext = FALSE;
391362
CHECK_FAILURE(iterator->MoveNext(&hasNext));
392363
frameInfoCount++;
393364
}
394-
rendererProcessResult.insert(
395-
0, std::to_wstring(frameInfoCount) +
396-
L" frameInfo(s) found in Renderer Process ID:" +
397-
std::to_wstring(processId) + L"\n");
398-
result.append(rendererProcessResult + L"\n");
365+
rendererProcessInfos
366+
<< std::to_wstring(frameInfoCount)
367+
<< L" frameInfo(s) found in Renderer Process ID:"
368+
<< std::to_wstring(processId) << L"\n"
369+
<< rendererProcess.str() << std::endl;
399370
//! [AssociatedFrameInfos]
400371
rendererProcessCount++;
401372
}
402373
else
403374
{
404-
otherProcessResult.append(
405-
L"Process Id:" + std::to_wstring(processId) +
406-
L" | Process Kind:" + ProcessKindToString(kind) + L"\n");
375+
otherProcessInfos << L"Process Id:" << std::to_wstring(processId)
376+
<< L" | Process Kind:"
377+
<< ProcessKindToString(kind) << std::endl;
407378
}
408379
}
409-
result.insert(
410-
0, std::to_wstring(processCount) + L" process(es) found, from which " +
411-
std::to_wstring(rendererProcessCount) +
412-
L" renderer process(es) found\n\n");
413-
otherProcessResult.insert(
414-
0, L"\nRemaining " +
415-
std::to_wstring(processCount - rendererProcessCount) +
416-
L" Process(es) Infos:\n");
417-
result.append(otherProcessResult);
418-
MessageBox(
419-
nullptr, result.c_str(), L"Process Info with Associated Frames", MB_OK);
380+
std::wstringstream message;
381+
message << std::to_wstring(processCount)
382+
<< L" process(es) found, from which "
383+
<< std::to_wstring(rendererProcessCount)
384+
<< L" renderer process(es) found\n\n"
385+
<< rendererProcessInfos.str() << L"Remaining Process(es) Infos:\n"
386+
<< otherProcessInfos.str();
387+
388+
m_appWindow->AsyncMessageBox(
389+
std::move(message.str()), L"Process Info with Associated Frames");
420390
return S_OK;
421391
})
422392
.Get()));

SampleApps/WebView2APISample/ProcessComponent.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ class ProcessComponent : public ComponentBase
5353
wil::com_ptr<ICoreWebView2ProcessInfoCollection> m_processCollection;
5454
EventRegistrationToken m_processFailedToken = {};
5555
EventRegistrationToken m_processInfosChangedToken = {};
56-
void AppendAncestorFrameInfo(
57-
wil::com_ptr<ICoreWebView2FrameInfo> frameInfo, std::wstring& result);
58-
void AppendFrameInfo(wil::com_ptr<ICoreWebView2FrameInfo> frameInfo, std::wstring& result);
56+
void AppendFrameInfo(
57+
wil::com_ptr<ICoreWebView2FrameInfo> frameInfo, std::wstringstream& result);
5958
wil::com_ptr<ICoreWebView2FrameInfo> GetAncestorFirstLevelFrameInfo(
6059
wil::com_ptr<ICoreWebView2FrameInfo> frameInfo);
6160
wil::com_ptr<ICoreWebView2FrameInfo> GetAncestorMainFrameInfo(
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 "ScenarioSaveAs.h"
8+
9+
#include "App.h"
10+
#include "AppWindow.h"
11+
#include "CheckFailure.h"
12+
#include "Shlwapi.h"
13+
#include "TextInputDialog.h"
14+
#include "resource.h"
15+
16+
using namespace Microsoft::WRL;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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 <string>
8+
9+
#include "AppWindow.h"
10+
#include "ComponentBase.h"

0 commit comments

Comments
 (0)