OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ | 5 #ifndef COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ |
6 #define COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ | 6 #define COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
18 #include "base/values.h" | 18 #include "base/values.h" |
19 #include "components/sync/base/model_type.h" | 19 #include "components/sync/base/model_type.h" |
20 #include "components/sync/engine_impl/loopback_server/loopback_server_entity.h" | 20 #include "components/sync/engine_impl/loopback_server/loopback_server_entity.h" |
21 #include "components/sync/engine_impl/net/server_connection_manager.h" | 21 #include "components/sync/engine_impl/net/server_connection_manager.h" |
22 #include "components/sync/protocol/loopback_server.pb.h" | 22 #include "components/sync/protocol/loopback_server.pb.h" |
23 #include "components/sync/protocol/sync.pb.h" | 23 #include "components/sync/protocol/sync.pb.h" |
24 | 24 |
| 25 namespace fake_server { |
| 26 class FakeServer; |
| 27 } |
| 28 |
25 namespace syncer { | 29 namespace syncer { |
26 | 30 |
27 // A loopback version of the Sync server used for local profile serialization. | 31 // A loopback version of the Sync server used for local profile serialization. |
28 class LoopbackServer { | 32 class LoopbackServer { |
29 public: | 33 public: |
| 34 class ObserverForTests { |
| 35 public: |
| 36 virtual ~ObserverForTests() {} |
| 37 |
| 38 // Called after the server has processed a successful commit. The types |
| 39 // updated as part of the commit are passed in |committed_model_types|. |
| 40 virtual void OnCommit(const std::string& committer_id, |
| 41 syncer::ModelTypeSet committed_model_types) = 0; |
| 42 }; |
| 43 |
30 explicit LoopbackServer(const base::FilePath& persistent_file); | 44 explicit LoopbackServer(const base::FilePath& persistent_file); |
31 virtual ~LoopbackServer(); | 45 virtual ~LoopbackServer(); |
32 | 46 |
33 // Handles a /command POST (with the given |request|) to the server. Three | 47 // Handles a /command POST (with the given |request|) to the server. Three |
34 // output arguments, |server_status|, |response_code|, and |response|, are | 48 // output arguments, |server_status|, |response_code|, and |response|, are |
35 // used to pass data back to the caller. The command has failed if the value | 49 // used to pass data back to the caller. The command has failed if the value |
36 // pointed to by |error_code| is nonzero. | 50 // pointed to by |error_code| is nonzero. |
37 void HandleCommand(const std::string& request, | 51 void HandleCommand(const std::string& request, |
38 HttpResponse::ServerConnectionCode* server_status, | 52 HttpResponse::ServerConnectionCode* server_status, |
39 int64_t* response_code, | 53 int64_t* response_code, |
40 std::string* response); | 54 std::string* response); |
41 | 55 |
42 private: | 56 private: |
| 57 // Allow the FakeServer decorator to inspect the internals of this class. |
| 58 friend class fake_server::FakeServer; |
| 59 |
43 using EntityMap = | 60 using EntityMap = |
44 std::map<std::string, std::unique_ptr<LoopbackServerEntity>>; | 61 std::map<std::string, std::unique_ptr<LoopbackServerEntity>>; |
45 | 62 |
| 63 using ResponseTypeProvider = |
| 64 base::RepeatingCallback<sync_pb::CommitResponse::ResponseType( |
| 65 const LoopbackServerEntity& entity)>; |
| 66 |
46 // Gets LoopbackServer ready for syncing. | 67 // Gets LoopbackServer ready for syncing. |
47 void Init(); | 68 void Init(); |
48 | 69 |
49 // Processes a GetUpdates call. | 70 // Processes a GetUpdates call. |
50 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, | 71 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, |
51 sync_pb::GetUpdatesResponse* response); | 72 sync_pb::GetUpdatesResponse* response); |
52 | 73 |
53 // Processes a Commit call. | 74 // Processes a Commit call. |
54 bool HandleCommitRequest(const sync_pb::CommitMessage& message, | 75 bool HandleCommitRequest(const sync_pb::CommitMessage& message, |
55 const std::string& invalidator_client_id, | 76 const std::string& invalidator_client_id, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 // |id|. A tombstone is not created for the entity itself. | 116 // |id|. A tombstone is not created for the entity itself. |
96 void DeleteChildren(const std::string& id); | 117 void DeleteChildren(const std::string& id); |
97 | 118 |
98 // Updates the |entity| to a new version and increments the version counter | 119 // Updates the |entity| to a new version and increments the version counter |
99 // that the server uses to assign versions. | 120 // that the server uses to assign versions. |
100 void UpdateEntityVersion(LoopbackServerEntity* entity); | 121 void UpdateEntityVersion(LoopbackServerEntity* entity); |
101 | 122 |
102 // Returns the store birthday. | 123 // Returns the store birthday. |
103 std::string GetStoreBirthday() const; | 124 std::string GetStoreBirthday() const; |
104 | 125 |
| 126 // Returns all entities stored by the server of the given |model_type|. |
| 127 // This method is only used in tests. |
| 128 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType( |
| 129 syncer::ModelType model_type); |
| 130 |
| 131 // Creates a DicionaryValue representation of all entities present in the |
| 132 // server. The dictionary keys are the strings generated by ModelTypeToString |
| 133 // and the values are ListValues containing StringValue versions of entity |
| 134 // names. Used by test to verify the contents of the server state. |
| 135 std::unique_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue(); |
| 136 |
| 137 // Modifies the entity on the server with the given |id|. The entity's |
| 138 // EntitySpecifics are replaced with |updated_specifics| and its version is |
| 139 // updated to n+1. If the given |id| does not exist or the ModelType of |
| 140 // |updated_specifics| does not match the entity, false is returned. |
| 141 // Otherwise, true is returned to represent a successful modification. |
| 142 // |
| 143 // This method sometimes updates entity data beyond EntitySpecifics. For |
| 144 // example, in the case of a bookmark, changing the BookmarkSpecifics title |
| 145 // field will modify the top-level entity's name field. |
| 146 // This method is only used in tests. |
| 147 bool ModifyEntitySpecifics(const std::string& id, |
| 148 const sync_pb::EntitySpecifics& updated_specifics); |
| 149 |
| 150 // This method is only used in tests. |
| 151 bool ModifyBookmarkEntity(const std::string& id, |
| 152 const std::string& parent_id, |
| 153 const sync_pb::EntitySpecifics& updated_specifics); |
| 154 |
| 155 // Use this callback to generate response types for entities. They will still |
| 156 // be "committed" and stored as normal, this only affects the response type |
| 157 // the client sees. This allows tests to still inspect what the client has |
| 158 // done, although not as useful of a mechanism for multi client tests. Care |
| 159 // should be taken when failing responses, as the client will go into |
| 160 // exponential backoff, which can cause tests to be slow or time out. |
| 161 // This method is only used in tests. |
| 162 void OverrideResponseType(ResponseTypeProvider response_type_override); |
| 163 |
105 // Serializes the server state to |proto|. | 164 // Serializes the server state to |proto|. |
106 void SerializeState(sync_pb::LoopbackServerProto* proto) const; | 165 void SerializeState(sync_pb::LoopbackServerProto* proto) const; |
107 | 166 |
108 // Populates the server state from |proto|. Returns true iff successful. | 167 // Populates the server state from |proto|. Returns true iff successful. |
109 bool DeSerializeState(const sync_pb::LoopbackServerProto& proto); | 168 bool DeSerializeState(const sync_pb::LoopbackServerProto& proto); |
110 | 169 |
111 // Saves all entities and server state to a protobuf file in |filename|. | 170 // Saves all entities and server state to a protobuf file in |filename|. |
112 bool SaveStateToFile(const base::FilePath& filename) const; | 171 bool SaveStateToFile(const base::FilePath& filename) const; |
113 | 172 |
114 // Loads all entities and server state from a protobuf file in |filename|. | 173 // Loads all entities and server state from a protobuf file in |filename|. |
115 bool LoadStateFromFile(const base::FilePath& filename); | 174 bool LoadStateFromFile(const base::FilePath& filename); |
116 | 175 |
| 176 void set_observer_for_tests(ObserverForTests* observer) { |
| 177 observer_for_tests_ = observer; |
| 178 } |
| 179 |
117 // This is the last version number assigned to an entity. The next entity will | 180 // This is the last version number assigned to an entity. The next entity will |
118 // have a version number of version_ + 1. | 181 // have a version number of version_ + 1. |
119 int64_t version_; | 182 int64_t version_; |
120 | 183 |
121 int64_t store_birthday_; | 184 int64_t store_birthday_; |
122 | 185 |
123 EntityMap entities_; | 186 EntityMap entities_; |
124 std::vector<std::string> keystore_keys_; | 187 std::vector<std::string> keystore_keys_; |
125 | 188 |
126 // The file used to store the local sync data. | 189 // The file used to store the local sync data. |
127 base::FilePath persistent_file_; | 190 base::FilePath persistent_file_; |
128 | 191 |
129 // Used to verify that LoopbackServer is only used from one thread. | 192 // Used to verify that LoopbackServer is only used from one thread. |
130 base::ThreadChecker thread_checker_; | 193 base::ThreadChecker thread_checker_; |
| 194 |
| 195 // Used to observe the completion of commit messages for the sake of testing. |
| 196 ObserverForTests* observer_for_tests_; |
| 197 |
| 198 // Response type override callback used in tests. |
| 199 ResponseTypeProvider response_type_override_; |
131 }; | 200 }; |
132 | 201 |
133 } // namespace syncer | 202 } // namespace syncer |
134 | 203 |
135 #endif // COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ | 204 #endif // COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ |
OLD | NEW |