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/protocol/pairing_registry.h" | 5 #include "remoting/protocol/pairing_registry.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/guid.h" | 9 #include "base/guid.h" |
10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 client_id_ == other.client_id_ && | 58 client_id_ == other.client_id_ && |
59 client_name_ == other.client_name_ && | 59 client_name_ == other.client_name_ && |
60 shared_secret_ == other.shared_secret_; | 60 shared_secret_ == other.shared_secret_; |
61 } | 61 } |
62 | 62 |
63 bool PairingRegistry::Pairing::is_valid() const { | 63 bool PairingRegistry::Pairing::is_valid() const { |
64 return !client_id_.empty() && !shared_secret_.empty(); | 64 return !client_id_.empty() && !shared_secret_.empty(); |
65 } | 65 } |
66 | 66 |
67 PairingRegistry::PairingRegistry(scoped_ptr<Delegate> delegate) | 67 PairingRegistry::PairingRegistry(scoped_ptr<Delegate> delegate) |
68 : delegate_(delegate.Pass()) { | 68 : delegate_(delegate.Pass()), |
| 69 servicing_request_(false) { |
69 DCHECK(delegate_); | 70 DCHECK(delegate_); |
70 } | 71 } |
71 | 72 |
72 PairingRegistry::~PairingRegistry() { | 73 PairingRegistry::~PairingRegistry() { |
73 } | 74 } |
74 | 75 |
75 PairingRegistry::Pairing PairingRegistry::CreatePairing( | 76 PairingRegistry::Pairing PairingRegistry::CreatePairing( |
76 const std::string& client_name) { | 77 const std::string& client_name) { |
77 DCHECK(CalledOnValidThread()); | 78 DCHECK(CalledOnValidThread()); |
78 Pairing result = Pairing::Create(client_name); | 79 Pairing result = Pairing::Create(client_name); |
79 AddPairing(result); | 80 AddPairing(result); |
80 return result; | 81 return result; |
81 } | 82 } |
82 | 83 |
83 void PairingRegistry::GetPairing(const std::string& client_id, | 84 void PairingRegistry::GetPairing(const std::string& client_id, |
84 const GetPairingCallback& callback) { | 85 const GetPairingCallback& callback) { |
85 DCHECK(CalledOnValidThread()); | 86 DCHECK(CalledOnValidThread()); |
| 87 GetPairingCallback wrapped_callback = base::Bind( |
| 88 &PairingRegistry::InvokeGetPairingCallbackAndScheduleNext, |
| 89 this, callback); |
| 90 base::Closure request = base::Bind( |
| 91 &PairingRegistry::GetPairingAndScheduleNext, |
| 92 this, client_id, wrapped_callback); |
| 93 ServiceOrQueueRequest(request); |
| 94 } |
| 95 |
| 96 void PairingRegistry::GetAllPairings( |
| 97 const GetAllPairingsCallback& callback) { |
| 98 DCHECK(CalledOnValidThread()); |
| 99 GetAllPairingsCallback wrapped_callback = base::Bind( |
| 100 &PairingRegistry::InvokeGetAllPairingsCallbackAndScheduleNext, |
| 101 this, callback); |
| 102 base::Closure request = base::Bind( |
| 103 &PairingRegistry::GetAllPairingsAndScheduleNext, |
| 104 this, wrapped_callback); |
| 105 ServiceOrQueueRequest(request); |
| 106 } |
| 107 |
| 108 void PairingRegistry::DeletePairing( |
| 109 const std::string& client_id, const SaveCallback& callback) { |
| 110 DCHECK(CalledOnValidThread()); |
| 111 SaveCallback wrapped_callback = base::Bind( |
| 112 &PairingRegistry::InvokeSaveCallbackAndScheduleNext, |
| 113 this, callback); |
| 114 base::Closure request = base::Bind( |
| 115 &PairingRegistry::DeletePairingAndScheduleNext, |
| 116 this, client_id, wrapped_callback); |
| 117 ServiceOrQueueRequest(request); |
| 118 } |
| 119 |
| 120 void PairingRegistry::ClearAllPairings( |
| 121 const SaveCallback& callback) { |
| 122 DCHECK(CalledOnValidThread()); |
| 123 SaveCallback wrapped_callback = base::Bind( |
| 124 &PairingRegistry::InvokeSaveCallbackAndScheduleNext, |
| 125 this, callback); |
| 126 base::Closure request = base::Bind( |
| 127 &PairingRegistry::ClearAllPairingsAndScheduleNext, |
| 128 this, wrapped_callback); |
| 129 ServiceOrQueueRequest(request); |
| 130 } |
| 131 |
| 132 void PairingRegistry::AddPairing(const Pairing& pairing) { |
| 133 SaveCallback callback = base::Bind( |
| 134 &PairingRegistry::InvokeSaveCallbackAndScheduleNext, |
| 135 this, SaveCallback()); |
| 136 base::Closure request = base::Bind( |
| 137 &PairingRegistry::SavePairingAndScheduleNext, |
| 138 this, pairing, callback); |
| 139 ServiceOrQueueRequest(request); |
| 140 } |
| 141 |
| 142 void PairingRegistry::SavePairingAndScheduleNext(Pairing pairing, |
| 143 const SaveCallback& callback) { |
| 144 DCHECK(CalledOnValidThread()); |
| 145 delegate_->Load( |
| 146 base::Bind(&PairingRegistry::MergePairingAndSave, |
| 147 this, pairing, callback)); |
| 148 } |
| 149 |
| 150 void PairingRegistry::GetPairingAndScheduleNext( |
| 151 const std::string& client_id, const GetPairingCallback& callback) { |
| 152 DCHECK(CalledOnValidThread()); |
86 delegate_->Load( | 153 delegate_->Load( |
87 base::Bind(&PairingRegistry::DoGetPairing, this, client_id, callback)); | 154 base::Bind(&PairingRegistry::DoGetPairing, this, client_id, callback)); |
88 } | 155 } |
89 | 156 |
90 void PairingRegistry::GetAllPairings(const GetAllPairingsCallback& callback) { | 157 void PairingRegistry::GetAllPairingsAndScheduleNext( |
| 158 const GetAllPairingsCallback& callback) { |
91 DCHECK(CalledOnValidThread()); | 159 DCHECK(CalledOnValidThread()); |
92 delegate_->Load( | 160 delegate_->Load( |
93 base::Bind(&PairingRegistry::SanitizePairings, this, callback)); | 161 base::Bind(&PairingRegistry::SanitizePairings, this, callback)); |
94 } | 162 } |
95 | 163 |
96 void PairingRegistry::DeletePairing(const std::string& client_id, | 164 void PairingRegistry::DeletePairingAndScheduleNext( |
97 const SaveCallback& callback) { | 165 const std::string& client_id, const SaveCallback& callback) { |
98 DCHECK(CalledOnValidThread()); | 166 DCHECK(CalledOnValidThread()); |
99 delegate_->Load( | 167 delegate_->Load( |
100 base::Bind(&PairingRegistry::DoDeletePairing, | 168 base::Bind(&PairingRegistry::DoDeletePairing, |
101 this, client_id, callback)); | 169 this, client_id, callback)); |
102 } | 170 } |
103 | 171 |
104 void PairingRegistry::ClearAllPairings(const SaveCallback& callback) { | 172 void PairingRegistry::ClearAllPairingsAndScheduleNext( |
| 173 const SaveCallback& callback) { |
105 DCHECK(CalledOnValidThread()); | 174 DCHECK(CalledOnValidThread()); |
106 delegate_->Save(EncodeJson(PairedClients()), callback); | 175 delegate_->Save(EncodeJson(PairedClients()), callback); |
107 } | 176 } |
108 | 177 |
109 void PairingRegistry::AddPairing(const Pairing& pairing) { | |
110 delegate_->Load( | |
111 base::Bind(&PairingRegistry::MergePairingAndSave, this, pairing)); | |
112 } | |
113 | |
114 void PairingRegistry::MergePairingAndSave(const Pairing& pairing, | 178 void PairingRegistry::MergePairingAndSave(const Pairing& pairing, |
| 179 const SaveCallback& callback, |
115 const std::string& pairings_json) { | 180 const std::string& pairings_json) { |
116 DCHECK(CalledOnValidThread()); | 181 DCHECK(CalledOnValidThread()); |
117 PairedClients clients = DecodeJson(pairings_json); | 182 PairedClients clients = DecodeJson(pairings_json); |
118 clients[pairing.client_id()] = pairing; | 183 clients[pairing.client_id()] = pairing; |
119 std::string new_pairings_json = EncodeJson(clients); | 184 std::string new_pairings_json = EncodeJson(clients); |
120 delegate_->Save(new_pairings_json, SaveCallback()); | 185 delegate_->Save(new_pairings_json, callback); |
121 } | 186 } |
122 | 187 |
123 void PairingRegistry::DoGetPairing(const std::string& client_id, | 188 void PairingRegistry::DoGetPairing(const std::string& client_id, |
124 const GetPairingCallback& callback, | 189 const GetPairingCallback& callback, |
125 const std::string& pairings_json) { | 190 const std::string& pairings_json) { |
126 PairedClients clients = DecodeJson(pairings_json); | 191 PairedClients clients = DecodeJson(pairings_json); |
127 Pairing result = clients[client_id]; | 192 Pairing result = clients[client_id]; |
128 callback.Run(result); | 193 callback.Run(result); |
129 } | 194 } |
130 | 195 |
131 void PairingRegistry::SanitizePairings(const GetAllPairingsCallback& callback, | 196 void PairingRegistry::SanitizePairings(const GetAllPairingsCallback& callback, |
132 const std::string& pairings_json) { | 197 const std::string& pairings_json) { |
133 PairedClients clients = DecodeJson(pairings_json); | 198 PairedClients clients = DecodeJson(pairings_json); |
134 callback.Run(ConvertToListValue(clients, false)); | 199 callback.Run(ConvertToListValue(clients, false)); |
135 } | 200 } |
136 | 201 |
137 void PairingRegistry::DoDeletePairing(const std::string& client_id, | 202 void PairingRegistry::DoDeletePairing(const std::string& client_id, |
138 const SaveCallback& callback, | 203 const SaveCallback& callback, |
139 const std::string& pairings_json) { | 204 const std::string& pairings_json) { |
140 PairedClients clients = DecodeJson(pairings_json); | 205 PairedClients clients = DecodeJson(pairings_json); |
141 clients.erase(client_id); | 206 clients.erase(client_id); |
142 std::string new_pairings_json = EncodeJson(clients); | 207 std::string new_pairings_json = EncodeJson(clients); |
143 delegate_->Save(new_pairings_json, callback); | 208 delegate_->Save(new_pairings_json, callback); |
144 } | 209 } |
145 | 210 |
| 211 void PairingRegistry::InvokeLoadCallbackAndScheduleNext( |
| 212 const LoadCallback& callback, const std::string& pairings_json) { |
| 213 callback.Run(pairings_json); |
| 214 servicing_request_ = false; |
| 215 ServiceNextRequest(); |
| 216 } |
| 217 |
| 218 void PairingRegistry::InvokeSaveCallbackAndScheduleNext( |
| 219 const SaveCallback& callback, bool success) { |
| 220 // CreatePairing doesn't have a callback, so the callback can be null. |
| 221 if (!callback.is_null()) { |
| 222 callback.Run(success); |
| 223 } |
| 224 servicing_request_ = false; |
| 225 ServiceNextRequest(); |
| 226 } |
| 227 |
| 228 void PairingRegistry::InvokeGetPairingCallbackAndScheduleNext( |
| 229 const GetPairingCallback& callback, Pairing pairing) { |
| 230 callback.Run(pairing); |
| 231 servicing_request_ = false; |
| 232 ServiceNextRequest(); |
| 233 } |
| 234 |
| 235 void PairingRegistry::InvokeGetAllPairingsCallbackAndScheduleNext( |
| 236 const GetAllPairingsCallback& callback, |
| 237 scoped_ptr<base::ListValue> pairings) { |
| 238 callback.Run(pairings.Pass()); |
| 239 servicing_request_ = false; |
| 240 ServiceNextRequest(); |
| 241 } |
| 242 |
146 // static | 243 // static |
147 PairingRegistry::PairedClients PairingRegistry::DecodeJson( | 244 PairingRegistry::PairedClients PairingRegistry::DecodeJson( |
148 const std::string& pairings_json) { | 245 const std::string& pairings_json) { |
149 PairedClients result; | 246 PairedClients result; |
150 | 247 |
151 if (pairings_json.empty()) { | 248 if (pairings_json.empty()) { |
152 return result; | 249 return result; |
153 } | 250 } |
154 | 251 |
155 JSONStringValueSerializer registry(pairings_json); | 252 JSONStringValueSerializer registry(pairings_json); |
(...skipping 26 matching lines...) Expand all Loading... |
182 result[client_id] = Pairing( | 279 result[client_id] = Pairing( |
183 created_time, client_name, client_id, shared_secret); | 280 created_time, client_name, client_id, shared_secret); |
184 } else { | 281 } else { |
185 LOG(ERROR) << "Paired client " << i << " has unexpected format."; | 282 LOG(ERROR) << "Paired client " << i << " has unexpected format."; |
186 } | 283 } |
187 } | 284 } |
188 | 285 |
189 return result; | 286 return result; |
190 } | 287 } |
191 | 288 |
| 289 void PairingRegistry::ServiceOrQueueRequest(const base::Closure& request) { |
| 290 pending_requests_.push(request); |
| 291 if (!servicing_request_) { |
| 292 ServiceNextRequest(); |
| 293 } |
| 294 } |
| 295 |
| 296 void PairingRegistry::ServiceNextRequest() { |
| 297 if (pending_requests_.empty()) { |
| 298 return; |
| 299 } |
| 300 servicing_request_ = true; |
| 301 base::Closure request = pending_requests_.front(); |
| 302 pending_requests_.pop(); |
| 303 request.Run(); |
| 304 } |
| 305 |
192 // static | 306 // static |
193 std::string PairingRegistry::EncodeJson(const PairedClients& clients) { | 307 std::string PairingRegistry::EncodeJson(const PairedClients& clients) { |
194 scoped_ptr<base::ListValue> root = ConvertToListValue(clients, true); | 308 scoped_ptr<base::ListValue> root = ConvertToListValue(clients, true); |
195 std::string result; | 309 std::string result; |
196 JSONStringValueSerializer serializer(&result); | 310 JSONStringValueSerializer serializer(&result); |
197 serializer.Serialize(*root); | 311 serializer.Serialize(*root); |
198 | 312 |
199 return result; | 313 return result; |
200 } | 314 } |
201 | 315 |
(...skipping 11 matching lines...) Expand all Loading... |
213 if (include_shared_secrets) { | 327 if (include_shared_secrets) { |
214 pairing->SetString(kSharedSecretKey, i->second.shared_secret()); | 328 pairing->SetString(kSharedSecretKey, i->second.shared_secret()); |
215 } | 329 } |
216 root->Append(pairing); | 330 root->Append(pairing); |
217 } | 331 } |
218 return root.Pass(); | 332 return root.Pass(); |
219 } | 333 } |
220 | 334 |
221 } // namespace protocol | 335 } // namespace protocol |
222 } // namespace remoting | 336 } // namespace remoting |
OLD | NEW |