-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClientConfig.cpp
More file actions
218 lines (184 loc) · 7.97 KB
/
Copy pathClientConfig.cpp
File metadata and controls
218 lines (184 loc) · 7.97 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
#include "ClientConfig.h"
#include "util/PathUtils.h"
#include <windows.h>
namespace {
constexpr wchar_t kDefaultParentProcessName[] = L"explorer.exe";
constexpr wchar_t kStartUrl[] = L"http://127.0.0.1:9001/index.html";
// The native host owns poll timing and uses WebView2 ExecuteScript to invoke the page bridge entrypoint.
constexpr wchar_t kPollScript[] = LR"JS((async()=>await window.hostBridge.pollOnce())())JS";
constexpr wchar_t kProfileDirName[] = L"wv2_profile";
constexpr wchar_t kDefaultLogFileName[] = L"debug.log";
constexpr wchar_t kDefaultBrowserProfileName[] = L"Default";
constexpr wchar_t kChromeUserDataRelativePath[] = L"Google\\Chrome\\User Data";
constexpr wchar_t kEdgeUserDataRelativePath[] = L"Microsoft\\Edge\\User Data";
// Runtime overrides for client logging. Empty directory means "fall back to executable directory".
std::wstring g_logDirectory;
std::wstring g_logFileName = kDefaultLogFileName;
// The native app and WebLinkLib share a single verbosity threshold.
WebViewBridgeLogVerbosity g_logVerbosity = WebViewBridgeLogVerbosity::Debug;
// Returns true only when the path exists and points at a directory.
bool DirectoryExists(const std::wstring& path) {
if (path.empty()) {
return false;
}
const DWORD attrs = GetFileAttributesW(path.c_str());
return attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
// Reads LOCALAPPDATA for browser profile discovery.
std::wstring GetLocalAppDataDirectory() {
const DWORD required = GetEnvironmentVariableW(L"LOCALAPPDATA", nullptr, 0);
if (required == 0) {
return L"";
}
std::wstring value(required - 1, L'\0');
GetEnvironmentVariableW(L"LOCALAPPDATA", value.data(), required);
return value;
}
// Chooses the default profile name for each hosting mode.
std::wstring DefaultProfileNameForHost(ClientConfig::ProfileHostKind host) {
switch (host) {
case ClientConfig::ProfileHostKind::Chrome:
case ClientConfig::ProfileHostKind::MsEdge:
return kDefaultBrowserProfileName;
case ClientConfig::ProfileHostKind::Direct:
default:
return kProfileDirName;
}
}
// Picks the base directory for direct-hosted WebView2 profiles.
std::wstring DirectProfileBaseDirectory(const ClientConfig::ProfileOptions& options) {
if (!options.location.empty()) {
return options.location;
}
return GetExecutableDirectory();
}
// Resolves the browser user-data root for browser-hosted profile sharing.
std::wstring BrowserUserDataFolder(ClientConfig::ProfileHostKind host) {
const std::wstring localAppData = GetLocalAppDataDirectory();
if (localAppData.empty()) {
return L"";
}
switch (host) {
case ClientConfig::ProfileHostKind::Chrome:
return JoinWindowsPath(localAppData, kChromeUserDataRelativePath);
case ClientConfig::ProfileHostKind::MsEdge:
return JoinWindowsPath(localAppData, kEdgeUserDataRelativePath);
case ClientConfig::ProfileHostKind::Direct:
default:
return L"";
}
}
// Preserves the historical default log location beside the executable.
std::wstring DefaultLogDirectory() {
return GetExecutableDirectory();
}
}
namespace ClientConfig {
// Returns the default parent process name used during relocation bootstrap.
std::wstring ParentProcessName() {
return kDefaultParentProcessName;
}
// Returns the initial page URL loaded into the hidden WebView.
std::wstring StartUrl() {
return kStartUrl;
}
// Returns the script native code executes on each poll heartbeat.
std::wstring PollScript() {
return kPollScript;
}
// Resolves the final WebView2 profile location, including browser-hosted fallbacks.
ResolvedProfile ResolveProfile(const ProfileOptions& options) {
ResolvedProfile resolved{};
resolved.requestedHost = options.host;
if (options.host == ProfileHostKind::Chrome || options.host == ProfileHostKind::MsEdge) {
const std::wstring browserUserDataFolder = BrowserUserDataFolder(options.host);
if (DirectoryExists(browserUserDataFolder)) {
resolved.resolvedHost = options.host;
resolved.userDataFolder = browserUserDataFolder;
resolved.profileName = options.profileName.empty()
? DefaultProfileNameForHost(options.host)
: options.profileName;
resolved.profilePath = JoinWindowsPath(resolved.userDataFolder, resolved.profileName);
return resolved;
}
resolved.usedFallback = true;
}
resolved.resolvedHost = ProfileHostKind::Direct;
resolved.profileName = options.profileName.empty()
? DefaultProfileNameForHost(ProfileHostKind::Direct)
: options.profileName;
resolved.userDataFolder = JoinWindowsPath(
DirectProfileBaseDirectory(options),
resolved.profileName);
resolved.profilePath = resolved.userDataFolder;
return resolved;
}
// Resolves a direct-hosted profile under the requested location or executable directory.
ResolvedProfile ResolveDirectProfile(const std::wstring& profileName, const std::wstring& location) {
return ResolveProfile(ProfileOptions{
ProfileHostKind::Direct,
profileName,
location
});
}
// Resolves a Chrome-hosted profile when the Chrome user-data root exists.
ResolvedProfile ResolveChromeProfile(const std::wstring& profileName) {
return ResolveProfile(ProfileOptions{
ProfileHostKind::Chrome,
profileName,
L""
});
}
// Resolves an Edge-hosted profile when the Edge user-data root exists.
ResolvedProfile ResolveMsEdgeProfile(const std::wstring& profileName) {
return ResolveProfile(ProfileOptions{
ProfileHostKind::MsEdge,
profileName,
L""
});
}
// Convenience wrapper for Chrome's default profile name.
ResolvedProfile ResolveChromeDefaultProfile() {
return ResolveChromeProfile();
}
// Convenience wrapper for Edge's default profile name.
ResolvedProfile ResolveMsEdgeDefaultProfile() {
return ResolveMsEdgeProfile();
}
// Returns only the final profile path from ResolveProfile(...).
std::wstring ProfilePath(const ProfileOptions& options) {
return ResolveProfile(options).profilePath;
}
// Returns the configured log directory or the default executable directory.
std::wstring LogDirectory() {
if (!g_logDirectory.empty()) {
return g_logDirectory;
}
// Preserve the historical "next to the executable" default until the caller overrides it.
return DefaultLogDirectory();
}
// Overrides the directory used by LogPath().
void SetLogDirectory(const std::wstring& directory) {
g_logDirectory = directory;
}
// Returns the current log file name.
std::wstring LogFileName() {
return g_logFileName;
}
// Overrides the log file name, restoring the default when given an empty string.
void SetLogFileName(const std::wstring& fileName) {
g_logFileName = fileName.empty() ? kDefaultLogFileName : fileName;
}
// Builds the full native client log path.
std::wstring LogPath() {
return JoinWindowsPath(LogDirectory(), LogFileName());
}
// Returns the shared verbosity threshold used by the client app and bridge library.
WebViewBridgeLogVerbosity LogVerbosity() {
return g_logVerbosity;
}
// Updates the shared verbosity threshold used by the client app and bridge library.
void SetLogVerbosity(WebViewBridgeLogVerbosity level) {
g_logVerbosity = level;
}
}