Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: remoting/host/pairing_registry_delegate_linux.cc

Issue 21128006: Refactored PairingRegistry::Delegate such that it can retrieve/modify for a single client. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cosmetic Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/file_util.h" 8 #include "base/file_util.h"
9 #include "base/files/file_enumerator.h"
9 #include "base/files/important_file_writer.h" 10 #include "base/files/important_file_writer.h"
11 #include "base/json/json_file_value_serializer.h"
12 #include "base/json/json_string_value_serializer.h"
10 #include "base/location.h" 13 #include "base/location.h"
11 #include "base/single_thread_task_runner.h" 14 #include "base/strings/stringprintf.h"
12 #include "base/thread_task_runner_handle.h" 15 #include "base/values.h"
13 #include "remoting/host/branding.h" 16 #include "remoting/host/branding.h"
14 17
15 namespace { 18 namespace {
16 const char kRegistryFilename[] = "paired-clients.json"; 19
20 // The pairing registry path relative to the configuration directory.
21 const char kRegistryDirectory[] = "paired-clients";
22
23 const char kPairingFilenameFormat[] = "%s.json";
24 const char kPairingFilenamePattern[] = "*.json";
25
17 } // namespace 26 } // namespace
18 27
19 namespace remoting { 28 namespace remoting {
20 29
21 using protocol::PairingRegistry; 30 using protocol::PairingRegistry;
22 31
23 PairingRegistryDelegateLinux::PairingRegistryDelegateLinux( 32 PairingRegistryDelegateLinux::PairingRegistryDelegateLinux() {
24 scoped_refptr<base::TaskRunner> task_runner)
25 : task_runner_(task_runner),
26 weak_factory_(this) {
27 } 33 }
28 34
29 PairingRegistryDelegateLinux::~PairingRegistryDelegateLinux() { 35 PairingRegistryDelegateLinux::~PairingRegistryDelegateLinux() {
30 } 36 }
31 37
32 void PairingRegistryDelegateLinux::Save( 38 scoped_ptr<base::ListValue> PairingRegistryDelegateLinux::LoadAll() {
33 const std::string& pairings_json, 39 scoped_ptr<base::ListValue> pairings(new base::ListValue());
34 const PairingRegistry::SaveCallback& callback) { 40
35 // If a callback was supplied, wrap it in a helper function that will 41 // Enumerate all pairing files in the pairing registry.
36 // run it on this thread. 42 base::FilePath registry_path = GetRegistryPath();
37 PairingRegistry::SaveCallback run_callback_on_this_thread; 43 base::FileEnumerator enumerator(registry_path, false,
38 if (!callback.is_null()) { 44 base::FileEnumerator::FILES,
39 run_callback_on_this_thread = 45 kPairingFilenamePattern);
40 base::Bind(&PairingRegistryDelegateLinux::RunSaveCallbackOnThread, 46 for (base::FilePath pairing_file = enumerator.Next(); !pairing_file.empty();
41 base::ThreadTaskRunnerHandle::Get(), 47 pairing_file = enumerator.Next()) {
42 callback); 48 // Read the JSON containing pairing data.
49 JSONFileValueSerializer serializer(pairing_file);
50 int error_code;
51 std::string error_message;
52 scoped_ptr<base::Value> pairing_json(
53 serializer.Deserialize(&error_code, &error_message));
54 if (!pairing_json) {
55 LOG(WARNING) << "Failed to load '" << pairing_file.value() << "' ("
56 << error_code << ").";
57 continue;
58 }
59
60 pairings->Append(pairing_json.release());
43 } 61 }
44 task_runner_->PostTask( 62
45 FROM_HERE, 63 return pairings.Pass();
46 base::Bind(&PairingRegistryDelegateLinux::DoSave,
47 weak_factory_.GetWeakPtr(),
48 pairings_json,
49 run_callback_on_this_thread));
50 } 64 }
51 65
52 void PairingRegistryDelegateLinux::Load( 66 bool PairingRegistryDelegateLinux::DeleteAll() {
53 const PairingRegistry::LoadCallback& callback) { 67 // Delete all pairing files in the pairing registry.
54 // Wrap the callback in a helper function that will run it on this thread. 68 base::FilePath registry_path = GetRegistryPath();
55 // Note that, unlike AddPairing, the GetPairing callback is mandatory. 69 base::FileEnumerator enumerator(registry_path, false,
56 PairingRegistry::LoadCallback run_callback_on_this_thread = 70 base::FileEnumerator::FILES,
57 base::Bind(&PairingRegistryDelegateLinux::RunLoadCallbackOnThread, 71 kPairingFilenamePattern);
58 base::ThreadTaskRunnerHandle::Get(), 72
59 callback); 73 bool success = true;
60 task_runner_->PostTask( 74 for (base::FilePath pairing_file = enumerator.Next(); !pairing_file.empty();
61 FROM_HERE, 75 pairing_file = enumerator.Next()) {
62 base::Bind(&PairingRegistryDelegateLinux::DoLoad, 76 success = success && base::DeleteFile(pairing_file, false);
63 weak_factory_.GetWeakPtr(), 77 }
64 run_callback_on_this_thread)); 78
79 return success;
65 } 80 }
66 81
67 void PairingRegistryDelegateLinux::RunSaveCallbackOnThread( 82 PairingRegistry::Pairing PairingRegistryDelegateLinux::Load(
68 scoped_refptr<base::TaskRunner> task_runner, 83 const std::string& client_id) {
69 const PairingRegistry::SaveCallback& callback, 84 base::FilePath registry_path = GetRegistryPath();
70 bool success) { 85 base::FilePath pairing_file = registry_path.Append(
71 task_runner->PostTask(FROM_HERE, base::Bind(callback, success)); 86 base::StringPrintf(kPairingFilenameFormat, client_id.c_str()));
87
88 JSONFileValueSerializer serializer(pairing_file);
89 int error_code;
90 std::string error_message;
91 scoped_ptr<base::Value> pairing(
92 serializer.Deserialize(&error_code, &error_message));
93 if (!pairing) {
94 LOG(WARNING) << "Failed to load pairing information: " << error_message
95 << " (" << error_code << ").";
96 return PairingRegistry::Pairing();
97 }
98
99 return PairingRegistry::Pairing::CreateFromValue(*pairing);
72 } 100 }
73 101
74 void PairingRegistryDelegateLinux::RunLoadCallbackOnThread( 102 bool PairingRegistryDelegateLinux::Save(
75 scoped_refptr<base::TaskRunner> task_runner, 103 const PairingRegistry::Pairing& pairing) {
76 const PairingRegistry::LoadCallback& callback, 104 base::FilePath registry_path = GetRegistryPath();
77 const std::string& pairings_json) { 105 base::PlatformFileError error;
78 task_runner->PostTask(FROM_HERE, base::Bind(callback, pairings_json)); 106 if (!file_util::CreateDirectoryAndGetError(registry_path, &error)) {
107 LOG(ERROR) << "Could not create pairing registry directory: " << error;
108 return false;
109 }
110
111 std::string pairing_json;
112 JSONStringValueSerializer serializer(&pairing_json);
113 if (!serializer.Serialize(*pairing.ToValue())) {
114 LOG(ERROR) << "Failed to serialize pairing data for "
115 << pairing.client_id();
116 return false;
117 }
118
119 base::FilePath pairing_file = registry_path.Append(
120 base::StringPrintf(kPairingFilenameFormat, pairing.client_id().c_str()));
121 if (!base::ImportantFileWriter::WriteFileAtomically(pairing_file,
122 pairing_json)) {
123 LOG(ERROR) << "Could not save pairing data for " << pairing.client_id();
124 return false;
125 }
126
127 return true;
79 } 128 }
80 129
81 void PairingRegistryDelegateLinux::DoSave( 130 bool PairingRegistryDelegateLinux::Delete(const std::string& client_id) {
82 const std::string& pairings_json, 131 base::FilePath registry_path = GetRegistryPath();
83 const PairingRegistry::SaveCallback& callback) { 132 base::FilePath pairing_file = registry_path.Append(
84 base::FilePath registry_path = GetRegistryFilePath(); 133 base::StringPrintf(kPairingFilenameFormat, client_id.c_str()));
85 base::FilePath parent_directory = registry_path.DirName();
86 base::PlatformFileError error;
87 if (!file_util::CreateDirectoryAndGetError(parent_directory, &error)) {
88 LOG(ERROR) << "Could not create pairing registry directory: " << error;
89 return;
90 }
91 if (!base::ImportantFileWriter::WriteFileAtomically(registry_path,
92 pairings_json)) {
93 LOG(ERROR) << "Could not save pairing registry.";
94 }
95 134
96 if (!callback.is_null()) { 135 return base::DeleteFile(pairing_file, false);
97 callback.Run(true);
98 }
99 } 136 }
100 137
101 void PairingRegistryDelegateLinux::DoLoad( 138 base::FilePath PairingRegistryDelegateLinux::GetRegistryPath() {
102 const PairingRegistry::LoadCallback& callback) { 139 if (!registry_path_for_testing_.empty()) {
103 base::FilePath registry_path = GetRegistryFilePath(); 140 return registry_path_for_testing_;
104 std::string result;
105 if (!file_util::ReadFileToString(registry_path, &result)) {
106 LOG(ERROR) << "Load failed.";
107 }
108 callback.Run(result);
109 }
110
111 base::FilePath PairingRegistryDelegateLinux::GetRegistryFilePath() {
112 if (!filename_for_testing_.empty()) {
113 return filename_for_testing_;
114 } 141 }
115 142
116 base::FilePath config_dir = remoting::GetConfigDir(); 143 base::FilePath config_dir = remoting::GetConfigDir();
117 return config_dir.Append(kRegistryFilename); 144 return config_dir.Append(kRegistryDirectory);
118 } 145 }
119 146
120 void PairingRegistryDelegateLinux::SetFilenameForTesting( 147 void PairingRegistryDelegateLinux::SetRegistryPathForTesting(
121 const base::FilePath &filename) { 148 const base::FilePath& registry_path) {
122 filename_for_testing_ = filename; 149 registry_path_for_testing_ = registry_path;
123 } 150 }
124 151
125 152
126 scoped_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate( 153 scoped_ptr<PairingRegistry::Delegate> CreatePairingRegistryDelegate() {
127 scoped_refptr<base::TaskRunner> task_runner) {
128 return scoped_ptr<PairingRegistry::Delegate>( 154 return scoped_ptr<PairingRegistry::Delegate>(
129 new PairingRegistryDelegateLinux(task_runner)); 155 new PairingRegistryDelegateLinux());
130 } 156 }
131 157
132 } // namespace remoting 158 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698