summaryrefslogtreecommitdiff
path: root/components/policy/core/common/plist_writer.cc
blob: 68c1db5494c91fcf63820770b0a8001822f706b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2018 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 "components/policy/core/common/plist_writer.h"

#include "base/strings/string_number_conversions.h"
#include "build/build_config.h"
#include "third_party/libxml/chromium/libxml_utils.h"

namespace policy {

namespace {

// Called recursively to build the Plist xml. When completed,
// |plist_writer| will contain the Plist. Return true on success and false on
// failure.
bool BuildPlistString(const base::Value& node, XmlWriter& plist_writer) {
  switch (node.type()) {
    case base::Value::Type::BOOLEAN: {
      bool value = node.GetBool();
      plist_writer.StartElement(value ? "true" : "false");
      plist_writer.EndElement();
      return true;
    }

    case base::Value::Type::INTEGER: {
      int value = node.GetInt();
      plist_writer.WriteElement("integer", base::IntToString(value));
      return true;
    }

    case base::Value::Type::STRING: {
      std::string value = node.GetString();
      plist_writer.WriteElement("string", value);
      return true;
    }

    case base::Value::Type::LIST: {
      plist_writer.StartElement("array");

      for (const auto& value : node.GetList()) {
        if (!BuildPlistString(value, plist_writer))
          return false;
      }

      plist_writer.EndElement();
      return true;
    }

    case base::Value::Type::DICTIONARY: {
      plist_writer.StartElement("dict");

      const base::DictionaryValue* dict = nullptr;
      bool result = node.GetAsDictionary(&dict);
      DCHECK(result);
      for (base::DictionaryValue::Iterator itr(*dict); !itr.IsAtEnd();
           itr.Advance()) {
        plist_writer.WriteElement("key", itr.key());

        if (!BuildPlistString(itr.value(), plist_writer))
          result = false;
      }

      plist_writer.EndElement();
      return result;
    }

    default:
      NOTREACHED();
      return false;
  }
}

}  // namespace

bool PlistWrite(const base::Value& node, std::string* plist) {
  // Where we write Plist data as we generate it.
  XmlWriter plist_writer;
  plist_writer.StartWriting();
  plist_writer.StartIndenting();
  plist_writer.StartElement("plist");
  bool result = BuildPlistString(node, plist_writer);
  plist_writer.EndElement();
  plist_writer.StopWriting();

  *plist = plist_writer.GetWrittenString();
  return result;
}

}  // namespace policy