forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScenarioThrottlingControl.cpp
291 lines (253 loc) · 11.5 KB
/
ScenarioThrottlingControl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include "ScenarioThrottlingControl.h"
#include "CheckFailure.h"
#include "ScriptComponent.h"
using namespace Microsoft::WRL;
ScenarioThrottlingControl::ScenarioThrottlingControl(AppWindow* appWindow)
: m_appWindow(appWindow), m_webview(appWindow->GetWebView())
{
#pragma region init_monitor
m_appWindow->EnableHandlingNewWindowRequest(false);
CHECK_FAILURE(m_webview->add_NewWindowRequested(
Callback<ICoreWebView2NewWindowRequestedEventHandler>(
[this,
appWindow](ICoreWebView2* sender, ICoreWebView2NewWindowRequestedEventArgs* args)
{
wil::com_ptr<ICoreWebView2Deferral> deferral;
CHECK_FAILURE(args->GetDeferral(&deferral));
auto initCallback = [args, deferral, this]()
{
m_monitorWebview = m_monitorAppWindow->GetWebView();
CHECK_FAILURE(args->put_NewWindow(m_monitorAppWindow->GetWebView()));
CHECK_FAILURE(args->put_Handled(TRUE));
CHECK_FAILURE(m_monitorWebview->add_WebMessageReceived(
Microsoft::WRL::Callback<ICoreWebView2WebMessageReceivedEventHandler>(
[this](
ICoreWebView2* sender,
ICoreWebView2WebMessageReceivedEventArgs* args)
{
WebViewMessageReceived(args);
return S_OK;
})
.Get(),
&m_webMessageReceivedToken));
CHECK_FAILURE(deferral->Complete());
};
// passing "none" as uri as its a noinitialnavigation
m_monitorAppWindow = new AppWindow(
IDM_CREATION_MODE_WINDOWED, appWindow->GetWebViewOption(), L"none",
appWindow->GetUserDataFolder(), false /* isMainWindow */,
initCallback /* webviewCreatedCallback */, true /* customWindowRect */,
{100, 100, 1720, 1360}, false /* shouldHaveToolbar */, true /* isPopup */);
m_monitorAppWindow->SetOnAppWindowClosing(
[&] { m_appWindow->DeleteComponent(this); });
return S_OK;
})
.Get(),
nullptr));
#pragma endregion init_monitor
#pragma region init_target
// Turn off this scenario if we navigate away from the sample page
CHECK_FAILURE(m_webview->add_ContentLoading(
Callback<ICoreWebView2ContentLoadingEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2ContentLoadingEventArgs* args) -> HRESULT
{
wil::unique_cotaskmem_string uri;
sender->get_Source(&uri);
if (uri.get() != m_targetUri)
{
m_appWindow->DeleteComponent(this);
}
return S_OK;
})
.Get(),
&m_contentLoadingToken));
// While actual WebView creation in this sample app is outside the scope of
// this component, calling these functions here is equivalent to calling
// them upon WebView creation for the purposes of this feature. In a real
// application, you would usually call these after WebView is created and
// before the first navigation.
OnWebViewCreated();
SetupIsolatedFramesHandler();
m_targetUri = m_appWindow->GetLocalUri(std::wstring(L"ScenarioThrottlingControl.html"));
m_webview->Navigate(m_targetUri.c_str());
#pragma endregion init_target
}
// App is responsible for calling this function. An app would usually call this
// function from within the callback passed to
// CreateCoreWebView2Controller(WithOptions).
void ScenarioThrottlingControl::OnWebViewCreated()
{
wil::com_ptr<ICoreWebView2Settings> settings;
m_webview->get_Settings(&settings);
auto settings9 = settings.try_query<ICoreWebView2ExperimentalSettings9>();
// Store the default values from the WebView2 Runtime so we can restore them
// later.
CHECK_FAILURE(settings9->get_PreferredForegroundTimerWakeIntervalInMilliseconds(
&m_defaultIntervalForeground));
CHECK_FAILURE(settings9->get_PreferredBackgroundTimerWakeIntervalInMilliseconds(
&m_defaultIntervalBackground));
CHECK_FAILURE(settings9->get_PreferredIntensiveTimerWakeIntervalInMilliseconds(
&m_defaultIntervalIntensive));
CHECK_FAILURE(settings9->get_PreferredOverrideTimerWakeIntervalInMilliseconds(
&m_defaultIntervalOverride));
}
// The primary use-case here is an app embedding 3rd party content and wanting
// to be able to independently limit the performance impact of it. Generally,
// that's something like "low battery, throttle more" or "giving the frame N
// seconds to run some logic, throttle less".
void ScenarioThrottlingControl::SetupIsolatedFramesHandler()
{
auto webview4 = m_webview.try_query<ICoreWebView2_4>();
CHECK_FEATURE_RETURN_EMPTY(webview4);
// You can use the frame properties to determine whether it should be
// marked to be throttled separately from main frame.
CHECK_FAILURE(webview4->add_FrameCreated(
Callback<ICoreWebView2FrameCreatedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2FrameCreatedEventArgs* args) -> HRESULT
{
wil::com_ptr<ICoreWebView2Frame> webviewFrame;
CHECK_FAILURE(args->get_Frame(&webviewFrame));
auto webviewExperimentalFrame7 =
webviewFrame.try_query<ICoreWebView2ExperimentalFrame7>();
CHECK_FEATURE_RETURN_HRESULT(webviewExperimentalFrame7);
wil::unique_cotaskmem_string name;
CHECK_FAILURE(webviewFrame->get_Name(&name));
if (wcscmp(name.get(), L"untrusted") == 0)
{
CHECK_FAILURE(
webviewExperimentalFrame7->put_UseOverrideTimerWakeInterval(TRUE));
}
return S_OK;
})
.Get(),
&m_frameCreatedToken));
wil::com_ptr<ICoreWebView2Settings> settings;
m_webview->get_Settings(&settings);
auto settings9 = settings.try_query<ICoreWebView2ExperimentalSettings9>();
// Restrict frames selected by the above callback to always match the default
// timer interval for background frames.
CHECK_FAILURE(settings9->put_PreferredOverrideTimerWakeIntervalInMilliseconds(
m_defaultIntervalBackground));
}
void ScenarioThrottlingControl::WebViewMessageReceived(
ICoreWebView2WebMessageReceivedEventArgs* args)
{
// received command from monitor, handle
wil::unique_cotaskmem_string json;
CHECK_FAILURE(args->get_WebMessageAsJson(&json));
auto command = GetJSONStringField(json.get(), L"command");
if (command.compare(L"set-interval") == 0)
{
auto category = GetJSONStringField(json.get(), L"priority");
auto interval = GetJSONStringField(json.get(), L"intervalMs");
wil::com_ptr<ICoreWebView2Settings> settings;
m_webview->get_Settings(&settings);
auto settings9 = settings.try_query<ICoreWebView2ExperimentalSettings9>();
CHECK_FEATURE_RETURN_EMPTY(settings9);
if (category.compare(L"foreground") == 0)
{
CHECK_FAILURE(settings9->put_PreferredForegroundTimerWakeIntervalInMilliseconds(
std::stoul(interval)));
}
else if (category.compare(L"background") == 0)
{
CHECK_FAILURE(settings9->put_PreferredBackgroundTimerWakeIntervalInMilliseconds(
std::stoul(interval)));
}
else if (category.compare(L"untrusted") == 0)
{
CHECK_FAILURE(settings9->put_PreferredOverrideTimerWakeIntervalInMilliseconds(
std::stoul(interval)));
}
}
else if (command.compare(L"toggle-visibility") == 0)
{
BOOL visible;
m_appWindow->GetWebViewController()->get_IsVisible(&visible);
m_appWindow->GetWebViewController()->put_IsVisible(!visible);
}
else if (command.compare(L"scenario") == 0)
{
auto label = GetJSONStringField(json.get(), L"label");
if (label.compare(L"interaction-throttle") == 0)
{
OnNoUserInteraction();
}
else if (label.compare(L"interaction-reset") == 0)
{
OnUserInteraction();
}
else if (label.compare(L"hidden-unthrottle") == 0)
{
HideWebView();
}
else if (label.compare(L"hidden-reset") == 0)
{
ShowWebView();
}
}
}
// This sample app calls this method when receiving a simulated event from its
// control monitor, but your app can decide how and when to go into this state.
void ScenarioThrottlingControl::OnNoUserInteraction()
{
wil::com_ptr<ICoreWebView2Settings> settings;
m_webview->get_Settings(&settings);
auto settings9 = settings.try_query<ICoreWebView2ExperimentalSettings9>();
CHECK_FEATURE_RETURN_EMPTY(settings9);
// User is not interactive, keep webview visible but throttle foreground
// timers to 500ms.
CHECK_FAILURE(settings9->put_PreferredForegroundTimerWakeIntervalInMilliseconds(500));
}
void ScenarioThrottlingControl::OnUserInteraction()
{
wil::com_ptr<ICoreWebView2Settings> settings;
m_webview->get_Settings(&settings);
auto settings9 = settings.try_query<ICoreWebView2ExperimentalSettings9>();
CHECK_FEATURE_RETURN_EMPTY(settings9);
// User is interactive again, set foreground timer interval back to its
// default value.
CHECK_FAILURE(settings9->put_PreferredForegroundTimerWakeIntervalInMilliseconds(
m_defaultIntervalForeground));
}
// Prepares the WebView to go into hidden mode with no background timer
// throttling.
void ScenarioThrottlingControl::HideWebView()
{
wil::com_ptr<ICoreWebView2Settings> settings;
m_webview->get_Settings(&settings);
auto settings9 = settings.try_query<ICoreWebView2ExperimentalSettings9>();
CHECK_FEATURE_RETURN_EMPTY(settings9);
// This WebView2 will remain hidden but needs to keep running timers.
// Unthrottle background timers.
CHECK_FAILURE(settings9->put_PreferredBackgroundTimerWakeIntervalInMilliseconds(0));
// Effectively disable intensive throttling by overriding its timer interval.
CHECK_FAILURE(settings9->put_PreferredIntensiveTimerWakeIntervalInMilliseconds(0));
CHECK_FAILURE(m_appWindow->GetWebViewController()->put_IsVisible(FALSE));
}
// Shows the WebView and restores default throttling behavior.
void ScenarioThrottlingControl::ShowWebView()
{
CHECK_FAILURE(m_appWindow->GetWebViewController()->put_IsVisible(TRUE));
wil::com_ptr<ICoreWebView2Settings> settings;
m_webview->get_Settings(&settings);
auto settings9 = settings.try_query<ICoreWebView2ExperimentalSettings9>();
CHECK_FEATURE_RETURN_EMPTY(settings9);
CHECK_FAILURE(settings9->put_PreferredBackgroundTimerWakeIntervalInMilliseconds(
m_defaultIntervalBackground));
CHECK_FAILURE(settings9->put_PreferredIntensiveTimerWakeIntervalInMilliseconds(
m_defaultIntervalIntensive));
}
ScenarioThrottlingControl::~ScenarioThrottlingControl()
{
if (m_monitorAppWindow)
{
m_monitorAppWindow->SetOnAppWindowClosing(nullptr);
::PostMessage(m_monitorAppWindow->GetMainWindow(), WM_CLOSE, NULL, NULL);
m_monitorAppWindow = nullptr;
}
}