Index: remoting/protocol/pairing_registry.cc |
diff --git a/remoting/protocol/pairing_registry.cc b/remoting/protocol/pairing_registry.cc |
index 6c6b1a229127f525a021d6b77848fbe4c047546a..a430a31f1b37142e3b979cd0bcee302eb86b42e1 100644 |
--- a/remoting/protocol/pairing_registry.cc |
+++ b/remoting/protocol/pairing_registry.cc |
@@ -36,6 +36,9 @@ PairingRegistry::Pairing::Pairing(const base::Time& created_time, |
shared_secret_(shared_secret) { |
} |
+PairingRegistry::Pairing::~Pairing() { |
+} |
+ |
PairingRegistry::Pairing PairingRegistry::Pairing::Create( |
const std::string& client_name) { |
base::Time created_time = base::Time::Now(); |
@@ -50,7 +53,36 @@ PairingRegistry::Pairing PairingRegistry::Pairing::Create( |
return Pairing(created_time, client_name, client_id, shared_secret); |
} |
-PairingRegistry::Pairing::~Pairing() { |
+PairingRegistry::Pairing PairingRegistry::Pairing::CreateFromJson( |
+ const base::Value& pairing_json) { |
+ const base::DictionaryValue* pairing = NULL; |
+ if (!pairing_json.GetAsDictionary(&pairing)) { |
+ LOG(ERROR) << "Failed to load pairing information: not a dictionary."; |
+ return Pairing(); |
+ } |
+ |
+ std::string client_name, client_id, shared_secret; |
+ double created_time_value; |
+ if (pairing->GetDouble(kCreatedTimeKey, &created_time_value) && |
+ pairing->GetString(kClientNameKey, &client_name) && |
+ pairing->GetString(kClientIdKey, &client_id) && |
+ pairing->GetString(kSharedSecretKey, &shared_secret)) { |
+ base::Time created_time = base::Time::FromJsTime(created_time_value); |
+ return Pairing(created_time, client_name, client_id, shared_secret); |
+ } |
+ |
+ LOG(ERROR) << "Failed to load pairing information: unexpected format."; |
+ return Pairing(); |
+} |
+ |
+scoped_ptr<base::Value> PairingRegistry::Pairing::EncodeJson() const { |
+ scoped_ptr<base::DictionaryValue> pairing(new base::DictionaryValue()); |
+ pairing->SetDouble(kCreatedTimeKey, created_time().ToJsTime()); |
+ pairing->SetString(kClientNameKey, client_name()); |
+ pairing->SetString(kClientIdKey, client_id()); |
+ if (!shared_secret().empty()) |
+ pairing->SetString(kSharedSecretKey, shared_secret()); |
+ return pairing.PassAs<base::Value>(); |
} |
bool PairingRegistry::Pairing::operator==(const Pairing& other) const { |
@@ -86,14 +118,12 @@ void PairingRegistry::GetPairing(const std::string& client_id, |
GetPairingCallback wrapped_callback = base::Bind( |
&PairingRegistry::InvokeGetPairingCallbackAndScheduleNext, |
this, callback); |
- LoadCallback load_callback = base::Bind( |
- &PairingRegistry::DoGetPairing, this, client_id, wrapped_callback); |
// |Unretained| and |get| are both safe here because the delegate is owned |
// by the pairing registry and so is guaranteed to exist when the request |
// is serviced. |
base::Closure request = base::Bind( |
&PairingRegistry::Delegate::Load, |
- base::Unretained(delegate_.get()), load_callback); |
+ base::Unretained(delegate_.get()), client_id, wrapped_callback); |
ServiceOrQueueRequest(request); |
} |
@@ -103,100 +133,52 @@ void PairingRegistry::GetAllPairings( |
GetAllPairingsCallback wrapped_callback = base::Bind( |
&PairingRegistry::InvokeGetAllPairingsCallbackAndScheduleNext, |
this, callback); |
- LoadCallback load_callback = base::Bind( |
- &PairingRegistry::SanitizePairings, this, wrapped_callback); |
base::Closure request = base::Bind( |
- &PairingRegistry::Delegate::Load, |
- base::Unretained(delegate_.get()), load_callback); |
+ &PairingRegistry::Delegate::LoadAll, |
+ base::Unretained(delegate_.get()), wrapped_callback); |
ServiceOrQueueRequest(request); |
} |
void PairingRegistry::DeletePairing( |
- const std::string& client_id, const SaveCallback& callback) { |
+ const std::string& client_id, const DoneCallback& callback) { |
DCHECK(CalledOnValidThread()); |
- SaveCallback wrapped_callback = base::Bind( |
- &PairingRegistry::InvokeSaveCallbackAndScheduleNext, |
+ DoneCallback wrapped_callback = base::Bind( |
+ &PairingRegistry::InvokeDoneCallbackAndScheduleNext, |
this, callback); |
- LoadCallback load_callback = base::Bind( |
- &PairingRegistry::DoDeletePairing, this, client_id, wrapped_callback); |
base::Closure request = base::Bind( |
- &PairingRegistry::Delegate::Load, |
- base::Unretained(delegate_.get()), load_callback); |
+ &PairingRegistry::Delegate::Delete, |
+ base::Unretained(delegate_.get()), client_id, wrapped_callback); |
ServiceOrQueueRequest(request); |
} |
void PairingRegistry::ClearAllPairings( |
- const SaveCallback& callback) { |
+ const DoneCallback& callback) { |
DCHECK(CalledOnValidThread()); |
- SaveCallback wrapped_callback = base::Bind( |
- &PairingRegistry::InvokeSaveCallbackAndScheduleNext, |
+ DoneCallback wrapped_callback = base::Bind( |
+ &PairingRegistry::InvokeDoneCallbackAndScheduleNext, |
this, callback); |
base::Closure request = base::Bind( |
- &PairingRegistry::Delegate::Save, |
- base::Unretained(delegate_.get()), |
- EncodeJson(PairedClients()), |
- wrapped_callback); |
+ &PairingRegistry::Delegate::DeleteAll, |
+ base::Unretained(delegate_.get()), wrapped_callback); |
ServiceOrQueueRequest(request); |
} |
void PairingRegistry::AddPairing(const Pairing& pairing) { |
- SaveCallback callback = base::Bind( |
- &PairingRegistry::InvokeSaveCallbackAndScheduleNext, |
- this, SaveCallback()); |
- LoadCallback load_callback = base::Bind( |
- &PairingRegistry::MergePairingAndSave, this, pairing, callback); |
+ DoneCallback wrapped_callback = base::Bind( |
+ &PairingRegistry::InvokeDoneCallbackAndScheduleNext, |
+ this, DoneCallback()); |
base::Closure request = base::Bind( |
- &PairingRegistry::Delegate::Load, |
- base::Unretained(delegate_.get()), load_callback); |
+ &PairingRegistry::Delegate::Save, |
+ base::Unretained(delegate_.get()), pairing, wrapped_callback); |
ServiceOrQueueRequest(request); |
} |
-void PairingRegistry::MergePairingAndSave(const Pairing& pairing, |
- const SaveCallback& callback, |
- const std::string& pairings_json) { |
- DCHECK(CalledOnValidThread()); |
- PairedClients clients = DecodeJson(pairings_json); |
- clients[pairing.client_id()] = pairing; |
- std::string new_pairings_json = EncodeJson(clients); |
- delegate_->Save(new_pairings_json, callback); |
-} |
- |
-void PairingRegistry::DoGetPairing(const std::string& client_id, |
- const GetPairingCallback& callback, |
- const std::string& pairings_json) { |
- PairedClients clients = DecodeJson(pairings_json); |
- Pairing result = clients[client_id]; |
- callback.Run(result); |
-} |
- |
-void PairingRegistry::SanitizePairings(const GetAllPairingsCallback& callback, |
- const std::string& pairings_json) { |
- PairedClients clients = DecodeJson(pairings_json); |
- callback.Run(ConvertToListValue(clients, false)); |
-} |
- |
-void PairingRegistry::DoDeletePairing(const std::string& client_id, |
- const SaveCallback& callback, |
- const std::string& pairings_json) { |
- PairedClients clients = DecodeJson(pairings_json); |
- clients.erase(client_id); |
- std::string new_pairings_json = EncodeJson(clients); |
- delegate_->Save(new_pairings_json, callback); |
-} |
- |
-void PairingRegistry::InvokeLoadCallbackAndScheduleNext( |
- const LoadCallback& callback, const std::string& pairings_json) { |
- callback.Run(pairings_json); |
- pending_requests_.pop(); |
- ServiceNextRequest(); |
-} |
- |
-void PairingRegistry::InvokeSaveCallbackAndScheduleNext( |
- const SaveCallback& callback, bool success) { |
+void PairingRegistry::InvokeDoneCallbackAndScheduleNext( |
+ const DoneCallback& callback, bool success) { |
// CreatePairing doesn't have a callback, so the callback can be null. |
- if (!callback.is_null()) { |
+ if (!callback.is_null()) |
callback.Run(success); |
- } |
+ |
pending_requests_.pop(); |
ServiceNextRequest(); |
} |
@@ -216,52 +198,6 @@ void PairingRegistry::InvokeGetAllPairingsCallbackAndScheduleNext( |
ServiceNextRequest(); |
} |
-// static |
-PairingRegistry::PairedClients PairingRegistry::DecodeJson( |
- const std::string& pairings_json) { |
- PairedClients result; |
- |
- if (pairings_json.empty()) { |
- return result; |
- } |
- |
- JSONStringValueSerializer registry(pairings_json); |
- int error_code; |
- std::string error_message; |
- scoped_ptr<base::Value> root( |
- registry.Deserialize(&error_code, &error_message)); |
- if (!root) { |
- LOG(ERROR) << "Failed to load paired clients: " << error_message |
- << " (" << error_code << ")."; |
- return result; |
- } |
- |
- base::ListValue* root_list = NULL; |
- if (!root->GetAsList(&root_list)) { |
- LOG(ERROR) << "Failed to load paired clients: root node is not a list."; |
- return result; |
- } |
- |
- for (size_t i = 0; i < root_list->GetSize(); ++i) { |
- base::DictionaryValue* pairing = NULL; |
- std::string client_name, client_id, shared_secret; |
- double created_time_value; |
- if (root_list->GetDictionary(i, &pairing) && |
- pairing->GetDouble(kCreatedTimeKey, &created_time_value) && |
- pairing->GetString(kClientNameKey, &client_name) && |
- pairing->GetString(kClientIdKey, &client_id) && |
- pairing->GetString(kSharedSecretKey, &shared_secret)) { |
- base::Time created_time = base::Time::FromJsTime(created_time_value); |
- result[client_id] = Pairing( |
- created_time, client_name, client_id, shared_secret); |
- } else { |
- LOG(ERROR) << "Paired client " << i << " has unexpected format."; |
- } |
- } |
- |
- return result; |
-} |
- |
void PairingRegistry::ServiceOrQueueRequest(const base::Closure& request) { |
bool servicing_request = !pending_requests_.empty(); |
pending_requests_.push(request); |
@@ -271,41 +207,12 @@ void PairingRegistry::ServiceOrQueueRequest(const base::Closure& request) { |
} |
void PairingRegistry::ServiceNextRequest() { |
- if (pending_requests_.empty()) { |
+ if (pending_requests_.empty()) |
return; |
- } |
+ |
base::Closure request = pending_requests_.front(); |
request.Run(); |
} |
-// static |
-std::string PairingRegistry::EncodeJson(const PairedClients& clients) { |
- scoped_ptr<base::ListValue> root = ConvertToListValue(clients, true); |
- std::string result; |
- JSONStringValueSerializer serializer(&result); |
- serializer.Serialize(*root); |
- |
- return result; |
-} |
- |
-// static |
-scoped_ptr<base::ListValue> PairingRegistry::ConvertToListValue( |
- const PairedClients& clients, |
- bool include_shared_secrets) { |
- scoped_ptr<base::ListValue> root(new base::ListValue()); |
- for (PairedClients::const_iterator i = clients.begin(); |
- i != clients.end(); ++i) { |
- base::DictionaryValue* pairing = new base::DictionaryValue(); |
- pairing->SetDouble(kCreatedTimeKey, i->second.created_time().ToJsTime()); |
- pairing->SetString(kClientNameKey, i->second.client_name()); |
- pairing->SetString(kClientIdKey, i->second.client_id()); |
- if (include_shared_secrets) { |
- pairing->SetString(kSharedSecretKey, i->second.shared_secret()); |
- } |
- root->Append(pairing); |
- } |
- return root.Pass(); |
-} |
- |
} // namespace protocol |
} // namespace remoting |