Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(381)

Side by Side Diff: chrome/browser/policy/policy_path_parser_mac.mm

Issue 214233003: Refactorise the policy_path_parser framework (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Avoids static initialization Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/policy/policy_path_parser.h" 5 #include "chrome/browser/policy/policy_path_parser.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #import "base/mac/scoped_nsautorelease_pool.h" 10 #import "base/mac/scoped_nsautorelease_pool.h"
11 #include "base/strings/sys_string_conversions.h" 11 #include "base/strings/sys_string_conversions.h"
12 #include "policy/policy_constants.h" 12 #include "policy/policy_constants.h"
13 13
14 #import <Cocoa/Cocoa.h> 14 #import <Cocoa/Cocoa.h>
15 #import <SystemConfiguration/SCDynamicStore.h> 15 #import <SystemConfiguration/SCDynamicStore.h>
16 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h> 16 #import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
17 17
18 #include <string> 18 #include <string>
19 19
20 namespace policy { 20 namespace policy {
21 21
22 namespace path_parser { 22 namespace path_parser {
23 23
24 namespace internal {
25
26 bool GetFolder(NSSearchPathDirectory id, base::FilePath::StringType* value) {
27 NSArray* searchpaths =
28 NSSearchPathForDirectoriesInDomains(id, NSAllDomainsMask, true);
29 if ([searchpaths count] > 0) {
30 NSString* variable_value = [searchpaths objectAtIndex:0];
31 *value = base::SysNSStringToUTF8(variable_value);
32 return true;
33 }
34 return false;
35 }
36
37 #define WRAP_GET_FORLDER_FUNCTION(FunctionName, MacId) \
38 bool FunctionName(base::FilePath::StringType* value) { \
39 return GetFolder(MacId, value); \
40 }
41
42 WRAP_GET_FORLDER_FUNCTION(GetMacUserFolderPath, NSUserDirectory)
43 WRAP_GET_FORLDER_FUNCTION(GetMacDocumentsFolderPath, NSDocumentDirectory)
44
45 bool GetUserName(base::FilePath::StringType* value) {
46 NSString* username = NSUserName();
47 if (username)
48 *value = base::SysNSStringToUTF8(username);
49 else
50 LOG(ERROR) << "Username variable can not be resolved.";
51 return (username != NULL);
52 }
53
54 bool GetMachineName(base::FilePath::StringType* value) {
55 SCDynamicStoreContext context = {0, NULL, NULL, NULL};
56 SCDynamicStoreRef store = SCDynamicStoreCreate(
57 kCFAllocatorDefault, CFSTR("policy_subsystem"), NULL, &context);
58 CFStringRef machinename = SCDynamicStoreCopyLocalHostName(store);
59 if (machinename) {
60 *value = base::SysCFStringRefToUTF8(machinename);
61 CFRelease(machinename);
62 } else {
63 LOG(ERROR) << "Machine name variable can not be resolved.";
64 }
65 CFRelease(store);
66 return (machinename != NULL);
67 }
68
24 const char* kUserNamePolicyVarName = "${user_name}"; 69 const char* kUserNamePolicyVarName = "${user_name}";
25 const char* kMachineNamePolicyVarName = "${machine_name}"; 70 const char* kMachineNamePolicyVarName = "${machine_name}";
26 const char* kMacUsersDirectory = "${users}"; 71 const char* kMacUsersDirectory = "${users}";
27 const char* kMacDocumentsFolderVarName = "${documents}"; 72 const char* kMacDocumentsFolderVarName = "${documents}";
28 73
29 struct MacFolderNamesToSPDMaping { 74 // A table mapping variable names to their respective get value function
30 const char* name; 75 // pointers.
31 NSSearchPathDirectory id; 76 const VariableNameAndValueCallback kVariableNameAndValueCallbacks[] = {
32 }; 77 {kUserNamePolicyVarName, &GetUserName},
78 {kMachineNamePolicyVarName, &GetMachineName},
79 {kMacUsersDirectory, &GetMacUserFolderPath},
80 {kMacDocumentsFolderVarName, &GetMacDocumentsFolderPath}};
33 81
34 // Mapping from variable names to MacOS NSSearchPathDirectory ids. 82 // Total number of entries in the mapping table.
35 const MacFolderNamesToSPDMaping mac_folder_mapping[] = { 83 const int kNoOfVariables = arraysize(kVariableNameAndValueCallbacks);
36 { kMacUsersDirectory, NSUserDirectory},
37 { kMacDocumentsFolderVarName, NSDocumentDirectory}
38 };
39 84
40 // Replaces all variable occurrences in the policy string with the respective 85 } // namespace internal
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)
46 return result;
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);
53 }
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));
64 }
65 }
66 }
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();
71 if (username) {
72 result.replace(position, strlen(kUserNamePolicyVarName),
73 base::SysNSStringToUTF8(username));
74 } else {
75 LOG(ERROR) << "Username variable can not be resolved.";
76 }
77 }
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"),
83 NULL, &context);
84 CFStringRef machinename = SCDynamicStoreCopyLocalHostName(store);
85 if (machinename) {
86 result.replace(position, strlen(kMachineNamePolicyVarName),
87 base::SysCFStringRefToUTF8(machinename));
88 CFRelease(machinename);
89 } else {
90 LOG(ERROR) << "Machine name variable can not be resolved.";
91 }
92 CFRelease(store);
93 }
94 return result;
95 }
96 86
97 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) { 87 void CheckUserDataDirPolicy(base::FilePath* user_data_dir) {
98 base::mac::ScopedNSAutoreleasePool pool; 88 base::mac::ScopedNSAutoreleasePool pool;
99 89
100 // Since the configuration management infrastructure is not initialized when 90 // Since the configuration management infrastructure is not initialized when
101 // this code runs, read the policy preference directly. 91 // this code runs, read the policy preference directly.
102 NSString* key = base::SysUTF8ToNSString(policy::key::kUserDataDir); 92 NSString* key = base::SysUTF8ToNSString(policy::key::kUserDataDir);
103 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 93 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
104 NSString* value = [defaults stringForKey:key]; 94 NSString* value = [defaults stringForKey:key];
105 if (value && [defaults objectIsForcedForKey:key]) { 95 if (value && [defaults objectIsForcedForKey:key]) {
106 std::string string_value = base::SysNSStringToUTF8(value); 96 std::string string_value = base::SysNSStringToUTF8(value);
107 // Now replace any vars the user might have used. 97 // Now replace any vars the user might have used.
108 string_value = policy::path_parser::ExpandPathVariables(string_value); 98 string_value = policy::path_parser::ExpandPathVariables(string_value);
109 *user_data_dir = base::FilePath(string_value); 99 *user_data_dir = base::FilePath(string_value);
110 } 100 }
111 } 101 }
112 102
113 } // namespace path_parser 103 } // namespace path_parser
114 104
115 } // namespace policy 105 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698