Skip to content

Commit c2c182d

Browse files
authored
Create Freeze.md
1 parent e7087aa commit c2c182d

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

specs/Freeze.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Background
2+
Edge browser has a sleeping tab feature which reduces resource usage when the tab is in the background. We are introducing WebView2 APIs
3+
to access this feature so that invisible WebView can use less resources. We'd appreciate your feedback.
4+
5+
6+
# Description
7+
When a WebView in Win32 app becomes invisible, the app calls `TryFreeze` API so that it consumes less memory.
8+
For an Universal Windows Platform app, the app calls the API in suspended event handler before completing the suspended event.
9+
10+
# Examples
11+
## .Net, WinRT
12+
```c#
13+
async protected void OnSuspending(object sender, SuspendingEventArgs args)
14+
{
15+
SuspendingDeferral deferral = args.SuspendingOperation.GetDeferral();
16+
await webView.CoreWebView2.TryFreezeAsync();
17+
deferral.Complete();
18+
}
19+
```
20+
## Win32 C++
21+
As unfreeze is very fast and automatically happens when WebView becomes visible, the app can generaly immediately call `TryFreeze` when a WebView becomes invisible.
22+
```cpp
23+
bool ViewComponent::HandleWindowMessage(
24+
HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT* result)
25+
{
26+
if (message == WM_SYSCOMMAND)
27+
{
28+
if (wParam == SC_MINIMIZE)
29+
{
30+
// Hide the webview when the app window is minimized, and freeze it.
31+
m_controller->put_IsVisible(FALSE);
32+
Freeze();
33+
}
34+
else if (wParam == SC_RESTORE)
35+
{
36+
// When the app window is restored, show the webview
37+
// (unless the user has toggle visibility off).
38+
if (m_isVisible)
39+
{
40+
Unfreeze();
41+
m_controller->put_IsVisible(TRUE);
42+
}
43+
}
44+
}
45+
}
46+
47+
void ViewComponent::Freeze()
48+
{
49+
HRESULT hr = webView->TryFreeze(
50+
Callback<ICoreWebView2StagingTryFreezeCompletedHandler>(
51+
[](HRESULT errorCode, BOOL isSuccessful) -> HRESULT {
52+
std::wstringstream formattedMessage;
53+
formattedMessage << "TryFreeze result (0x" << std::hex << errorCode
54+
<< ") " << (isSuccessful ? "succeeded" : "failed");
55+
MessageBox(nullptr, formattedMessage.str().c_str(), nullptr, MB_OK);
56+
return S_OK;
57+
})
58+
.Get());
59+
if (FAILED(hr))
60+
ShowFailure(hr, L"Call to TryFreeze failed");
61+
}
62+
63+
void ViewComponent::Unfreeze()
64+
{
65+
wil::com_ptr<ICoreWebView2Staging2> webView;
66+
webView = m_webView.query<ICoreWebView2Staging2>();
67+
webView->Unfreeze();
68+
}
69+
```
70+
71+
# Remarks
72+
WebView2 controller must be invisible when the API is called. Otherwise, the
73+
API fails with `HRESULT_FROM_WIN32(ERROR_INVALID_STATE)`.
74+
Freezing is similar to putting a tab into sleeping in browser. Freezing pauses
75+
WebView script timers and animations, minimizes CPU usage for the associated
76+
browser renderer process and allows the operating system to reuse the memory that was
77+
used by the renderer process for other processes.
78+
See [Sleeping Tabs FAQ](https://techcommunity.microsoft.com/t5/articles/sleeping-tabs-faq/m-p/1705434)
79+
for conditions that might prevent WebView from being frozen. In those situations,
80+
TryFreeze operation will fail and the completed handler will be invoked with isSuccessful as false.
81+
WebView will be automatically unfrozen when it becomes visible. Therefore, the app normally doesn't have to call Unfreeze.
82+
The app can call `Unfreeze` and then `TryFreeze` periodically for an invisibile WebView so that the WebView can sync up with
83+
latest data to show fresh content when it becomes visible.
84+
85+
# API Notes
86+
See [API Details](#api-details) section below for API reference.
87+
88+
# API Details
89+
90+
## Win32 C++
91+
```IDL
92+
interface ICoreWebView2_2 : ICoreWebView2 {
93+
94+
HRESULT TryFreeze([in] ICoreWebView2StagingTryFreezeCompletedHandler* handler);
95+
96+
HRESULT Unfreeze();
97+
}
98+
99+
/// The caller implements this interface to receive the TryFreeze result.
100+
[uuid(00F206A7-9D17-4605-91F6-4E8E4DE192E3), object, pointer_default(unique)]
101+
interface ICoreWebView2StagingTryFreezeCompletedHandler : IUnknown {
102+
103+
HRESULT Invoke([in] HRESULT errorCode, [in] BOOL isSuccessful);
104+
105+
}
106+
```
107+
## .Net WinRT
108+
```c#
109+
namespace Microsoft.Web.WebView2.Core
110+
{
111+
public partial class CoreWebView2
112+
{
113+
// There are other API in this interface that we are not showing
114+
public Task<int> TryFreezeAsync();
115+
public void Unfreeze();
116+
}
117+
}
118+
```

0 commit comments

Comments
 (0)