blob: 6040c74fb99ad849b5b5fe01ef80145a0af6d0ed [file] [log] [blame]
[email protected]b1ae9012013-06-23 14:10:301// 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 Bang138fde32018-01-18 23:13:427#include <memory>
dcheng5d090492016-06-09 17:53:348#include <utility>
9
[email protected]b1ae9012013-06-23 14:10:3010#include "base/bind.h"
[email protected]378466032013-08-02 01:35:1611#include "base/files/file_enumerator.h"
thestig1ecdcf42014-09-12 05:09:1412#include "base/files/file_util.h"
[email protected]b1ae9012013-06-23 14:10:3013#include "base/files/important_file_writer.h"
[email protected]378466032013-08-02 01:35:1614#include "base/json/json_file_value_serializer.h"
15#include "base/json/json_string_value_serializer.h"
[email protected]b1ae9012013-06-23 14:10:3016#include "base/location.h"
[email protected]378466032013-08-02 01:35:1617#include "base/strings/stringprintf.h"
18#include "base/values.h"
[email protected]b1ae9012013-06-23 14:10:3019#include "remoting/host/branding.h"
20
21namespace {
[email protected]378466032013-08-02 01:35:1622
23// The pairing registry path relative to the configuration directory.
24const char kRegistryDirectory[] = "paired-clients";
25
26const char kPairingFilenameFormat[] = "%s.json";
27const char kPairingFilenamePattern[] = "*.json";
28
[email protected]b1ae9012013-06-23 14:10:3029} // namespace
30
31namespace remoting {
32
33using protocol::PairingRegistry;
34
Chris Watkins6fe52aa2017-11-28 03:24:0535PairingRegistryDelegateLinux::PairingRegistryDelegateLinux() = default;
[email protected]b1ae9012013-06-23 14:10:3036
Chris Watkins6fe52aa2017-11-28 03:24:0537PairingRegistryDelegateLinux::~PairingRegistryDelegateLinux() = default;
[email protected]b1ae9012013-06-23 14:10:3038
dcheng0765c492016-04-06 22:41:5339std::unique_ptr<base::ListValue> PairingRegistryDelegateLinux::LoadAll() {
40 std::unique_ptr<base::ListValue> pairings(new base::ListValue());
[email protected]378466032013-08-02 01:35:1641
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.
prashhir54a994502015-03-05 09:30:5750 JSONFileValueDeserializer deserializer(pairing_file);
[email protected]378466032013-08-02 01:35:1651 int error_code;
52 std::string error_message;
dcheng0765c492016-04-06 22:41:5353 std::unique_ptr<base::Value> pairing_json =
olli.raulaba045252015-10-16 06:16:4054 deserializer.Deserialize(&error_code, &error_message);
[email protected]378466032013-08-02 01:35:1655 if (!pairing_json) {
56 LOG(WARNING) << "Failed to load '" << pairing_file.value() << "' ("
57 << error_code << ").";
58 continue;
59 }
60
dcheng5d090492016-06-09 17:53:3461 pairings->Append(std::move(pairing_json));
[email protected]b1ae9012013-06-23 14:10:3062 }
[email protected]378466032013-08-02 01:35:1663
sergeyu1417e0132015-12-23 19:01:2264 return pairings;
[email protected]b1ae9012013-06-23 14:10:3065}
66
[email protected]378466032013-08-02 01:35:1667bool 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]b1ae9012013-06-23 14:10:3081}
82
[email protected]378466032013-08-02 01:35:1683PairingRegistry::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
prashhir54a994502015-03-05 09:30:5789 JSONFileValueDeserializer deserializer(pairing_file);
[email protected]378466032013-08-02 01:35:1690 int error_code;
91 std::string error_message;
dcheng0765c492016-04-06 22:41:5392 std::unique_ptr<base::Value> pairing =
olli.raulaba045252015-10-16 06:16:4093 deserializer.Deserialize(&error_code, &error_message);
[email protected]378466032013-08-02 01:35:1694 if (!pairing) {
95 LOG(WARNING) << "Failed to load pairing information: " << error_message
96 << " (" << error_code << ").";
97 return PairingRegistry::Pairing();
98 }
99
[email protected]20014052013-08-09 21:31:01100 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]b1ae9012013-06-23 14:10:30107}
108
[email protected]378466032013-08-02 01:35:16109bool PairingRegistryDelegateLinux::Save(
110 const PairingRegistry::Pairing& pairing) {
111 base::FilePath registry_path = GetRegistryPath();
[email protected]54124ed02014-01-07 10:06:58112 base::File::Error error;
[email protected]426d1c92013-12-03 20:08:54113 if (!base::CreateDirectoryAndGetError(registry_path, &error)) {
[email protected]b1ae9012013-06-23 14:10:30114 LOG(ERROR) << "Could not create pairing registry directory: " << error;
[email protected]378466032013-08-02 01:35:16115 return false;
[email protected]b1ae9012013-06-23 14:10:30116 }
117
[email protected]378466032013-08-02 01:35:16118 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]b1ae9012013-06-23 14:10:30124 }
[email protected]378466032013-08-02 01:35:16125
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]b1ae9012013-06-23 14:10:30135}
136
[email protected]378466032013-08-02 01:35:16137bool 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]b1ae9012013-06-23 14:10:30143}
144
[email protected]378466032013-08-02 01:35:16145base::FilePath PairingRegistryDelegateLinux::GetRegistryPath() {
146 if (!registry_path_for_testing_.empty()) {
147 return registry_path_for_testing_;
[email protected]b1ae9012013-06-23 14:10:30148 }
149
150 base::FilePath config_dir = remoting::GetConfigDir();
[email protected]378466032013-08-02 01:35:16151 return config_dir.Append(kRegistryDirectory);
[email protected]b1ae9012013-06-23 14:10:30152}
153
[email protected]378466032013-08-02 01:35:16154void PairingRegistryDelegateLinux::SetRegistryPathForTesting(
155 const base::FilePath& registry_path) {
156 registry_path_for_testing_ = registry_path;
[email protected]b1ae9012013-06-23 14:10:30157}
158
dcheng0765c492016-04-06 22:41:53159std::unique_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate() {
Jinho Bang138fde32018-01-18 23:13:42160 return std::make_unique<PairingRegistryDelegateLinux>();
[email protected]b1ae9012013-06-23 14:10:30161}
162
163} // namespace remoting