| 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 | |
| 29 namespace syncer { | 25 namespace syncer { |
| 30 | 26 |
| 31 // A loopback version of the Sync server used for local profile serialization. | 27 // A loopback version of the Sync server used for local profile serialization. |
| 32 class LoopbackServer { | 28 class LoopbackServer { |
| 33 public: | 29 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 | |
| 44 explicit LoopbackServer(const base::FilePath& persistent_file); | 30 explicit LoopbackServer(const base::FilePath& persistent_file); |
| 45 virtual ~LoopbackServer(); | 31 virtual ~LoopbackServer(); |
| 46 | 32 |
| 47 // Handles a /command POST (with the given |request|) to the server. Three | 33 // Handles a /command POST (with the given |request|) to the server. Three |
| 48 // output arguments, |server_status|, |response_code|, and |response|, are | 34 // output arguments, |server_status|, |response_code|, and |response|, are |
| 49 // used to pass data back to the caller. The command has failed if the value | 35 // used to pass data back to the caller. The command has failed if the value |
| 50 // pointed to by |error_code| is nonzero. | 36 // pointed to by |error_code| is nonzero. |
| 51 void HandleCommand(const std::string& request, | 37 void HandleCommand(const std::string& request, |
| 52 HttpResponse::ServerConnectionCode* server_status, | 38 HttpResponse::ServerConnectionCode* server_status, |
| 53 int64_t* response_code, | 39 int64_t* response_code, |
| 54 std::string* response); | 40 std::string* response); |
| 55 | 41 |
| 56 private: | 42 private: |
| 57 // Allow the FakeServer decorator to inspect the internals of this class. | |
| 58 friend class fake_server::FakeServer; | |
| 59 | |
| 60 using EntityMap = | 43 using EntityMap = |
| 61 std::map<std::string, std::unique_ptr<LoopbackServerEntity>>; | 44 std::map<std::string, std::unique_ptr<LoopbackServerEntity>>; |
| 62 | 45 |
| 63 // Gets LoopbackServer ready for syncing. | 46 // Gets LoopbackServer ready for syncing. |
| 64 void Init(); | 47 void Init(); |
| 65 | 48 |
| 66 // Processes a GetUpdates call. | 49 // Processes a GetUpdates call. |
| 67 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, | 50 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, |
| 68 sync_pb::GetUpdatesResponse* response); | 51 sync_pb::GetUpdatesResponse* response); |
| 69 | 52 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 // |id|. A tombstone is not created for the entity itself. | 95 // |id|. A tombstone is not created for the entity itself. |
| 113 void DeleteChildren(const std::string& id); | 96 void DeleteChildren(const std::string& id); |
| 114 | 97 |
| 115 // Updates the |entity| to a new version and increments the version counter | 98 // Updates the |entity| to a new version and increments the version counter |
| 116 // that the server uses to assign versions. | 99 // that the server uses to assign versions. |
| 117 void UpdateEntityVersion(LoopbackServerEntity* entity); | 100 void UpdateEntityVersion(LoopbackServerEntity* entity); |
| 118 | 101 |
| 119 // Returns the store birthday. | 102 // Returns the store birthday. |
| 120 std::string GetStoreBirthday() const; | 103 std::string GetStoreBirthday() const; |
| 121 | 104 |
| 122 // Returns all entities stored by the server of the given |model_type|. | |
| 123 // This method is only used in tests. | |
| 124 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType( | |
| 125 syncer::ModelType model_type); | |
| 126 | |
| 127 // Creates a DicionaryValue representation of all entities present in the | |
| 128 // server. The dictionary keys are the strings generated by ModelTypeToString | |
| 129 // and the values are ListValues containing StringValue versions of entity | |
| 130 // names. Used by test to verify the contents of the server state. | |
| 131 std::unique_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue(); | |
| 132 | |
| 133 // Modifies the entity on the server with the given |id|. The entity's | |
| 134 // EntitySpecifics are replaced with |updated_specifics| and its version is | |
| 135 // updated to n+1. If the given |id| does not exist or the ModelType of | |
| 136 // |updated_specifics| does not match the entity, false is returned. | |
| 137 // Otherwise, true is returned to represent a successful modification. | |
| 138 // | |
| 139 // This method sometimes updates entity data beyond EntitySpecifics. For | |
| 140 // example, in the case of a bookmark, changing the BookmarkSpecifics title | |
| 141 // field will modify the top-level entity's name field. | |
| 142 // This method is only used in tests. | |
| 143 bool ModifyEntitySpecifics(const std::string& id, | |
| 144 const sync_pb::EntitySpecifics& updated_specifics); | |
| 145 | |
| 146 // This method is only used in tests. | |
| 147 bool ModifyBookmarkEntity(const std::string& id, | |
| 148 const std::string& parent_id, | |
| 149 const sync_pb::EntitySpecifics& updated_specifics); | |
| 150 | |
| 151 // Serializes the server state to |proto|. | 105 // Serializes the server state to |proto|. |
| 152 void SerializeState(sync_pb::LoopbackServerProto* proto) const; | 106 void SerializeState(sync_pb::LoopbackServerProto* proto) const; |
| 153 | 107 |
| 154 // Populates the server state from |proto|. Returns true iff successful. | 108 // Populates the server state from |proto|. Returns true iff successful. |
| 155 bool DeSerializeState(const sync_pb::LoopbackServerProto& proto); | 109 bool DeSerializeState(const sync_pb::LoopbackServerProto& proto); |
| 156 | 110 |
| 157 // Saves all entities and server state to a protobuf file in |filename|. | 111 // Saves all entities and server state to a protobuf file in |filename|. |
| 158 bool SaveStateToFile(const base::FilePath& filename) const; | 112 bool SaveStateToFile(const base::FilePath& filename) const; |
| 159 | 113 |
| 160 // Loads all entities and server state from a protobuf file in |filename|. | 114 // Loads all entities and server state from a protobuf file in |filename|. |
| 161 bool LoadStateFromFile(const base::FilePath& filename); | 115 bool LoadStateFromFile(const base::FilePath& filename); |
| 162 | 116 |
| 163 void set_observer_for_tests(ObserverForTests* observer) { | |
| 164 observer_for_tests_ = observer; | |
| 165 } | |
| 166 | |
| 167 // This is the last version number assigned to an entity. The next entity will | 117 // This is the last version number assigned to an entity. The next entity will |
| 168 // have a version number of version_ + 1. | 118 // have a version number of version_ + 1. |
| 169 int64_t version_; | 119 int64_t version_; |
| 170 | 120 |
| 171 int64_t store_birthday_; | 121 int64_t store_birthday_; |
| 172 | 122 |
| 173 EntityMap entities_; | 123 EntityMap entities_; |
| 174 std::vector<std::string> keystore_keys_; | 124 std::vector<std::string> keystore_keys_; |
| 175 | 125 |
| 176 // The file used to store the local sync data. | 126 // The file used to store the local sync data. |
| 177 base::FilePath persistent_file_; | 127 base::FilePath persistent_file_; |
| 178 | 128 |
| 179 // Used to verify that LoopbackServer is only used from one thread. | 129 // Used to verify that LoopbackServer is only used from one thread. |
| 180 base::ThreadChecker thread_checker_; | 130 base::ThreadChecker thread_checker_; |
| 181 | |
| 182 // Used to observe the completion of commit messages for the sake of testing. | |
| 183 ObserverForTests* observer_for_tests_; | |
| 184 }; | 131 }; |
| 185 | 132 |
| 186 } // namespace syncer | 133 } // namespace syncer |
| 187 | 134 |
| 188 #endif // COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ | 135 #endif // COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ |
| OLD | NEW |