forked from MicrosoftEdge/WebView2Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppStartPage.cpp
108 lines (83 loc) · 3.27 KB
/
AppStartPage.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
// 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 <pathcch.h>
#include <psapi.h>
#include "AppStartPage.h"
#include "AppWindow.h"
#include "CheckFailure.h"
using namespace Microsoft::WRL;
namespace AppStartPage
{
bool AreFileUrisEqual(std::wstring leftUri, std::wstring rightUri)
{
// Have to to lower due to current bug
std::transform(leftUri.begin(), leftUri.end(),
leftUri.begin(), ::tolower);
std::transform(rightUri.begin(), rightUri.end(),
rightUri.begin(), ::tolower);
return leftUri == rightUri;
}
std::wstring ResolvePathAndTrimFile(std::wstring path)
{
wchar_t resultPath[MAX_PATH];
PathCchCanonicalize(resultPath, ARRAYSIZE(resultPath), path.c_str());
PathCchRemoveFileSpec(resultPath, ARRAYSIZE(resultPath));
return resultPath;
}
std::wstring GetSdkBuild()
{
auto options = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();
wil::unique_cotaskmem_string targetVersion;
CHECK_FAILURE(options->get_TargetCompatibleBrowserVersion(&targetVersion));
// The full version string A.B.C.D
const wchar_t* targetVersionMajorAndRest = targetVersion.get();
// Should now be .B.C.D
const wchar_t* targetVersionMinorAndRest = wcschr(targetVersionMajorAndRest, L'.');
CHECK_FAILURE((targetVersionMinorAndRest != nullptr && *targetVersionMinorAndRest == L'.') ? S_OK : E_UNEXPECTED);
// Should now be .C.D
const wchar_t* targetVersionBuildAndRest = wcschr(targetVersionMinorAndRest + 1, L'.');
CHECK_FAILURE((targetVersionBuildAndRest != nullptr && *targetVersionBuildAndRest == L'.') ? S_OK : E_UNEXPECTED);
// Return + 1 to skip the first . so just C.D
return targetVersionBuildAndRest + 1;
}
std::wstring GetRuntimeVersion(AppWindow* appWindow)
{
wil::com_ptr<ICoreWebView2Environment> environment = appWindow->GetWebViewEnvironment();
wil::unique_cotaskmem_string runtimeVersion;
CHECK_FAILURE(environment->get_BrowserVersionString(&runtimeVersion));
return runtimeVersion.get();
}
std::wstring GetAppPath()
{
wchar_t appPath[MAX_PATH];
GetModuleFileName(nullptr, appPath, ARRAYSIZE(appPath));
return ResolvePathAndTrimFile(appPath);
}
std::wstring GetRuntimePath(AppWindow* appWindow)
{
wil::com_ptr<ICoreWebView2> webview = appWindow->GetWebView();
UINT32 browserProcessId = 0;
wchar_t runtimePath[MAX_PATH];
CHECK_FAILURE(webview->get_BrowserProcessId(&browserProcessId));
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, browserProcessId);
CHECK_FAILURE(processHandle == nullptr ? E_FAIL : S_OK);
GetModuleFileNameEx(processHandle, nullptr, runtimePath, ARRAYSIZE(runtimePath));
CloseHandle(processHandle);
return ResolvePathAndTrimFile(runtimePath);
}
std::wstring GetUri(AppWindow* appWindow)
{
std::wstring uri = appWindow->GetLocalUri(L"AppStartPage.html", true);
uri += L"?sdkBuild=";
uri += GetSdkBuild();
uri += L"&runtimeVersion=";
uri += GetRuntimeVersion(appWindow);
uri += L"&appPath=";
uri += GetAppPath();
uri += L"&runtimePath=";
uri += GetRuntimePath(appWindow);
return uri;
}
}; // namespace AppStartPage