[email protected] | 26d4acc | 2012-01-24 18:24:26 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 2 | // 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 | |
wez | 3b6ea037 | 2014-11-23 02:03:19 | [diff] [blame] | 7 | #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" |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
wez | 3b6ea037 | 2014-11-23 02:03:19 | [diff] [blame] | 12 | #include "base/values.h" |
| 13 | |
[email protected] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 14 | namespace remoting { |
| 15 | |
Lambros Lambrou | 53f3dd0 | 2017-08-14 20:19:48 | [diff] [blame] | 16 | const char kHostEnabledConfigPath[] = "enabled"; |
| 17 | const char kHostOwnerConfigPath[] = "host_owner"; |
| 18 | const char kHostOwnerEmailConfigPath[] = "host_owner_email"; |
| 19 | const char kXmppLoginConfigPath[] = "xmpp_login"; |
| 20 | const char kOAuthRefreshTokenConfigPath[] = "oauth_refresh_token"; |
| 21 | const char kHostIdConfigPath[] = "host_id"; |
| 22 | const char kHostNameConfigPath[] = "host_name"; |
| 23 | const char kHostSecretHashConfigPath[] = "host_secret_hash"; |
| 24 | const char kPrivateKeyConfigPath[] = "private_key"; |
| 25 | const char kUsageStatsConsentConfigPath[] = "usage_stats_consent"; |
| 26 | const char kEnableVp9ConfigPath[] = "enable_vp9"; |
| 27 | const char kEnableH264ConfigPath[] = "enable_h264"; |
| 28 | const char kFrameRecorderBufferKbConfigPath[] = "frame-recorder-buffer-kb"; |
| 29 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 30 | std::unique_ptr<base::DictionaryValue> HostConfigFromJson( |
wez | 3b6ea037 | 2014-11-23 02:03:19 | [diff] [blame] | 31 | const std::string& json) { |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 32 | std::unique_ptr<base::Value> value = |
olli.raula | ab51ffcb | 2015-08-28 04:43:28 | [diff] [blame] | 33 | base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 34 | if (!value || !value->is_dict()) { |
wez | 3b6ea037 | 2014-11-23 02:03:19 | [diff] [blame] | 35 | LOG(WARNING) << "Failed to parse host config from JSON"; |
| 36 | return nullptr; |
| 37 | } |
| 38 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 39 | return base::WrapUnique(static_cast<base::DictionaryValue*>(value.release())); |
wez | 3b6ea037 | 2014-11-23 02:03:19 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | std::string HostConfigToJson(const base::DictionaryValue& host_config) { |
| 43 | std::string data; |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 44 | base::JSONWriter::Write(host_config, &data); |
wez | 3b6ea037 | 2014-11-23 02:03:19 | [diff] [blame] | 45 | return data; |
| 46 | } |
| 47 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 48 | std::unique_ptr<base::DictionaryValue> HostConfigFromJsonFile( |
wez | 3b6ea037 | 2014-11-23 02:03:19 | [diff] [blame] | 49 | 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 | |
| 60 | bool 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] | 7620735 | 2010-06-17 23:43:00 | [diff] [blame] | 66 | |
| 67 | } // namespace remoting |