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