diff options
author | kaliamoorthi@chromium.org <kaliamoorthi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-03 12:21:06 +0000 |
---|---|---|
committer | kaliamoorthi@chromium.org <kaliamoorthi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-03 12:21:06 +0000 |
commit | f2dc9f306053f1fcbba99badf5edfab6a64b1392 (patch) | |
tree | 54ac015f36115071e2a3ee98c4e180582edad591 /chrome | |
parent | 06114b427935cb23724c12176be20d856d00ac58 (diff) | |
download | chromium_src-f2dc9f306053f1fcbba99badf5edfab6a64b1392.zip chromium_src-f2dc9f306053f1fcbba99badf5edfab6a64b1392.tar.gz chromium_src-f2dc9f306053f1fcbba99badf5edfab6a64b1392.tar.bz2 |
Refactorise the policy_path_parser framework
This CL performs a refactorisation of the policy path parser framework.
BUG=352627
Review URL: https://codereview.chromium.org/214233003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@261370 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/policy/policy_path_parser.cc | 67 | ||||
-rw-r--r-- | chrome/browser/policy/policy_path_parser.h | 19 | ||||
-rw-r--r-- | chrome/browser/policy/policy_path_parser_linux.cc | 64 | ||||
-rw-r--r-- | chrome/browser/policy/policy_path_parser_mac.mm | 129 | ||||
-rw-r--r-- | chrome/browser/policy/policy_path_parser_unittest.cc | 50 | ||||
-rw-r--r-- | chrome/browser/policy/policy_path_parser_win.cc | 174 | ||||
-rw-r--r-- | chrome/policy.gypi | 1 |
7 files changed, 309 insertions, 195 deletions
diff --git a/chrome/browser/policy/policy_path_parser.cc b/chrome/browser/policy/policy_path_parser.cc new file mode 100644 index 0000000..cb64af9 --- /dev/null +++ b/chrome/browser/policy/policy_path_parser.cc @@ -0,0 +1,67 @@ +// Copyright 2014 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 "base/bind.h" +#include "base/files/file_path.h" + +namespace { + +void TrimQuotes(base::FilePath::StringType* path) { + if (path->length() > 1 && + (((*path)[0] == FILE_PATH_LITERAL('"') && + (*path)[path->length() - 1] == FILE_PATH_LITERAL('"')) || + ((*path)[0] == FILE_PATH_LITERAL('\'') && + (*path)[path->length() - 1] == FILE_PATH_LITERAL('\'')))) { + // Strip first and last char which should be matching quotes now. + *path = path->substr(1, path->length() - 2); + } +} + +} // namespace + +namespace policy { + +namespace path_parser { + +// This function performs a lazy call to the GetValueCallback, that is the +// callback is invoked only if the variable is found in the path. This is done +// to reduce the overhead during initialization. +void ReplaceVariableInPathWithValue( + const base::FilePath::StringType& variable, + const policy::path_parser::internal::GetValueFuncPtr& value_func_ptr, + base::FilePath::StringType* path) { + size_t position = path->find(variable); + base::FilePath::StringType value; + if (position != base::FilePath::StringType::npos && value_func_ptr(&value)) + path->replace(position, variable.length(), value); +} + +// Replaces all variable occurrences 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); + + if (result.length() == 0) + return result; + + // Sanitize quotes in case of any around the whole string. + TrimQuotes(&result); + + base::FilePath::StringType variable; + internal::GetValueFuncPtr val_func_ptr; + for (int i = 0; i < internal::kNoOfVariables; ++i) { + variable = internal::kVariableNameAndValueCallbacks[i].name; + val_func_ptr = internal::kVariableNameAndValueCallbacks[i].value_func_ptr; + ReplaceVariableInPathWithValue(variable, val_func_ptr, &result); + } + + return result; +} + +} // namespace path_parser + +} // namespace policy diff --git a/chrome/browser/policy/policy_path_parser.h b/chrome/browser/policy/policy_path_parser.h index 5bf0345..c0d4c22 100644 --- a/chrome/browser/policy/policy_path_parser.h +++ b/chrome/browser/policy/policy_path_parser.h @@ -7,12 +7,31 @@ #include <string> +#include "base/bind.h" #include "base/files/file_path.h" namespace policy { namespace path_parser { +namespace internal { + +typedef bool (*GetValueFuncPtr)(base::FilePath::StringType*); + +struct VariableNameAndValueCallback { + const base::FilePath::CharType* name; + const GetValueFuncPtr value_func_ptr; +}; + +// Different set of variables are supported in each platform (see below). Hence +// this table is filled in with different elements in each platform. +extern const VariableNameAndValueCallback kVariableNameAndValueCallbacks[]; + +// Since the number of elements in the table could vary for each platform, this +// variable is used to keep track of it. +extern const int kNoOfVariables; +} + // This function is used to expand the variables in policy strings that // represent paths. The set of supported variables differs between platforms // but generally covers most standard locations that might be needed in the diff --git a/chrome/browser/policy/policy_path_parser_linux.cc b/chrome/browser/policy/policy_path_parser_linux.cc index e0bed09..4509f18 100644 --- a/chrome/browser/policy/policy_path_parser_linux.cc +++ b/chrome/browser/policy/policy_path_parser_linux.cc @@ -8,51 +8,47 @@ #include "chrome/browser/policy/policy_path_parser.h" +#include "base/bind.h" #include "base/logging.h" namespace policy { namespace path_parser { +namespace internal { + const char* kMachineNamePolicyVarName = "${machine_name}"; const char* kUserNamePolicyVarName = "${user_name}"; -// Replaces all variable occurrences 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); - if (result.length() == 0) - return result; - // Sanitize quotes in case of any around the whole string. - if (result.length() > 1 && - ((result[0] == '"' && result[result.length() - 1] == '"') || - (result[0] == '\'' && result[result.length() - 1] == '\''))) { - // Strip first and last char which should be matching quotes now. - result = result.substr(1, result.length() - 2); - } - // Translate two special variables ${user_name} and ${machine_name} - size_t position = result.find(kUserNamePolicyVarName); - if (position != std::string::npos) { - struct passwd* user = getpwuid(geteuid()); - if (user) { - result.replace(position, strlen(kUserNamePolicyVarName), user->pw_name); - } else { - LOG(ERROR) << "Username variable can not be resolved. "; - } - } - position = result.find(kMachineNamePolicyVarName); - if (position != std::string::npos) { - char machinename[255]; - if (gethostname(machinename, 255) == 0) { - result.replace(position, strlen(kMachineNamePolicyVarName), machinename); - } else { - LOG(ERROR) << "Machine name variable can not be resolved."; - } - } - return result; +bool GetUserName(base::FilePath::StringType* value) { + struct passwd* user = getpwuid(geteuid()); + if (user) + *value = base::FilePath::StringType(user->pw_name); + else + LOG(ERROR) << "Username variable can not be resolved. "; + return (user != NULL); +} + +bool GetMachineName(base::FilePath::StringType* value) { + char machinename[255]; + int status; + if ((status = gethostname(machinename, 255)) == 0) + *value = base::FilePath::StringType(machinename); + else + LOG(ERROR) << "Machine name variable can not be resolved."; + return (status == 0); } +// A table mapping variable name to their respective getvalue callbacks. +const VariableNameAndValueCallback kVariableNameAndValueCallbacks[] = { + {kUserNamePolicyVarName, &GetUserName}, + {kMachineNamePolicyVarName, &GetMachineName}}; + +// Total number of entries in the mapping table. +const int kNoOfVariables = arraysize(kVariableNameAndValueCallbacks); + +} // namespace internal + void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { // This function is not implemented in Linux because we don't support the // policy on this platform. diff --git a/chrome/browser/policy/policy_path_parser_mac.mm b/chrome/browser/policy/policy_path_parser_mac.mm index b734faa3..9be470a 100644 --- a/chrome/browser/policy/policy_path_parser_mac.mm +++ b/chrome/browser/policy/policy_path_parser_mac.mm @@ -4,7 +4,7 @@ #include "chrome/browser/policy/policy_path_parser.h" -#include "base/basictypes.h" +#include "base/bind.h" #include "base/files/file_path.h" #include "base/logging.h" #import "base/mac/scoped_nsautorelease_pool.h" @@ -21,79 +21,72 @@ namespace policy { namespace path_parser { -const char* kUserNamePolicyVarName = "${user_name}"; -const char* kMachineNamePolicyVarName = "${machine_name}"; -const char* kMacUsersDirectory = "${users}"; -const char* kMacDocumentsFolderVarName = "${documents}"; +namespace internal { -struct MacFolderNamesToSPDMaping { - const char* name; - NSSearchPathDirectory id; -}; - -// Mapping from variable names to MacOS NSSearchPathDirectory ids. -const MacFolderNamesToSPDMaping mac_folder_mapping[] = { - { kMacUsersDirectory, NSUserDirectory}, - { kMacDocumentsFolderVarName, NSDocumentDirectory} -}; - -// Replaces all variable occurrences 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); - if (result.length() == 0) - return result; - // Sanitize quotes in case of any around the whole string. - if (result.length() > 1 && - ((result[0] == '"' && result[result.length() - 1] == '"') || - (result[0] == '\'' && result[result.length() - 1] == '\''))) { - // Strip first and last char which should be matching quotes now. - result = result.substr(1, result.length() - 2); - } - // First translate all path variables we recognize. - for (size_t i = 0; i < arraysize(mac_folder_mapping); ++i) { - size_t position = result.find(mac_folder_mapping[i].name); - if (position != std::string::npos) { - NSArray* searchpaths = NSSearchPathForDirectoriesInDomains( - mac_folder_mapping[i].id, NSAllDomainsMask, true); - if ([searchpaths count] > 0) { - NSString *variable_value = [searchpaths objectAtIndex:0]; - result.replace(position, strlen(mac_folder_mapping[i].name), - base::SysNSStringToUTF8(variable_value)); - } - } +bool GetFolder(NSSearchPathDirectory id, base::FilePath::StringType* value) { + NSArray* searchpaths = + NSSearchPathForDirectoriesInDomains(id, NSAllDomainsMask, true); + if ([searchpaths count] > 0) { + NSString* variable_value = [searchpaths objectAtIndex:0]; + *value = base::SysNSStringToUTF8(variable_value); + return true; } - // Next translate two special variables ${user_name} and ${machine_name} - size_t position = result.find(kUserNamePolicyVarName); - if (position != std::string::npos) { - NSString* username = NSUserName(); - if (username) { - result.replace(position, strlen(kUserNamePolicyVarName), - base::SysNSStringToUTF8(username)); - } else { - LOG(ERROR) << "Username variable can not be resolved."; - } + return false; +} + +// The wrapper is used instead of callbacks since these function are +// initialized in a global table and using callbacks in the table results in +// the increase in size of static initializers. +#define WRAP_GET_FORLDER_FUNCTION(FunctionName, FolderId) \ + bool FunctionName(base::FilePath::StringType* value) { \ + return GetFolder(FolderId, value); \ } - position = result.find(kMachineNamePolicyVarName); - if (position != std::string::npos) { - SCDynamicStoreContext context = { 0, NULL, NULL, NULL }; - SCDynamicStoreRef store = SCDynamicStoreCreate(kCFAllocatorDefault, - CFSTR("policy_subsystem"), - NULL, &context); - CFStringRef machinename = SCDynamicStoreCopyLocalHostName(store); - if (machinename) { - result.replace(position, strlen(kMachineNamePolicyVarName), - base::SysCFStringRefToUTF8(machinename)); - CFRelease(machinename); - } else { - LOG(ERROR) << "Machine name variable can not be resolved."; - } - CFRelease(store); + +WRAP_GET_FORLDER_FUNCTION(GetMacUserFolderPath, NSUserDirectory) +WRAP_GET_FORLDER_FUNCTION(GetMacDocumentsFolderPath, NSDocumentDirectory) + +bool GetUserName(base::FilePath::StringType* value) { + NSString* username = NSUserName(); + if (username) + *value = base::SysNSStringToUTF8(username); + else + LOG(ERROR) << "Username variable can not be resolved."; + return (username != NULL); +} + +bool GetMachineName(base::FilePath::StringType* value) { + SCDynamicStoreContext context = {0, NULL, NULL, NULL}; + SCDynamicStoreRef store = SCDynamicStoreCreate( + kCFAllocatorDefault, CFSTR("policy_subsystem"), NULL, &context); + CFStringRef machinename = SCDynamicStoreCopyLocalHostName(store); + if (machinename) { + *value = base::SysCFStringRefToUTF8(machinename); + CFRelease(machinename); + } else { + LOG(ERROR) << "Machine name variable can not be resolved."; } - return result; + CFRelease(store); + return (machinename != NULL); } +const char* kUserNamePolicyVarName = "${user_name}"; +const char* kMachineNamePolicyVarName = "${machine_name}"; +const char* kMacUsersDirectory = "${users}"; +const char* kMacDocumentsFolderVarName = "${documents}"; + +// A table mapping variable names to their respective get value function +// pointers. +const VariableNameAndValueCallback kVariableNameAndValueCallbacks[] = { + {kUserNamePolicyVarName, &GetUserName}, + {kMachineNamePolicyVarName, &GetMachineName}, + {kMacUsersDirectory, &GetMacUserFolderPath}, + {kMacDocumentsFolderVarName, &GetMacDocumentsFolderPath}}; + +// Total number of entries in the mapping table. +const int kNoOfVariables = arraysize(kVariableNameAndValueCallbacks); + +} // namespace internal + void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { base::mac::ScopedNSAutoreleasePool pool; diff --git a/chrome/browser/policy/policy_path_parser_unittest.cc b/chrome/browser/policy/policy_path_parser_unittest.cc index 633ef78..8fec189 100644 --- a/chrome/browser/policy/policy_path_parser_unittest.cc +++ b/chrome/browser/policy/policy_path_parser_unittest.cc @@ -20,13 +20,41 @@ class PolicyPathParserTests : public testing::Test { } }; -#if defined(OS_MACOSX) -// http://crbug.com/327520 -#define MAYBE_AllPlatformVariables DISABLED_AllPlatformVariables -#else -#define MAYBE_AllPlatformVariables AllPlatformVariables -#endif -TEST_F(PolicyPathParserTests, MAYBE_AllPlatformVariables) { +namespace path_parser { + +// The test needs access to a routine that is not exposed via the public header. +void ReplaceVariableInPathWithValue( + const base::FilePath::StringType& variable, + const policy::path_parser::internal::GetValueFuncPtr& value_func_ptr, + base::FilePath::StringType* path); + +} // namespace path_parser; + +bool GetBuggy(base::FilePath::StringType* value) { + *value = base::FilePath::StringType(FILE_PATH_LITERAL("ok")); + return true; +} + +TEST_F(PolicyPathParserTests, ReplaceVariableInPathWithValue) { + // This is custom variable with custom callback. It should replace ${buggy} + // with ok. + base::FilePath::StringType custom_vars(FILE_PATH_LITERAL("//$C/${buggy}")); + base::FilePath::StringType custom_vars_expected(FILE_PATH_LITERAL("//$C/ok")); + base::FilePath::StringType buggy(FILE_PATH_LITERAL("${buggy}")); + + path_parser::ReplaceVariableInPathWithValue(buggy, &GetBuggy, &custom_vars); + ASSERT_EQ(custom_vars, custom_vars_expected); + + // There is no ${buggy} in input, so it should remain unchanged. + base::FilePath::StringType custom2_vars(FILE_PATH_LITERAL("//$C/ok")); + base::FilePath::StringType custom2_vars_expected( + FILE_PATH_LITERAL("//$C/ok")); + + path_parser::ReplaceVariableInPathWithValue(buggy, &GetBuggy, &custom2_vars); + ASSERT_EQ(custom2_vars, custom2_vars_expected); +} + +TEST_F(PolicyPathParserTests, CommonExpandPathVariables) { // No vars whatsoever no substitution should occur. base::FilePath::StringType no_vars(FILE_PATH_LITERAL("//$C/shares")); base::FilePath::StringType no_vars_result = @@ -50,7 +78,15 @@ TEST_F(PolicyPathParserTests, MAYBE_AllPlatformVariables) { ASSERT_EQ(quotes_result, no_quotes); quotes_result = path_parser::ExpandPathVariables(double_quotes); ASSERT_EQ(quotes_result, no_quotes); +} +#if defined(OS_MACOSX) +// http://crbug.com/327520 +#define MAYBE_AllPlatformVariables DISABLED_AllPlatformVariables +#else +#define MAYBE_AllPlatformVariables AllPlatformVariables +#endif +TEST_F(PolicyPathParserTests, MAYBE_AllPlatformVariables) { // Both should have been substituted. base::FilePath::StringType vars( FILE_PATH_LITERAL("${user_name}${machine_name}")); diff --git a/chrome/browser/policy/policy_path_parser_win.cc b/chrome/browser/policy/policy_path_parser_win.cc index 71c9011..6f496d8 100644 --- a/chrome/browser/policy/policy_path_parser_win.cc +++ b/chrome/browser/policy/policy_path_parser_win.cc @@ -8,6 +8,7 @@ #include "chrome/browser/policy/policy_path_parser.h" +#include "base/bind.h" #include "base/memory/scoped_ptr.h" #include "base/strings/utf_string_conversions.h" #include "base/win/registry.h" @@ -28,104 +29,105 @@ bool LoadUserDataDirPolicyFromRegistry(HKEY hive, base::FilePath* dir) { return false; } -const WCHAR* kMachineNamePolicyVarName = L"${machine_name}"; -const WCHAR* kUserNamePolicyVarName = L"${user_name}"; -const WCHAR* kWinDocumentsFolderVarName = L"${documents}"; -const WCHAR* kWinLocalAppDataFolderVarName = L"${local_app_data}"; -const WCHAR* kWinRoamingAppDataFolderVarName = L"${roaming_app_data}"; -const WCHAR* kWinProfileFolderVarName = L"${profile}"; -const WCHAR* kWinProgramDataFolderVarName = L"${global_app_data}"; -const WCHAR* kWinProgramFilesFolderVarName = L"${program_files}"; -const WCHAR* kWinWindowsFolderVarName = L"${windows}"; -const WCHAR* kWinClientName = L"${client_name}"; - -struct WinFolderNamesToCSIDLMapping { - const WCHAR* name; - int id; -}; - -// Mapping from variable names to Windows CSIDL ids. -const WinFolderNamesToCSIDLMapping win_folder_mapping[] = { - { kWinWindowsFolderVarName, CSIDL_WINDOWS}, - { kWinProgramFilesFolderVarName, CSIDL_PROGRAM_FILES}, - { kWinProgramDataFolderVarName, CSIDL_COMMON_APPDATA}, - { kWinProfileFolderVarName, CSIDL_PROFILE}, - { kWinLocalAppDataFolderVarName, CSIDL_LOCAL_APPDATA}, - { kWinRoamingAppDataFolderVarName, CSIDL_APPDATA}, - { kWinDocumentsFolderVarName, CSIDL_PERSONAL} -}; - } // namespace namespace policy { namespace path_parser { -// 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); - if (result.length() == 0) - return result; - // Sanitize quotes in case of any around the whole string. - if (result.length() > 1 && - ((result[0] == L'"' && result[result.length() - 1] == L'"') || - (result[0] == L'\'' && result[result.length() - 1] == L'\''))) { - // Strip first and last char which should be matching quotes now. - result = result.substr(1, result.length() - 2); - } - // First translate all path variables we recognize. - for (int 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); - std::wstring path_string(path); - result.replace(position, wcslen(win_folder_mapping[i].name), path_string); - } - } - // Next translate other windows specific variables. - size_t position = result.find(kUserNamePolicyVarName); - if (position != std::wstring::npos) { - DWORD return_length = 0; - ::GetUserName(NULL, &return_length); - if (return_length != 0) { - scoped_ptr<WCHAR[]> username(new WCHAR[return_length]); - ::GetUserName(username.get(), &return_length); - std::wstring username_string(username.get()); - result.replace(position, wcslen(kUserNamePolicyVarName), username_string); - } +namespace internal { + +bool GetSpecialFolderPath(int id, base::FilePath::StringType* value) { + WCHAR path[MAX_PATH]; + ::SHGetSpecialFolderPath(0, path, id, false); + *value = base::FilePath::StringType(path); + return true; +} + +// The wrapper is used instead of callbacks since these function are +// initialized in a global table and using callbacks in the table results in +// the increase in size of static initializers. +#define WRAP_GET_FORLDER_FUNCTION(FunctionName, FolderId) \ + bool FunctionName(base::FilePath::StringType* value) { \ + return GetSpecialFolderPath(FolderId, value); \ } - position = result.find(kMachineNamePolicyVarName); - if (position != std::wstring::npos) { - DWORD return_length = 0; - ::GetComputerNameEx(ComputerNamePhysicalDnsHostname, NULL, &return_length); - if (return_length != 0) { - scoped_ptr<WCHAR[]> machinename(new WCHAR[return_length]); - ::GetComputerNameEx(ComputerNamePhysicalDnsHostname, - machinename.get(), &return_length); - std::wstring machinename_string(machinename.get()); - result.replace( - position, wcslen(kMachineNamePolicyVarName), machinename_string); - } + +WRAP_GET_FORLDER_FUNCTION(GetWindowsFolderPath, CSIDL_WINDOWS) +WRAP_GET_FORLDER_FUNCTION(GetProgramFilesFolderPath, CSIDL_PROGRAM_FILES) +WRAP_GET_FORLDER_FUNCTION(GetProgramDataFolderPath, CSIDL_COMMON_APPDATA) +WRAP_GET_FORLDER_FUNCTION(GetProfileFolderPath, CSIDL_PROFILE) +WRAP_GET_FORLDER_FUNCTION(GetLocalAppDataFolderPath, CSIDL_LOCAL_APPDATA) +WRAP_GET_FORLDER_FUNCTION(GetRoamingAppDataFolderPath, CSIDL_APPDATA) +WRAP_GET_FORLDER_FUNCTION(GetDocumentsFolderPath, CSIDL_PERSONAL) + +bool GetWindowsUserName(base::FilePath::StringType* value) { + DWORD return_length = 0; + ::GetUserName(NULL, &return_length); + if (return_length) { + scoped_ptr<WCHAR[]> username(new WCHAR[return_length]); + ::GetUserName(username.get(), &return_length); + *value = base::FilePath::StringType(username.get()); } - 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)) { - std::wstring clientname_string(buffer); - result.replace(position, wcslen(kWinClientName), clientname_string); - ::WTSFreeMemory(buffer); - } + return (return_length != 0); +} + +bool GetMachineName(base::FilePath::StringType* value) { + DWORD return_length = 0; + ::GetComputerNameEx(ComputerNamePhysicalDnsHostname, NULL, &return_length); + if (return_length) { + scoped_ptr<WCHAR[]> machinename(new WCHAR[return_length]); + ::GetComputerNameEx( + ComputerNamePhysicalDnsHostname, machinename.get(), &return_length); + *value = base::FilePath::StringType(machinename.get()); } + return (return_length != 0); +} - return result; +bool GetClientName(base::FilePath::StringType* value) { + LPWSTR buffer = NULL; + DWORD buffer_length = 0; + BOOL status; + if ((status = ::WTSQuerySessionInformation(WTS_CURRENT_SERVER, + WTS_CURRENT_SESSION, + WTSClientName, + &buffer, + &buffer_length))) { + *value = base::FilePath::StringType(buffer); + ::WTSFreeMemory(buffer); + } + return (status == TRUE); } +const WCHAR* kMachineNamePolicyVarName = L"${machine_name}"; +const WCHAR* kUserNamePolicyVarName = L"${user_name}"; +const WCHAR* kWinDocumentsFolderVarName = L"${documents}"; +const WCHAR* kWinLocalAppDataFolderVarName = L"${local_app_data}"; +const WCHAR* kWinRoamingAppDataFolderVarName = L"${roaming_app_data}"; +const WCHAR* kWinProfileFolderVarName = L"${profile}"; +const WCHAR* kWinProgramDataFolderVarName = L"${global_app_data}"; +const WCHAR* kWinProgramFilesFolderVarName = L"${program_files}"; +const WCHAR* kWinWindowsFolderVarName = L"${windows}"; +const WCHAR* kWinClientName = L"${client_name}"; + +// A table mapping variable names to their respective get value function +// pointers. +const VariableNameAndValueCallback kVariableNameAndValueCallbacks[] = { + {kWinWindowsFolderVarName, &GetWindowsFolderPath}, + {kWinProgramFilesFolderVarName, &GetProgramFilesFolderPath}, + {kWinProgramDataFolderVarName, &GetProgramDataFolderPath}, + {kWinProfileFolderVarName, &GetProfileFolderPath}, + {kWinLocalAppDataFolderVarName, &GetLocalAppDataFolderPath}, + {kWinRoamingAppDataFolderVarName, &GetRoamingAppDataFolderPath}, + {kWinDocumentsFolderVarName, &GetDocumentsFolderPath}, + {kUserNamePolicyVarName, &GetWindowsUserName}, + {kMachineNamePolicyVarName, &GetMachineName}, + {kWinClientName, &GetClientName}}; + +// Total number of entries in the mapping table. +const int kNoOfVariables = arraysize(kVariableNameAndValueCallbacks); + +} // namespace internal + void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { DCHECK(user_data_dir); // Policy from the HKLM hive has precedence over HKCU. diff --git a/chrome/policy.gypi b/chrome/policy.gypi index 79cc543..60de7fe 100644 --- a/chrome/policy.gypi +++ b/chrome/policy.gypi @@ -20,6 +20,7 @@ 'browser/policy/policy_path_parser_linux.cc', 'browser/policy/policy_path_parser_mac.mm', 'browser/policy/policy_path_parser_win.cc', + 'browser/policy/policy_path_parser.cc', ], }, ], |