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
3 // found in the LICENSE file.
5 #include "chrome/browser/policy/policy_path_parser.h"
7 #include "base/basictypes.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #import "base/mac/scoped_nsautorelease_pool.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "policy/policy_constants.h"
14 #import <Cocoa/Cocoa.h>
15 #import <SystemConfiguration/SCDynamicStore.h>
16 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
22 namespace path_parser {
24 const char* kUserNamePolicyVarName = "${user_name}";
25 const char* kMachineNamePolicyVarName = "${machine_name}";
26 const char* kMacUsersDirectory = "${users}";
27 const char* kMacDocumentsFolderVarName = "${documents}";
29 struct MacFolderNamesToSPDMaping {
31 NSSearchPathDirectory id;
34 // Mapping from variable names to MacOS NSSearchPathDirectory ids.
35 const MacFolderNamesToSPDMaping mac_folder_mapping[] = {
36 { kMacUsersDirectory, NSUserDirectory},
37 { kMacDocumentsFolderVarName, NSDocumentDirectory}
40 // Replaces all variable occurrences in the policy string with the respective
41 // system settings values.
42 base::FilePath::StringType ExpandPathVariables(
43 const base::FilePath::StringType& untranslated_string) {
44 base::FilePath::StringType result(untranslated_string);
45 if (result.length() == 0)
47 // Sanitize quotes in case of any around the whole string.
48 if (result.length() > 1 &&
49 ((result[0] == '"' && result[result.length() - 1] == '"') ||
50 (result[0] == '\'' && result[result.length() - 1] == '\''))) {
51 // Strip first and last char which should be matching quotes now.
52 result = result.substr(1, result.length() - 2);
54 // First translate all path variables we recognize.
55 for (size_t i = 0; i < arraysize(mac_folder_mapping); ++i) {
56 size_t position = result.find(mac_folder_mapping[i].name);
57 if (position != std::string::npos) {
58 NSArray* searchpaths = NSSearchPathForDirectoriesInDomains(
59 mac_folder_mapping[i].id, NSAllDomainsMask, true);
60 if ([searchpaths count] > 0) {
61 NSString *variable_value = [searchpaths objectAtIndex:0];
62 result.replace(position, strlen(mac_folder_mapping[i].name),
63 base::SysNSStringToUTF8(variable_value));
67 // Next translate two special variables ${user_name} and ${machine_name}
68 size_t position = result.find(kUserNamePolicyVarName);
69 if (position != std::string::npos) {
70 NSString* username = NSUserName();
72 result.replace(position, strlen(kUserNamePolicyVarName),
73 base::SysNSStringToUTF8(username));
75 LOG(ERROR) << "Username variable can not be resolved.";
78 position = result.find(kMachineNamePolicyVarName);
79 if (position != std::string::npos) {
80 SCDynamicStoreContext context = { 0, NULL, NULL, NULL };
81 SCDynamicStoreRef store = SCDynamicStoreCreate(kCFAllocatorDefault,
82 CFSTR("policy_subsystem"),
84 CFStringRef machinename = SCDynamicStoreCopyLocalHostName(store);
86 result.replace(position, strlen(kMachineNamePolicyVarName),
87 base::SysCFStringRefToUTF8(machinename));
88 CFRelease(machinename);
90 LOG(ERROR) << "Machine name variable can not be resolved.";
97 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) {
98 base::mac::ScopedNSAutoreleasePool pool;
100 // Since the configuration management infrastructure is not initialized when
101 // this code runs, read the policy preference directly.
102 NSString* key = base::SysUTF8ToNSString(policy::key::kUserDataDir);
103 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
104 NSString* value = [defaults stringForKey:key];
105 if (value && [defaults objectIsForcedForKey:key]) {
106 std::string string_value = base::SysNSStringToUTF8(value);
107 // Now replace any vars the user might have used.
109 policy::path_parser::ExpandPathVariables(string_value);
110 *user_data_dir = base::FilePath(string_value);
114 } // namespace path_parser
116 } // namespace policy