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

Unified 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, 9 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/policy/policy_path_parser_mac.mm
diff --git a/chrome/browser/policy/policy_path_parser_mac.mm b/chrome/browser/policy/policy_path_parser_mac.mm
index b734faa3b554cdb5bb98d5d751997a5a2f3b519b..f723a89682cf2770f45cea4182a20e8a2f87c430 100644
--- a/chrome/browser/policy/policy_path_parser_mac.mm
+++ b/chrome/browser/policy/policy_path_parser_mac.mm
@@ -4,7 +4,7 @@
#include "chrome/browser/policy/policy_path_parser.h"
-#include "base/basictypes.h"
+#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#import "base/mac/scoped_nsautorelease_pool.h"
@@ -21,79 +21,69 @@ namespace policy {
namespace path_parser {
-const char* kUserNamePolicyVarName = "${user_name}";
-const char* kMachineNamePolicyVarName = "${machine_name}";
-const char* kMacUsersDirectory = "${users}";
-const char* kMacDocumentsFolderVarName = "${documents}";
+namespace internal {
-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.
-base::FilePath::StringType ExpandPathVariables(
- const base::FilePath::StringType& untranslated_string) {
- base::FilePath::StringType result(untranslated_string);
- if (result.length() == 0)
- return result;
- // Sanitize quotes in case of any around the whole string.
- if (result.length() > 1 &&
- ((result[0] == '"' && result[result.length() - 1] == '"') ||
- (result[0] == '\'' && result[result.length() - 1] == '\''))) {
- // Strip first and last char which should be matching quotes now.
- result = result.substr(1, result.length() - 2);
- }
- // 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));
- }
- }
+bool GetFolder(NSSearchPathDirectory id, base::FilePath::StringType* value) {
+ NSArray* searchpaths =
+ NSSearchPathForDirectoriesInDomains(id, NSAllDomainsMask, true);
+ if ([searchpaths count] > 0) {
+ NSString* variable_value = [searchpaths objectAtIndex:0];
+ *value = base::SysNSStringToUTF8(variable_value);
+ return true;
}
- // 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.";
- }
+ return false;
+}
+
+#define WRAP_GET_FORLDER_FUNCTION(FunctionName, MacId) \
+ bool FunctionName(base::FilePath::StringType* value) { \
+ return GetFolder(MacId, value); \
}
- position = result.find(kMachineNamePolicyVarName);
- if (position != std::string::npos) {
- SCDynamicStoreContext context = { 0, NULL, NULL, NULL };
- SCDynamicStoreRef store = SCDynamicStoreCreate(kCFAllocatorDefault,
- CFSTR("policy_subsystem"),
- NULL, &context);
- CFStringRef machinename = SCDynamicStoreCopyLocalHostName(store);
- if (machinename) {
- result.replace(position, strlen(kMachineNamePolicyVarName),
- base::SysCFStringRefToUTF8(machinename));
- CFRelease(machinename);
- } else {
- LOG(ERROR) << "Machine name variable can not be resolved.";
- }
- CFRelease(store);
+
+WRAP_GET_FORLDER_FUNCTION(GetMacUserFolderPath, NSUserDirectory)
+WRAP_GET_FORLDER_FUNCTION(GetMacDocumentsFolderPath, NSDocumentDirectory)
+
+bool GetUserName(base::FilePath::StringType* value) {
+ NSString* username = NSUserName();
+ if (username)
+ *value = base::SysNSStringToUTF8(username);
+ else
+ LOG(ERROR) << "Username variable can not be resolved.";
+ return (username != NULL);
+}
+
+bool GetMachineName(base::FilePath::StringType* value) {
+ SCDynamicStoreContext context = {0, NULL, NULL, NULL};
+ SCDynamicStoreRef store = SCDynamicStoreCreate(
+ kCFAllocatorDefault, CFSTR("policy_subsystem"), NULL, &context);
+ CFStringRef machinename = SCDynamicStoreCopyLocalHostName(store);
+ if (machinename) {
+ *value = base::SysCFStringRefToUTF8(machinename);
+ CFRelease(machinename);
+ } else {
+ LOG(ERROR) << "Machine name variable can not be resolved.";
}
- return result;
+ CFRelease(store);
+ return (machinename != NULL);
}
+const char* kUserNamePolicyVarName = "${user_name}";
+const char* kMachineNamePolicyVarName = "${machine_name}";
+const char* kMacUsersDirectory = "${users}";
+const char* kMacDocumentsFolderVarName = "${documents}";
+
+// A table mapping variable names to their respective get value function
+// pointers.
+const VariableNameAndValueCallback kVariableNameAndValueCallbacks[] = {
+ {kUserNamePolicyVarName, &GetUserName},
+ {kMachineNamePolicyVarName, &GetMachineName},
+ {kMacUsersDirectory, &GetMacUserFolderPath},
+ {kMacDocumentsFolderVarName, &GetMacDocumentsFolderPath}};
+
+// Total number of entries in the mapping table.
+const int kNoOfVariables = arraysize(kVariableNameAndValueCallbacks);
+
+} // namespace internal
+
void CheckUserDataDirPolicy(base::FilePath* user_data_dir) {
base::mac::ScopedNSAutoreleasePool pool;

Powered by Google App Engine
This is Rietveld 408576698