Skip to content

Commit

Permalink
Sample update for 1.0.2950-prerelease (#259)
Browse files Browse the repository at this point in the history
* Updates for Win32, WPF, WinForms, UWP and WinUI3 sample apps from 132.0.2950.0

* Updated package version for Win32, WPF and WinForms sample apps to 1.0.2950-prerelease

---------

Co-authored-by: WebView2 Github Bot <[email protected]>
  • Loading branch information
soumamazu and WebView2GithubBot authored Nov 19, 2024
1 parent 1709010 commit 3d18366
Show file tree
Hide file tree
Showing 14 changed files with 434 additions and 293 deletions.
29 changes: 14 additions & 15 deletions SampleApps/WebView2APISample/AppWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "ScenarioDragDrop.h"
#include "ScenarioExtensionsManagement.h"
#include "ScenarioFileSystemHandleShare.h"
#include "ScenarioFileTypePolicy.h"
#include "ScenarioIFrameDevicePermission.h"
#include "ScenarioNavigateWithWebResourceRequest.h"
#include "ScenarioNonClientRegionSupport.h"
Expand All @@ -49,7 +50,6 @@
#include "ScenarioSharedBuffer.h"
#include "ScenarioSharedWorkerWRR.h"
#include "ScenarioThrottlingControl.h"
#include "ScenarioFileTypePolicy.h"
#include "ScenarioVirtualHostMappingForPopUpWindow.h"
#include "ScenarioVirtualHostMappingForSW.h"
#include "ScenarioWebMessage.h"
Expand Down Expand Up @@ -201,7 +201,7 @@ AppWindow::AppWindow(
WCHAR szTitle[s_maxLoadString]; // The title bar text
LoadStringW(g_hInstance, IDS_APP_TITLE, szTitle, s_maxLoadString);
m_appTitle = szTitle;

DWORD windowStyle = WS_OVERLAPPEDWINDOW;
if (userDataFolderParam.length() > 0)
{
m_userDataFolder = userDataFolderParam;
Expand All @@ -210,15 +210,15 @@ AppWindow::AppWindow(
if (customWindowRect)
{
m_mainWindow = CreateWindowExW(
WS_EX_CONTROLPARENT, GetWindowClass(), szTitle, WS_OVERLAPPEDWINDOW,
windowRect.left, windowRect.top, windowRect.right - windowRect.left,
WS_EX_CONTROLPARENT, GetWindowClass(), szTitle, windowStyle, windowRect.left,
windowRect.top, windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top, nullptr, nullptr, g_hInstance, nullptr);
}
else
{
m_mainWindow = CreateWindowExW(
WS_EX_CONTROLPARENT, GetWindowClass(), szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
0, CW_USEDEFAULT, 0, nullptr, nullptr, g_hInstance, nullptr);
WS_EX_CONTROLPARENT, GetWindowClass(), szTitle, windowStyle, CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0, nullptr, nullptr, g_hInstance, nullptr);
}

m_appBackgroundImageHandle = (HBITMAP)LoadImage(
Expand Down Expand Up @@ -1429,11 +1429,11 @@ HRESULT AppWindow::OnCreateEnvironmentCompleted(
m_webViewEnvironment = environment;

if (m_webviewOption.entry == WebViewCreateEntry::EVER_FROM_CREATE_WITH_OPTION_MENU ||
m_creationModeId == IDM_CREATION_MODE_HOST_INPUT_PROCESSING)
m_creationModeId == IDM_CREATION_MODE_HOST_INPUT_PROCESSING
)
{
return CreateControllerWithOptions();
}

auto webViewEnvironment3 = m_webViewEnvironment.try_query<ICoreWebView2Environment3>();

if (webViewEnvironment3 && (m_dcompDevice || m_wincompCompositor))
Expand Down Expand Up @@ -1625,7 +1625,6 @@ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(
m_creationModeId == IDM_CREATION_MODE_TARGET_DCOMP);
NewComponent<AudioComponent>(this);
NewComponent<ControlComponent>(this, &m_toolbar);

m_webView3 = coreWebView2.try_query<ICoreWebView2_3>();
if (m_webView3)
{
Expand Down Expand Up @@ -1969,13 +1968,13 @@ void AppWindow::RegisterEventHandlers()
RunAsync(
[this]()
{
std::wstring message =
L"We detected there is a critical update for WebView2 runtime.";
std::wstring message = L"We detected there is a critical "
L"update for WebView2 runtime.";
if (m_webView)
{
message += L"Do you want to restart the app? \n\n";
message +=
L"Click No if you only want to re-create the webviews. \n";
message += L"Click No if you only want to re-create the "
L"webviews. \n";
message += L"Click Cancel for no action. \n";
}
int response = MessageBox(
Expand Down Expand Up @@ -2036,11 +2035,11 @@ void AppWindow::ResizeEverything()
RECT availableBounds = {0};
GetClientRect(m_mainWindow, &availableBounds);

if (!m_containsFullscreenElement)
if (!m_containsFullscreenElement
)
{
availableBounds = m_toolbar.Resize(availableBounds);
}

if (auto view = GetComponent<ViewComponent>())
{
view->SetBounds(availableBounds);
Expand Down
17 changes: 6 additions & 11 deletions SampleApps/WebView2APISample/AppWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ struct WebViewCreateOption
}
WebViewCreateOption(
const std::wstring& profile_, bool inPrivate, const std::wstring& downloadPath,
const std::wstring& scriptLocale_, WebViewCreateEntry entry_, bool useOSRegion_)
const std::wstring& scriptLocale_, WebViewCreateEntry entry_, bool useOSRegion_
)
: profile(profile_), isInPrivate(inPrivate), downloadPath(downloadPath),
scriptLocale(scriptLocale_), entry(entry_), useOSRegion(useOSRegion_)
{
}

WebViewCreateOption(const WebViewCreateOption& opt)
{
profile = opt.profile;
Expand Down Expand Up @@ -87,15 +87,10 @@ class AppWindow
{
public:
AppWindow(
UINT creationModeId,
const WebViewCreateOption& opt,
const std::wstring& initialUri = L"",
const std::wstring& userDataFolderParam = L"",
bool isMainWindow = false,
std::function<void()> webviewCreatedCallback = nullptr,
bool customWindowRect = false,
RECT windowRect = {0},
bool shouldHaveToolbar = true,
UINT creationModeId, const WebViewCreateOption& opt,
const std::wstring& initialUri = L"", const std::wstring& userDataFolderParam = L"",
bool isMainWindow = false, std::function<void()> webviewCreatedCallback = nullptr,
bool customWindowRect = false, RECT windowRect = {0}, bool shouldHaveToolbar = true,
bool isPopup = false);

~AppWindow();
Expand Down
187 changes: 98 additions & 89 deletions SampleApps/WebView2APISample/ScenarioAddHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,50 @@ ScenarioAddHostObject::ScenarioAddHostObject(AppWindow* appWindow)

m_hostObject = Microsoft::WRL::Make<HostObjectSample>(
[appWindow = m_appWindow](std::function<void(void)> callback)
{
appWindow->RunAsync(callback);
});
{ appWindow->RunAsync(callback); });

CHECK_FAILURE(m_webView->add_NavigationStarting(
Microsoft::WRL::Callback<ICoreWebView2NavigationStartingEventHandler>(
[this, sampleUri](ICoreWebView2* sender, ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT
{
wil::unique_cotaskmem_string navigationTargetUri;
CHECK_FAILURE(args->get_Uri(&navigationTargetUri));
std::wstring uriTarget(navigationTargetUri.get());

if (AreFileUrisEqual(sampleUri, uriTarget))
{
//! [AddHostObjectToScript]
VARIANT remoteObjectAsVariant = {};
m_hostObject.query_to<IDispatch>(&remoteObjectAsVariant.pdispVal);
remoteObjectAsVariant.vt = VT_DISPATCH;

// We can call AddHostObjectToScript multiple times in a row without
// calling RemoveHostObject first. This will replace the previous object
// with the new object. In our case this is the same object and everything
// is fine.
CHECK_FAILURE(
m_webView->AddHostObjectToScript(L"sample", &remoteObjectAsVariant));
remoteObjectAsVariant.pdispVal->Release();
//! [AddHostObjectToScript]
}
else
{
// We can call RemoveHostObject multiple times in a row without
// calling AddHostObjectToScript first. This will produce an error
// result so we ignore the failure.
m_webView->RemoveHostObjectFromScript(L"sample");

// When we navigate elsewhere we're off of the sample
// scenario page and so should remove the scenario.
m_appWindow->DeleteComponent(this);
}

return S_OK;
}).Get(), &m_navigationStartingToken));
[this, sampleUri](
ICoreWebView2* sender,
ICoreWebView2NavigationStartingEventArgs* args) -> HRESULT
{
wil::unique_cotaskmem_string navigationTargetUri;
CHECK_FAILURE(args->get_Uri(&navigationTargetUri));
std::wstring uriTarget(navigationTargetUri.get());

if (AreFileUrisEqual(sampleUri, uriTarget))
{
//! [AddHostObjectToScript]
VARIANT remoteObjectAsVariant = {};
m_hostObject.query_to<IDispatch>(&remoteObjectAsVariant.pdispVal);
remoteObjectAsVariant.vt = VT_DISPATCH;

// We can call AddHostObjectToScript multiple times in a row without
// calling RemoveHostObject first. This will replace the previous object
// with the new object. In our case this is the same object and everything
// is fine.
CHECK_FAILURE(
m_webView->AddHostObjectToScript(L"sample", &remoteObjectAsVariant));
remoteObjectAsVariant.pdispVal->Release();
//! [AddHostObjectToScript]
}
else
{
// We can call RemoveHostObject multiple times in a row without
// calling AddHostObjectToScript first. This will produce an error
// result so we ignore the failure.
m_webView->RemoveHostObjectFromScript(L"sample");

// When we navigate elsewhere we're off of the sample
// scenario page and so should remove the scenario.
m_appWindow->DeleteComponent(this);
}

return S_OK;
})
.Get(),
&m_navigationStartingToken));

wil::com_ptr<ICoreWebView2_4> webview2_4 = m_webView.try_query<ICoreWebView2_4>();
if (webview2_4)
Expand All @@ -83,63 +85,70 @@ ScenarioAddHostObject::ScenarioAddHostObject(AppWindow* appWindow)
CHECK_FAILURE(webview2_4->add_FrameCreated(
Callback<ICoreWebView2FrameCreatedEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2FrameCreatedEventArgs* args) -> HRESULT
{
wil::com_ptr<ICoreWebView2Frame> webviewFrame;
CHECK_FAILURE(args->get_Frame(&webviewFrame));

wil::unique_cotaskmem_string name;
CHECK_FAILURE(webviewFrame->get_Name(&name));
if (std::wcscmp(name.get(), L"iframe_name") == 0)
{
//! [AddHostObjectToScriptWithOrigins]
wil::unique_variant remoteObjectAsVariant;
// It will throw if m_hostObject fails the QI, but because it is our object
// it should always succeed.
m_hostObject.query_to<IDispatch>(&remoteObjectAsVariant.pdispVal);
remoteObjectAsVariant.vt = VT_DISPATCH;

// Create list of origins which will be checked.
// iframe will have access to host object only if its origin belongs
// to this list.
LPCWSTR origin = L"https://appassets.example/";

CHECK_FAILURE(webviewFrame->AddHostObjectToScriptWithOrigins(
L"sample", &remoteObjectAsVariant, 1, &origin));
//! [AddHostObjectToScriptWithOrigins]
}

// Subscribe to frame name changed event
webviewFrame->add_NameChanged(
Callback<ICoreWebView2FrameNameChangedEventHandler>(
[this](ICoreWebView2Frame* sender, IUnknown* args) -> HRESULT {
wil::unique_cotaskmem_string newName;
CHECK_FAILURE(sender->get_Name(&newName));
// Handle name changed event
return S_OK;
}).Get(), NULL);

// Subscribe to frame destroyed event
webviewFrame->add_Destroyed(
Callback<ICoreWebView2FrameDestroyedEventHandler>(
[this](ICoreWebView2Frame* sender, IUnknown* args) -> HRESULT {
/*Cleanup on frame destruction*/
return S_OK;
})
.Get(),
NULL);
return S_OK;
}).Get(), &m_frameCreatedToken));
ICoreWebView2* sender, ICoreWebView2FrameCreatedEventArgs* args) -> HRESULT
{
wil::com_ptr<ICoreWebView2Frame> webviewFrame;
CHECK_FAILURE(args->get_Frame(&webviewFrame));

wil::unique_cotaskmem_string name;
CHECK_FAILURE(webviewFrame->get_Name(&name));
if (std::wcscmp(name.get(), L"iframe_name") == 0)
{
//! [AddHostObjectToScriptWithOrigins]
wil::unique_variant remoteObjectAsVariant;
// It will throw if m_hostObject fails the QI, but because it is our
// object it should always succeed.
m_hostObject.query_to<IDispatch>(&remoteObjectAsVariant.pdispVal);
remoteObjectAsVariant.vt = VT_DISPATCH;

// Create list of origins which will be checked.
// iframe will have access to host object only if its origin belongs
// to this list.
LPCWSTR origin = L"https://appassets.example/";

CHECK_FAILURE(webviewFrame->AddHostObjectToScriptWithOrigins(
L"sample", &remoteObjectAsVariant, 1, &origin));
//! [AddHostObjectToScriptWithOrigins]
}

// Subscribe to frame name changed event
webviewFrame->add_NameChanged(
Callback<ICoreWebView2FrameNameChangedEventHandler>(
[this](ICoreWebView2Frame* sender, IUnknown* args) -> HRESULT
{
wil::unique_cotaskmem_string newName;
CHECK_FAILURE(sender->get_Name(&newName));
// Handle name changed event
return S_OK;
})
.Get(),
NULL);

// Subscribe to frame destroyed event
webviewFrame->add_Destroyed(
Callback<ICoreWebView2FrameDestroyedEventHandler>(
[this](ICoreWebView2Frame* sender, IUnknown* args) -> HRESULT
{
/*Cleanup on frame destruction*/
return S_OK;
})
.Get(),
NULL);
return S_OK;
})
.Get(),
&m_frameCreatedToken));
}

