Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / cloud_devices / common / cloud_device_description.cc
blobd24ef0c69758d32a3cbe6f9ac8902557b64b1ce1
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.
5 #include "components/cloud_devices/common/cloud_device_description.h"
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"
13 namespace cloud_devices {
15 CloudDeviceDescription::CloudDeviceDescription() {
16 Reset();
19 CloudDeviceDescription::~CloudDeviceDescription() {
22 void CloudDeviceDescription::Reset() {
23 root_.reset(new base::DictionaryValue);
24 root_->SetString(json::kVersion, json::kVersion10);
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;
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));
47 std::string CloudDeviceDescription::ToString() const {
48 std::string json;
49 base::JSONWriter::WriteWithOptions(
50 *root_, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
51 return json;
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;
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;
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;
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;
82 } // namespace cloud_devices