Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / cloud_devices / cloud_device_description.cc
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 "components/cloud_devices/cloud_device_description.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/logging.h"
10 #include "base/values.h"
11 #include "components/cloud_devices/cloud_device_description_consts.h"
12
13 namespace cloud_devices {
14
15 CloudDeviceDescription::CloudDeviceDescription() {
16   Reset();
17 }
18
19 CloudDeviceDescription::~CloudDeviceDescription() {
20 }
21
22 void CloudDeviceDescription::Reset() {
23   root_.reset(new base::DictionaryValue);
24   root_->SetString(json::kVersion, json::kVersion10);
25 }
26
27 bool CloudDeviceDescription::InitFromDictionary(
28     scoped_ptr<base::DictionaryValue> root) {
29   if (!root)
30     return false;
31   Reset();
32   root_ = root.Pass();
33   std::string version;
34   root_->GetString(json::kVersion, &version);
35   return version == json::kVersion10;
36 }
37
38 bool CloudDeviceDescription::InitFromString(const std::string& json) {
39   scoped_ptr<base::Value> parsed(base::JSONReader::Read(json));
40   base::DictionaryValue* description = NULL;
41   if (!parsed || !parsed->GetAsDictionary(&description))
42     return false;
43   ignore_result(parsed.release());
44   return InitFromDictionary(make_scoped_ptr(description));
45 }
46
47 std::string CloudDeviceDescription::ToString() const {
48   std::string json;
49   base::JSONWriter::WriteWithOptions(root_.get(),
50                                      base::JSONWriter::OPTIONS_PRETTY_PRINT,
51                                      &json);
52   return json;
53 }
54
55 const base::DictionaryValue* CloudDeviceDescription::GetItem(
56     const std::string& path) const {
57   const base::DictionaryValue* value = NULL;
58   root_->GetDictionary(path, &value);
59   return value;
60 }
61
62 base::DictionaryValue* CloudDeviceDescription::CreateItem(
63     const std::string& path) {
64   base::DictionaryValue* value = new base::DictionaryValue;
65   root_->Set(path, value);
66   return value;
67 }
68
69 const base::ListValue* CloudDeviceDescription::GetListItem(
70     const std::string& path) const {
71   const base::ListValue* value = NULL;
72   root_->GetList(path, &value);
73   return value;
74 }
75
76 base::ListValue* CloudDeviceDescription::CreateListItem(
77     const std::string& path) {
78   base::ListValue* value = new base::ListValue;
79   root_->Set(path, value);
80   return value;
81 }
82
83 }  // namespace cloud_devices