diff options
author | pastarmovj@chromium.org <pastarmovj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-11 16:17:36 +0000 |
---|---|---|
committer | pastarmovj@chromium.org <pastarmovj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-02-11 16:17:36 +0000 |
commit | f2c72badbed1c564c1d0813c96be40f70ce3c110 (patch) | |
tree | d0277e069222771230f217bd43e988db329cd51d /chrome/app | |
parent | a8274453e82a243b65436b8b3e5c5bff80ab90c7 (diff) | |
download | chromium_src-f2c72badbed1c564c1d0813c96be40f70ce3c110.zip chromium_src-f2c72badbed1c564c1d0813c96be40f70ce3c110.tar.gz chromium_src-f2c72badbed1c564c1d0813c96be40f70ce3c110.tar.bz2 |
Mac implementation of the UserDataDir policy.
BUG=49601
TEST=Manual. Set the policy in the plist file and it should be equivalent to using the --user-data-dir option.
Review URL: http://codereview.chromium.org/6349082
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@74602 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/app')
-rw-r--r-- | chrome/app/chrome_main.cc | 2 | ||||
-rw-r--r-- | chrome/app/chrome_main_mac.mm | 95 | ||||
-rw-r--r-- | chrome/app/chrome_main_win.cc | 4 |
3 files changed, 100 insertions, 1 deletions
diff --git a/chrome/app/chrome_main.cc b/chrome/app/chrome_main.cc index 051b43a..3933855 100644 --- a/chrome/app/chrome_main.cc +++ b/chrome/app/chrome_main.cc @@ -122,7 +122,7 @@ int ChromeMain(int argc, char** argv); // |user_data_dir| parameter. If no policy is set the parameter is not changed. void CheckUserDataDirPolicy(FilePath* user_data_dir); -#if !defined(OS_WIN) +#if !defined(OS_WIN) && !defined(OS_MACOSX) // 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 . diff --git a/chrome/app/chrome_main_mac.mm b/chrome/app/chrome_main_mac.mm new file mode 100644 index 0000000..3aca6e4 --- /dev/null +++ b/chrome/app/chrome_main_mac.mm @@ -0,0 +1,95 @@ +// 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. + +#import <Cocoa/Cocoa.h> + +#include <string> + +#include "base/basictypes.h" +#include "base/file_path.h" +#include "base/logging.h" +#include "base/sys_string_conversions.h" +#include "policy/policy_constants.h" + +namespace { + +const char* kUserNamePolicyVarName = "${user_name}"; +const char* kMachineNamePolicyVarName = "${machine_name}"; +const char* kMacUsersDirectory = "${users}"; +const char* kMacDocumentsFolderVarName = "${documents}"; + +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. +std::string TranslateMacVariablesInPolicy( + const std::string& untranslated_string) { + std::string result(untranslated_string); + // 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)); + } + } + } + // 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."; + } + } + position = result.find(kMachineNamePolicyVarName); + if (position != std::string::npos) { + NSString *machinename = [[NSHost currentHost] name]; + if (machinename) { + result.replace(position, strlen(kMachineNamePolicyVarName), + base::SysNSStringToUTF8(machinename)); + } else { + LOG(ERROR) << "Machine name variable can not be resolved."; + } + } + return result; +} + +} // namespace + +// 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) { + // Since the configuration management infrastructure is not initialized when + // this code runs, read the policy preference directly. + CFStringRef key = base::SysUTF8ToCFStringRef(policy::key::kUserDataDir); + CFPropertyListRef value = + CFPreferencesCopyAppValue(key, kCFPreferencesCurrentApplication); + if (value && + CFPreferencesAppValueIsForced(key, kCFPreferencesCurrentApplication)) { + if (CFGetTypeID(value) == CFStringGetTypeID()) { + std::string string_value = + base::SysCFStringRefToUTF8((CFStringRef)value); + // Now replace any vars the user might have used. + string_value = TranslateMacVariablesInPolicy(string_value); + *user_data_dir = FilePath(string_value); + } + } +} diff --git a/chrome/app/chrome_main_win.cc b/chrome/app/chrome_main_win.cc index 774d3a4..021fd9b 100644 --- a/chrome/app/chrome_main_win.cc +++ b/chrome/app/chrome_main_win.cc @@ -10,6 +10,8 @@ #include "base/win/registry.h" #include "policy/policy_constants.h" +namespace { + const WCHAR* kMachineNamePolicyVarName = L"${machine_name}"; const WCHAR* kUserNamePolicyVarName = L"${user_name}"; const WCHAR* kWinDocumentsFolderVarName = L"${documents}"; @@ -92,6 +94,8 @@ bool LoadUserDataDirPolicyFromRegistry(HKEY hive, FilePath* user_data_dir) { return false; } +} // namespace + // 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) { |