OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/cloud_devices/cloud_device_description.h" | 5 #include "components/cloud_devices/cloud_device_description.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "components/cloud_devices/cloud_device_description_consts.h" | 11 #include "components/cloud_devices/cloud_device_description_consts.h" |
12 | 12 |
13 namespace cloud_devices { | 13 namespace cloud_devices { |
14 | 14 |
15 CloudDeviceDescription::CloudDeviceDescription() { | 15 CloudDeviceDescription::CloudDeviceDescription() { |
16 Reset(); | 16 Reset(); |
17 } | 17 } |
18 | 18 |
19 CloudDeviceDescription::~CloudDeviceDescription() { | 19 CloudDeviceDescription::~CloudDeviceDescription() { |
20 } | 20 } |
21 | 21 |
22 void CloudDeviceDescription::Reset() { | 22 void CloudDeviceDescription::Reset() { |
23 root_.reset(new base::DictionaryValue); | 23 root_.reset(new base::DictionaryValue); |
24 root_->SetString(json::kVersion, json::kVersion10); | 24 root_->SetString(json::kVersion, json::kVersion10); |
25 } | 25 } |
26 | 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 |
27 bool CloudDeviceDescription::InitFromString(const std::string& json) { | 38 bool CloudDeviceDescription::InitFromString(const std::string& json) { |
28 Reset(); | |
29 | |
30 scoped_ptr<base::Value> parsed(base::JSONReader::Read(json)); | 39 scoped_ptr<base::Value> parsed(base::JSONReader::Read(json)); |
31 base::DictionaryValue* description = NULL; | 40 base::DictionaryValue* description = NULL; |
32 if (!parsed || !parsed->GetAsDictionary(&description)) | 41 if (!parsed || !parsed->GetAsDictionary(&description)) |
33 return false; | 42 return false; |
34 root_.reset(description); | |
35 ignore_result(parsed.release()); | 43 ignore_result(parsed.release()); |
36 | 44 return InitFromDictionary(make_scoped_ptr(description)); |
37 std::string version; | |
38 description->GetString(json::kVersion, &version); | |
39 return version == json::kVersion10; | |
40 } | 45 } |
41 | 46 |
42 std::string CloudDeviceDescription::ToString() const { | 47 std::string CloudDeviceDescription::ToString() const { |
43 std::string json; | 48 std::string json; |
44 base::JSONWriter::WriteWithOptions(root_.get(), | 49 base::JSONWriter::WriteWithOptions(root_.get(), |
45 base::JSONWriter::OPTIONS_PRETTY_PRINT, | 50 base::JSONWriter::OPTIONS_PRETTY_PRINT, |
46 &json); | 51 &json); |
47 return json; | 52 return json; |
48 } | 53 } |
49 | 54 |
(...skipping 19 matching lines...) Expand all Loading... |
69 } | 74 } |
70 | 75 |
71 base::ListValue* CloudDeviceDescription::CreateListItem( | 76 base::ListValue* CloudDeviceDescription::CreateListItem( |
72 const std::string& path) { | 77 const std::string& path) { |
73 base::ListValue* value = new base::ListValue; | 78 base::ListValue* value = new base::ListValue; |
74 root_->Set(path, value); | 79 root_->Set(path, value); |
75 return value; | 80 return value; |
76 } | 81 } |
77 | 82 |
78 } // namespace cloud_devices | 83 } // namespace cloud_devices |
OLD | NEW |