CHECK_FAILURE(m_webView->Navigate(sampleUri.c_str()));
}

ScenarioAddHostObject::~ScenarioAddHostObject()
{
m_webView->RemoveHostObjectFromScript(L"sample");
m_webView->remove_NavigationStarting(m_navigationStartingToken);
if (m_navigationCompletedToken.value)
{
m_webView->remove_NavigationCompleted(m_navigationCompletedToken);
}
wil::com_ptr<ICoreWebView2_4> webview2_4 = m_webView.try_query<ICoreWebView2_4>();
if (webview2_4)
{
Expand Down
1 change: 1 addition & 0 deletions SampleApps/WebView2APISample/ScenarioAddHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ class ScenarioAddHostObject : public ComponentBase
wil::com_ptr<HostObjectSample> m_hostObject;

EventRegistrationToken m_navigationStartingToken = {};
EventRegistrationToken m_navigationCompletedToken = {};
EventRegistrationToken m_frameCreatedToken = {};
};
9 changes: 9 additions & 0 deletions SampleApps/WebView2APISample/Toolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ void Toolbar::SetItemEnabled(Item item, bool enabled)
EnableWindow(m_items[item], enabled);
}

void Toolbar::Hide()
{
DisableAllItems();
for (HWND hwnd : m_items)
{
ShowWindow(hwnd, SW_HIDE);
}
}

