OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "sync/test/fake_server/fake_server.h" | 5 #include "sync/test/fake_server/fake_server.h" |
6 | 6 |
7 #include <algorithm> | |
7 #include <limits> | 8 #include <limits> |
8 #include <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
12 #include "base/guid.h" | 13 #include "base/guid.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
19 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
20 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
21 #include "net/http/http_status_code.h" | 22 #include "net/http/http_status_code.h" |
22 #include "sync/internal_api/public/base/model_type.h" | 23 #include "sync/internal_api/public/base/model_type.h" |
23 #include "sync/protocol/sync.pb.h" | 24 #include "sync/protocol/sync.pb.h" |
24 #include "sync/test/fake_server/bookmark_entity.h" | 25 #include "sync/test/fake_server/bookmark_entity.h" |
25 #include "sync/test/fake_server/permanent_entity.h" | 26 #include "sync/test/fake_server/permanent_entity.h" |
26 #include "sync/test/fake_server/tombstone_entity.h" | 27 #include "sync/test/fake_server/tombstone_entity.h" |
27 #include "sync/test/fake_server/unique_client_entity.h" | 28 #include "sync/test/fake_server/unique_client_entity.h" |
28 | 29 |
29 using std::string; | 30 using std::string; |
30 using std::vector; | 31 using std::vector; |
31 | 32 |
32 using base::AutoLock; | |
33 using syncer::GetModelType; | 33 using syncer::GetModelType; |
34 using syncer::ModelType; | 34 using syncer::ModelType; |
35 using syncer::ModelTypeSet; | |
35 | 36 |
36 // The default birthday value. | 37 // The default birthday value. |
37 static const char kDefaultBirthday[] = "1234567890"; | 38 static const char kDefaultBirthday[] = "1234567890"; |
38 | 39 |
39 // The default keystore key. | 40 // The default keystore key. |
40 static const char kDefaultKeystoreKey[] = "1111111111111111"; | 41 static const char kDefaultKeystoreKey[] = "1111111111111111"; |
41 | 42 |
42 namespace fake_server { | 43 namespace fake_server { |
43 | 44 |
44 class FakeServerEntity; | 45 class FakeServerEntity; |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 SaveEntity(mobile_bookmarks_entity); | 231 SaveEntity(mobile_bookmarks_entity); |
231 return true; | 232 return true; |
232 } | 233 } |
233 | 234 |
234 void FakeServer::SaveEntity(FakeServerEntity* entity) { | 235 void FakeServer::SaveEntity(FakeServerEntity* entity) { |
235 delete entities_[entity->GetId()]; | 236 delete entities_[entity->GetId()]; |
236 entity->SetVersion(++version_); | 237 entity->SetVersion(++version_); |
237 entities_[entity->GetId()] = entity; | 238 entities_[entity->GetId()] = entity; |
238 } | 239 } |
239 | 240 |
240 int FakeServer::HandleCommand(const string& request, | 241 void FakeServer::HandleCommand(const string& request, |
241 int* response_code, | 242 const base::Closure& callback, |
242 string* response) { | 243 int* error_code, |
243 AutoLock lock(lock_); | 244 int* response_code, |
244 | 245 string* response) { |
245 sync_pb::ClientToServerMessage message; | 246 sync_pb::ClientToServerMessage message; |
246 bool parsed = message.ParseFromString(request); | 247 bool parsed = message.ParseFromString(request); |
247 DCHECK(parsed); | 248 DCHECK(parsed); |
248 | 249 |
249 sync_pb::ClientToServerResponse response_proto; | 250 sync_pb::ClientToServerResponse response_proto; |
250 bool success; | 251 bool success; |
251 switch (message.message_contents()) { | 252 switch (message.message_contents()) { |
252 case sync_pb::ClientToServerMessage::GET_UPDATES: | 253 case sync_pb::ClientToServerMessage::GET_UPDATES: |
253 success = HandleGetUpdatesRequest(message.get_updates(), | 254 success = HandleGetUpdatesRequest(message.get_updates(), |
254 response_proto.mutable_get_updates()); | 255 response_proto.mutable_get_updates()); |
255 break; | 256 break; |
256 case sync_pb::ClientToServerMessage::COMMIT: | 257 case sync_pb::ClientToServerMessage::COMMIT: |
257 success = HandleCommitRequest(message.commit(), | 258 success = HandleCommitRequest(message.commit(), |
258 response_proto.mutable_commit()); | 259 response_proto.mutable_commit()); |
259 break; | 260 break; |
260 default: | 261 default: |
261 return net::ERR_NOT_IMPLEMENTED; | 262 *error_code = net::ERR_NOT_IMPLEMENTED; |
263 callback.Run(); | |
264 return; | |
262 } | 265 } |
263 | 266 |
264 if (!success) { | 267 if (!success) { |
265 // TODO(pvalenzuela): Add logging here so that tests have more info about | 268 // TODO(pvalenzuela): Add logging here so that tests have more info about |
266 // the failure. | 269 // the failure. |
267 return net::HTTP_BAD_REQUEST; | 270 *error_code = net::HTTP_BAD_REQUEST; |
pval...(no longer on Chromium)
2014/04/24 01:08:53
this value was incorrect (it's the error code, not
| |
271 callback.Run(); | |
272 return; | |
268 } | 273 } |
269 | 274 |
270 response_proto.set_error_code(sync_pb::SyncEnums::SUCCESS); | 275 response_proto.set_error_code(sync_pb::SyncEnums::SUCCESS); |
271 response_proto.set_store_birthday(birthday_); | 276 response_proto.set_store_birthday(birthday_); |
272 *response_code = net::HTTP_OK; | 277 *response_code = net::HTTP_OK; |
273 *response = response_proto.SerializeAsString(); | 278 *response = response_proto.SerializeAsString(); |
274 return 0; | 279 *error_code = 0; |
280 callback.Run(); | |
275 } | 281 } |
276 | 282 |
277 bool FakeServer::HandleGetUpdatesRequest( | 283 bool FakeServer::HandleGetUpdatesRequest( |
278 const sync_pb::GetUpdatesMessage& get_updates, | 284 const sync_pb::GetUpdatesMessage& get_updates, |
279 sync_pb::GetUpdatesResponse* response) { | 285 sync_pb::GetUpdatesResponse* response) { |
280 // TODO(pvalenzuela): Implement batching instead of sending all information | 286 // TODO(pvalenzuela): Implement batching instead of sending all information |
281 // at once. | 287 // at once. |
282 response->set_changes_remaining(0); | 288 response->set_changes_remaining(0); |
283 | 289 |
284 scoped_ptr<UpdateSieve> sieve = UpdateSieve::Create(get_updates); | 290 scoped_ptr<UpdateSieve> sieve = UpdateSieve::Create(get_updates); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 } | 326 } |
321 | 327 |
322 sieve->UpdateProgressMarkers(max_response_version, response); | 328 sieve->UpdateProgressMarkers(max_response_version, response); |
323 return true; | 329 return true; |
324 } | 330 } |
325 | 331 |
326 bool FakeServer::CommitEntity( | 332 bool FakeServer::CommitEntity( |
327 const sync_pb::SyncEntity& client_entity, | 333 const sync_pb::SyncEntity& client_entity, |
328 sync_pb::CommitResponse_EntryResponse* entry_response, | 334 sync_pb::CommitResponse_EntryResponse* entry_response, |
329 string client_guid, | 335 string client_guid, |
336 ModelType* model_type, | |
rlarocque
2014/04/22 18:22:22
This is starting to look a little awkward. Could
pval...(no longer on Chromium)
2014/04/24 01:08:53
done / looks good to me
| |
330 std::map<string, string>* client_to_server_ids) { | 337 std::map<string, string>* client_to_server_ids) { |
331 if (client_entity.version() == 0 && client_entity.deleted()) { | 338 if (client_entity.version() == 0 && client_entity.deleted()) { |
332 return false; | 339 return false; |
333 } | 340 } |
334 | 341 |
335 FakeServerEntity* entity; | 342 FakeServerEntity* entity; |
336 if (client_entity.deleted()) { | 343 if (client_entity.deleted()) { |
337 entity = TombstoneEntity::Create(client_entity.id_string()); | 344 entity = TombstoneEntity::Create(client_entity.id_string()); |
338 // TODO(pvalenzuela): Change the behavior of DeleteChilden so that it does | 345 // TODO(pvalenzuela): Change the behavior of DeleteChilden so that it does |
339 // not modify server data if it fails. | 346 // not modify server data if it fails. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
377 return false; | 384 return false; |
378 } | 385 } |
379 | 386 |
380 // Record the ID if it was renamed. | 387 // Record the ID if it was renamed. |
381 if (client_entity.id_string() != entity->GetId()) { | 388 if (client_entity.id_string() != entity->GetId()) { |
382 (*client_to_server_ids)[client_entity.id_string()] = entity->GetId(); | 389 (*client_to_server_ids)[client_entity.id_string()] = entity->GetId(); |
383 } | 390 } |
384 | 391 |
385 SaveEntity(entity); | 392 SaveEntity(entity); |
386 BuildEntryResponseForSuccessfulCommit(entry_response, entity); | 393 BuildEntryResponseForSuccessfulCommit(entry_response, entity); |
394 *model_type = entity->GetModelType(); | |
387 return true; | 395 return true; |
388 } | 396 } |
389 | 397 |
390 void FakeServer::BuildEntryResponseForSuccessfulCommit( | 398 void FakeServer::BuildEntryResponseForSuccessfulCommit( |
391 sync_pb::CommitResponse_EntryResponse* entry_response, | 399 sync_pb::CommitResponse_EntryResponse* entry_response, |
392 FakeServerEntity* entity) { | 400 FakeServerEntity* entity) { |
393 entry_response->set_response_type(sync_pb::CommitResponse::SUCCESS); | 401 entry_response->set_response_type(sync_pb::CommitResponse::SUCCESS); |
394 entry_response->set_id_string(entity->GetId()); | 402 entry_response->set_id_string(entity->GetId()); |
395 | 403 |
396 if (entity->IsDeleted()) { | 404 if (entity->IsDeleted()) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
434 } | 442 } |
435 | 443 |
436 return true; | 444 return true; |
437 } | 445 } |
438 | 446 |
439 bool FakeServer::HandleCommitRequest( | 447 bool FakeServer::HandleCommitRequest( |
440 const sync_pb::CommitMessage& commit, | 448 const sync_pb::CommitMessage& commit, |
441 sync_pb::CommitResponse* response) { | 449 sync_pb::CommitResponse* response) { |
442 std::map<string, string> client_to_server_ids; | 450 std::map<string, string> client_to_server_ids; |
443 string guid = commit.cache_guid(); | 451 string guid = commit.cache_guid(); |
452 ModelTypeSet committed_model_types; | |
444 | 453 |
445 // TODO(pvalenzuela): Add validation of CommitMessage.entries. | 454 // TODO(pvalenzuela): Add validation of CommitMessage.entries. |
446 ::google::protobuf::RepeatedPtrField<sync_pb::SyncEntity>::const_iterator it; | 455 ::google::protobuf::RepeatedPtrField<sync_pb::SyncEntity>::const_iterator it; |
447 for (it = commit.entries().begin(); it != commit.entries().end(); ++it) { | 456 for (it = commit.entries().begin(); it != commit.entries().end(); ++it) { |
448 sync_pb::CommitResponse_EntryResponse* entry_response = | 457 sync_pb::CommitResponse_EntryResponse* entry_response = |
449 response->add_entryresponse(); | 458 response->add_entryresponse(); |
450 | 459 |
451 if (!CommitEntity(*it, entry_response, guid, &client_to_server_ids)) { | 460 ModelType model_type; |
461 if (!CommitEntity(*it, | |
462 entry_response, | |
463 guid, | |
464 &model_type, | |
465 &client_to_server_ids)) { | |
452 return false; | 466 return false; |
453 } | 467 } |
468 committed_model_types.Put(model_type); | |
454 } | 469 } |
455 | 470 |
471 FOR_EACH_OBSERVER(Observer, observers_, OnCommit(committed_model_types)); | |
456 return true; | 472 return true; |
457 } | 473 } |
458 | 474 |
475 void FakeServer::AddObserver(Observer* observer) { | |
476 observers_.AddObserver(observer); | |
477 } | |
478 | |
459 } // namespace fake_server | 479 } // namespace fake_server |
OLD | NEW |