Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(380)

Side by Side Diff: remoting/host/json_host_config.cc

Issue 10007048: Cleanup and simplify HostConfig and JsonHostConfig. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « remoting/host/json_host_config.h ('k') | remoting/host/json_host_config_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/synchronization/lock.h"
14 #include "base/values.h" 13 #include "base/values.h"
15 14
16 namespace remoting { 15 namespace remoting {
17 16
18 JsonHostConfig::JsonHostConfig( 17 JsonHostConfig::JsonHostConfig(const FilePath& filename)
19 const FilePath& filename, 18 : filename_(filename) {
20 base::MessageLoopProxy* file_message_loop_proxy)
21 : filename_(filename),
22 message_loop_proxy_(file_message_loop_proxy) {
23 } 19 }
24 20
25 JsonHostConfig::~JsonHostConfig() {} 21 JsonHostConfig::~JsonHostConfig() {}
26 22
27 bool JsonHostConfig::Read() { 23 bool JsonHostConfig::Read() {
24 DCHECK(CalledOnValidThread());
25
28 // TODO(sergeyu): Implement better error handling here. 26 // TODO(sergeyu): Implement better error handling here.
29 std::string file_content; 27 std::string file_content;
30 if (!file_util::ReadFileToString(filename_, &file_content)) { 28 if (!file_util::ReadFileToString(filename_, &file_content)) {
29 LOG(WARNING) << "Failed to read " << filename_.value();
31 return false; 30 return false;
32 } 31 }
33 32
34 scoped_ptr<Value> value(base::JSONReader::Read(file_content, true)); 33 scoped_ptr<Value> value(base::JSONReader::Read(file_content, true));
35 if (value.get() == NULL || !value->IsType(Value::TYPE_DICTIONARY)) { 34 if (value.get() == NULL || !value->IsType(Value::TYPE_DICTIONARY)) {
35 LOG(WARNING) << "Failed to parse " << filename_.value();
36 return false; 36 return false;
37 } 37 }
38 38
39 DictionaryValue* dictionary = static_cast<DictionaryValue*>(value.release()); 39 DictionaryValue* dictionary = static_cast<DictionaryValue*>(value.release());
40 base::AutoLock auto_lock(lock_);
41 values_.reset(dictionary); 40 values_.reset(dictionary);
42 return true; 41 return true;
43 } 42 }
44 43
45 void JsonHostConfig::Save() { 44 bool JsonHostConfig::Save() {
46 message_loop_proxy_->PostTask( 45 DCHECK(CalledOnValidThread());
47 FROM_HERE, base::Bind(&JsonHostConfig::DoWrite, this));
48 }
49 46
50 void JsonHostConfig::DoWrite() {
51 std::string file_content; 47 std::string file_content;
52 base::AutoLock auto_lock(lock_);
53 base::JSONWriter::WriteWithOptions(values_.get(), 48 base::JSONWriter::WriteWithOptions(values_.get(),
54 base::JSONWriter::OPTIONS_PRETTY_PRINT, 49 base::JSONWriter::OPTIONS_PRETTY_PRINT,
55 &file_content); 50 &file_content);
56 // TODO(sergeyu): Move ImportantFileWriter to base and use it here. 51 // TODO(sergeyu): Move ImportantFileWriter to base and use it here.
57 file_util::WriteFile(filename_, file_content.c_str(), file_content.size()); 52 int result = file_util::WriteFile(filename_, file_content.c_str(),
53 file_content.size());
54 return result == static_cast<int>(file_content.size());
58 } 55 }
59 56
60 } // namespace remoting 57 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/json_host_config.h ('k') | remoting/host/json_host_config_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698