diff options
author | pastarmovj@chromium.org <pastarmovj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-09 15:23:02 +0000 |
---|---|---|
committer | pastarmovj@chromium.org <pastarmovj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-09 15:23:02 +0000 |
commit | d24b937688ac39a04e84450d5205596c8e08fd92 (patch) | |
tree | 3dec80eded91702acd28bcd10d6a88101541a77b /chrome/app | |
parent | 1930a11fbd7d49a75ad341b0c33489c60dfaaa11 (diff) | |
download | chromium_src-d24b937688ac39a04e84450d5205596c8e08fd92.zip chromium_src-d24b937688ac39a04e84450d5205596c8e08fd92.tar.gz chromium_src-d24b937688ac39a04e84450d5205596c8e08fd92.tar.bz2 |
Replaced environment variables for our own ones in the UserDataDir policy for Windows.
BUG=49601
TEST=Manual. Set the policy in windows and it should be equivalent to using the --user-data-dir option.
Review URL: http://codereview.chromium.org/6250192
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@74290 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/app')
-rw-r--r-- | chrome/app/chrome_main.cc | 51 | ||||
-rw-r--r-- | chrome/app/chrome_main_win.cc | 102 |
2 files changed, 114 insertions, 39 deletions
diff --git a/chrome/app/chrome_main.cc b/chrome/app/chrome_main.cc index 45b60f0..051b43a 100644 --- a/chrome/app/chrome_main.cc +++ b/chrome/app/chrome_main.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 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. @@ -47,7 +47,6 @@ #include <malloc.h> #include <new.h> #include "base/win/registry.h" -#include "policy/policy_constants.h" #include "sandbox/src/sandbox.h" #include "tools/memory_watcher/memory_watcher.h" #endif @@ -119,6 +118,17 @@ int ChromeMain(int argc, char** argv); } #endif +// Checks if the UserDataDir policy has been set and returns its value in the +// |user_data_dir| parameter. If no policy is set the parameter is not changed. +void CheckUserDataDirPolicy(FilePath* user_data_dir); + +#if !defined(OS_WIN) +// This policy is not implemented for Linux and ChromeOS. The win and mac ver- +// sions are implemented in the platform specific version of chrome_main.cc e.g. +// chrome_main_win.cc or chrome_main_mac.mm . +void CheckUserDataDirPolicy(FilePath* user_data_dir) {} +#endif + namespace { #if defined(OS_WIN) @@ -517,43 +527,6 @@ void HandleHelpSwitches(const CommandLine& command_line) { #endif // OS_POSIX -#if defined(OS_WIN) -// Checks if the registry key exists in the given hive and expands any -// environment variables in the string. -bool LoadUserDataDirPolicyFromRegistry(HKEY hive, FilePath* user_data_dir) { - std::wstring key_name = UTF8ToWide(policy::key::kUserDataDir); - std::wstring value; - - base::win::RegKey hklm_policy_key(hive, policy::kRegistrySubKey, KEY_READ); - if (hklm_policy_key.ReadValue(key_name.c_str(), &value) == ERROR_SUCCESS) { - // Try to expand the env variables if not possible fallback to no expansion. - DWORD expanded_len = ::ExpandEnvironmentStrings(value.c_str(), NULL, 0); - if (expanded_len) { - scoped_array<WCHAR> expanded_value(new WCHAR[expanded_len]); - ::ExpandEnvironmentStrings(value.c_str(), - expanded_value.get(), expanded_len); - *user_data_dir = FilePath::FromWStringHack(expanded_value.get()); - } else { - *user_data_dir = FilePath::FromWStringHack(value); - } - return true; - } - return false; -} -#endif // OS_WIN - -// Checks if the UserDataDir policy has been set and returns its value in the -// |user_data_dir| parameter. If no policy is set the parameter is not changed. -void CheckUserDataDirPolicy(FilePath* user_data_dir) { -#if defined(OS_WIN) - // Policy from the HKLM hive has precedence over HKCU so if we have one here - // we don't have to try to load HKCU. - if (LoadUserDataDirPolicyFromRegistry(HKEY_LOCAL_MACHINE, user_data_dir)) - return; - LoadUserDataDirPolicyFromRegistry(HKEY_CURRENT_USER, user_data_dir); -#endif -} - // We dispatch to a process-type-specific FooMain() based on a command-line // flag. This struct is used to build a table of (flag, main function) pairs. struct MainFunction { diff --git a/chrome/app/chrome_main_win.cc b/chrome/app/chrome_main_win.cc new file mode 100644 index 0000000..774d3a4 --- /dev/null +++ b/chrome/app/chrome_main_win.cc @@ -0,0 +1,102 @@ +// Copyright (c) 2011 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 <shlobj.h> + +#include "base/file_path.h" +#include "base/scoped_ptr.h" +#include "base/utf_string_conversions.h" +#include "base/win/registry.h" +#include "policy/policy_constants.h" + +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* kWinProfileFolderVarName = L"${profile}"; +const WCHAR* kWinProgramDataFolderVarName = L"${global_app_data}"; +const WCHAR* kWinProgramFilesFolderVarName = L"${program_files}"; +const WCHAR* kWinWindowsFolderVarName = L"${windows}"; + +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}, + { kWinDocumentsFolderVarName, CSIDL_PERSONAL} +}; + +// Replaces all variable occurances in the policy string with the respective +// system settings values. +std::wstring TranslateWinVariablesInPolicy( + const std::wstring& untranslated_string) { + std::wstring result(untranslated_string); + // 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 two speacial variables ${user_name} and ${machine_name} + size_t position = result.find(kUserNamePolicyVarName); + if (position != std::wstring::npos) { + DWORD return_length = 0; + ::GetUserName(NULL, &return_length); + if (return_length != 0) { + scoped_array<WCHAR> username(new WCHAR[return_length]); + ::GetUserName(username.get(), &return_length); + std::wstring username_string(username.get()); + result.replace(position, wcslen(kUserNamePolicyVarName), username_string); + } + } + position = result.find(kMachineNamePolicyVarName); + if (position != std::wstring::npos) { + DWORD return_length = 0; + ::GetComputerNameEx(ComputerNamePhysicalDnsHostname, NULL, &return_length); + if (return_length != 0) { + scoped_array<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); + } + } + return result; +} + +// Checks if the registry key exists in the given hive and expands any +// variables in the string. +bool LoadUserDataDirPolicyFromRegistry(HKEY hive, FilePath* user_data_dir) { + std::wstring key_name = UTF8ToWide(policy::key::kUserDataDir); + std::wstring value; + + base::win::RegKey hklm_policy_key(hive, policy::kRegistrySubKey, KEY_READ); + if (hklm_policy_key.ReadValue(key_name.c_str(), &value) == ERROR_SUCCESS) { + *user_data_dir = FilePath::FromWStringHack( + TranslateWinVariablesInPolicy(value)); + return true; + } + return false; +} + +// Checks if the UserDataDir policy has been set and returns its value in the +// |user_data_dir| parameter. If no policy is set the parameter is not changed. +void CheckUserDataDirPolicy(FilePath* user_data_dir) { + // Policy from the HKLM hive has precedence over HKCU so if we have one here + // we don't have to try to load HKCU. + if (!LoadUserDataDirPolicyFromRegistry(HKEY_LOCAL_MACHINE, user_data_dir)) + LoadUserDataDirPolicyFromRegistry(HKEY_CURRENT_USER, user_data_dir); +} |