blob: 3cae6f7570a003052c572a4399f7d66aa7bc3dd0 [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
dcheng5d090492016-06-09 17:53:347#include <utility>
8
[email protected]b1ae9012013-06-23 14:10:309#include "base/bind.h"
[email protected]378466032013-08-02 01:35:1610#include "base/files/file_enumerator.h"
thestig1ecdcf42014-09-12 05:09:1411#include "base/files/file_util.h"
[email protected]b1ae9012013-06-23 14:10:3012#include "base/files/important_file_writer.h"
[email protected]378466032013-08-02 01:35:1613#include "base/json/json_file_value_serializer.h"
14#include "base/json/json_string_value_serializer.h"
[email protected]b1ae9012013-06-23 14:10:3015#include "base/location.h"
dcheng0765c492016-04-06 22:41:5316#include "base/memory/ptr_util.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
[email protected]378466032013-08-02 01:35:1635PairingRegistryDelegateLinux::PairingRegistryDelegateLinux() {
[email protected]b1ae9012013-06-23 14:10:3036}
37
38PairingRegistryDelegateLinux::~PairingRegistryDelegateLinux() {
39}
40
dcheng0765c492016-04-06 22:41:5341std::unique_ptr<base::ListValue> PairingRegistryDelegateLinux::LoadAll() {
42 std::unique_ptr<base::ListValue> pairings(new base::ListValue());
[email protected]378466032013-08-02 01:35:1643
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.
prashhir54a994502015-03-05 09:30:5752 JSONFileValueDeserializer deserializer(pairing_file);
[email protected]378466032013-08-02 01:35:1653 int error_code;
54 std::string error_message;
dcheng0765c492016-04-06 22:41:5355 std::unique_ptr<base::Value> pairing_json =
olli.raulaba045252015-10-16 06:16:4056 deserializer.Deserialize(&error_code, &error_message);
[email protected]378466032013-08-02 01:35:1657 if (!pairing_json) {
58 LOG(WARNING) << "Failed to load '" << pairing_file.value() << "' ("
59 << error_code << ").";
60 continue;
61 }
62
dcheng5d090492016-06-09 17:53:3463 pairings->Append(std::move(pairing_json));
[email protected]b1ae9012013-06-23 14:10:3064 }
[email protected]378466032013-08-02 01:35:1665
sergeyu1417e0132015-12-23 19:01:2266 return pairings;
[email protected]b1ae9012013-06-23 14:10:3067}
68
[email protected]378466032013-08-02 01:35:1669bool 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]b1ae9012013-06-23 14:10:3083}
84
[email protected]378466032013-08-02 01:35:1685PairingRegistry::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
prashhir54a994502015-03-05 09:30:5791 JSONFileValueDeserializer deserializer(pairing_file);
[email protected]378466032013-08-02 01:35:1692 int error_code;
93 std::string error_message;
dcheng0765c492016-04-06 22:41:5394 std::unique_ptr<base::Value> pairing =
olli.raulaba045252015-10-16 06:16:4095 deserializer.Deserialize(&error_code, &error_message);
[email protected]378466032013-08-02 01:35:1696 if (!pairing) {
97 LOG(WARNING) << "Failed to load pairing information: " << error_message
98 << " (" << error_code << ").";
99 return PairingRegistry::Pairing();
100 }
101
[email protected]20014052013-08-09 21:31:01102 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]b1ae9012013-06-23 14:10:30109}
110
[email protected]378466032013-08-02 01:35:16111bool PairingRegistryDelegateLinux::Save(
112 const PairingRegistry::Pairing& pairing) {
113 base::FilePath registry_path = GetRegistryPath();
[email protected]54124ed02014-01-07 10:06:58114 base::File::Error error;
[email protected]426d1c92013-12-03 20:08:54115 if (!base::CreateDirectoryAndGetError(registry_path, &error)) {
[email protected]b1ae9012013-06-23 14:10:30116 LOG(ERROR) << "Could not create pairing registry directory: " << error;
[email protected]378466032013-08-02 01:35:16117 return false;
[email protected]b1ae9012013-06-23 14:10:30118 }
119
[email protected]378466032013-08-02 01:35:16120 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]b1ae9012013-06-23 14:10:30126 }
[email protected]378466032013-08-02 01:35:16127
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]b1ae9012013-06-23 14:10:30137}
138
[email protected]378466032013-08-02 01:35:16139bool 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]b1ae9012013-06-23 14:10:30145}
146
[email protected]378466032013-08-02 01:35:16147base::FilePath PairingRegistryDelegateLinux::GetRegistryPath() {
148 if (!registry_path_for_testing_.empty()) {
149 return registry_path_for_testing_;
[email protected]b1ae9012013-06-23 14:10:30150 }
151
152 base::FilePath config_dir = remoting::GetConfigDir();
[email protected]378466032013-08-02 01:35:16153 return config_dir.Append(kRegistryDirectory);
[email protected]b1ae9012013-06-23 14:10:30154}
155
[email protected]378466032013-08-02 01:35:16156void PairingRegistryDelegateLinux::SetRegistryPathForTesting(
157 const base::FilePath& registry_path) {
158 registry_path_for_testing_ = registry_path;
[email protected]b1ae9012013-06-23 14:10:30159}
160
dcheng0765c492016-04-06 22:41:53161std::unique_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate() {
ricea68860bd2016-08-22 02:48:56162 return base::MakeUnique<PairingRegistryDelegateLinux>();
[email protected]b1ae9012013-06-23 14:10:30163}
164
165} // namespace remoting