Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <shlobj.h> | 5 #include <shlobj.h> |
| 6 #include <wtsapi32.h> | 6 #include <wtsapi32.h> |
| 7 #pragma comment(lib, "wtsapi32.lib") | 7 #pragma comment(lib, "wtsapi32.lib") |
| 8 | 8 |
| 9 #include "chrome/browser/policy/policy_path_parser.h" | 9 #include "chrome/browser/policy/policy_path_parser.h" |
| 10 | 10 |
| 11 #include "base/bind.h" | |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/win/registry.h" | 14 #include "base/win/registry.h" |
| 14 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 15 #include "policy/policy_constants.h" | 16 #include "policy/policy_constants.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // Checks if the key exists in the given hive and expands any string variables. | 20 // Checks if the key exists in the given hive and expands any string variables. |
| 20 bool LoadUserDataDirPolicyFromRegistry(HKEY hive, base::FilePath* dir) { | 21 bool LoadUserDataDirPolicyFromRegistry(HKEY hive, base::FilePath* dir) { |
| 21 std::wstring value; | 22 std::wstring value; |
| 22 std::wstring key_name(base::ASCIIToWide(policy::key::kUserDataDir)); | 23 std::wstring key_name(base::ASCIIToWide(policy::key::kUserDataDir)); |
| 23 base::win::RegKey key(hive, policy::kRegistryChromePolicyKey, KEY_READ); | 24 base::win::RegKey key(hive, policy::kRegistryChromePolicyKey, KEY_READ); |
| 24 if (key.ReadValue(key_name.c_str(), &value) == ERROR_SUCCESS) { | 25 if (key.ReadValue(key_name.c_str(), &value) == ERROR_SUCCESS) { |
| 25 *dir = base::FilePath(policy::path_parser::ExpandPathVariables(value)); | 26 *dir = base::FilePath(policy::path_parser::ExpandPathVariables(value)); |
| 26 return true; | 27 return true; |
| 27 } | 28 } |
| 28 return false; | 29 return false; |
| 29 } | 30 } |
| 30 | 31 |
| 32 } // namespace | |
| 33 | |
| 34 namespace policy { | |
| 35 | |
| 36 namespace path_parser { | |
| 37 | |
| 38 namespace internal { | |
| 39 | |
| 40 bool GetSpecialFolderPath(int id, base::FilePath::StringType* value) { | |
| 41 WCHAR path[MAX_PATH]; | |
| 42 ::SHGetSpecialFolderPath(0, path, id, false); | |
| 43 *value = base::FilePath::StringType(path); | |
| 44 return true; | |
| 45 } | |
| 46 | |
| 47 #define WRAP_GET_FORLDER_FUNCTION(FunctionName, WindowsId) \ | |
|
pastarmovj
2014/04/01 09:17:29
I think FolderId is more appropriate name here.
kaliamoorthi
2014/04/01 13:43:17
Done.
| |
| 48 bool FunctionName(base::FilePath::StringType* value) { \ | |
| 49 return GetSpecialFolderPath(WindowsId, value); \ | |
| 50 } | |
| 51 | |
| 52 WRAP_GET_FORLDER_FUNCTION(GetWindowsFolderPath, CSIDL_WINDOWS) | |
| 53 WRAP_GET_FORLDER_FUNCTION(GetProgramFilesFolderPath, CSIDL_PROGRAM_FILES) | |
| 54 WRAP_GET_FORLDER_FUNCTION(GetProgramDataFolderPath, CSIDL_COMMON_APPDATA) | |
| 55 WRAP_GET_FORLDER_FUNCTION(GetProfileFolderPath, CSIDL_PROFILE) | |
| 56 WRAP_GET_FORLDER_FUNCTION(GetLocalAppDataFolderPath, CSIDL_LOCAL_APPDATA) | |
| 57 WRAP_GET_FORLDER_FUNCTION(GetRoamingAppDataFolderPath, CSIDL_APPDATA) | |
| 58 WRAP_GET_FORLDER_FUNCTION(GetDocumentsFolderPath, CSIDL_PERSONAL) | |
| 59 | |
| 60 bool GetWindowsUserName(base::FilePath::StringType* value) { | |
| 61 DWORD return_length = 0; | |
| 62 ::GetUserName(NULL, &return_length); | |
| 63 if (return_length) { | |
| 64 scoped_ptr<WCHAR[]> username(new WCHAR[return_length]); | |
| 65 ::GetUserName(username.get(), &return_length); | |
| 66 *value = base::FilePath::StringType(username.get()); | |
| 67 } | |
| 68 return (return_length != 0); | |
| 69 } | |
| 70 | |
| 71 bool GetMachineName(base::FilePath::StringType* value) { | |
| 72 DWORD return_length = 0; | |
| 73 ::GetComputerNameEx(ComputerNamePhysicalDnsHostname, NULL, &return_length); | |
| 74 if (return_length) { | |
| 75 scoped_ptr<WCHAR[]> machinename(new WCHAR[return_length]); | |
| 76 ::GetComputerNameEx( | |
| 77 ComputerNamePhysicalDnsHostname, machinename.get(), &return_length); | |
| 78 *value = base::FilePath::StringType(machinename.get()); | |
| 79 } | |
| 80 return (return_length != 0); | |
| 81 } | |
| 82 | |
| 83 bool GetClientName(base::FilePath::StringType* value) { | |
| 84 LPWSTR buffer = NULL; | |
| 85 DWORD buffer_length = 0; | |
| 86 BOOL status; | |
| 87 if ((status = ::WTSQuerySessionInformation(WTS_CURRENT_SERVER, | |
| 88 WTS_CURRENT_SESSION, | |
| 89 WTSClientName, | |
| 90 &buffer, | |
| 91 &buffer_length))) { | |
| 92 *value = base::FilePath::StringType(buffer); | |
| 93 ::WTSFreeMemory(buffer); | |
| 94 } | |
| 95 return (status == TRUE); | |
| 96 } | |
| 97 | |
| 31 const WCHAR* kMachineNamePolicyVarName = L"${machine_name}"; | 98 const WCHAR* kMachineNamePolicyVarName = L"${machine_name}"; |
| 32 const WCHAR* kUserNamePolicyVarName = L"${user_name}"; | 99 const WCHAR* kUserNamePolicyVarName = L"${user_name}"; |
| 33 const WCHAR* kWinDocumentsFolderVarName = L"${documents}"; | 100 const WCHAR* kWinDocumentsFolderVarName = L"${documents}"; |
| 34 const WCHAR* kWinLocalAppDataFolderVarName = L"${local_app_data}"; | 101 const WCHAR* kWinLocalAppDataFolderVarName = L"${local_app_data}"; |
| 35 const WCHAR* kWinRoamingAppDataFolderVarName = L"${roaming_app_data}"; | 102 const WCHAR* kWinRoamingAppDataFolderVarName = L"${roaming_app_data}"; |
| 36 const WCHAR* kWinProfileFolderVarName = L"${profile}"; | 103 const WCHAR* kWinProfileFolderVarName = L"${profile}"; |
| 37 const WCHAR* kWinProgramDataFolderVarName = L"${global_app_data}"; | 104 const WCHAR* kWinProgramDataFolderVarName = L"${global_app_data}"; |
| 38 const WCHAR* kWinProgramFilesFolderVarName = L"${program_files}"; | 105 const WCHAR* kWinProgramFilesFolderVarName = L"${program_files}"; |
| 39 const WCHAR* kWinWindowsFolderVarName = L"${windows}"; | 106 const WCHAR* kWinWindowsFolderVarName = L"${windows}"; |
| 40 const WCHAR* kWinClientName = L"${client_name}"; | 107 const WCHAR* kWinClientName = L"${client_name}"; |
| 41 | 108 |
| 42 struct WinFolderNamesToCSIDLMapping { | 109 // A table mapping variable names to their respective get value function |
| 43 const WCHAR* name; | 110 // pointers. |
| 44 int id; | 111 const VariableNameAndValueCallback kVariableNameAndValueCallbacks[] = { |
| 45 }; | 112 {kWinWindowsFolderVarName, &GetWindowsFolderPath}, |
| 113 {kWinProgramFilesFolderVarName, &GetProgramFilesFolderPath}, | |
| 114 {kWinProgramDataFolderVarName, &GetProgramDataFolderPath}, | |
| 115 {kWinProfileFolderVarName, &GetProfileFolderPath}, | |
| 116 {kWinLocalAppDataFolderVarName, &GetLocalAppDataFolderPath}, | |
| 117 {kWinRoamingAppDataFolderVarName, &GetRoamingAppDataFolderPath}, | |
| 118 {kWinDocumentsFolderVarName, &GetDocumentsFolderPath}, | |
| 119 {kUserNamePolicyVarName, &GetWindowsUserName}, | |
| 120 {kMachineNamePolicyVarName, &GetMachineName}, | |
| 121 {kWinClientName, &GetClientName}}; | |
| 46 | 122 |
| 47 // Mapping from variable names to Windows CSIDL ids. | 123 // Total number of entries in the mapping table. |
| 48 const WinFolderNamesToCSIDLMapping win_folder_mapping[] = { | 124 const int kNoOfVariables = arraysize(kVariableNameAndValueCallbacks); |
| 49 { kWinWindowsFolderVarName, CSIDL_WINDOWS}, | |
| 50 { kWinProgramFilesFolderVarName, CSIDL_PROGRAM_FILES}, | |
| 51 { kWinProgramDataFolderVarName, CSIDL_COMMON_APPDATA}, | |
| 52 { kWinProfileFolderVarName, CSIDL_PROFILE}, | |
| 53 { kWinLocalAppDataFolderVarName, CSIDL_LOCAL_APPDATA}, | |
| 54 { kWinRoamingAppDataFolderVarName, CSIDL_APPDATA}, | |
| 55 { kWinDocumentsFolderVarName, CSIDL_PERSONAL} | |
| 56 }; | |
| 57 | 125 |
| 58 } // namespace | 126 } // namespace internal |
| 59 | |
| 60 namespace policy { | |
| 61 | |
| 62 namespace path_parser { | |
| 63 | |
| 64 // Replaces all variable occurances in the policy string with the respective | |
| 65 // system settings values. | |
| 66 base::FilePath::StringType ExpandPathVariables( | |
| 67 const base::FilePath::StringType& untranslated_string) { | |
| 68 base::FilePath::StringType result(untranslated_string); | |
| 69 if (result.length() == 0) | |
| 70 return result; | |
| 71 // Sanitize quotes in case of any around the whole string. | |
| 72 if (result.length() > 1 && | |
| 73 ((result[0] == L'"' && result[result.length() - 1] == L'"') || | |
| 74 (result[0] == L'\'' && result[result.length() - 1] == L'\''))) { | |
| 75 // Strip first and last char which should be matching quotes now. | |
| 76 result = result.substr(1, result.length() - 2); | |
| 77 } | |
| 78 // First translate all path variables we recognize. | |
| 79 for (int i = 0; i < arraysize(win_folder_mapping); ++i) { | |
| 80 size_t position = result.find(win_folder_mapping[i].name); | |
| 81 if (position != std::wstring::npos) { | |
| 82 WCHAR path[MAX_PATH]; | |
| 83 ::SHGetSpecialFolderPath(0, path, win_folder_mapping[i].id, false); | |
| 84 std::wstring path_string(path); | |
| 85 result.replace(position, wcslen(win_folder_mapping[i].name), path_string); | |
| 86 } | |
| 87 } | |
| 88 // Next translate other windows specific variables. | |
| 89 size_t position = result.find(kUserNamePolicyVarName); | |
| 90 if (position != std::wstring::npos) { | |
| 91 DWORD return_length = 0; | |
| 92 ::GetUserName(NULL, &return_length); | |
| 93 if (return_length != 0) { | |
| 94 scoped_ptr<WCHAR[]> username(new WCHAR[return_length]); | |
| 95 ::GetUserName(username.get(), &return_length); | |
| 96 std::wstring username_string(username.get()); | |
| 97 result.replace(position, wcslen(kUserNamePolicyVarName), username_string); | |
| 98 } | |
| 99 } | |
| 100 position = result.find(kMachineNamePolicyVarName); | |
| 101 if (position != std::wstring::npos) { | |
| 102 DWORD return_length = 0; | |
| 103 ::GetComputerNameEx(ComputerNamePhysicalDnsHostname, NULL, &return_length); | |
| 104 if (return_length != 0) { | |
| 105 scoped_ptr<WCHAR[]> machinename(new WCHAR[return_length]); | |
| 106 ::GetComputerNameEx(ComputerNamePhysicalDnsHostname, | |
| 107 machinename.get(), &return_length); | |
| 108 std::wstring machinename_string(machinename.get()); | |
| 109 result.replace( | |
| 110 position, wcslen(kMachineNamePolicyVarName), machinename_string); | |
| 111 } | |
| 112 } | |
| 113 position = result.find(kWinClientName); | |
| 114 if (position != std::wstring::npos) { | |
| 115 LPWSTR buffer = NULL; | |
| 116 DWORD buffer_length = 0; | |
| 117 if (::WTSQuerySessionInformation(WTS_CURRENT_SERVER, WTS_CURRENT_SESSION, | |
| 118 WTSClientName, | |
| 119 &buffer, &buffer_length)) { | |
| 120 std::wstring clientname_string(buffer); | |
| 121 result.replace(position, wcslen(kWinClientName), clientname_string); | |
| 122 ::WTSFreeMemory(buffer); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 return result; | |
| 127 } | |
| 128 | 127 |
| 129 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { | 128 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { |
| 130 DCHECK(user_data_dir); | 129 DCHECK(user_data_dir); |
| 131 // Policy from the HKLM hive has precedence over HKCU. | 130 // Policy from the HKLM hive has precedence over HKCU. |
| 132 if (!LoadUserDataDirPolicyFromRegistry(HKEY_LOCAL_MACHINE, user_data_dir)) | 131 if (!LoadUserDataDirPolicyFromRegistry(HKEY_LOCAL_MACHINE, user_data_dir)) |
| 133 LoadUserDataDirPolicyFromRegistry(HKEY_CURRENT_USER, user_data_dir); | 132 LoadUserDataDirPolicyFromRegistry(HKEY_CURRENT_USER, user_data_dir); |
| 134 } | 133 } |
| 135 | 134 |
| 136 } // namespace path_parser | 135 } // namespace path_parser |
| 137 | 136 |
| 138 } // namespace policy | 137 } // namespace policy |
| OLD | NEW |