Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / cloud_devices / common / 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/common/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/common/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(
50       root_.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
51   return json;
52 }
53
54 const base::DictionaryValue* CloudDeviceDescription::GetItem(
55     const std::string& path) const {
56   const base::DictionaryValue* value = NULL;
57   root_->GetDictionary(path, &value);
58   return value;
59 }
60
61 base::DictionaryValue* CloudDeviceDescription::CreateItem(
62     const std::string& path) {
63   base::DictionaryValue* value = new base::DictionaryValue;
64   root_->Set(path, value);
65   return value;
66 }
67
68 const base::ListValue* CloudDeviceDescription::GetListItem(
69     const std::string& path) const {
70   const base::ListValue* value = NULL;
71   root_->GetList(path, &value);
72   return value;
73 }
74
75 base::ListValue* CloudDeviceDescription::CreateListItem(
76     const std::string& path) {
77   base::ListValue* value = new base::ListValue;
78   root_->Set(path, value);
79   return value;
80 }
81
82 }  // namespace cloud_devices