1+ // Copyright (C) Microsoft Corporation. All rights reserved.
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ #include " stdafx.h"
6+
7+ #include < algorithm>
8+ #include < pathcch.h>
9+ #include < Psapi.h>
10+
11+ #include " AppStartPage.h"
12+ #include " AppWindow.h"
13+ #include " CheckFailure.h"
14+
15+ using namespace Microsoft ::WRL;
16+
17+ namespace AppStartPage
18+ {
19+
20+ bool AreFileUrisEqual (std::wstring leftUri, std::wstring rightUri)
21+ {
22+ // Have to to lower due to current bug
23+ std::transform (leftUri.begin (), leftUri.end (),
24+ leftUri.begin (), ::tolower);
25+ std::transform (rightUri.begin (), rightUri.end (),
26+ rightUri.begin (), ::tolower);
27+
28+ return leftUri == rightUri;
29+ }
30+
31+ std::wstring ResolvePathAndTrimFile (std::wstring path)
32+ {
33+ wchar_t resultPath[MAX_PATH];
34+ PathCchCanonicalize (resultPath, ARRAYSIZE (resultPath), path.c_str ());
35+ PathCchRemoveFileSpec (resultPath, ARRAYSIZE (resultPath));
36+ return resultPath;
37+ }
38+
39+ std::wstring GetSdkBuild ()
40+ {
41+ auto options = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();
42+ wil::unique_cotaskmem_string targetVersion;
43+ CHECK_FAILURE (options->get_TargetCompatibleBrowserVersion (&targetVersion));
44+
45+ // The full version string A.B.C.D
46+ const wchar_t * targetVersionMajorAndRest = targetVersion.get ();
47+ // Should now be .B.C.D
48+ const wchar_t * targetVersionMinorAndRest = wcschr (targetVersionMajorAndRest, L' .' );
49+ CHECK_FAILURE ((targetVersionMinorAndRest != nullptr && *targetVersionMinorAndRest == L' .' ) ? S_OK : E_UNEXPECTED);
50+
51+ // Should now be .C.D
52+ const wchar_t * targetVersionBuildAndRest = wcschr (targetVersionMinorAndRest + 1 , L' .' );
53+ CHECK_FAILURE ((targetVersionBuildAndRest != nullptr && *targetVersionBuildAndRest == L' .' ) ? S_OK : E_UNEXPECTED);
54+
55+ // Return + 1 to skip the first . so just C.D
56+ return targetVersionBuildAndRest + 1 ;
57+ }
58+
59+ std::wstring GetRuntimeVersion (AppWindow* appWindow)
60+ {
61+ wil::com_ptr<ICoreWebView2Environment> environment = appWindow->GetWebViewEnvironment ();
62+ wil::unique_cotaskmem_string runtimeVersion;
63+ CHECK_FAILURE (environment->get_BrowserVersionString (&runtimeVersion));
64+
65+ return runtimeVersion.get ();
66+ }
67+
68+ std::wstring GetAppPath ()
69+ {
70+ wchar_t appPath[MAX_PATH];
71+ GetModuleFileName (nullptr , appPath, ARRAYSIZE (appPath));
72+ return ResolvePathAndTrimFile (appPath);
73+ }
74+
75+ std::wstring GetRuntimePath (AppWindow* appWindow)
76+ {
77+ wil::com_ptr<ICoreWebView2> webview = appWindow->GetWebView ();
78+ UINT32 browserProcessId = 0 ;
79+ wchar_t runtimePath[MAX_PATH];
80+ CHECK_FAILURE (webview->get_BrowserProcessId (&browserProcessId));
81+
82+ HANDLE processHandle = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE , browserProcessId);
83+ CHECK_FAILURE (processHandle == nullptr ? E_FAIL : S_OK);
84+ GetModuleFileNameEx (processHandle, nullptr , runtimePath, ARRAYSIZE (runtimePath));
85+ CloseHandle (processHandle);
86+
87+ return ResolvePathAndTrimFile (runtimePath);
88+ }
89+
90+ std::wstring GetUri (AppWindow* appWindow)
91+ {
92+ std::wstring uri = appWindow->GetLocalUri (L" AppStartPage.html" );
93+
94+ uri += L" ?sdkBuild=" ;
95+ uri += GetSdkBuild ();
96+
97+ uri += L" &runtimeVersion=" ;
98+ uri += GetRuntimeVersion (appWindow);
99+
100+ uri += L" &appPath=" ;
101+ uri += GetAppPath ();
102+
103+ uri += L" &runtimePath=" ;
104+ uri += GetRuntimePath (appWindow);
105+
106+ return uri;
107+ }
108+
109+ }; // namespace AppStartPage
0 commit comments