@@ -4,25 +4,25 @@ to access this feature so that invisible WebView can use less resources. We'd ap
44
55
66# Description
7- You may call the ` TryFeezeAsync ` API to have the WebView2 consume less memory. This is useful when your Win32 app becomes invisible, or when your Universal Windows Platform app is suspended, during the suspended event handler before completing the suspended event.
7+ You may call the ` TrySuspendAsync ` API to have the WebView2 consume less memory. This is useful when your Win32 app becomes invisible, or when your Universal Windows Platform app is being suspended, during the suspended event handler before completing the suspended event.
88
99# Examples
1010## .Net, WinRT
1111``` c#
1212async protected void OnSuspending (object sender , SuspendingEventArgs args )
1313{
1414 SuspendingDeferral deferral = args .SuspendingOperation .GetDeferral ();
15- // Set webView.Visibility to false first .
16- // WebView2 must be invisible for TryFreezeAsync to succeed .
15+ // Ensure that CoreWebView2Controller is invisible, it must be invisible for TrySuspendAsync to succeed .
16+ // webView here is Microsoft.Web.WebView2.Wpf.WebView2 .
1717 webView .Visibility = false ;
18- await webView .CoreWebView2 .TryFreezeAsync ();
18+ await webView .CoreWebView2 .TrySuspendAsync ();
1919 deferral .Complete ();
2020}
21- async protected void OnSuspending (object sender , Object args )
21+ async protected void OnResuming (object sender , Object args )
2222{
23- // Making a WebView2 visible will automatically unfreeze it
24- // But you can also explicitly call Unfreeze without making a WebView2 visible to unfreeze it.
25- webView .CoreWebView2 .Unfreeze ();
23+ // Making a WebView2 visible will automatically resume it
24+ // But you can also explicitly call Resume without making a WebView2 visible to resume it.
25+ webView .CoreWebView2 .Resume ();
2626 webView .Visibility = true ;
2727}
2828```
@@ -38,61 +38,66 @@ bool ViewComponent::HandleWindowMessage(
3838 {
3939 // Hide the webview when the app window is minimized, and freeze it.
4040 m_controller->put_IsVisible(FALSE);
41- Freeze ();
41+ Suspend ();
4242 }
4343 else if (wParam == SC_RESTORE)
4444 {
4545 // When the app window is restored, show the webview
4646 // (unless the user has toggle visibility off).
4747 if (m_isVisible)
4848 {
49- Unfreeze ();
49+ Resume ();
5050 m_controller->put_IsVisible(TRUE);
5151 }
5252 }
5353 }
5454}
5555
56- void ViewComponent::Freeze ()
56+ void ViewComponent::Suspend ()
5757{
58- HRESULT hr = webView->TryFreeze (
59- Callback<ICoreWebView2StagingTryFreezeCompletedHandler >(
58+ HRESULT hr = webView->TrySuspend (
59+ Callback<ICoreWebView2StagingTrySuspendCompletedHandler >(
6060 [ ] (HRESULT errorCode, BOOL isSuccessful) -> HRESULT {
6161 std::wstringstream formattedMessage;
62- formattedMessage << "TryFreeze result (0x" << std::hex << errorCode
62+ formattedMessage << "TrySuspend result (0x" << std::hex << errorCode
6363 << ") " << (isSuccessful ? "succeeded" : "failed");
6464 MessageBox(nullptr, formattedMessage.str().c_str(), nullptr, MB_OK);
6565 return S_OK;
6666 })
6767 .Get());
6868 if (FAILED(hr))
69- ShowFailure(hr, L"Call to TryFreeze failed");
69+ ShowFailure(hr, L"Call to TrySuspend failed");
7070}
7171
72- void ViewComponent::Unfreeze ()
72+ void ViewComponent::Resume ()
7373{
7474 wil::com_ptr<ICoreWebView2Staging2 > webView;
7575 webView = m_webView.query<ICoreWebView2Staging2 >();
76- webView->Unfreeze ();
76+ webView->Resume ();
7777}
7878```
7979
8080# Remarks
8181The CoreWebView2Controller must be invisible when the API is called. Otherwise, the
8282API fails with `HRESULT_FROM_WIN32(ERROR_INVALID_STATE)`.
83- Freezing is similar to putting a tab to sleep in the Edge browser. Freezing pauses
83+ Suspending is similar to putting a tab to sleep in the Edge browser. Suspending pauses
8484WebView script timers and animations, minimizes CPU usage for the associated
8585browser renderer process and allows the operating system to reuse the memory that was
8686used by the renderer process for other processes.
87- Note that the freeze is best effort and considered completed successfully once the request
87+ Note that the Suspend is best effort and considered completed successfully once the request
8888is sent to browser renderer process. If there is a running script, the script will continue
89- to run and the renderer process will be frozen after that script is done.
89+ to run and the renderer process will be suspended after that script is done.
9090See [Sleeping Tabs FAQ](https://techcommunity.microsoft.com/t5/articles/sleeping-tabs-faq/m-p/1705434)
91- for conditions that might prevent WebView from being frozen. In those situations,
92- The TryFreeze operation will fail and the completed handler will be invoked with isSuccessful as false.
93- The WebView will be automatically unfrozen when it becomes visible. Therefore, the app normally doesn't have to call Unfreeze.
94- The app can call `Unfreeze` and then `TryFreeze` periodically for an invisible WebView so that the invisible WebView can sync up with
95- latest data and the page ready to show fresh content when it becomes visible.
91+ for conditions that might prevent WebView from being suspended. In those situations,
92+ The the completed handler will be invoked with isSuccessful as false and errorCode as S_OK.
93+ The WebView will be automatically resumed when it becomes visible. Therefore, the
94+ app normally does not have to call Resume.
95+ The app can call `Resume` and then `TrySuspend` periodically for an invisible WebView so that
96+ the invisible WebView can sync up with latest data and the page ready to show fresh content
97+ when it becomes visible.
98+ When WebView is suspended, While WebView properties can still be accessed, WebView methods are generally not accessible.
99+ After `TrySuspend` is called and until WebView is resumed or `TrySuspend` resulted in not being successfully suspended,
100+ calling WebView methods will fail with `HRESULT_FROM_WIN32(ERROR_INVALID_STATE)`.
96101
97102# API Notes
98103See [API Details](#api-details) section below for API reference.
@@ -103,40 +108,55 @@ See [API Details](#api-details) section below for API reference.
103108```IDL
104109interface ICoreWebView2_2 : ICoreWebView2 {
105110
106- /// An app may call the `TryFeezeAsync ` API to have the WebView2 consume less memory.
111+ /// An app may call the `TrySuspend ` API to have the WebView2 consume less memory.
107112 /// This is useful when a Win32 app becomes invisible, or when a Universal Windows
108- /// Platform app is suspended, during the suspended event handler before completing
113+ /// Platform app is being suspended, during the suspended event handler before completing
109114 /// the suspended event.
110115 /// The CoreWebView2Controller must be invisible when the API is called. Otherwise, the
111116 /// API fails with `HRESULT_FROM_WIN32(ERROR_INVALID_STATE)`.
112- /// Freezing is similar to putting a tab to sleep in the Edge browser. Freezing pauses
117+ /// Suspending is similar to putting a tab to sleep in the Edge browser. Suspending pauses
113118 /// WebView script timers and animations, minimizes CPU usage for the associated
114119 /// browser renderer process and allows the operating system to reuse the memory that was
115120 /// used by the renderer process for other processes.
116- /// Note that the freeze is best effort and considered completed successfully once the request
121+ /// Note that the Suspend is best effort and considered completed successfully once the request
117122 /// is sent to browser renderer process. If there is a running script, the script will continue
118- /// to run and the renderer process will be frozen after that script is done.
123+ /// to run and the renderer process will be suspended after that script is done.
119124 /// See [Sleeping Tabs FAQ](https://techcommunity.microsoft.com/t5/articles/sleeping-tabs-faq/m-p/1705434)
120- /// for conditions that might prevent WebView from being frozen . In those situations,
121- /// The TryFreeze operation will fail and the completed handler will be invoked with isSuccessful as false.
122- /// The WebView will be automatically unfrozen when it becomes visible. Therefore, the
123- /// app normally doesn't have to call Unfreeze .
124- /// The app can call `Unfreeze ` and then `TryFreeze ` periodically for an invisible WebView so that
125+ /// for conditions that might prevent WebView from being suspended . In those situations,
126+ /// The the completed handler will be invoked with isSuccessful as false and errorCode as S_OK .
127+ /// The WebView will be automatically resumed when it becomes visible. Therefore, the
128+ /// app normally does not have to call Resume .
129+ /// The app can call `Resume ` and then `TrySuspend ` periodically for an invisible WebView so that
125130 /// the invisible WebView can sync up with latest data and the page ready to show fresh content
126131 /// when it becomes visible.
127- HRESULT TryFreeze([in] ICoreWebView2StagingTryFreezeCompletedHandler* handler);
132+ /// When WebView is suspended, while WebView properties can still be accessed, WebView methods are
133+ /// generally not accessible. After `TrySuspend` is called and until WebView is resumed or `TrySuspend`
134+ /// resulted in not being successfully suspended, calling WebView methods will fail with
135+ /// `HRESULT_FROM_WIN32(ERROR_INVALID_STATE)`.
136+ HRESULT TrySuspend([in] ICoreWebView2StagingTrySuspendCompletedHandler* handler);
128137
129- /// Unfreeze the WebView so that it would resume activities on the web page.
138+ /// Resume the WebView so that it would resume activities on the web page.
130139 /// This API can be called while the WebView2 controller is invisible.
131- /// The app can interact with the WebView immediately after unfreeze.
132- /// WebView will be automatically unfrozen when it becomes visible.
133- HRESULT Unfreeze();
140+ /// The app can interact with the WebView immediately after Resume.
141+ /// WebView will be automatically resumed when it becomes visible.
142+ ///
143+ /// \snippet ViewComponent.cpp ToggleIsVisibleOnMinimize
144+ ///
145+ /// \snippet ViewComponent.cpp Resume
146+ ///
147+ HRESULT Resume();
148+
149+ /// `TRUE` when WebView is suspended.
150+ [propget] HRESULT IsSuspended([out, retval] BOOL* isSuspended);
134151}
135152
136153/// The caller implements this interface to receive the TryFreeze result.
137- [uuid(00F206A7-9D17-4605-91F6-4E8E4DE192E3), object, pointer_default(unique)]
138- interface ICoreWebView2StagingTryFreezeCompletedHandler : IUnknown {
154+ interface ICoreWebView2StagingTrySuspendCompletedHandler : IUnknown {
139155
156+ /// Provide the result of the TrySuspend operation.
157+ /// See [Sleeping Tabs FAQ](https://techcommunity.microsoft.com/t5/articles/sleeping-tabs-faq/m-p/1705434)
158+ /// for conditions that might prevent WebView from being suspended. In those situations,
159+ /// isSuccessful will be false and errorCode is S_OK.
140160 HRESULT Invoke([in] HRESULT errorCode, [in] BOOL isSuccessful);
141161
142162}
@@ -148,8 +168,9 @@ namespace Microsoft.Web.WebView2.Core
148168 public partial class CoreWebView2
149169 {
150170 // There are other API in this interface that we are not showing
151- public Task <bool > TryFreezeAsync ();
152- public void Unfreeze ();
171+ public Task <bool > TrySuspendAsync ();
172+ public void Resume ();
173+ public bool IsSuspended { get ; }
153174 }
154175}
155176```
0 commit comments