Commit 026e21eb authored by Avi Drissman's avatar Avi Drissman Committed by Commit Bot

Perform minor cleanup on the Mac policy path parser.

Code should use the scoped retain/release classes.

BUG=none

Change-Id: I0c538d7ae64127bba9b783fd08584f3164dc949d
Reviewed-on: https://chromium-review.googlesource.com/1148476
Commit-Queue: Avi Drissman <avi@chromium.org>
Reviewed-by: default avatarJulian Pastarmov <pastarmovj@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577960}
parent a30c58ea
...@@ -29,16 +29,15 @@ const char kMachineNamePolicyVarName[] = "${machine_name}"; ...@@ -29,16 +29,15 @@ const char kMachineNamePolicyVarName[] = "${machine_name}";
const char kMacUsersDirectory[] = "${users}"; const char kMacUsersDirectory[] = "${users}";
const char kMacDocumentsFolderVarName[] = "${documents}"; const char kMacDocumentsFolderVarName[] = "${documents}";
struct MacFolderNamesToSPDMaping { struct MacFolderNamesToSPDMapping {
const char* name; const char* name;
NSSearchPathDirectory id; NSSearchPathDirectory id;
}; };
// Mapping from variable names to MacOS NSSearchPathDirectory ids. // Mapping from variable names to MacOS NSSearchPathDirectory ids.
const MacFolderNamesToSPDMaping mac_folder_mapping[] = { const MacFolderNamesToSPDMapping mac_folder_mapping[] = {
{ kMacUsersDirectory, NSUserDirectory}, {kMacUsersDirectory, NSUserDirectory},
{ kMacDocumentsFolderVarName, NSDocumentDirectory} {kMacDocumentsFolderVarName, NSDocumentDirectory}};
};
// Replaces all variable occurrences in the policy string with the respective // Replaces all variable occurrences in the policy string with the respective
// system settings values. // system settings values.
...@@ -55,14 +54,14 @@ base::FilePath::StringType ExpandPathVariables( ...@@ -55,14 +54,14 @@ base::FilePath::StringType ExpandPathVariables(
result = result.substr(1, result.length() - 2); result = result.substr(1, result.length() - 2);
} }
// First translate all path variables we recognize. // First translate all path variables we recognize.
for (size_t i = 0; i < arraysize(mac_folder_mapping); ++i) { for (const auto& mapping : mac_folder_mapping) {
size_t position = result.find(mac_folder_mapping[i].name); size_t position = result.find(mapping.name);
if (position != std::string::npos) { if (position != std::string::npos) {
NSArray* searchpaths = NSSearchPathForDirectoriesInDomains( NSArray* searchpaths = NSSearchPathForDirectoriesInDomains(
mac_folder_mapping[i].id, NSAllDomainsMask, true); mapping.id, NSAllDomainsMask, true);
if ([searchpaths count] > 0) { if ([searchpaths count] > 0) {
NSString *variable_value = [searchpaths objectAtIndex:0]; NSString *variable_value = [searchpaths objectAtIndex:0];
result.replace(position, strlen(mac_folder_mapping[i].name), result.replace(position, strlen(mapping.name),
base::SysNSStringToUTF8(variable_value)); base::SysNSStringToUTF8(variable_value));
} }
} }
...@@ -80,21 +79,19 @@ base::FilePath::StringType ExpandPathVariables( ...@@ -80,21 +79,19 @@ base::FilePath::StringType ExpandPathVariables(
} }
position = result.find(kMachineNamePolicyVarName); position = result.find(kMachineNamePolicyVarName);
if (position != std::string::npos) { if (position != std::string::npos) {
SCDynamicStoreContext context = { 0, NULL, NULL, NULL }; SCDynamicStoreContext context = {0, nullptr, nullptr, nullptr};
SCDynamicStoreRef store = SCDynamicStoreCreate(kCFAllocatorDefault, base::ScopedCFTypeRef<SCDynamicStoreRef> store(SCDynamicStoreCreate(
CFSTR("policy_subsystem"), kCFAllocatorDefault, CFSTR("policy_subsystem"), nullptr, &context));
NULL, &context); base::ScopedCFTypeRef<CFStringRef> machinename(
CFStringRef machinename = SCDynamicStoreCopyLocalHostName(store); SCDynamicStoreCopyLocalHostName(store));
if (machinename) { if (machinename) {
result.replace(position, strlen(kMachineNamePolicyVarName), result.replace(position, strlen(kMachineNamePolicyVarName),
base::SysCFStringRefToUTF8(machinename)); base::SysCFStringRefToUTF8(machinename));
CFRelease(machinename);
} else { } else {
int error = SCError(); int error = SCError();
LOG(ERROR) << "Machine name variable can not be resolved. Error: " LOG(ERROR) << "Machine name variable can not be resolved. Error: "
<< error << " - " << SCErrorString(error); << error << " - " << SCErrorString(error);
} }
CFRelease(store);
} }
return result; return result;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment