Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / remoting / host / host_config.cc
blob40ae940b46770eb03dd8d45f9b7a4b516120c848
1 // Copyright (c) 2012 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 "remoting/host/host_config.h"
7 #include "base/files/file_util.h"
8 #include "base/files/important_file_writer.h"
9 #include "base/json/json_reader.h"
10 #include "base/json/json_writer.h"
11 #include "base/values.h"
13 namespace remoting {
15 scoped_ptr<base::DictionaryValue> HostConfigFromJson(
16 const std::string& json) {
17 scoped_ptr<base::Value> value(
18 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS));
19 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) {
20 LOG(WARNING) << "Failed to parse host config from JSON";
21 return nullptr;
24 scoped_ptr<base::DictionaryValue> config(
25 static_cast<base::DictionaryValue*>(value.release()));
26 return config.Pass();
29 std::string HostConfigToJson(const base::DictionaryValue& host_config) {
30 std::string data;
31 base::JSONWriter::Write(&host_config, &data);
32 return data;
35 scoped_ptr<base::DictionaryValue> HostConfigFromJsonFile(
36 const base::FilePath& config_file) {
37 // TODO(sergeyu): Implement better error handling here.
38 std::string serialized;
39 if (!base::ReadFileToString(config_file, &serialized)) {
40 LOG(WARNING) << "Failed to read " << config_file.value();
41 return nullptr;
44 return HostConfigFromJson(serialized);
47 bool HostConfigToJsonFile(const base::DictionaryValue& host_config,
48 const base::FilePath& config_file) {
49 std::string serialized = HostConfigToJson(host_config);
50 return base::ImportantFileWriter::WriteFileAtomically(config_file,
51 serialized);
54 } // namespace remoting