summaryrefslogtreecommitdiffstats
path: root/components/cloud_devices/cloud_device_description.cc
blob: 124bb49c1fd3a429aaeeaaf978f7e0745191650b (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
// 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 "components/cloud_devices/cloud_device_description.h"

#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/values.h"
#include "components/cloud_devices/cloud_device_description_consts.h"

namespace cloud_devices {

CloudDeviceDescription::CloudDeviceDescription() {
  Reset();
}

CloudDeviceDescription::~CloudDeviceDescription() {
}

void CloudDeviceDescription::Reset() {
  root_.reset(new base::DictionaryValue);
  root_->SetString(json::kVersion, json::kVersion10);
}

bool CloudDeviceDescription::InitFromString(const std::string& json) {
  Reset();

  scoped_ptr<base::Value> parsed(base::JSONReader::Read(json));
  base::DictionaryValue* description = NULL;
  if (!parsed || !parsed->GetAsDictionary(&description))
    return false;
  root_.reset(description);
  ignore_result(parsed.release());

  std::string version;
  description->GetString(json::kVersion, &version);
  return version == json::kVersion10;
}

std::string CloudDeviceDescription::ToString() const {
  std::string json;
  base::JSONWriter::WriteWithOptions(root_.get(),
                                     base::JSONWriter::OPTIONS_PRETTY_PRINT,
                                     &json);
  return json;
}

const base::DictionaryValue* CloudDeviceDescription::GetItem(
    const std::string& path) const {
  const base::DictionaryValue* value = NULL;
  root_->GetDictionary(path, &value);
  return value;
}

base::DictionaryValue* CloudDeviceDescription::CreateItem(
    const std::string& path) {
  base::DictionaryValue* value = new base::DictionaryValue;
  root_->Set(path, value);
  return value;
}

const base::ListValue* CloudDeviceDescription::GetListItem(
    const std::string& path) const {
  const base::ListValue* value = NULL;
  root_->GetList(path, &value);
  return value;
}

base::ListValue* CloudDeviceDescription::CreateListItem(
    const std::string& path) {
  base::ListValue* value = new base::ListValue;
  root_->Set(path, value);
  return value;
}

}  // namespace cloud_devices