void Toolbar::DisableAllItems()
{
for (HWND hwnd : m_items)
Expand Down
1 change: 1 addition & 0 deletions SampleApps/WebView2APISample/Toolbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Toolbar
void UpdateDpiAndTextScale();
void SelectAll();
void SelectAddressBar();
void Hide();

private:
int GetItemLogicalWidth(Item item, int clientLogicalWidth) const;
Expand Down
4 changes: 2 additions & 2 deletions SampleApps/WebView2APISample/WebView2APISample.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,13 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
<Import Project="..\packages\Microsoft.Web.WebView2.1.0.2895-prerelease\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.2895-prerelease\build\native\Microsoft.Web.WebView2.targets')" />
<Import Project="..\packages\Microsoft.Web.WebView2.1.0.2950-prerelease\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('..\packages\Microsoft.Web.WebView2.1.0.2950-prerelease\build\native\Microsoft.Web.WebView2.targets')" />
</ImportGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Web.WebView2.1.0.2895-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.2895-prerelease\build\native\Microsoft.Web.WebView2.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.Web.WebView2.1.0.2950-prerelease\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Web.WebView2.1.0.2950-prerelease\build\native\Microsoft.Web.WebView2.targets'))" />
</Target>
</Project>
Loading

0 comments on commit 3d18366

Please sign in to comment.