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

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

Issue 2491463002: Revert of Windows install_static refactor. (Closed)
Patch Set: Created 4 years, 1 month 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 InitializeProductDetailsForPrimaryModule() {
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];
69 *value = L'\0';
70 for (const wchar_t* variable : kProgramFilesVariables) {
71 *value = L'\0';
72 DWORD ret = ::GetEnvironmentVariableW(variable, value, _countof(value));
73 if (ret && ret < _countof(value) && IsPathParentOf(value, ret, path))
74 return true;
75 }
76
77 return false;
78 }
79
80 std::wstring GetInstallSuffix(const std::wstring& exe_path) {
81 // Search backwards from the end of the path for "\Application", using a
82 // manual search for the sake of case-insensitivity.
83 static constexpr wchar_t kInstallBinaryDir[] = L"\\Application";
84 constexpr size_t kInstallBinaryDirLength = _countof(kInstallBinaryDir) - 1;
85 if (exe_path.size() < kProductPathNameLength + kInstallBinaryDirLength)
86 return std::wstring();
87 std::wstring::const_reverse_iterator scan =
88 exe_path.crbegin() + (kInstallBinaryDirLength - 1);
89 while (_wcsnicmp(&*scan, kInstallBinaryDir, kInstallBinaryDirLength) &&
90 ++scan != exe_path.crend()) {
91 }
92 if (scan == exe_path.crend())
93 return std::wstring();
94
95 // Ensure that the dir is followed by a separator or is at the end of the
96 // path.
97 if (scan - exe_path.crbegin() != kInstallBinaryDirLength - 1 &&
98 *(scan - kInstallBinaryDirLength) != L'\\') {
99 return std::wstring();
100 }
101
102 // Scan backwards to the next separator or the beginning of the path.
103 std::wstring::const_reverse_iterator name =
104 std::find(scan + 1, exe_path.crend(), L'\\');
105 // Back up one character to ignore the separator/end of iteration.
106 if (name == exe_path.crend())
107 name = exe_path.crbegin() + exe_path.size() - 1;
108 else
109 --name;
110
111 // Check for a match of the product directory name.
112 if (_wcsnicmp(&*name, kProductPathName, kProductPathNameLength))
113 return std::wstring();
114
115 // Return the (possibly empty) suffix betwixt the product name and install
116 // binary dir.
117 return std::wstring(&*(name - kProductPathNameLength),
118 (name - scan) - kProductPathNameLength);
119 }
120
121 bool IsMultiInstall(const InstallConstants& mode, bool system_level) {
122 assert(mode.supports_multi_install);
123 std::wstring args;
124 return nt::QueryRegValueSZ(system_level ? nt::HKLM : nt::HKCU, nt::WOW6432,
125 GetClientStateKeyPath(mode.app_guid).c_str(),
126 L"UninstallArguments", &args) &&
127 args.find(L"--multi-install") != std::wstring::npos;
128 }
129
130 std::unique_ptr<PrimaryInstallDetails> MakeProductDetails(
131 const std::wstring& exe_path) {
132 std::unique_ptr<PrimaryInstallDetails> details(new PrimaryInstallDetails());
133
134 const InstallConstants* mode = FindInstallMode(GetInstallSuffix(exe_path));
135 const bool system_level =
136 mode->supports_system_level && PathIsInProgramFiles(exe_path);
137 const bool multi_install =
138 mode->supports_multi_install && IsMultiInstall(*mode, system_level);
139
140 details->set_mode(mode);
141 details->set_system_level(system_level);
142 details->set_multi_install(multi_install);
143 details->set_channel(DetermineChannel(*mode, system_level, multi_install));
144
145 return details;
146 }
147
148 } // namespace install_static
OLDNEW
« no previous file with comments | « chrome/install_static/product_install_details.h ('k') | chrome/install_static/product_install_details_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698