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 |
46 // Gets LoopbackServer ready for syncing. | 63 // Gets LoopbackServer ready for syncing. |
47 void Init(); | 64 void Init(); |
48 | 65 |
49 // Processes a GetUpdates call. | 66 // Processes a GetUpdates call. |
50 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, | 67 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, |
51 sync_pb::GetUpdatesResponse* response); | 68 sync_pb::GetUpdatesResponse* response); |
52 | 69 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 // |id|. A tombstone is not created for the entity itself. | 112 // |id|. A tombstone is not created for the entity itself. |
96 void DeleteChildren(const std::string& id); | 113 void DeleteChildren(const std::string& id); |
97 | 114 |
98 // Updates the |entity| to a new version and increments the version counter | 115 // Updates the |entity| to a new version and increments the version counter |
99 // that the server uses to assign versions. | 116 // that the server uses to assign versions. |
100 void UpdateEntityVersion(LoopbackServerEntity* entity); | 117 void UpdateEntityVersion(LoopbackServerEntity* entity); |
101 | 118 |
102 // Returns the store birthday. | 119 // Returns the store birthday. |
103 std::string GetStoreBirthday() const; | 120 std::string GetStoreBirthday() const; |
104 | 121 |
| 122 // Returns all entities stored by the server of the given |model_type|. |
| 123 // This method returns SyncEntity protocol buffer objects (instead of |
| 124 // FakeServerEntity) so that callers can inspect datatype-specific data |
| 125 // (e.g., the URL of a session tab). |
| 126 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType( |
| 127 syncer::ModelType model_type); |
| 128 |
| 129 // Creates a DicionaryValue representation of all entities present in the |
| 130 // server. The dictionary keys are the strings generated by ModelTypeToString |
| 131 // and the values are ListValues containing StringValue versions of entity |
| 132 // names. Used by test to verify the contents of the server state. |
| 133 std::unique_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue(); |
| 134 |
| 135 // Modifies the entity on the server with the given |id|. The entity's |
| 136 // EntitySpecifics are replaced with |updated_specifics| and its version is |
| 137 // updated. If the given |id| does not exist or the ModelType of |
| 138 // |updated_specifics| does not match the entity, false is returned. |
| 139 // Otherwise, true is returned to represent a successful modification. |
| 140 // |
| 141 // This method sometimes updates entity data beyond EntitySpecifics. For |
| 142 // example, in the case of a bookmark, changing the BookmarkSpecifics title |
| 143 // field will modify the top-level entity's name field. |
| 144 // This method is only used in tests. |
| 145 bool ModifyEntitySpecifics(const std::string& id, |
| 146 const sync_pb::EntitySpecifics& updated_specifics); |
| 147 |
105 // Serializes the server state to |proto|. | 148 // Serializes the server state to |proto|. |
106 void SerializeState(sync_pb::LoopbackServerProto* proto) const; | 149 void SerializeState(sync_pb::LoopbackServerProto* proto) const; |
107 | 150 |
108 // Populates the server state from |proto|. Returns true iff successful. | 151 // Populates the server state from |proto|. Returns true iff successful. |
109 bool DeSerializeState(const sync_pb::LoopbackServerProto& proto); | 152 bool DeSerializeState(const sync_pb::LoopbackServerProto& proto); |
110 | 153 |
111 // Saves all entities and server state to a protobuf file in |filename|. | 154 // Saves all entities and server state to a protobuf file in |filename|. |
112 bool SaveStateToFile(const base::FilePath& filename) const; | 155 bool SaveStateToFile(const base::FilePath& filename) const; |
113 | 156 |
114 // Loads all entities and server state from a protobuf file in |filename|. | 157 // Loads all entities and server state from a protobuf file in |filename|. |
115 bool LoadStateFromFile(const base::FilePath& filename); | 158 bool LoadStateFromFile(const base::FilePath& filename); |
116 | 159 |
| 160 void set_observer_for_tests(ObserverForTests* observer) { |
| 161 observer_for_tests_ = observer; |
| 162 } |
| 163 |
117 // This is the last version number assigned to an entity. The next entity will | 164 // This is the last version number assigned to an entity. The next entity will |
118 // have a version number of version_ + 1. | 165 // have a version number of version_ + 1. |
119 int64_t version_; | 166 int64_t version_; |
120 | 167 |
121 int64_t store_birthday_; | 168 int64_t store_birthday_; |
122 | 169 |
123 EntityMap entities_; | 170 EntityMap entities_; |
124 std::vector<std::string> keystore_keys_; | 171 std::vector<std::string> keystore_keys_; |
125 | 172 |
126 // The file used to store the local sync data. | 173 // The file used to store the local sync data. |
127 base::FilePath persistent_file_; | 174 base::FilePath persistent_file_; |
128 | 175 |
129 // Used to verify that LoopbackServer is only used from one thread. | 176 // Used to verify that LoopbackServer is only used from one thread. |
130 base::ThreadChecker thread_checker_; | 177 base::ThreadChecker thread_checker_; |
| 178 |
| 179 // Used to observe the completion of commit messages for the sake of testing. |
| 180 ObserverForTests* observer_for_tests_; |
131 }; | 181 }; |
132 | 182 |
133 } // namespace syncer | 183 } // namespace syncer |
134 | 184 |
135 #endif // COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ | 185 #endif // COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ |
OLD | NEW |