blob: d7bead40611a5d4d3c6ab0921a486120370e35ec [file] [log] [blame]
[email protected]26d4acc2012-01-24 18:24:261// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]76207352010-06-17 23:43:002// 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/host_config.h"
6
wez3b6ea0372014-11-23 02:03:197#include "base/files/file_util.h"
8#include "base/files/important_file_writer.h"
9#include "base/json/json_reader.h"
10#include "base/json/json_writer.h"
dcheng0765c492016-04-06 22:41:5311#include "base/memory/ptr_util.h"
wez3b6ea0372014-11-23 02:03:1912#include "base/values.h"
13
[email protected]76207352010-06-17 23:43:0014namespace remoting {
15
Lambros Lambrou53f3dd02017-08-14 20:19:4816const char kHostEnabledConfigPath[] = "enabled";
17const char kHostOwnerConfigPath[] = "host_owner";
18const char kHostOwnerEmailConfigPath[] = "host_owner_email";
19const char kXmppLoginConfigPath[] = "xmpp_login";
20const char kOAuthRefreshTokenConfigPath[] = "oauth_refresh_token";
21const char kHostIdConfigPath[] = "host_id";
22const char kHostNameConfigPath[] = "host_name";
23const char kHostSecretHashConfigPath[] = "host_secret_hash";
24const char kPrivateKeyConfigPath[] = "private_key";
25const char kUsageStatsConsentConfigPath[] = "usage_stats_consent";
26const char kEnableVp9ConfigPath[] = "enable_vp9";
27const char kEnableH264ConfigPath[] = "enable_h264";
28const char kFrameRecorderBufferKbConfigPath[] = "frame-recorder-buffer-kb";
29
dcheng0765c492016-04-06 22:41:5330std::unique_ptr<base::DictionaryValue> HostConfigFromJson(
wez3b6ea0372014-11-23 02:03:1931 const std::string& json) {
dcheng0765c492016-04-06 22:41:5332 std::unique_ptr<base::Value> value =
olli.raulaab51ffcb2015-08-28 04:43:2833 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS);
jdoerriee48b26a2017-12-09 14:19:0834 if (!value || !value->is_dict()) {
wez3b6ea0372014-11-23 02:03:1935 LOG(WARNING) << "Failed to parse host config from JSON";
36 return nullptr;
37 }
38
dcheng0765c492016-04-06 22:41:5339 return base::WrapUnique(static_cast<base::DictionaryValue*>(value.release()));
wez3b6ea0372014-11-23 02:03:1940}
41
42std::string HostConfigToJson(const base::DictionaryValue& host_config) {
43 std::string data;
estade8d046462015-05-16 01:02:3444 base::JSONWriter::Write(host_config, &data);
wez3b6ea0372014-11-23 02:03:1945 return data;
46}
47
dcheng0765c492016-04-06 22:41:5348std::unique_ptr<base::DictionaryValue> HostConfigFromJsonFile(
wez3b6ea0372014-11-23 02:03:1949 const base::FilePath& config_file) {
50 // TODO(sergeyu): Implement better error handling here.
51 std::string serialized;
52 if (!base::ReadFileToString(config_file, &serialized)) {
53 LOG(WARNING) << "Failed to read " << config_file.value();
54 return nullptr;
55 }
56
57 return HostConfigFromJson(serialized);
58}
59
60bool HostConfigToJsonFile(const base::DictionaryValue& host_config,
61 const base::FilePath& config_file) {
62 std::string serialized = HostConfigToJson(host_config);
63 return base::ImportantFileWriter::WriteFileAtomically(config_file,
64 serialized);
65}
[email protected]76207352010-06-17 23:43:0066
67} // namespace remoting