OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "remoting/host/json_host_config.h" | 5 #include "remoting/host/json_host_config.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 11 #include "base/synchronization/lock.h" |
11 #include "base/task.h" | 12 #include "base/task.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
13 | 14 |
14 namespace remoting { | 15 namespace remoting { |
15 | 16 |
16 JsonHostConfig::JsonHostConfig( | 17 JsonHostConfig::JsonHostConfig( |
17 const FilePath& filename, | 18 const FilePath& filename, |
18 base::MessageLoopProxy* file_message_loop_proxy) | 19 base::MessageLoopProxy* file_message_loop_proxy) |
19 : filename_(filename), | 20 : filename_(filename), |
20 message_loop_proxy_(file_message_loop_proxy) { | 21 message_loop_proxy_(file_message_loop_proxy) { |
21 } | 22 } |
22 | 23 |
23 JsonHostConfig::~JsonHostConfig() {} | 24 JsonHostConfig::~JsonHostConfig() {} |
24 | 25 |
25 bool JsonHostConfig::Read() { | 26 bool JsonHostConfig::Read() { |
26 // TODO(sergeyu): Implement better error handling here. | 27 // TODO(sergeyu): Implement better error handling here. |
27 std::string file_content; | 28 std::string file_content; |
28 if (!file_util::ReadFileToString(filename_, &file_content)) { | 29 if (!file_util::ReadFileToString(filename_, &file_content)) { |
29 return false; | 30 return false; |
30 } | 31 } |
31 | 32 |
32 scoped_ptr<Value> value(base::JSONReader::Read(file_content, true)); | 33 scoped_ptr<Value> value(base::JSONReader::Read(file_content, true)); |
33 if (value.get() == NULL || !value->IsType(Value::TYPE_DICTIONARY)) { | 34 if (value.get() == NULL || !value->IsType(Value::TYPE_DICTIONARY)) { |
34 return false; | 35 return false; |
35 } | 36 } |
36 | 37 |
37 DictionaryValue* dictionary = static_cast<DictionaryValue*>(value.release()); | 38 DictionaryValue* dictionary = static_cast<DictionaryValue*>(value.release()); |
38 AutoLock auto_lock(lock_); | 39 base::AutoLock auto_lock(lock_); |
39 values_.reset(dictionary); | 40 values_.reset(dictionary); |
40 return true; | 41 return true; |
41 } | 42 } |
42 | 43 |
43 void JsonHostConfig::Save() { | 44 void JsonHostConfig::Save() { |
44 message_loop_proxy_->PostTask( | 45 message_loop_proxy_->PostTask( |
45 FROM_HERE, NewRunnableMethod(this, &JsonHostConfig::DoWrite)); | 46 FROM_HERE, NewRunnableMethod(this, &JsonHostConfig::DoWrite)); |
46 } | 47 } |
47 | 48 |
48 void JsonHostConfig::DoWrite() { | 49 void JsonHostConfig::DoWrite() { |
49 std::string file_content; | 50 std::string file_content; |
50 AutoLock auto_lock(lock_); | 51 base::AutoLock auto_lock(lock_); |
51 base::JSONWriter::Write(values_.get(), true, &file_content); | 52 base::JSONWriter::Write(values_.get(), true, &file_content); |
52 // TODO(sergeyu): Move ImportantFileWriter to base and use it here. | 53 // TODO(sergeyu): Move ImportantFileWriter to base and use it here. |
53 file_util::WriteFile(filename_, file_content.c_str(), file_content.size()); | 54 file_util::WriteFile(filename_, file_content.c_str(), file_content.size()); |
54 } | 55 } |
55 | 56 |
56 } // namespace remoting | 57 } // namespace remoting |
OLD | NEW |