summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/policy_path_parser.cc
diff options
context:
space:
mode:
authorkaliamoorthi@chromium.org <kaliamoorthi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-03 12:21:06 +0000
committerkaliamoorthi@chromium.org <kaliamoorthi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-03 12:21:06 +0000
commitf2dc9f306053f1fcbba99badf5edfab6a64b1392 (patch)
tree54ac015f36115071e2a3ee98c4e180582edad591 /chrome/browser/policy/policy_path_parser.cc
parent06114b427935cb23724c12176be20d856d00ac58 (diff)
downloadchromium_src-f2dc9f306053f1fcbba99badf5edfab6a64b1392.zip
chromium_src-f2dc9f306053f1fcbba99badf5edfab6a64b1392.tar.gz
chromium_src-f2dc9f306053f1fcbba99badf5edfab6a64b1392.tar.bz2
Refactorise the policy_path_parser framework
This CL performs a refactorisation of the policy path parser framework. BUG=352627 Review URL: https://codereview.chromium.org/214233003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@261370 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/policy/policy_path_parser.cc')
-rw-r--r--chrome/browser/policy/policy_path_parser.cc67
1 files changed, 67 insertions, 0 deletions
diff --git a/chrome/browser/policy/policy_path_parser.cc b/chrome/browser/policy/policy_path_parser.cc
new file mode 100644
index 0000000..cb64af9
--- /dev/null
+++ b/chrome/browser/policy/policy_path_parser.cc
@@ -0,0 +1,67 @@
+// Copyright 2014 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.
+
+#include "chrome/browser/policy/policy_path_parser.h"
+
+#include "base/bind.h"
+#include "base/files/file_path.h"
+
+namespace {
+
+void TrimQuotes(base::FilePath::StringType* path) {
+ if (path->length() > 1 &&
+ (((*path)[0] == FILE_PATH_LITERAL('"') &&
+ (*path)[path->length() - 1] == FILE_PATH_LITERAL('"')) ||
+ ((*path)[0] == FILE_PATH_LITERAL('\'') &&
+ (*path)[path->length() - 1] == FILE_PATH_LITERAL('\'')))) {
+ // Strip first and last char which should be matching quotes now.
+ *path = path->substr(1, path->length() - 2);
+ }
+}
+
+} // namespace
+
+namespace policy {
+
+namespace path_parser {
+
+// This function performs a lazy call to the GetValueCallback, that is the
+// callback is invoked only if the variable is found in the path. This is done
+// to reduce the overhead during initialization.
+void ReplaceVariableInPathWithValue(
+ const base::FilePath::StringType& variable,
+ const policy::path_parser::internal::GetValueFuncPtr& value_func_ptr,
+ base::FilePath::StringType* path) {
+ size_t position = path->find(variable);
+ base::FilePath::StringType value;
+ if (position != base::FilePath::StringType::npos && value_func_ptr(&value))
+ path->replace(position, variable.length(), value);
+}
+
+// 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.
+ TrimQuotes(&result);
+
+ base::FilePath::StringType variable;
+ internal::GetValueFuncPtr val_func_ptr;
+ for (int i = 0; i < internal::kNoOfVariables; ++i) {
+ variable = internal::kVariableNameAndValueCallbacks[i].name;
+ val_func_ptr = internal::kVariableNameAndValueCallbacks[i].value_func_ptr;
+ ReplaceVariableInPathWithValue(variable, val_func_ptr, &result);
+ }
+
+ return result;
+}
+
+} // namespace path_parser
+
+} // namespace policy