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/json_host_config.h"
8 #include "base/files/file_util.h"
9 #include "base/files/important_file_writer.h"
10 #include "base/json/json_reader.h"
11 #include "base/json/json_writer.h"
12 #include "base/location.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/values.h"
18 JsonHostConfig::JsonHostConfig(const base::FilePath
& filename
)
19 : filename_(filename
) {
22 JsonHostConfig::~JsonHostConfig() {}
24 bool JsonHostConfig::Read() {
25 DCHECK(CalledOnValidThread());
27 // TODO(sergeyu): Implement better error handling here.
28 std::string file_content
;
29 if (!base::ReadFileToString(filename_
, &file_content
)) {
30 LOG(WARNING
) << "Failed to read " << filename_
.value();
34 return SetSerializedData(file_content
);
37 bool JsonHostConfig::Save() {
38 DCHECK(CalledOnValidThread());
40 return base::ImportantFileWriter::WriteFileAtomically(filename_
,
44 std::string
JsonHostConfig::GetSerializedData() {
46 base::JSONWriter::Write(values_
.get(), &data
);
50 bool JsonHostConfig::SetSerializedData(const std::string
& config
) {
51 scoped_ptr
<base::Value
> value(
52 base::JSONReader::Read(config
, base::JSON_ALLOW_TRAILING_COMMAS
));
53 if (value
.get() == NULL
|| !value
->IsType(base::Value::TYPE_DICTIONARY
)) {
54 LOG(WARNING
) << "Failed to parse " << filename_
.value();
58 base::DictionaryValue
* dictionary
=
59 static_cast<base::DictionaryValue
*>(value
.release());
60 values_
.reset(dictionary
);
64 } // namespace remoting