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