summaryrefslogtreecommitdiffstats
path: root/remoting/host/json_host_config.cc
blob: 36b39f2eff284acfb3a7217c4b6d4e265eaa7031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/json_host_config.h"

#include "base/file_util.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/message_loop_proxy.h"
#include "base/synchronization/lock.h"
#include "base/task.h"
#include "base/values.h"

namespace remoting {

JsonHostConfig::JsonHostConfig(
    const FilePath& filename,
    base::MessageLoopProxy* file_message_loop_proxy)
    : filename_(filename),
      message_loop_proxy_(file_message_loop_proxy) {
}

JsonHostConfig::~JsonHostConfig() {}

bool JsonHostConfig::Read() {
  // TODO(sergeyu): Implement better error handling here.
  std::string file_content;
  if (!file_util::ReadFileToString(filename_, &file_content)) {
    return false;
  }

  scoped_ptr<Value> value(base::JSONReader::Read(file_content, true));
  if (value.get() == NULL || !value->IsType(Value::TYPE_DICTIONARY)) {
    return false;
  }

  DictionaryValue* dictionary = static_cast<DictionaryValue*>(value.release());
  base::AutoLock auto_lock(lock_);
  values_.reset(dictionary);
  return true;
}

void JsonHostConfig::Save() {
  message_loop_proxy_->PostTask(
      FROM_HERE, NewRunnableMethod(this, &JsonHostConfig::DoWrite));
}

void JsonHostConfig::DoWrite() {
  std::string file_content;
  base::AutoLock auto_lock(lock_);
  base::JSONWriter::Write(values_.get(), true, &file_content);
  // TODO(sergeyu): Move ImportantFileWriter to base and use it here.
  file_util::WriteFile(filename_, file_content.c_str(), file_content.size());
}

}  // namespace remoting