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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/pairing_registry_delegate_linux.h"
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/files/important_file_writer.h"
#include "base/functional/bind.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_string_value_serializer.h"
#include "base/json/json_writer.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "remoting/host/branding.h"
namespace {
// The pairing registry path relative to the configuration directory.
const char kRegistryDirectory[] = "paired-clients";
const char kPairingFilenameFormat[] = "%s.json";
const char kPairingFilenamePattern[] = "*.json";
} // namespace
namespace remoting {
using protocol::PairingRegistry;
PairingRegistryDelegateLinux::PairingRegistryDelegateLinux() = default;
PairingRegistryDelegateLinux::~PairingRegistryDelegateLinux() = default;
base::Value::List PairingRegistryDelegateLinux::LoadAll() {
base::Value::List pairings;
// Enumerate all pairing files in the pairing registry.
base::FilePath registry_path = GetRegistryPath();
base::FileEnumerator enumerator(registry_path, false,
base::FileEnumerator::FILES,
kPairingFilenamePattern);
for (base::FilePath pairing_file = enumerator.Next(); !pairing_file.empty();
pairing_file = enumerator.Next()) {
// Read the JSON containing pairing data.
JSONFileValueDeserializer deserializer(pairing_file);
int error_code;
std::string error_message;
std::unique_ptr<base::Value> pairing_json =
deserializer.Deserialize(&error_code, &error_message);
if (!pairing_json) {
LOG(WARNING) << "Failed to load '" << pairing_file.value() << "' ("
<< error_code << ").";
continue;
}
pairings.Append(base::Value::FromUniquePtrValue(std::move(pairing_json)));
}
return pairings;
}
bool PairingRegistryDelegateLinux::DeleteAll() {
// Delete all pairing files in the pairing registry.
base::FilePath registry_path = GetRegistryPath();
base::FileEnumerator enumerator(registry_path, false,
base::FileEnumerator::FILES,
kPairingFilenamePattern);
bool success = true;
for (base::FilePath pairing_file = enumerator.Next(); !pairing_file.empty();
pairing_file = enumerator.Next()) {
success = success && base::DeleteFile(pairing_file);
}
return success;
}
PairingRegistry::Pairing PairingRegistryDelegateLinux::Load(
const std::string& client_id) {
base::FilePath registry_path = GetRegistryPath();
base::FilePath pairing_file = registry_path.Append(
base::StringPrintf(kPairingFilenameFormat, client_id.c_str()));
JSONFileValueDeserializer deserializer(pairing_file);
int error_code;
std::string error_message;
std::unique_ptr<base::Value> pairing =
deserializer.Deserialize(&error_code, &error_message);
if (!pairing) {
LOG(WARNING) << "Failed to load pairing information: " << error_message
<< " (" << error_code << ").";
return PairingRegistry::Pairing();
}
if (!pairing->is_dict()) {
LOG(WARNING) << "Failed to parse pairing information: not a dictionary.";
return PairingRegistry::Pairing();
}
return PairingRegistry::Pairing::CreateFromValue(pairing->GetDict());
}
bool PairingRegistryDelegateLinux::Save(
const PairingRegistry::Pairing& pairing) {
base::FilePath registry_path = GetRegistryPath();
base::File::Error error;
if (!base::CreateDirectoryAndGetError(registry_path, &error)) {
LOG(ERROR) << "Could not create pairing registry directory: " << error;
return false;
}
std::optional<std::string> pairing_json = base::WriteJson(pairing.ToValue());
if (!pairing_json.has_value()) {
LOG(ERROR) << "Failed to serialize pairing data for "
<< pairing.client_id();
return false;
}
base::FilePath pairing_file = registry_path.Append(
base::StringPrintf(kPairingFilenameFormat, pairing.client_id().c_str()));
if (!base::ImportantFileWriter::WriteFileAtomically(pairing_file,
*pairing_json)) {
LOG(ERROR) << "Could not save pairing data for " << pairing.client_id();
return false;
}
return true;
}
bool PairingRegistryDelegateLinux::Delete(const std::string& client_id) {
base::FilePath registry_path = GetRegistryPath();
base::FilePath pairing_file = registry_path.Append(
base::StringPrintf(kPairingFilenameFormat, client_id.c_str()));
return base::DeleteFile(pairing_file);
}
base::FilePath PairingRegistryDelegateLinux::GetRegistryPath() {
if (!registry_path_for_testing_.empty()) {
return registry_path_for_testing_;
}
base::FilePath config_dir = remoting::GetConfigDir();
return config_dir.Append(kRegistryDirectory);
}
void PairingRegistryDelegateLinux::SetRegistryPathForTesting(
const base::FilePath& registry_path) {
registry_path_for_testing_ = registry_path;
}
std::unique_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate() {
return std::make_unique<PairingRegistryDelegateLinux>();
}
} // namespace remoting
|