| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_SYNC_ENGINE_IMPL_NET_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ |
| 6 #define COMPONENTS_SYNC_ENGINE_IMPL_NET_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <map> |
| 11 #include <memory> |
| 12 #include <string> |
| 13 #include <vector> |
| 14 |
| 15 #include "base/callback.h" |
| 16 #include "base/observer_list.h" |
| 17 #include "base/threading/thread_checker.h" |
| 18 #include "base/values.h" |
| 19 #include "components/sync/base/model_type.h" |
| 20 #include "components/sync/engine_impl/net/loopback_server/loopback_server_entity
.h" |
| 21 #include "components/sync/engine_impl/net/server_connection_manager.h" |
| 22 #include "components/sync/protocol/loopback_server.pb.h" |
| 23 #include "components/sync/protocol/sync.pb.h" |
| 24 |
| 25 namespace syncer { |
| 26 |
| 27 // A loopback version of the Sync server used for testing. This class is not |
| 28 // thread safe. |
| 29 class LoopbackServer { |
| 30 public: |
| 31 class Observer { |
| 32 public: |
| 33 virtual ~Observer() {} |
| 34 |
| 35 // Called after LoopbackServer has processed a successful commit. The types |
| 36 // updated as part of the commit are passed in |committed_model_types|. |
| 37 virtual void OnCommit( |
| 38 const std::string& committer_id, |
| 39 syncer::ModelTypeSet committed_model_types) = 0; |
| 40 }; |
| 41 |
| 42 LoopbackServer(); |
| 43 virtual ~LoopbackServer(); |
| 44 |
| 45 // Handles a /command POST (with the given |request|) to the server. Three |
| 46 // output arguments, |server_status|, |response_code|, and |response|, are |
| 47 // used to pass data back to the caller. The command has failed if the value |
| 48 // pointed to by |error_code| is nonzero. |
| 49 void HandleCommand(const std::string& request, |
| 50 HttpResponse::ServerConnectionCode* server_status, |
| 51 int64_t* response_code, |
| 52 std::string* response); |
| 53 |
| 54 // Returns all entities stored by the server of the given |model_type|. |
| 55 // This method returns SyncEntity protocol buffer objects (instead of |
| 56 // LoopbackServerEntity) so that callers can inspect datatype-specific data |
| 57 // (e.g., the URL of a session tab). |
| 58 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType( |
| 59 syncer::ModelType model_type); |
| 60 |
| 61 // Clears server data simulating a "dashboard stop and clear" and sets a new |
| 62 // store birthday. |
| 63 void ClearServerData(); |
| 64 |
| 65 // Adds |observer| to LoopbackServer's observer list. This should be called |
| 66 // before the Profile associated with |observer| is connected to the server. |
| 67 void AddObserver(Observer* observer); |
| 68 |
| 69 // Removes |observer| from the LoopbackServer's observer list. This method |
| 70 // must be called if AddObserver was ever called with |observer|. |
| 71 void RemoveObserver(Observer* observer); |
| 72 |
| 73 // Returns the current LoopbackServer as a WeakPtr. |
| 74 base::WeakPtr<LoopbackServer> AsWeakPtr(); |
| 75 |
| 76 private: |
| 77 using EntityMap = |
| 78 std::map<std::string, std::unique_ptr<LoopbackServerEntity>>; |
| 79 |
| 80 // Gets LoopbackServer ready for syncing. |
| 81 void Init(); |
| 82 |
| 83 // Processes a GetUpdates call. |
| 84 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, |
| 85 sync_pb::GetUpdatesResponse* response); |
| 86 |
| 87 // Processes a Commit call. |
| 88 bool HandleCommitRequest(const sync_pb::CommitMessage& message, |
| 89 const std::string& invalidator_client_id, |
| 90 sync_pb::CommitResponse* response); |
| 91 |
| 92 // Creates and saves a permanent folder for Bookmarks (e.g., Bookmark Bar). |
| 93 bool CreatePermanentBookmarkFolder(const std::string& server_tag, |
| 94 const std::string& name); |
| 95 |
| 96 // Inserts the default permanent items in |entities_|. |
| 97 bool CreateDefaultPermanentItems(); |
| 98 |
| 99 // Saves a |entity| to |entities_|. |
| 100 void SaveEntity(std::unique_ptr<LoopbackServerEntity> entity); |
| 101 |
| 102 // Commits a client-side SyncEntity to the server as a LoopbackServerEntity. |
| 103 // The client that sent the commit is identified via |client_guid|. The |
| 104 // parent ID string present in |client_entity| should be ignored in favor |
| 105 // of |parent_id|. If the commit is successful, the entity's server ID string |
| 106 // is returned and a new LoopbackServerEntity is saved in |entities_|. |
| 107 std::string CommitEntity( |
| 108 const sync_pb::SyncEntity& client_entity, |
| 109 sync_pb::CommitResponse_EntryResponse* entry_response, |
| 110 const std::string& client_guid, |
| 111 const std::string& parent_id); |
| 112 |
| 113 // Populates |entry_response| based on the stored entity identified by |
| 114 // |entity_id|. It is assumed that the entity identified by |entity_id| has |
| 115 // already been stored using SaveEntity. |
| 116 void BuildEntryResponseForSuccessfulCommit( |
| 117 const std::string& entity_id, |
| 118 sync_pb::CommitResponse_EntryResponse* entry_response); |
| 119 |
| 120 // Determines whether the SyncEntity with id_string |id| is a child of an |
| 121 // entity with id_string |potential_parent_id|. |
| 122 bool IsChild(const std::string& id, const std::string& potential_parent_id); |
| 123 |
| 124 // Creates and saves tombstones for all children of the entity with the given |
| 125 // |id|. A tombstone is not created for the entity itself. |
| 126 void DeleteChildren(const std::string& id); |
| 127 |
| 128 // Updates the |entity| to a new version and increments the version counter |
| 129 // that the server uses to assign versions. |
| 130 void UpdateEntityVersion(LoopbackServerEntity* entity); |
| 131 |
| 132 // Returns the store birthday. |
| 133 std::string GetStoreBirthday() const; |
| 134 |
| 135 // Serializes the server state to |proto|. |
| 136 void SerializeState(sync_pb::LoopbackServerProto* proto) const; |
| 137 |
| 138 // Populates the server state from |proto|. Returns true iff successful. |
| 139 bool DeSerializeState(const sync_pb::LoopbackServerProto& proto); |
| 140 |
| 141 // This is the last version number assigned to an entity. The next entity will |
| 142 // have a version number of version_ + 1. |
| 143 int64_t version_; |
| 144 |
| 145 // The current store birthday value. |
| 146 int64_t store_birthday_; |
| 147 |
| 148 // All SyncEntity objects saved by the server. The key value is the entity's |
| 149 // id string. |
| 150 EntityMap entities_; |
| 151 |
| 152 // All Keystore keys known to the server. |
| 153 std::vector<std::string> keystore_keys_; |
| 154 |
| 155 // Used as the error_code field of ClientToServerResponse on all responses |
| 156 // except when |triggered_actionable_error_| is set. |
| 157 sync_pb::SyncEnums::ErrorType error_type_; |
| 158 |
| 159 // LoopbackServer's observers. |
| 160 base::ObserverList<Observer, true> observers_; |
| 161 |
| 162 // Used to verify that LoopbackServer is only used from one thread. |
| 163 base::ThreadChecker thread_checker_; |
| 164 |
| 165 // Creates WeakPtr versions of the current LoopbackServer. This must be the |
| 166 // last data member! |
| 167 base::WeakPtrFactory<LoopbackServer> weak_ptr_factory_; |
| 168 }; |
| 169 |
| 170 } // namespace syncer |
| 171 |
| 172 #endif // COMPONENTS_SYNC_ENGINE_IMPL_NET_LOOPBACK_SERVER_LOOPBACK_SERVER_H_ |
| OLD | NEW |