OLD | NEW |
| (Empty) |
1 // Copyright 2014 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. | |
4 | |
5 #include "chrome/browser/policy/policy_path_parser.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/files/file_path.h" | |
9 | |
10 namespace { | |
11 | |
12 void TrimQuotes(base::FilePath::StringType* path) { | |
13 if (path->length() > 1 && | |
14 (((*path)[0] == FILE_PATH_LITERAL('"') && | |
15 (*path)[path->length() - 1] == FILE_PATH_LITERAL('"')) || | |
16 ((*path)[0] == FILE_PATH_LITERAL('\'') && | |
17 (*path)[path->length() - 1] == FILE_PATH_LITERAL('\'')))) { | |
18 // Strip first and last char which should be matching quotes now. | |
19 *path = path->substr(1, path->length() - 2); | |
20 } | |
21 } | |
22 | |
23 } // namespace | |
24 | |
25 namespace policy { | |
26 | |
27 namespace path_parser { | |
28 | |
29 // This function performs a lazy call to the GetValueCallback, that is the | |
30 // callback is invoked only if the variable is found in the path. This is done | |
31 // to reduce the overhead during initialization. | |
32 void ReplaceVariableInPathWithValue( | |
33 const base::FilePath::StringType& variable, | |
34 const policy::path_parser::internal::GetValueFuncPtr& value_func_ptr, | |
35 base::FilePath::StringType* path) { | |
36 size_t position = path->find(variable); | |
37 base::FilePath::StringType value; | |
38 if (position != base::FilePath::StringType::npos && value_func_ptr(&value)) | |
39 path->replace(position, variable.length(), value); | |
40 } | |
41 | |
42 // Replaces all variable occurrences in the policy string with the respective | |
43 // system settings values. | |
44 base::FilePath::StringType ExpandPathVariables( | |
45 const base::FilePath::StringType& untranslated_string) { | |
46 base::FilePath::StringType result(untranslated_string); | |
47 | |
48 if (result.length() == 0) | |
49 return result; | |
50 | |
51 // Sanitize quotes in case of any around the whole string. | |
52 TrimQuotes(&result); | |
53 | |
54 base::FilePath::StringType variable; | |
55 internal::GetValueFuncPtr val_func_ptr; | |
56 for (int i = 0; i < internal::kNoOfVariables; ++i) { | |
57 variable = internal::kVariableNameAndValueCallbacks[i].name; | |
58 val_func_ptr = internal::kVariableNameAndValueCallbacks[i].value_func_ptr; | |
59 ReplaceVariableInPathWithValue(variable, val_func_ptr, &result); | |
60 } | |
61 | |
62 return result; | |
63 } | |
64 | |
65 } // namespace path_parser | |
66 | |
67 } // namespace policy | |
OLD | NEW |