Index: chrome/install_static/policy_path_parser.cc |
diff --git a/chrome/browser/policy/policy_path_parser_win.cc b/chrome/install_static/policy_path_parser.cc |
similarity index 61% |
copy from chrome/browser/policy/policy_path_parser_win.cc |
copy to chrome/install_static/policy_path_parser.cc |
index 9de7ed9da96a9f77ac5b1848d2bce64c45010ced..2de14c61fdb32b1440cfa10ef8cb4985faf31ff1 100644 |
--- a/chrome/browser/policy/policy_path_parser_win.cc |
+++ b/chrome/install_static/policy_path_parser.cc |
@@ -1,8 +1,8 @@ |
-// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "chrome/browser/policy/policy_path_parser.h" |
+#include "chrome/install_static/policy_path_parser.h" |
#include <shlobj.h> |
#include <stddef.h> |
@@ -10,28 +10,8 @@ |
#include <memory> |
-#include "base/macros.h" |
-#include "base/strings/utf_string_conversions.h" |
-#include "base/win/registry.h" |
-#include "chrome/common/chrome_switches.h" |
-#include "components/policy/policy_constants.h" |
- |
namespace { |
-// Checks if the key exists in the given hive and expands any string variables. |
-bool LoadUserDataDirPolicyFromRegistry(HKEY hive, |
- const char* key_name_str, |
- base::FilePath* dir) { |
- base::string16 value; |
- base::string16 key_name(base::ASCIIToUTF16(key_name_str)); |
- base::win::RegKey key(hive, policy::kRegistryChromePolicyKey, KEY_READ); |
- if (key.ReadValue(key_name.c_str(), &value) == ERROR_SUCCESS) { |
- *dir = base::FilePath(policy::path_parser::ExpandPathVariables(value)); |
- return true; |
- } |
- return false; |
-} |
- |
const WCHAR* kMachineNamePolicyVarName = L"${machine_name}"; |
const WCHAR* kUserNamePolicyVarName = L"${user_name}"; |
const WCHAR* kWinDocumentsFolderVarName = L"${documents}"; |
@@ -62,15 +42,19 @@ const WinFolderNamesToCSIDLMapping win_folder_mapping[] = { |
} // namespace |
-namespace policy { |
+namespace install_static { |
-namespace path_parser { |
+template <typename T, size_t N> char (&ArraySizeHelper(T (&array)[N]))[N]; |
Mark Mentovai
2016/11/10 22:06:17
If C++11 is guaranteed, we can use the neater C++1
scottmg
2016/11/15 23:23:15
Done.
|
+#define arraysize(array) (sizeof(ArraySizeHelper(array))) |
// Replaces all variable occurances in the policy string with the respective |
// system settings values. |
-base::FilePath::StringType ExpandPathVariables( |
- const base::FilePath::StringType& untranslated_string) { |
- base::FilePath::StringType result(untranslated_string); |
+// Note that this uses GetProcAddress to load DLLs that cannot be loaded before |
+// the blacklist in the DllMain of chrome_elf has been applied. This function |
+// should only be used after DllMain() has run. |
+std::wstring ExpandPathVariables( |
+ const std::wstring& untranslated_string) { |
+ std::wstring result(untranslated_string); |
if (result.length() == 0) |
return result; |
// Sanitize quotes in case of any around the whole string. |
@@ -80,24 +64,29 @@ base::FilePath::StringType ExpandPathVariables( |
// Strip first and last char which should be matching quotes now. |
result = result.substr(1, result.length() - 2); |
} |
+ static auto sh_get_special_folder_path = |
+ reinterpret_cast<decltype(::SHGetSpecialFolderPath)*>(GetProcAddress( |
+ LoadLibrary(L"shell32.dll"), "SHGetSpecialFolderPathW")); |
// First translate all path variables we recognize. |
for (size_t i = 0; i < arraysize(win_folder_mapping); ++i) { |
size_t position = result.find(win_folder_mapping[i].name); |
if (position != std::wstring::npos) { |
WCHAR path[MAX_PATH]; |
- ::SHGetSpecialFolderPath(0, path, win_folder_mapping[i].id, false); |
+ sh_get_special_folder_path(0, path, win_folder_mapping[i].id, false); |
std::wstring path_string(path); |
result.replace(position, wcslen(win_folder_mapping[i].name), path_string); |
} |
} |
// Next translate other windows specific variables. |
+ static auto get_user_name = reinterpret_cast<decltype(::GetUserName)*>( |
+ GetProcAddress(LoadLibrary(L"advapi32.dll"), "GetUserNameW")); |
size_t position = result.find(kUserNamePolicyVarName); |
if (position != std::wstring::npos) { |
DWORD return_length = 0; |
- ::GetUserName(NULL, &return_length); |
+ get_user_name(NULL, &return_length); |
if (return_length != 0) { |
std::unique_ptr<WCHAR[]> username(new WCHAR[return_length]); |
- ::GetUserName(username.get(), &return_length); |
+ get_user_name(username.get(), &return_length); |
std::wstring username_string(username.get()); |
result.replace(position, wcslen(kUserNamePolicyVarName), username_string); |
} |
@@ -115,28 +104,32 @@ base::FilePath::StringType ExpandPathVariables( |
position, wcslen(kMachineNamePolicyVarName), machinename_string); |
} |
} |
+ static auto wts_query_session_information = |
+ reinterpret_cast<decltype(::WTSQuerySessionInformation)*>(GetProcAddress( |
+ LoadLibrary(L"wtsapi32.dll"), "WTSQuerySessionInformationW")); |
+ static auto wts_free_memory = reinterpret_cast<decltype(::WTSFreeMemory)*>( |
+ GetProcAddress(LoadLibrary(L"wtsapi32.dll"), "WTSFreeMemory")); |
position = result.find(kWinClientName); |
if (position != std::wstring::npos) { |
LPWSTR buffer = NULL; |
DWORD buffer_length = 0; |
- if (::WTSQuerySessionInformation(WTS_CURRENT_SERVER, WTS_CURRENT_SESSION, |
- WTSClientName, |
- &buffer, &buffer_length)) { |
+ if (wts_query_session_information(WTS_CURRENT_SERVER, WTS_CURRENT_SESSION, |
+ WTSClientName, &buffer, &buffer_length)) { |
std::wstring clientname_string(buffer); |
result.replace(position, wcslen(kWinClientName), clientname_string); |
- ::WTSFreeMemory(buffer); |
+ wts_free_memory(buffer); |
} |
} |
position = result.find(kWinSessionName); |
if (position != std::wstring::npos) { |
LPWSTR buffer = NULL; |
DWORD buffer_length = 0; |
- if (::WTSQuerySessionInformation(WTS_CURRENT_SERVER, WTS_CURRENT_SESSION, |
- WTSWinStationName, |
- &buffer, &buffer_length)) { |
+ if (wts_query_session_information(WTS_CURRENT_SERVER, WTS_CURRENT_SESSION, |
+ WTSWinStationName, &buffer, |
+ &buffer_length)) { |
std::wstring sessionname_string(buffer); |
result.replace(position, wcslen(kWinSessionName), sessionname_string); |
- ::WTSFreeMemory(buffer); |
+ wts_free_memory(buffer); |
} |
} |
// TODO(pastarmovj): Consider reorganizing this code once there are even more |
@@ -146,25 +139,4 @@ base::FilePath::StringType ExpandPathVariables( |
return result; |
} |
-void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { |
- DCHECK(user_data_dir); |
- // Policy from the HKLM hive has precedence over HKCU. |
- if (!LoadUserDataDirPolicyFromRegistry(HKEY_LOCAL_MACHINE, key::kUserDataDir, |
- user_data_dir)) { |
- LoadUserDataDirPolicyFromRegistry(HKEY_CURRENT_USER, key::kUserDataDir, |
- user_data_dir); |
- } |
-} |
- |
-void CheckDiskCacheDirPolicy(base::FilePath* disk_cache_dir) { |
- DCHECK(disk_cache_dir); |
- if (!LoadUserDataDirPolicyFromRegistry(HKEY_LOCAL_MACHINE, key::kDiskCacheDir, |
- disk_cache_dir)) { |
- LoadUserDataDirPolicyFromRegistry(HKEY_CURRENT_USER, key::kDiskCacheDir, |
- disk_cache_dir); |
- } |
-} |
- |
-} // namespace path_parser |
- |
-} // namespace policy |
+} // namespace install_static |