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

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

Issue 2487783002: Make Crashpad use the user data dir, rather than always default location (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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/policy_path_parser.h" 5 #include "chrome/install_static/policy_path_parser.h"
6 6
7 #include <shlobj.h> 7 #include <shlobj.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <wtsapi32.h> 9 #include <wtsapi32.h>
10 10
11 #include <memory> 11 #include <memory>
12 12
13 #include "base/macros.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/win/registry.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "components/policy/policy_constants.h"
18
19 namespace { 13 namespace {
20 14
21 // Checks if the key exists in the given hive and expands any string variables.
22 bool LoadUserDataDirPolicyFromRegistry(HKEY hive,
23 const char* key_name_str,
24 base::FilePath* dir) {
25 base::string16 value;
26 base::string16 key_name(base::ASCIIToUTF16(key_name_str));
27 base::win::RegKey key(hive, policy::kRegistryChromePolicyKey, KEY_READ);
28 if (key.ReadValue(key_name.c_str(), &value) == ERROR_SUCCESS) {
29 *dir = base::FilePath(policy::path_parser::ExpandPathVariables(value));
30 return true;
31 }
32 return false;
33 }
34
35 const WCHAR* kMachineNamePolicyVarName = L"${machine_name}"; 15 const WCHAR* kMachineNamePolicyVarName = L"${machine_name}";
36 const WCHAR* kUserNamePolicyVarName = L"${user_name}"; 16 const WCHAR* kUserNamePolicyVarName = L"${user_name}";
37 const WCHAR* kWinDocumentsFolderVarName = L"${documents}"; 17 const WCHAR* kWinDocumentsFolderVarName = L"${documents}";
38 const WCHAR* kWinLocalAppDataFolderVarName = L"${local_app_data}"; 18 const WCHAR* kWinLocalAppDataFolderVarName = L"${local_app_data}";
39 const WCHAR* kWinRoamingAppDataFolderVarName = L"${roaming_app_data}"; 19 const WCHAR* kWinRoamingAppDataFolderVarName = L"${roaming_app_data}";
40 const WCHAR* kWinProfileFolderVarName = L"${profile}"; 20 const WCHAR* kWinProfileFolderVarName = L"${profile}";
41 const WCHAR* kWinProgramDataFolderVarName = L"${global_app_data}"; 21 const WCHAR* kWinProgramDataFolderVarName = L"${global_app_data}";
42 const WCHAR* kWinProgramFilesFolderVarName = L"${program_files}"; 22 const WCHAR* kWinProgramFilesFolderVarName = L"${program_files}";
43 const WCHAR* kWinWindowsFolderVarName = L"${windows}"; 23 const WCHAR* kWinWindowsFolderVarName = L"${windows}";
44 const WCHAR* kWinClientName = L"${client_name}"; 24 const WCHAR* kWinClientName = L"${client_name}";
(...skipping 10 matching lines...) Expand all
55 { kWinProgramFilesFolderVarName, CSIDL_PROGRAM_FILES}, 35 { kWinProgramFilesFolderVarName, CSIDL_PROGRAM_FILES},
56 { kWinProgramDataFolderVarName, CSIDL_COMMON_APPDATA}, 36 { kWinProgramDataFolderVarName, CSIDL_COMMON_APPDATA},
57 { kWinProfileFolderVarName, CSIDL_PROFILE}, 37 { kWinProfileFolderVarName, CSIDL_PROFILE},
58 { kWinLocalAppDataFolderVarName, CSIDL_LOCAL_APPDATA}, 38 { kWinLocalAppDataFolderVarName, CSIDL_LOCAL_APPDATA},
59 { kWinRoamingAppDataFolderVarName, CSIDL_APPDATA}, 39 { kWinRoamingAppDataFolderVarName, CSIDL_APPDATA},
60 { kWinDocumentsFolderVarName, CSIDL_PERSONAL} 40 { kWinDocumentsFolderVarName, CSIDL_PERSONAL}
61 }; 41 };
62 42
63 } // namespace 43 } // namespace
64 44
65 namespace policy { 45 namespace install_static {
66 46
67 namespace path_parser { 47 template <class T, size_t N>
48 constexpr size_t arraysize(const T (&array)[N]) {
grt (UTC plus 2) 2016/11/16 13:24:52 use _countof from stdlib.h instead of this
scottmg 2016/11/16 21:01:18 Done.
49 return N;
50 }
68 51
69 // Replaces all variable occurances in the policy string with the respective 52 // Replaces all variable occurances in the policy string with the respective
70 // system settings values. 53 // system settings values.
71 base::FilePath::StringType ExpandPathVariables( 54 // Note that this uses GetProcAddress to load DLLs that cannot be loaded before
72 const base::FilePath::StringType& untranslated_string) { 55 // the blacklist in the DllMain of chrome_elf has been applied. This function
73 base::FilePath::StringType result(untranslated_string); 56 // should only be used after DllMain() has run.
grt (UTC plus 2) 2016/11/16 13:24:52 wdyt of adding something to enforce this? the firs
scottmg 2016/11/16 21:01:18 I think it's OK without that because it'll be pret
57 std::wstring ExpandPathVariables(
58 const std::wstring& untranslated_string) {
59 std::wstring result(untranslated_string);
74 if (result.length() == 0) 60 if (result.length() == 0)
75 return result; 61 return result;
76 // Sanitize quotes in case of any around the whole string. 62 // Sanitize quotes in case of any around the whole string.
77 if (result.length() > 1 && 63 if (result.length() > 1 &&
78 ((result.front() == L'"' && result.back() == L'"') || 64 ((result.front() == L'"' && result.back() == L'"') ||
79 (result.front() == L'\'' && result.back() == L'\''))) { 65 (result.front() == L'\'' && result.back() == L'\''))) {
80 // Strip first and last char which should be matching quotes now. 66 // Strip first and last char which should be matching quotes now.
81 result = result.substr(1, result.length() - 2); 67 result = result.substr(1, result.length() - 2);
grt (UTC plus 2) 2016/11/16 13:24:52 i would think that result.pop_back(); resu
scottmg 2016/11/16 21:01:18 Maybe a little... I admit I only moved this code,
82 } 68 }
69 static auto sh_get_special_folder_path =
70 reinterpret_cast<decltype(::SHGetSpecialFolderPath)*>(GetProcAddress(
71 LoadLibrary(L"shell32.dll"), "SHGetSpecialFolderPathW"));
grt (UTC plus 2) 2016/11/16 13:24:52 no matching calls to FreeLibrary for any of these.
scottmg 2016/11/16 21:01:18 I thought about that. But cpu@ always implied unlo
83 // First translate all path variables we recognize. 72 // First translate all path variables we recognize.
84 for (size_t i = 0; i < arraysize(win_folder_mapping); ++i) { 73 for (size_t i = 0; i < arraysize(win_folder_mapping); ++i) {
85 size_t position = result.find(win_folder_mapping[i].name); 74 size_t position = result.find(win_folder_mapping[i].name);
86 if (position != std::wstring::npos) { 75 if (position != std::wstring::npos) {
87 WCHAR path[MAX_PATH]; 76 WCHAR path[MAX_PATH];
88 ::SHGetSpecialFolderPath(0, path, win_folder_mapping[i].id, false); 77 sh_get_special_folder_path(0, path, win_folder_mapping[i].id, false);
89 std::wstring path_string(path); 78 std::wstring path_string(path);
90 result.replace(position, wcslen(win_folder_mapping[i].name), path_string); 79 result.replace(position, wcslen(win_folder_mapping[i].name), path_string);
91 } 80 }
92 } 81 }
93 // Next translate other windows specific variables. 82 // Next translate other windows specific variables.
83 static auto get_user_name = reinterpret_cast<decltype(::GetUserName)*>(
84 GetProcAddress(LoadLibrary(L"advapi32.dll"), "GetUserNameW"));
94 size_t position = result.find(kUserNamePolicyVarName); 85 size_t position = result.find(kUserNamePolicyVarName);
95 if (position != std::wstring::npos) { 86 if (position != std::wstring::npos) {
96 DWORD return_length = 0; 87 DWORD return_length = 0;
97 ::GetUserName(NULL, &return_length); 88 get_user_name(NULL, &return_length);
98 if (return_length != 0) { 89 if (return_length != 0) {
99 std::unique_ptr<WCHAR[]> username(new WCHAR[return_length]); 90 std::unique_ptr<WCHAR[]> username(new WCHAR[return_length]);
100 ::GetUserName(username.get(), &return_length); 91 get_user_name(username.get(), &return_length);
101 std::wstring username_string(username.get()); 92 std::wstring username_string(username.get());
102 result.replace(position, wcslen(kUserNamePolicyVarName), username_string); 93 result.replace(position, wcslen(kUserNamePolicyVarName), username_string);
103 } 94 }
104 } 95 }
105 position = result.find(kMachineNamePolicyVarName); 96 position = result.find(kMachineNamePolicyVarName);
106 if (position != std::wstring::npos) { 97 if (position != std::wstring::npos) {
107 DWORD return_length = 0; 98 DWORD return_length = 0;
108 ::GetComputerNameEx(ComputerNamePhysicalDnsHostname, NULL, &return_length); 99 ::GetComputerNameEx(ComputerNamePhysicalDnsHostname, NULL, &return_length);
109 if (return_length != 0) { 100 if (return_length != 0) {
110 std::unique_ptr<WCHAR[]> machinename(new WCHAR[return_length]); 101 std::unique_ptr<WCHAR[]> machinename(new WCHAR[return_length]);
111 ::GetComputerNameEx(ComputerNamePhysicalDnsHostname, 102 ::GetComputerNameEx(ComputerNamePhysicalDnsHostname,
112 machinename.get(), &return_length); 103 machinename.get(), &return_length);
113 std::wstring machinename_string(machinename.get()); 104 std::wstring machinename_string(machinename.get());
114 result.replace( 105 result.replace(
115 position, wcslen(kMachineNamePolicyVarName), machinename_string); 106 position, wcslen(kMachineNamePolicyVarName), machinename_string);
116 } 107 }
117 } 108 }
109 static auto wts_query_session_information =
110 reinterpret_cast<decltype(::WTSQuerySessionInformation)*>(GetProcAddress(
111 LoadLibrary(L"wtsapi32.dll"), "WTSQuerySessionInformationW"));
112 static auto wts_free_memory = reinterpret_cast<decltype(::WTSFreeMemory)*>(
113 GetProcAddress(LoadLibrary(L"wtsapi32.dll"), "WTSFreeMemory"));
118 position = result.find(kWinClientName); 114 position = result.find(kWinClientName);
119 if (position != std::wstring::npos) { 115 if (position != std::wstring::npos) {
120 LPWSTR buffer = NULL; 116 LPWSTR buffer = NULL;
121 DWORD buffer_length = 0; 117 DWORD buffer_length = 0;
122 if (::WTSQuerySessionInformation(WTS_CURRENT_SERVER, WTS_CURRENT_SESSION, 118 if (wts_query_session_information(WTS_CURRENT_SERVER, WTS_CURRENT_SESSION,
123 WTSClientName, 119 WTSClientName, &buffer, &buffer_length)) {
124 &buffer, &buffer_length)) {
125 std::wstring clientname_string(buffer); 120 std::wstring clientname_string(buffer);
126 result.replace(position, wcslen(kWinClientName), clientname_string); 121 result.replace(position, wcslen(kWinClientName), clientname_string);
127 ::WTSFreeMemory(buffer); 122 wts_free_memory(buffer);
128 } 123 }
129 } 124 }
130 position = result.find(kWinSessionName); 125 position = result.find(kWinSessionName);
131 if (position != std::wstring::npos) { 126 if (position != std::wstring::npos) {
132 LPWSTR buffer = NULL; 127 LPWSTR buffer = NULL;
133 DWORD buffer_length = 0; 128 DWORD buffer_length = 0;
134 if (::WTSQuerySessionInformation(WTS_CURRENT_SERVER, WTS_CURRENT_SESSION, 129 if (wts_query_session_information(WTS_CURRENT_SERVER, WTS_CURRENT_SESSION,
135 WTSWinStationName, 130 WTSWinStationName, &buffer,
136 &buffer, &buffer_length)) { 131 &buffer_length)) {
137 std::wstring sessionname_string(buffer); 132 std::wstring sessionname_string(buffer);
138 result.replace(position, wcslen(kWinSessionName), sessionname_string); 133 result.replace(position, wcslen(kWinSessionName), sessionname_string);
139 ::WTSFreeMemory(buffer); 134 wts_free_memory(buffer);
140 } 135 }
141 } 136 }
142 // TODO(pastarmovj): Consider reorganizing this code once there are even more 137 // TODO(pastarmovj): Consider reorganizing this code once there are even more
143 // variables to be supported. The search for the var and its replacement can 138 // variables to be supported. The search for the var and its replacement can
144 // be extracted as common functionality. 139 // be extracted as common functionality.
145 140
146 return result; 141 return result;
147 } 142 }
148 143
149 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { 144 } // namespace install_static
150 DCHECK(user_data_dir);
151 // Policy from the HKLM hive has precedence over HKCU.
152 if (!LoadUserDataDirPolicyFromRegistry(HKEY_LOCAL_MACHINE, key::kUserDataDir,
153 user_data_dir)) {
154 LoadUserDataDirPolicyFromRegistry(HKEY_CURRENT_USER, key::kUserDataDir,
155 user_data_dir);
156 }
157 }
158
159 void CheckDiskCacheDirPolicy(base::FilePath* disk_cache_dir) {
160 DCHECK(disk_cache_dir);
161 if (!LoadUserDataDirPolicyFromRegistry(HKEY_LOCAL_MACHINE, key::kDiskCacheDir,
162 disk_cache_dir)) {
163 LoadUserDataDirPolicyFromRegistry(HKEY_CURRENT_USER, key::kDiskCacheDir,
164 disk_cache_dir);
165 }
166 }
167
168 } // namespace path_parser
169
170 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698