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 #ifndef SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ | 5 #ifndef SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ |
6 #define SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ | 6 #define SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/callback.h" | |
12 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
13 #include "base/synchronization/lock.h" | 15 #include "base/observer_list.h" |
14 #include "sync/internal_api/public/base/model_type.h" | 16 #include "sync/internal_api/public/base/model_type.h" |
15 #include "sync/protocol/sync.pb.h" | 17 #include "sync/protocol/sync.pb.h" |
16 #include "sync/test/fake_server/fake_server_entity.h" | 18 #include "sync/test/fake_server/fake_server_entity.h" |
17 | 19 |
20 namespace base { | |
21 class WaitableEvent; | |
22 } | |
23 | |
18 namespace fake_server { | 24 namespace fake_server { |
19 | 25 |
20 // A fake version of the Sync server used for testing. | 26 // A fake version of the Sync server used for testing. |
21 class FakeServer { | 27 class FakeServer { |
22 public: | 28 public: |
29 class Observer { | |
30 public: | |
31 virtual ~Observer() {} | |
32 | |
33 // Called after FakeServer has processed a successful commit. The types | |
34 // updated as part of the commit are passed in |committed_model_types|. | |
35 virtual void OnCommit(syncer::ModelTypeSet committed_model_types) = 0; | |
36 }; | |
37 | |
23 FakeServer(); | 38 FakeServer(); |
24 virtual ~FakeServer(); | 39 virtual ~FakeServer(); |
25 | 40 |
26 // Handles a /command POST to the server. If the return value is 0 (success), | 41 // Handles a /command POST to the server. If the return value is 0 (success), |
27 // |response_code| and |response| will be set. Otherwise, the return value | 42 // |response_code| and |response| will be set. Otherwise, the return value |
28 // will be a network error code. | 43 // will be a network error code. |
29 int HandleCommand(const std::string& request, | 44 void HandleCommand(const std::string& request, |
30 int* response_code, | 45 const base::Closure& callback, |
rlarocque
2014/04/22 18:22:22
Rather than asking the caller to provide space for
pval...(no longer on Chromium)
2014/04/24 01:08:53
Done.
| |
31 std::string* response); | 46 int* error_code, |
47 int* response_code, | |
48 std::string* response); | |
49 | |
50 // Adds |observer| to FakeServer's observer list. This should be called | |
51 // before the Profile associated with |observer| is connected to the server. | |
52 // |observer| will remain an Observer for the rest of FakeServer's lifetime. | |
53 void AddObserver(Observer* observer); | |
rlarocque
2014/04/22 18:22:22
This is a bit confusing. Normally the observed ob
pval...(no longer on Chromium)
2014/04/24 01:08:53
I've added a RemoveAllObservers method to FakeServ
rlarocque
2014/04/25 17:41:46
I'd prefer to remove them each individually. Havi
pval...(no longer on Chromium)
2014/04/29 00:06:07
I've added TODOs in SyncTest.{Enable,Disable}Netwo
rlarocque
2014/04/29 00:13:02
That helps with some concerns, but I still worry a
| |
32 | 54 |
33 private: | 55 private: |
34 typedef std::map<std::string, FakeServerEntity*> EntityMap; | 56 typedef std::map<std::string, FakeServerEntity*> EntityMap; |
35 | 57 |
36 // Processes a GetUpdates call. | 58 // Processes a GetUpdates call. |
37 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, | 59 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, |
38 sync_pb::GetUpdatesResponse* response); | 60 sync_pb::GetUpdatesResponse* response); |
39 | 61 |
40 // Processes a Commit call. | 62 // Processes a Commit call. |
41 bool HandleCommitRequest(const sync_pb::CommitMessage& commit, | 63 bool HandleCommitRequest(const sync_pb::CommitMessage& commit, |
(...skipping 10 matching lines...) Expand all Loading... | |
52 void SaveEntity(FakeServerEntity* entity); | 74 void SaveEntity(FakeServerEntity* entity); |
53 | 75 |
54 // Commits a client-side SyncEntity to the server as a FakeServerEntity. | 76 // Commits a client-side SyncEntity to the server as a FakeServerEntity. |
55 // The client that sent the commit is identified via |client_guid| and all | 77 // The client that sent the commit is identified via |client_guid| and all |
56 // entity ID renaming is tracked with |client_to_server_ids|. If the commit | 78 // entity ID renaming is tracked with |client_to_server_ids|. If the commit |
57 // is successful, true is returned and the server's version of the SyncEntity | 79 // is successful, true is returned and the server's version of the SyncEntity |
58 // is stored in |server_entity|. | 80 // is stored in |server_entity|. |
59 bool CommitEntity(const sync_pb::SyncEntity& client_entity, | 81 bool CommitEntity(const sync_pb::SyncEntity& client_entity, |
60 sync_pb::CommitResponse_EntryResponse* entry_response, | 82 sync_pb::CommitResponse_EntryResponse* entry_response, |
61 std::string client_guid, | 83 std::string client_guid, |
84 syncer::ModelType* model_type, | |
62 std::map<std::string, std::string>* client_to_server_ids); | 85 std::map<std::string, std::string>* client_to_server_ids); |
63 | 86 |
64 // Populates |entry_response| based on |entity|. It is assumed that | 87 // Populates |entry_response| based on |entity|. It is assumed that |
65 // SaveEntity has already been called on |entity|. | 88 // SaveEntity has already been called on |entity|. |
66 void BuildEntryResponseForSuccessfulCommit( | 89 void BuildEntryResponseForSuccessfulCommit( |
67 sync_pb::CommitResponse_EntryResponse* entry_response, | 90 sync_pb::CommitResponse_EntryResponse* entry_response, |
68 FakeServerEntity* entity); | 91 FakeServerEntity* entity); |
69 | 92 |
70 // Determines whether the SyncEntity with id_string |id| is a child of an | 93 // Determines whether the SyncEntity with id_string |id| is a child of an |
71 // entity with id_string |potential_parent_id|. | 94 // entity with id_string |potential_parent_id|. |
72 bool IsChild(const std::string& id, const std::string& potential_parent_id); | 95 bool IsChild(const std::string& id, const std::string& potential_parent_id); |
73 | 96 |
74 // Creates and saves tombstones for all children of the entity with the given | 97 // Creates and saves tombstones for all children of the entity with the given |
75 // |id|. A tombstone is not created for the entity itself. | 98 // |id|. A tombstone is not created for the entity itself. |
76 bool DeleteChildren(const std::string& id); | 99 bool DeleteChildren(const std::string& id); |
77 | 100 |
78 // The lock used to ensure that only one client is communicating with server | |
79 // at any given time. | |
80 // | |
81 // It is probably preferable to have FakeServer operate on its own thread and | |
82 // communicate with it via PostTask, but clients would still need to wait for | |
83 // requests to finish before proceeding. | |
84 base::Lock lock_; | |
85 | |
86 // This is the last version number assigned to an entity. The next entity will | 101 // This is the last version number assigned to an entity. The next entity will |
87 // have a version number of version_ + 1. | 102 // have a version number of version_ + 1. |
88 int64 version_; | 103 int64 version_; |
89 | 104 |
90 // The current birthday value. | 105 // The current birthday value. |
91 std::string birthday_; | 106 std::string birthday_; |
92 | 107 |
93 // All SyncEntity objects saved by the server. The key value is the entity's | 108 // All SyncEntity objects saved by the server. The key value is the entity's |
94 // id string. | 109 // id string. |
95 EntityMap entities_; | 110 EntityMap entities_; |
96 | 111 |
97 // All Keystore keys known to the server. | 112 // All Keystore keys known to the server. |
98 std::vector<std::string> keystore_keys_; | 113 std::vector<std::string> keystore_keys_; |
99 | 114 |
100 // All ModelTypes for which permanent entities have been created. These types | 115 // All ModelTypes for which permanent entities have been created. These types |
101 // are kept track of so that permanent entities are not recreated for new | 116 // are kept track of so that permanent entities are not recreated for new |
102 // clients. | 117 // clients. |
103 syncer::ModelTypeSet created_permanent_entity_types_; | 118 syncer::ModelTypeSet created_permanent_entity_types_; |
119 | |
120 // FakeServer's observers. | |
121 ObserverList<Observer> observers_; | |
104 }; | 122 }; |
105 | 123 |
106 } // namespace fake_server | 124 } // namespace fake_server |
107 | 125 |
108 #endif // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ | 126 #endif // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ |
OLD | NEW |