• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "remoting/host/json_host_config.h"
6 
7 #include "base/bind.h"
8 #include "base/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"
15 
16 namespace remoting {
17 
JsonHostConfig(const base::FilePath & filename)18 JsonHostConfig::JsonHostConfig(const base::FilePath& filename)
19     : filename_(filename) {
20 }
21 
~JsonHostConfig()22 JsonHostConfig::~JsonHostConfig() {}
23 
Read()24 bool JsonHostConfig::Read() {
25   DCHECK(CalledOnValidThread());
26 
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();
31     return false;
32   }
33 
34   return SetSerializedData(file_content);
35 }
36 
Save()37 bool JsonHostConfig::Save() {
38   DCHECK(CalledOnValidThread());
39 
40   return base::ImportantFileWriter::WriteFileAtomically(filename_,
41                                                         GetSerializedData());
42 }
43 
GetSerializedData()44 std::string JsonHostConfig::GetSerializedData() {
45   std::string data;
46   base::JSONWriter::Write(values_.get(), &data);
47   return data;
48 }
49 
SetSerializedData(const std::string & config)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();
55     return false;
56   }
57 
58   base::DictionaryValue* dictionary =
59       static_cast<base::DictionaryValue*>(value.release());
60   values_.reset(dictionary);
61   return true;
62 }
63 
64 }  // namespace remoting
65