Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/host/pairing_registry_delegate_linux.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/environment.h" | |
| 9 #include "base/file_util.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" | |
| 13 #include "base/location.h" | |
| 14 #include "base/single_thread_task_runner.h" | |
| 15 #include "base/string_number_conversions.h" | |
| 16 #include "base/thread_task_runner_handle.h" | |
| 17 #include "base/values.h" | |
| 18 #include "remoting/host/branding.h" | |
| 19 | |
| 20 namespace { | |
| 21 const char kRegistryFilename[] = "paired-clients.json"; | |
| 22 const char kCreatedTimeKey[] = "created-time"; | |
| 23 const char kClientIdKey[] = "client-id"; | |
| 24 const char kClientNameKey[] = "client-name"; | |
| 25 const char kSharedSecretKey[] = "shared-secret"; | |
| 26 } // namespace | |
| 27 | |
| 28 namespace remoting { | |
| 29 namespace protocol { | |
| 30 | |
| 31 PairingRegistryDelegateLinux::PairingRegistryDelegateLinux( | |
| 32 scoped_refptr<base::TaskRunner> task_runner) | |
| 33 : task_runner_(task_runner), | |
| 34 weak_factory_(this) { | |
| 35 } | |
| 36 | |
| 37 PairingRegistryDelegateLinux::~PairingRegistryDelegateLinux() { | |
| 38 } | |
| 39 | |
| 40 void PairingRegistryDelegateLinux::AddPairing( | |
| 41 const PairingRegistry::Pairing& new_paired_client, | |
| 42 const PairingRegistry::AddPairingCallback& callback) { | |
|
Lambros
2013/06/20 17:59:20
nit: indentation
Jamie
2013/06/20 20:17:09
Done.
| |
| 43 // If a callback was supplied, wrap it in a helper function that will | |
| 44 // run it on this thread. | |
| 45 PairingRegistry::AddPairingCallback run_callback_on_this_thread; | |
| 46 if (!callback.is_null()) { | |
| 47 run_callback_on_this_thread = | |
| 48 base::Bind(&PairingRegistryDelegateLinux::RunAddPairingCallbackOnThread, | |
|
Lambros
2013/06/20 17:59:20
RunAddPairingCallbackOnThread doesn't access any m
Jamie
2013/06/20 20:17:09
I'm made the methods static, but I don't see how t
| |
| 49 weak_factory_.GetWeakPtr(), | |
| 50 base::ThreadTaskRunnerHandle::Get(), | |
| 51 callback); | |
| 52 } | |
| 53 task_runner_->PostTask( | |
| 54 FROM_HERE, | |
| 55 base::Bind(&PairingRegistryDelegateLinux::DoAddPairing, | |
| 56 weak_factory_.GetWeakPtr(), | |
| 57 new_paired_client, | |
| 58 run_callback_on_this_thread)); | |
| 59 } | |
| 60 | |
| 61 void PairingRegistryDelegateLinux::GetPairing( | |
| 62 const std::string& client_id, | |
| 63 const PairingRegistry::GetPairingCallback& callback) { | |
|
Lambros
2013/06/20 17:59:20
nit: remove extra space
Jamie
2013/06/20 20:17:09
Done.
| |
| 64 // Wrap the callback in a helper function that will run it on this thread. | |
| 65 // Note that, unlike AddPairing, the GetPairing callback is mandatory. | |
| 66 PairingRegistry::GetPairingCallback run_callback_on_this_thread = | |
| 67 base::Bind(&PairingRegistryDelegateLinux::RunGetPairingCallbackOnThread, | |
| 68 weak_factory_.GetWeakPtr(), | |
| 69 base::ThreadTaskRunnerHandle::Get(), | |
| 70 callback); | |
| 71 task_runner_->PostTask( | |
| 72 FROM_HERE, | |
| 73 base::Bind(&PairingRegistryDelegateLinux::DoGetPairing, | |
| 74 weak_factory_.GetWeakPtr(), | |
| 75 client_id, | |
| 76 run_callback_on_this_thread)); | |
| 77 } | |
| 78 | |
| 79 void PairingRegistryDelegateLinux::RunAddPairingCallbackOnThread( | |
| 80 scoped_refptr<base::TaskRunner> task_runner, | |
| 81 const PairingRegistry::AddPairingCallback& callback, | |
| 82 bool success) { | |
| 83 task_runner->PostTask(FROM_HERE, base::Bind(callback, success)); | |
| 84 } | |
| 85 | |
| 86 void PairingRegistryDelegateLinux::RunGetPairingCallbackOnThread( | |
| 87 scoped_refptr<base::TaskRunner> task_runner, | |
| 88 const PairingRegistry::GetPairingCallback& callback, | |
| 89 PairingRegistry::Pairing pairing) { | |
| 90 task_runner->PostTask(FROM_HERE, base::Bind(callback, pairing)); | |
| 91 } | |
| 92 | |
| 93 void PairingRegistryDelegateLinux::DoAddPairing( | |
| 94 const PairingRegistry::Pairing& new_paired_client, | |
| 95 const PairingRegistry::AddPairingCallback& callback) { | |
| 96 PairingRegistry::PairedClients paired_clients = LoadPairings(); | |
| 97 paired_clients[new_paired_client.client_id()] = new_paired_client; | |
| 98 SavePairings(paired_clients); | |
| 99 if (!callback.is_null()) { | |
| 100 callback.Run(true); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 void PairingRegistryDelegateLinux::DoGetPairing( | |
| 105 const std::string& client_id, | |
| 106 const PairingRegistry::GetPairingCallback& callback) { | |
| 107 PairingRegistry::PairedClients paired_clients = LoadPairings(); | |
| 108 PairingRegistry::PairedClients::const_iterator i = | |
| 109 paired_clients.find(client_id); | |
| 110 PairingRegistry::Pairing result; | |
| 111 if (i != paired_clients.end()) { | |
| 112 result = i->second; | |
| 113 } | |
| 114 callback.Run(result); | |
| 115 } | |
| 116 | |
| 117 PairingRegistry::PairedClients PairingRegistryDelegateLinux::LoadPairings() { | |
| 118 PairingRegistry::PairedClients result; | |
| 119 | |
| 120 base::FilePath registry_path = GetRegistryFilePath(); | |
| 121 JSONFileValueSerializer registry(registry_path); | |
| 122 int error_code; | |
| 123 std::string error_message; | |
| 124 scoped_ptr<base::Value> root( | |
| 125 registry.Deserialize(&error_code, &error_message)); | |
| 126 if (!root) { | |
| 127 LOG(ERROR) << "Failed to load paired clients: " << error_message | |
| 128 << " (" << error_code << ")."; | |
| 129 return result; | |
| 130 } | |
| 131 | |
| 132 base::ListValue* root_list = NULL; | |
| 133 if (!root->GetAsList(&root_list)) { | |
| 134 LOG(ERROR) << "Failed to load paired clients: root node is not a list."; | |
| 135 return result; | |
| 136 } | |
| 137 | |
| 138 for (size_t i = 0; i < root_list->GetSize(); ++i) { | |
| 139 base::DictionaryValue* pairing = NULL; | |
| 140 std::string created_time, client_name, client_id, shared_secret; | |
| 141 if (root_list->GetDictionary(i, &pairing) && | |
| 142 pairing->GetString(kCreatedTimeKey, &created_time) && | |
| 143 pairing->GetString(kClientNameKey, &client_name) && | |
| 144 pairing->GetString(kClientIdKey, &client_id) && | |
| 145 pairing->GetString(kSharedSecretKey, &shared_secret)) { | |
| 146 int64 created_time_value; | |
| 147 if (!base::StringToInt64(created_time, &created_time_value)) { | |
| 148 LOG(ERROR) << "Paired client " << i << " has invalid creation time."; | |
| 149 continue; | |
| 150 } | |
| 151 result[client_id] = PairingRegistry::Pairing( | |
| 152 base::Time::FromInternalValue(created_time_value), | |
| 153 client_name, client_id, shared_secret); | |
| 154 } else { | |
| 155 LOG(ERROR) << "Paired client " << i << " has unexpected format."; | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 return result; | |
| 160 } | |
| 161 | |
| 162 void PairingRegistryDelegateLinux::SavePairings( | |
| 163 const PairingRegistry::PairedClients& paired_clients) { | |
| 164 base::ListValue root; | |
| 165 for (PairingRegistry::PairedClients::const_iterator i = | |
| 166 paired_clients.begin(); i != paired_clients.end(); ++i) { | |
| 167 base::DictionaryValue* pairing = new base::DictionaryValue(); | |
| 168 std::string created_time = | |
| 169 base::Int64ToString(i->second.created_time().ToInternalValue()); | |
| 170 pairing->SetString(kCreatedTimeKey, created_time); | |
| 171 pairing->SetString(kClientNameKey, i->second.client_name()); | |
| 172 pairing->SetString(kClientIdKey, i->second.client_id()); | |
| 173 pairing->SetString(kSharedSecretKey, i->second.shared_secret()); | |
| 174 root.Append(pairing); | |
| 175 } | |
| 176 | |
| 177 std::string pairings_json; | |
| 178 JSONStringValueSerializer serializer(&pairings_json); | |
| 179 serializer.Serialize(root); | |
| 180 | |
| 181 base::FilePath registry_path = GetRegistryFilePath(); | |
| 182 base::FilePath parent_directory = registry_path.DirName(); | |
| 183 base::PlatformFileError error; | |
| 184 if (!file_util::CreateDirectoryAndGetError(parent_directory, &error)) { | |
| 185 LOG(ERROR) << "Could not create pairing registry directory: " << error; | |
| 186 return; | |
| 187 } | |
| 188 if (!base::ImportantFileWriter::WriteFileAtomically(registry_path, | |
| 189 pairings_json)) { | |
| 190 LOG(ERROR) << "Could not save pairing registry."; | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 base::FilePath PairingRegistryDelegateLinux::GetRegistryFilePath() { | |
| 195 if (!filename_for_testing_.empty()) { | |
| 196 return filename_for_testing_; | |
| 197 } | |
| 198 | |
| 199 base::FilePath config_dir = remoting::GetConfigDir(); | |
| 200 return config_dir.Append(kRegistryFilename); | |
| 201 } | |
| 202 | |
| 203 void PairingRegistryDelegateLinux::SetFilenameForTesting( | |
| 204 const base::FilePath &filename) { | |
| 205 filename_for_testing_ = filename; | |
| 206 } | |
| 207 | |
| 208 | |
| 209 scoped_ptr<PairingRegistry::Delegate> PairingRegistry::CreateDelegate( | |
| 210 scoped_refptr<base::TaskRunner> task_runner) { | |
| 211 return scoped_ptr<PairingRegistry::Delegate>( | |
| 212 new PairingRegistryDelegateLinux(task_runner)); | |
| 213 } | |
| 214 | |
| 215 } // namespace protocol | |
| 216 } // namespace remoting | |
| OLD | NEW |