[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 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/pairing_registry_delegate_linux.h" |
| 6 | |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 7 | #include <memory> |
dcheng | 5d09049 | 2016-06-09 17:53:34 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 11 | #include "base/files/file_enumerator.h" |
thestig | 1ecdcf4 | 2014-09-12 05:09:14 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 13 | #include "base/files/important_file_writer.h" |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 14 | #include "base/json/json_file_value_serializer.h" |
| 15 | #include "base/json/json_string_value_serializer.h" |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 16 | #include "base/location.h" |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 17 | #include "base/strings/stringprintf.h" |
| 18 | #include "base/values.h" |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 19 | #include "remoting/host/branding.h" |
| 20 | |
| 21 | namespace { |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 22 | |
| 23 | // The pairing registry path relative to the configuration directory. |
| 24 | const char kRegistryDirectory[] = "paired-clients"; |
| 25 | |
| 26 | const char kPairingFilenameFormat[] = "%s.json"; |
| 27 | const char kPairingFilenamePattern[] = "*.json"; |
| 28 | |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 29 | } // namespace |
| 30 | |
| 31 | namespace remoting { |
| 32 | |
| 33 | using protocol::PairingRegistry; |
| 34 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 35 | PairingRegistryDelegateLinux::PairingRegistryDelegateLinux() = default; |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 36 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 37 | PairingRegistryDelegateLinux::~PairingRegistryDelegateLinux() = default; |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 38 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 39 | std::unique_ptr<base::ListValue> PairingRegistryDelegateLinux::LoadAll() { |
| 40 | std::unique_ptr<base::ListValue> pairings(new base::ListValue()); |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 41 | |
| 42 | // Enumerate all pairing files in the pairing registry. |
| 43 | base::FilePath registry_path = GetRegistryPath(); |
| 44 | base::FileEnumerator enumerator(registry_path, false, |
| 45 | base::FileEnumerator::FILES, |
| 46 | kPairingFilenamePattern); |
| 47 | for (base::FilePath pairing_file = enumerator.Next(); !pairing_file.empty(); |
| 48 | pairing_file = enumerator.Next()) { |
| 49 | // Read the JSON containing pairing data. |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 50 | JSONFileValueDeserializer deserializer(pairing_file); |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 51 | int error_code; |
| 52 | std::string error_message; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 53 | std::unique_ptr<base::Value> pairing_json = |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame] | 54 | deserializer.Deserialize(&error_code, &error_message); |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 55 | if (!pairing_json) { |
| 56 | LOG(WARNING) << "Failed to load '" << pairing_file.value() << "' (" |
| 57 | << error_code << ")."; |
| 58 | continue; |
| 59 | } |
| 60 | |
dcheng | 5d09049 | 2016-06-09 17:53:34 | [diff] [blame] | 61 | pairings->Append(std::move(pairing_json)); |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 62 | } |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 63 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 64 | return pairings; |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 67 | bool PairingRegistryDelegateLinux::DeleteAll() { |
| 68 | // Delete all pairing files in the pairing registry. |
| 69 | base::FilePath registry_path = GetRegistryPath(); |
| 70 | base::FileEnumerator enumerator(registry_path, false, |
| 71 | base::FileEnumerator::FILES, |
| 72 | kPairingFilenamePattern); |
| 73 | |
| 74 | bool success = true; |
| 75 | for (base::FilePath pairing_file = enumerator.Next(); !pairing_file.empty(); |
| 76 | pairing_file = enumerator.Next()) { |
| 77 | success = success && base::DeleteFile(pairing_file, false); |
| 78 | } |
| 79 | |
| 80 | return success; |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 83 | PairingRegistry::Pairing PairingRegistryDelegateLinux::Load( |
| 84 | const std::string& client_id) { |
| 85 | base::FilePath registry_path = GetRegistryPath(); |
| 86 | base::FilePath pairing_file = registry_path.Append( |
| 87 | base::StringPrintf(kPairingFilenameFormat, client_id.c_str())); |
| 88 | |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 89 | JSONFileValueDeserializer deserializer(pairing_file); |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 90 | int error_code; |
| 91 | std::string error_message; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 92 | std::unique_ptr<base::Value> pairing = |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame] | 93 | deserializer.Deserialize(&error_code, &error_message); |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 94 | if (!pairing) { |
| 95 | LOG(WARNING) << "Failed to load pairing information: " << error_message |
| 96 | << " (" << error_code << ")."; |
| 97 | return PairingRegistry::Pairing(); |
| 98 | } |
| 99 | |
[email protected] | 2001405 | 2013-08-09 21:31:01 | [diff] [blame] | 100 | base::DictionaryValue* pairing_dictionary; |
| 101 | if (!pairing->GetAsDictionary(&pairing_dictionary)) { |
| 102 | LOG(WARNING) << "Failed to parse pairing information: not a dictionary."; |
| 103 | return PairingRegistry::Pairing(); |
| 104 | } |
| 105 | |
| 106 | return PairingRegistry::Pairing::CreateFromValue(*pairing_dictionary); |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 109 | bool PairingRegistryDelegateLinux::Save( |
| 110 | const PairingRegistry::Pairing& pairing) { |
| 111 | base::FilePath registry_path = GetRegistryPath(); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 112 | base::File::Error error; |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 113 | if (!base::CreateDirectoryAndGetError(registry_path, &error)) { |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 114 | LOG(ERROR) << "Could not create pairing registry directory: " << error; |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 115 | return false; |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 118 | std::string pairing_json; |
| 119 | JSONStringValueSerializer serializer(&pairing_json); |
| 120 | if (!serializer.Serialize(*pairing.ToValue())) { |
| 121 | LOG(ERROR) << "Failed to serialize pairing data for " |
| 122 | << pairing.client_id(); |
| 123 | return false; |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 124 | } |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 125 | |
| 126 | base::FilePath pairing_file = registry_path.Append( |
| 127 | base::StringPrintf(kPairingFilenameFormat, pairing.client_id().c_str())); |
| 128 | if (!base::ImportantFileWriter::WriteFileAtomically(pairing_file, |
| 129 | pairing_json)) { |
| 130 | LOG(ERROR) << "Could not save pairing data for " << pairing.client_id(); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | return true; |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 137 | bool PairingRegistryDelegateLinux::Delete(const std::string& client_id) { |
| 138 | base::FilePath registry_path = GetRegistryPath(); |
| 139 | base::FilePath pairing_file = registry_path.Append( |
| 140 | base::StringPrintf(kPairingFilenameFormat, client_id.c_str())); |
| 141 | |
| 142 | return base::DeleteFile(pairing_file, false); |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 145 | base::FilePath PairingRegistryDelegateLinux::GetRegistryPath() { |
| 146 | if (!registry_path_for_testing_.empty()) { |
| 147 | return registry_path_for_testing_; |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | base::FilePath config_dir = remoting::GetConfigDir(); |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 151 | return config_dir.Append(kRegistryDirectory); |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 37846603 | 2013-08-02 01:35:16 | [diff] [blame] | 154 | void PairingRegistryDelegateLinux::SetRegistryPathForTesting( |
| 155 | const base::FilePath& registry_path) { |
| 156 | registry_path_for_testing_ = registry_path; |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 157 | } |
| 158 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 159 | std::unique_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate() { |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 160 | return std::make_unique<PairingRegistryDelegateLinux>(); |
[email protected] | b1ae901 | 2013-06-23 14:10:30 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | } // namespace remoting |