Repository navigation
Expand file tree
/
Copy pathApp.cpp
More file actions
298 lines (272 loc) · 10.9 KB
/
Copy pathApp.cpp
File metadata and controls
298 lines (272 loc) · 10.9 KB
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
292
293
294
295
296
297
298
// 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 "App.h"
#include <map>
#include <shellapi.h>
#include <shellscalingapi.h>
#include <shobjidl.h>
#include <string.h>
#include <vector>
#include "AppWindow.h"
#include "CheckFailure.h"
#include "DpiUtil.h"
#include "ScenarioClusterEnvironment.h"
HINSTANCE g_hInstance;
int g_nCmdShow;
bool g_autoTabHandle = true;
static std::map<DWORD, HANDLE> s_threads;
static int RunMessagePump();
static DWORD WINAPI ThreadProc(void* pvParam);
static void WaitForOtherThreads();
#define NEXT_PARAM_CONTAINS(command) \
_wcsnicmp(nextParam.c_str(), command, ARRAYSIZE(command) - 1) == 0
int APIENTRY
wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow)
{
g_hInstance = hInstance;
UNREFERENCED_PARAMETER(hPrevInstance);
g_nCmdShow = nCmdShow;
// Default DPI awareness to PerMonitorV2. The commandline parameters can
// override this.
DPI_AWARENESS_CONTEXT dpiAwarenessContext = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2;
std::wstring appId(L"EBWebView.SampleApp");
std::wstring userDataFolder(L"");
std::wstring initialUri;
DWORD creationModeId = IDM_CREATION_MODE_WINDOWED;
WebViewCreateOption opt;
// When launched with --clustername=..., this instance is a secondary host
// process that should join an existing shared cluster environment instead
// of creating a normal (private) environment.
bool joinCluster = false;
ClusterEnvironmentSpec clusterSpec;
if (lpCmdLine && lpCmdLine[0])
{
int paramCount = 0;
LPWSTR* params = CommandLineToArgvW(GetCommandLineW(), ¶mCount);
for (int i = 0; i < paramCount; ++i)
{
std::wstring nextParam;
if (params[i][0] == L'-')
{
if (params[i][1] == L'-')
{
nextParam.assign(params[i] + 2);
}
else
{
nextParam.assign(params[i] + 1);
}
}
if (NEXT_PARAM_CONTAINS(L"dpiunaware"))
{
dpiAwarenessContext = DPI_AWARENESS_CONTEXT_UNAWARE;
}
else if (NEXT_PARAM_CONTAINS(L"dpisystemaware"))
{
dpiAwarenessContext = DPI_AWARENESS_CONTEXT_SYSTEM_AWARE;
}
else if (NEXT_PARAM_CONTAINS(L"dpipermonitorawarev2"))
{
dpiAwarenessContext = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2;
}
else if (NEXT_PARAM_CONTAINS(L"dpipermonitoraware"))
{
dpiAwarenessContext = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE;
}
else if (NEXT_PARAM_CONTAINS(L"noinitialnavigation"))
{
initialUri = L"none";
}
else if (NEXT_PARAM_CONTAINS(L"appid="))
{
appId = nextParam.substr(nextParam.find(L'=') + 1);
}
else if (NEXT_PARAM_CONTAINS(L"initialUri="))
{
initialUri = nextParam.substr(nextParam.find(L'=') + 1);
}
else if (NEXT_PARAM_CONTAINS(L"userdatafolder="))
{
userDataFolder = nextParam.substr(nextParam.find(L'=') + 1);
}
else if (NEXT_PARAM_CONTAINS(L"clustername="))
{
joinCluster = true;
clusterSpec.clusterName = nextParam.substr(nextParam.find(L'=') + 1);
}
else if (NEXT_PARAM_CONTAINS(L"clusterlang="))
{
clusterSpec.language = nextParam.substr(nextParam.find(L'=') + 1);
}
else if (NEXT_PARAM_CONTAINS(L"clusterargs="))
{
clusterSpec.additionalBrowserArguments =
nextParam.substr(nextParam.find(L'=') + 1);
}
else if (NEXT_PARAM_CONTAINS(L"clustersso="))
{
clusterSpec.allowSingleSignOn =
nextParam.substr(nextParam.find(L'=') + 1) == L"1";
}
else if (NEXT_PARAM_CONTAINS(L"clustertracking="))
{
clusterSpec.enableTrackingPrevention =
nextParam.substr(nextParam.find(L'=') + 1) == L"1";
}
else if (NEXT_PARAM_CONTAINS(L"clusterextensions="))
{
clusterSpec.areBrowserExtensionsEnabled =
nextParam.substr(nextParam.find(L'=') + 1) == L"1";
}
else if (NEXT_PARAM_CONTAINS(L"clusterisolation="))
{
clusterSpec.perHostProfileIsolation =
nextParam.substr(nextParam.find(L'=') + 1) == L"1";
}
else if (NEXT_PARAM_CONTAINS(L"clusterchannels="))
{
// wcstol rather than stoi: a malformed value should fall back to
// 0 (NONE) rather than throw out of command-line parsing. Masked
// because the value is cast to a flags enum, and a negative or
// oversized number would otherwise set every reserved bit.
const long channels =
wcstol(nextParam.substr(nextParam.find(L'=') + 1).c_str(), nullptr, 10);
clusterSpec.releaseChannels =
static_cast<COREWEBVIEW2_RELEASE_CHANNELS>(channels & kAllReleaseChannels);
}
else if (NEXT_PARAM_CONTAINS(L"clustersearchkind="))
{
// Parsed the same way the parent writes it, rather than by
// testing for L"1", so a value added to the enum later does not
// silently arrive as MOST_STABLE. Clamped for the same reason
// the channels are masked: this is cast to an enum.
const long searchKind =
wcstol(nextParam.substr(nextParam.find(L'=') + 1).c_str(), nullptr, 10);
clusterSpec.channelSearchKind =
searchKind == COREWEBVIEW2_CHANNEL_SEARCH_KIND_LEAST_STABLE
? COREWEBVIEW2_CHANNEL_SEARCH_KIND_LEAST_STABLE
: COREWEBVIEW2_CHANNEL_SEARCH_KIND_MOST_STABLE;
}
else if (NEXT_PARAM_CONTAINS(L"creationmode="))
{
nextParam = nextParam.substr(nextParam.find(L'=') + 1);
if (NEXT_PARAM_CONTAINS(L"windowed"))
{
creationModeId = IDM_CREATION_MODE_WINDOWED;
}
else if (NEXT_PARAM_CONTAINS(L"allowhostinput"))
{
creationModeId = IDM_CREATION_MODE_HOST_INPUT_PROCESSING;
}
else if (NEXT_PARAM_CONTAINS(L"visualdcomp"))
{
creationModeId = IDM_CREATION_MODE_VISUAL_DCOMP;
}
else if (NEXT_PARAM_CONTAINS(L"targetdcomp"))
{
creationModeId = IDM_CREATION_MODE_TARGET_DCOMP;
}
else if (NEXT_PARAM_CONTAINS(L"visualwincomp"))
{
creationModeId = IDM_CREATION_MODE_VISUAL_WINCOMP;
}
}
}
LocalFree(params);
}
SetCurrentProcessExplicitAppUserModelID(appId.c_str());
DpiUtil::SetProcessDpiAwarenessContext(dpiAwarenessContext);
if (joinCluster)
{
// COM is required before calling into the loader; AppWindow (created in
// the completion handler) will also initialize COM on this thread.
CHECK_FAILURE(OleInitialize(nullptr));
CreateOrJoinClusterAndOpenWindow(
clusterSpec, /*isMainWindow=*/true, /*parent=*/nullptr);
}
else
{
new AppWindow(creationModeId, opt, initialUri, userDataFolder, true);
}
int retVal = RunMessagePump();
WaitForOtherThreads();
return retVal;
}
// Run the message pump for one thread.
static int RunMessagePump()
{
HACCEL hAccelTable = LoadAccelerators(g_hInstance, MAKEINTRESOURCE(IDC_WEBVIEW2APISAMPLE));
MSG msg;
// Main message loop:
//! [MoveFocus0]
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
// Calling IsDialogMessage handles Tab traversal automatically. If the
// app wants the platform to auto handle tab, then call IsDialogMessage
// before calling TranslateMessage/DispatchMessage. If the app wants to
// handle tabbing itself, then skip calling IsDialogMessage and call
// TranslateMessage/DispatchMessage directly.
if (!g_autoTabHandle || !IsDialogMessage(GetAncestor(msg.hwnd, GA_ROOT), &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
//! [MoveFocus0]
DWORD threadId = GetCurrentThreadId();
auto it = s_threads.find(threadId);
if (it != s_threads.end())
{
CloseHandle(it->second);
s_threads.erase(threadId);
}
return (int)msg.wParam;
}
// Make a new thread.
void CreateNewThread(AppWindow* app)
{
DWORD threadId;
app->AddRef();
HANDLE thread = CreateThread(
nullptr, 0, ThreadProc, reinterpret_cast<LPVOID>(app),
STACK_SIZE_PARAM_IS_A_RESERVATION, &threadId);
s_threads.insert(std::pair<DWORD, HANDLE>(threadId, thread));
}
// This function is the starting point for new threads. It will open a new app window.
static DWORD WINAPI ThreadProc(void* pvParam)
{
AppWindow* app = static_cast<AppWindow*>(pvParam);
new AppWindow(app->GetCreationModeId(), app->GetWebViewOption());
app->Release();
return RunMessagePump();
}
// Called on the main thread. Wait for all other threads to complete before exiting.
static void WaitForOtherThreads()
{
while (!s_threads.empty())
{
std::vector<HANDLE> threadHandles;
for (auto it = s_threads.begin(); it != s_threads.end(); ++it)
{
threadHandles.push_back(it->second);
}
HANDLE* handleArray = threadHandles.data();
DWORD dwIndex = MsgWaitForMultipleObjects(
static_cast<DWORD>(threadHandles.size()), threadHandles.data(), FALSE, INFINITE,
QS_ALLEVENTS);
if (dwIndex == WAIT_OBJECT_0 + threadHandles.size())
{
MSG msg;
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}