Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(497)

Side by Side Diff: chrome/install_static/product_install_details.cc

Issue 2422643002: Windows install_static refactor. (Closed)
Patch Set: maybe fix nacl64 Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. 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 "chrome/install_static/product_install_details.h"
6
7 #include <windows.h>
8 #include <assert.h>
9
10 #include <algorithm>
11
12 #include "chrome/install_static/install_modes.h"
13 #include "chrome/install_static/install_util.h"
14 #include "chrome_elf/nt_registry/nt_registry.h"
15
16 namespace install_static {
17
18 namespace {
19
20 // Returns the executable path for the current process.
21 std::wstring GetCurrentProcessExePath() {
22 std::wstring exe_path(MAX_PATH, L'\0');
23 DWORD length = ::GetModuleFileName(nullptr, &exe_path[0], exe_path.size());
24 if (!length)
25 return std::wstring();
26 exe_path.resize(length);
27 return exe_path;
28 }
29
30 const InstallConstants* FindInstallMode(const std::wstring& suffix) {
31 // Search for a mode with the matching suffix.
32 for (int i = 0; i < NUM_INSTALL_MODES; ++i) {
33 const InstallConstants& mode = kInstallModes[i];
34 if (!_wcsicmp(suffix.c_str(), mode.install_suffix))
35 return &mode;
36 }
37 // The first mode is always the default if all else fails.
38 return &kInstallModes[0];
39 }
40
41 } // namespace
42
43 void InitializeModuleProductDetails() {
44 InstallDetails::SetForProcess(MakeProductDetails(GetCurrentProcessExePath()));
45 }
46
47 bool IsPathParentOf(const wchar_t* parent,
48 size_t parent_len,
49 const std::wstring& path) {
50 // Ignore all terminating path separators in |parent|.
51 while (parent_len && parent[parent_len - 1] == L'\\')
52 --parent_len;
53 // Pass if the parent was all separators.
54 if (!parent_len)
55 return false;
56 // This is a match if |parent| exactly matches |path| or is followed by a
57 // separator.
58 return !::wcsnicmp(path.c_str(), parent, parent_len) &&
59 (path.size() == parent_len || path[parent_len] == L'\\');
60 }
61
62 bool PathIsInProgramFiles(const std::wstring& path) {
63 static constexpr const wchar_t* kProgramFilesVariables[] = {
64 L"PROGRAMFILES", // With or without " (x86)" according to exe bitness.
65 L"PROGRAMFILES(X86)", // Always "C:\Program Files (x86)"
66 L"PROGRAMW6432", // Always "C:\Program Files" under WoW64.
67 };
68 wchar_t value[MAX_PATH];
robertshield 2016/10/17 05:27:23 = {} (though it's not really needed here, but to g
grt (UTC plus 2) 2016/10/24 11:17:51 added *value = L'\0'; on the next line so that i
robertshield 2016/10/24 14:59:26 Probably not? GetEnvironmentVariable is documente
grt (UTC plus 2) 2016/10/24 19:36:00 This code uses the length provided by GEV rather t
69 for (const wchar_t* variable : kProgramFilesVariables) {
70 *value = L'\0';
71 DWORD ret = ::GetEnvironmentVariableW(variable, value, _countof(value));
72 if (ret && ret < _countof(value) && IsPathParentOf(value, ret, path))
73 return true;
74 }
75
76 return false;
77 }
78
79 std::wstring GetInstallSuffix(const std::wstring& exe_path) {
80 // Search backwards from the end of the path for "\Application", using a
81 // manual search for the sake of case-insensitivity.
82 static constexpr wchar_t kInstallBinaryDir[] = L"\\Application";
83 constexpr size_t kInstallBinaryDirLength = _countof(kInstallBinaryDir) - 1;
84 if (exe_path.size() < kProductPathNameLength + kInstallBinaryDirLength)
85 return std::wstring();
86 std::wstring::const_reverse_iterator scan =
87 exe_path.crbegin() + (kInstallBinaryDirLength - 1);
88 while (_wcsnicmp(&*scan, kInstallBinaryDir, kInstallBinaryDirLength) &&
89 ++scan != exe_path.crend()) {
90 }
91 if (scan == exe_path.crend())
92 return std::wstring();
93
94 // Ensure that the dir is followed by a separator or is at the end of the
95 // path.
96 if (scan - exe_path.crbegin() != kInstallBinaryDirLength - 1 &&
97 *(scan - kInstallBinaryDirLength) != L'\\') {
98 return std::wstring();
99 }
100
101 // Scan backwards to the next separator or the beginning of the path.
102 std::wstring::const_reverse_iterator name =
103 std::find(scan + 1, exe_path.crend(), L'\\');
104 // Back up one character to ignore the separator/end of iteration.
105 if (name == exe_path.crend())
106 name = exe_path.crbegin() + exe_path.size() - 1;
107 else
108 --name;
109
110 // Check for a match of the product directory name.
111 if (_wcsnicmp(&*name, kProductPathName, kProductPathNameLength))
112 return std::wstring();
113
114 // Return the (possibly empty) suffix betwixt the product name and install
115 // binary dir.
116 return std::wstring(&*(name - kProductPathNameLength),
117 (name - scan) - kProductPathNameLength);
118 }
119
120 bool IsMultiInstall(const InstallConstants& mode, bool system_level) {
121 assert(mode.supports_multi_install);
122 std::wstring args;
123 return nt::QueryRegValueSZ(system_level ? nt::HKLM : nt::HKCU, nt::WOW6432,
124 GetClientStateKeyPath(mode.app_guid).c_str(),
125 L"UninstallArguments", &args) &&
126 args.find(L"--multi-install") != std::wstring::npos;
127 }
128
129 std::unique_ptr<PrimaryInstallDetails> MakeProductDetails(
130 const std::wstring& exe_path) {
131 std::unique_ptr<PrimaryInstallDetails> details(new PrimaryInstallDetails());
132
133 const InstallConstants* mode = FindInstallMode(GetInstallSuffix(exe_path));
134 const bool system_level =
135 mode->supports_system_level && PathIsInProgramFiles(exe_path);
136 const bool multi_install =
137 mode->supports_multi_install && IsMultiInstall(*mode, system_level);
138
139 details->set_mode(mode);
140 details->set_system_level(system_level);
141 details->set_multi_install(multi_install);
142 details->set_channel(DetermineChannel(*mode, system_level, multi_install));
143
144 return details;
145 }
146
147 } // namespace install_static
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698