| OLD | NEW |
| (Empty) | |
| 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::InitFromString(const std::string& json) { |
| 28 Reset(); |
| 29 |
| 30 scoped_ptr<base::Value> parsed(base::JSONReader::Read(json)); |
| 31 base::DictionaryValue* description = NULL; |
| 32 if (!parsed || !parsed->GetAsDictionary(&description)) |
| 33 return false; |
| 34 root_.reset(description); |
| 35 ignore_result(parsed.release()); |
| 36 |
| 37 std::string version; |
| 38 description->GetString(json::kVersion, &version); |
| 39 return version == json::kVersion10; |
| 40 } |
| 41 |
| 42 std::string CloudDeviceDescription::ToString() const { |
| 43 std::string json; |
| 44 base::JSONWriter::WriteWithOptions(root_.get(), |
| 45 base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 46 &json); |
| 47 return json; |
| 48 } |
| 49 |
| 50 const base::DictionaryValue* CloudDeviceDescription::GetItem( |
| 51 const std::string& path) const { |
| 52 const base::DictionaryValue* value = NULL; |
| 53 root_->GetDictionary(path, &value); |
| 54 return value; |
| 55 } |
| 56 |
| 57 base::DictionaryValue* CloudDeviceDescription::CreateItem( |
| 58 const std::string& path) { |
| 59 base::DictionaryValue* value = new base::DictionaryValue; |
| 60 root_->Set(path, value); |
| 61 return value; |
| 62 } |
| 63 |
| 64 const base::ListValue* CloudDeviceDescription::GetListItem( |
| 65 const std::string& path) const { |
| 66 const base::ListValue* value = NULL; |
| 67 root_->GetList(path, &value); |
| 68 return value; |
| 69 } |
| 70 |
| 71 base::ListValue* CloudDeviceDescription::CreateListItem( |
| 72 const std::string& path) { |
| 73 base::ListValue* value = new base::ListValue; |
| 74 root_->Set(path, value); |
| 75 return value; |
| 76 } |
| 77 |
| 78 } // namespace cloud_devices |
| OLD | NEW |