onc_utils.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 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#ifndef CHROMEOS_NETWORK_ONC_ONC_UTILS_H_
6#define CHROMEOS_NETWORK_ONC_ONC_UTILS_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/scoped_ptr.h"
12#include "chromeos/chromeos_export.h"
13#include "chromeos/network/onc/onc_constants.h"
14#include "chromeos/network/onc/onc_signature.h"
15
16namespace base {
17class DictionaryValue;
18}
19
20namespace chromeos {
21namespace onc {
22
23// A valid but empty (no networks and no certificates) and unencrypted
24// configuration.
25CHROMEOS_EXPORT extern const char kEmptyUnencryptedConfiguration[];
26
27// Parses |json| according to the JSON format. If |json| is a JSON formatted
28// dictionary, the function returns the dictionary as a DictionaryValue.
29// Otherwise returns NULL.
30CHROMEOS_EXPORT scoped_ptr<base::DictionaryValue> ReadDictionaryFromJson(
31    const std::string& json);
32
33// Decrypts the given EncryptedConfiguration |onc| (see the ONC specification)
34// using |passphrase|. The resulting UnencryptedConfiguration is returned. If an
35// error occurs, returns NULL.
36CHROMEOS_EXPORT scoped_ptr<base::DictionaryValue> Decrypt(
37    const std::string& passphrase,
38    const base::DictionaryValue& onc);
39
40// For logging only: strings not user facing.
41CHROMEOS_EXPORT std::string GetSourceAsString(ONCSource source);
42
43// Used for string expansion with function ExpandStringInOncObject(...).
44class CHROMEOS_EXPORT StringSubstitution {
45 public:
46  StringSubstitution() {}
47  virtual ~StringSubstitution() {}
48
49  // Returns the replacement string for |placeholder| in
50  // |substitute|. Currently, onc::substitutes::kLoginIDField and
51  // onc::substitutes::kEmailField are supported.
52  virtual bool GetSubstitute(std::string placeholder,
53                             std::string* substitute) const = 0;
54 private:
55  DISALLOW_COPY_AND_ASSIGN(StringSubstitution);
56};
57
58// Replaces all expandable fields that are mentioned in the ONC
59// specification. The object of |onc_object| is modified in place. Currently
60// onc::substitutes::kLoginIDField and onc::substitutes::kEmailField are
61// expanded. The replacement strings are obtained from |substitution|.
62CHROMEOS_EXPORT void ExpandStringsInOncObject(
63    const OncValueSignature& signature,
64    const StringSubstitution& substitution,
65    base::DictionaryValue* onc_object);
66
67}  // namespace onc
68}  // namespace chromeos
69
70#endif  // CHROMEOS_NETWORK_ONC_ONC_UTILS_H_
71