OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/pairing_registry_delegate_linux.h" | 5 #include "remoting/host/pairing_registry_delegate_linux.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_enumerator.h" | 8 #include "base/files/file_enumerator.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/important_file_writer.h" | 10 #include "base/files/important_file_writer.h" |
11 #include "base/json/json_file_value_serializer.h" | 11 #include "base/json/json_file_value_serializer.h" |
12 #include "base/json/json_string_value_serializer.h" | 12 #include "base/json/json_string_value_serializer.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/memory/ptr_util.h" |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "remoting/host/branding.h" | 17 #include "remoting/host/branding.h" |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 // The pairing registry path relative to the configuration directory. | 21 // The pairing registry path relative to the configuration directory. |
21 const char kRegistryDirectory[] = "paired-clients"; | 22 const char kRegistryDirectory[] = "paired-clients"; |
22 | 23 |
23 const char kPairingFilenameFormat[] = "%s.json"; | 24 const char kPairingFilenameFormat[] = "%s.json"; |
24 const char kPairingFilenamePattern[] = "*.json"; | 25 const char kPairingFilenamePattern[] = "*.json"; |
25 | 26 |
26 } // namespace | 27 } // namespace |
27 | 28 |
28 namespace remoting { | 29 namespace remoting { |
29 | 30 |
30 using protocol::PairingRegistry; | 31 using protocol::PairingRegistry; |
31 | 32 |
32 PairingRegistryDelegateLinux::PairingRegistryDelegateLinux() { | 33 PairingRegistryDelegateLinux::PairingRegistryDelegateLinux() { |
33 } | 34 } |
34 | 35 |
35 PairingRegistryDelegateLinux::~PairingRegistryDelegateLinux() { | 36 PairingRegistryDelegateLinux::~PairingRegistryDelegateLinux() { |
36 } | 37 } |
37 | 38 |
38 scoped_ptr<base::ListValue> PairingRegistryDelegateLinux::LoadAll() { | 39 std::unique_ptr<base::ListValue> PairingRegistryDelegateLinux::LoadAll() { |
39 scoped_ptr<base::ListValue> pairings(new base::ListValue()); | 40 std::unique_ptr<base::ListValue> pairings(new base::ListValue()); |
40 | 41 |
41 // Enumerate all pairing files in the pairing registry. | 42 // Enumerate all pairing files in the pairing registry. |
42 base::FilePath registry_path = GetRegistryPath(); | 43 base::FilePath registry_path = GetRegistryPath(); |
43 base::FileEnumerator enumerator(registry_path, false, | 44 base::FileEnumerator enumerator(registry_path, false, |
44 base::FileEnumerator::FILES, | 45 base::FileEnumerator::FILES, |
45 kPairingFilenamePattern); | 46 kPairingFilenamePattern); |
46 for (base::FilePath pairing_file = enumerator.Next(); !pairing_file.empty(); | 47 for (base::FilePath pairing_file = enumerator.Next(); !pairing_file.empty(); |
47 pairing_file = enumerator.Next()) { | 48 pairing_file = enumerator.Next()) { |
48 // Read the JSON containing pairing data. | 49 // Read the JSON containing pairing data. |
49 JSONFileValueDeserializer deserializer(pairing_file); | 50 JSONFileValueDeserializer deserializer(pairing_file); |
50 int error_code; | 51 int error_code; |
51 std::string error_message; | 52 std::string error_message; |
52 scoped_ptr<base::Value> pairing_json = | 53 std::unique_ptr<base::Value> pairing_json = |
53 deserializer.Deserialize(&error_code, &error_message); | 54 deserializer.Deserialize(&error_code, &error_message); |
54 if (!pairing_json) { | 55 if (!pairing_json) { |
55 LOG(WARNING) << "Failed to load '" << pairing_file.value() << "' (" | 56 LOG(WARNING) << "Failed to load '" << pairing_file.value() << "' (" |
56 << error_code << ")."; | 57 << error_code << ")."; |
57 continue; | 58 continue; |
58 } | 59 } |
59 | 60 |
60 pairings->Append(pairing_json.release()); | 61 pairings->Append(pairing_json.release()); |
61 } | 62 } |
62 | 63 |
(...skipping 18 matching lines...) Expand all Loading... |
81 | 82 |
82 PairingRegistry::Pairing PairingRegistryDelegateLinux::Load( | 83 PairingRegistry::Pairing PairingRegistryDelegateLinux::Load( |
83 const std::string& client_id) { | 84 const std::string& client_id) { |
84 base::FilePath registry_path = GetRegistryPath(); | 85 base::FilePath registry_path = GetRegistryPath(); |
85 base::FilePath pairing_file = registry_path.Append( | 86 base::FilePath pairing_file = registry_path.Append( |
86 base::StringPrintf(kPairingFilenameFormat, client_id.c_str())); | 87 base::StringPrintf(kPairingFilenameFormat, client_id.c_str())); |
87 | 88 |
88 JSONFileValueDeserializer deserializer(pairing_file); | 89 JSONFileValueDeserializer deserializer(pairing_file); |
89 int error_code; | 90 int error_code; |
90 std::string error_message; | 91 std::string error_message; |
91 scoped_ptr<base::Value> pairing = | 92 std::unique_ptr<base::Value> pairing = |
92 deserializer.Deserialize(&error_code, &error_message); | 93 deserializer.Deserialize(&error_code, &error_message); |
93 if (!pairing) { | 94 if (!pairing) { |
94 LOG(WARNING) << "Failed to load pairing information: " << error_message | 95 LOG(WARNING) << "Failed to load pairing information: " << error_message |
95 << " (" << error_code << ")."; | 96 << " (" << error_code << ")."; |
96 return PairingRegistry::Pairing(); | 97 return PairingRegistry::Pairing(); |
97 } | 98 } |
98 | 99 |
99 base::DictionaryValue* pairing_dictionary; | 100 base::DictionaryValue* pairing_dictionary; |
100 if (!pairing->GetAsDictionary(&pairing_dictionary)) { | 101 if (!pairing->GetAsDictionary(&pairing_dictionary)) { |
101 LOG(WARNING) << "Failed to parse pairing information: not a dictionary."; | 102 LOG(WARNING) << "Failed to parse pairing information: not a dictionary."; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 | 149 |
149 base::FilePath config_dir = remoting::GetConfigDir(); | 150 base::FilePath config_dir = remoting::GetConfigDir(); |
150 return config_dir.Append(kRegistryDirectory); | 151 return config_dir.Append(kRegistryDirectory); |
151 } | 152 } |
152 | 153 |
153 void PairingRegistryDelegateLinux::SetRegistryPathForTesting( | 154 void PairingRegistryDelegateLinux::SetRegistryPathForTesting( |
154 const base::FilePath& registry_path) { | 155 const base::FilePath& registry_path) { |
155 registry_path_for_testing_ = registry_path; | 156 registry_path_for_testing_ = registry_path; |
156 } | 157 } |
157 | 158 |
158 | 159 std::unique_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate() { |
159 scoped_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate() { | 160 return base::WrapUnique(new PairingRegistryDelegateLinux()); |
160 return make_scoped_ptr(new PairingRegistryDelegateLinux()); | |
161 } | 161 } |
162 | 162 |
163 } // namespace remoting | 163 } // namespace remoting |
OLD | NEW |