Index: sync/test/fake_server/fake_server.cc |
diff --git a/sync/test/fake_server/fake_server.cc b/sync/test/fake_server/fake_server.cc |
index d758e5dbf98ddea44a3045e5669aa4a0bf7c8583..798797e030dfbfdfd41573116588f7019a0a5ae7 100644 |
--- a/sync/test/fake_server/fake_server.cc |
+++ b/sync/test/fake_server/fake_server.cc |
@@ -4,6 +4,7 @@ |
#include "sync/test/fake_server/fake_server.h" |
+#include <algorithm> |
#include <limits> |
#include <string> |
#include <vector> |
@@ -29,7 +30,6 @@ |
using std::string; |
using std::vector; |
-using base::AutoLock; |
using syncer::GetModelType; |
using syncer::ModelType; |
using syncer::ModelTypeSet; |
@@ -238,11 +238,8 @@ void FakeServer::SaveEntity(FakeServerEntity* entity) { |
entities_[entity->GetId()] = entity; |
} |
-int FakeServer::HandleCommand(const string& request, |
- int* response_code, |
- string* response) { |
- AutoLock lock(lock_); |
- |
+void FakeServer::HandleCommand(const string& request, |
+ const HandleCommandCallback& callback) { |
sync_pb::ClientToServerMessage message; |
bool parsed = message.ParseFromString(request); |
DCHECK(parsed); |
@@ -259,20 +256,20 @@ int FakeServer::HandleCommand(const string& request, |
response_proto.mutable_commit()); |
break; |
default: |
- return net::ERR_NOT_IMPLEMENTED; |
+ callback.Run(net::ERR_NOT_IMPLEMENTED, 0, string());; |
+ return; |
} |
if (!success) { |
// TODO(pvalenzuela): Add logging here so that tests have more info about |
// the failure. |
- return net::HTTP_BAD_REQUEST; |
+ callback.Run(net::ERR_FAILED, 0, string()); |
+ return; |
} |
response_proto.set_error_code(sync_pb::SyncEnums::SUCCESS); |
response_proto.set_store_birthday(birthday_); |
- *response_code = net::HTTP_OK; |
- *response = response_proto.SerializeAsString(); |
- return 0; |
+ callback.Run(0, net::HTTP_OK, response_proto.SerializeAsString()); |
} |
bool FakeServer::HandleGetUpdatesRequest( |
@@ -324,13 +321,13 @@ bool FakeServer::HandleGetUpdatesRequest( |
return true; |
} |
-bool FakeServer::CommitEntity( |
+string FakeServer::CommitEntity( |
const sync_pb::SyncEntity& client_entity, |
sync_pb::CommitResponse_EntryResponse* entry_response, |
string client_guid, |
- std::map<string, string>* client_to_server_ids) { |
+ string parent_id) { |
if (client_entity.version() == 0 && client_entity.deleted()) { |
- return false; |
+ return string(); |
} |
FakeServerEntity* entity; |
@@ -339,7 +336,7 @@ bool FakeServer::CommitEntity( |
// TODO(pvalenzuela): Change the behavior of DeleteChilden so that it does |
// not modify server data if it fails. |
if (!DeleteChildren(client_entity.id_string())) { |
- return false; |
+ return string(); |
} |
} else if (GetModelType(client_entity) == syncer::NIGORI) { |
// NIGORI is the only permanent item type that should be updated by the |
@@ -356,11 +353,6 @@ bool FakeServer::CommitEntity( |
entity = UniqueClientEntity::CreateNew(client_entity); |
} |
} else { |
- string parent_id = client_entity.parent_id_string(); |
- if (client_to_server_ids->find(parent_id) != |
- client_to_server_ids->end()) { |
- parent_id = (*client_to_server_ids)[parent_id]; |
- } |
// TODO(pvalenzuela): Validate entity's parent ID. |
if (entities_.find(client_entity.id_string()) != entities_.end()) { |
entity = BookmarkEntity::CreateUpdatedVersion( |
@@ -375,17 +367,12 @@ bool FakeServer::CommitEntity( |
if (entity == NULL) { |
// TODO(pvalenzuela): Add logging so that it is easier to determine why |
// creation failed. |
- return false; |
- } |
- |
- // Record the ID if it was renamed. |
- if (client_entity.id_string() != entity->GetId()) { |
- (*client_to_server_ids)[client_entity.id_string()] = entity->GetId(); |
+ return string(); |
} |
SaveEntity(entity); |
BuildEntryResponseForSuccessfulCommit(entry_response, entity); |
- return true; |
+ return entity->GetId(); |
} |
void FakeServer::BuildEntryResponseForSuccessfulCommit( |
@@ -442,6 +429,7 @@ bool FakeServer::HandleCommitRequest( |
sync_pb::CommitResponse* response) { |
std::map<string, string> client_to_server_ids; |
string guid = commit.cache_guid(); |
+ ModelTypeSet committed_model_types; |
// TODO(pvalenzuela): Add validation of CommitMessage.entries. |
::google::protobuf::RepeatedPtrField<sync_pb::SyncEntity>::const_iterator it; |
@@ -449,11 +437,30 @@ bool FakeServer::HandleCommitRequest( |
sync_pb::CommitResponse_EntryResponse* entry_response = |
response->add_entryresponse(); |
- if (!CommitEntity(*it, entry_response, guid, &client_to_server_ids)) { |
+ sync_pb::SyncEntity client_entity = *it; |
+ string parent_id = client_entity.parent_id_string(); |
+ if (client_to_server_ids.find(parent_id) != |
+ client_to_server_ids.end()) { |
+ parent_id = client_to_server_ids[parent_id]; |
+ } |
+ |
+ string entity_id = CommitEntity(client_entity, |
+ entry_response, |
+ guid, |
+ parent_id); |
+ if (entity_id.empty()) { |
return false; |
} |
+ |
+ // Record the ID if it was renamed. |
+ if (entity_id != client_entity.id_string()) { |
+ client_to_server_ids[client_entity.id_string()] = entity_id; |
+ } |
+ FakeServerEntity* entity = entities_[entity_id]; |
+ committed_model_types.Put(entity->GetModelType()); |
} |
+ FOR_EACH_OBSERVER(Observer, observers_, OnCommit(committed_model_types)); |
return true; |
} |
@@ -489,4 +496,12 @@ scoped_ptr<base::DictionaryValue> FakeServer::GetEntitiesAsDictionaryValue() { |
return dictionary.Pass(); |
} |
+void FakeServer::AddObserver(Observer* observer) { |
+ observers_.AddObserver(observer); |
+} |
+ |
+void FakeServer::RemoveObserver(Observer* observer) { |
+ observers_.RemoveObserver(observer); |
+} |
+ |
} // namespace fake_server |