| 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 "base/values.h" | 16 #include "base/values.h" |
| 15 #include "sync/internal_api/public/base/model_type.h" | 17 #include "sync/internal_api/public/base/model_type.h" |
| 16 #include "sync/protocol/sync.pb.h" | 18 #include "sync/protocol/sync.pb.h" |
| 17 #include "sync/test/fake_server/fake_server_entity.h" | 19 #include "sync/test/fake_server/fake_server_entity.h" |
| 18 | 20 |
| 19 namespace fake_server { | 21 namespace fake_server { |
| 20 | 22 |
| 21 // A fake version of the Sync server used for testing. | 23 // A fake version of the Sync server used for testing. |
| 22 class FakeServer { | 24 class FakeServer { |
| 23 public: | 25 public: |
| 26 typedef base::Callback<void(int, int, const std::string&)> |
| 27 HandleCommandCallback; |
| 28 |
| 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 |
| 24 FakeServer(); | 38 FakeServer(); |
| 25 virtual ~FakeServer(); | 39 virtual ~FakeServer(); |
| 26 | 40 |
| 27 // Handles a /command POST to the server. If the return value is 0 (success), | 41 // Asynchronously handles a /command POST to the server. If the error_code is |
| 28 // |response_code| and |response| will be set. Otherwise, the return value | 42 // passed to |callback| is 0 (success), the POST's response code and content |
| 29 // will be a network error code. | 43 // will also be passed. |
| 30 int HandleCommand(const std::string& request, | 44 void HandleCommand(const std::string& request, |
| 31 int* response_code, | 45 const HandleCommandCallback& callback); |
| 32 std::string* response); | |
| 33 | 46 |
| 34 // Creates a DicionaryValue representation of all entities present in the | 47 // Creates a DicionaryValue representation of all entities present in the |
| 35 // server. The dictionary keys are the strings generated by ModelTypeToString | 48 // server. The dictionary keys are the strings generated by ModelTypeToString |
| 36 // and the values are ListValues containing StringValue versions of entity | 49 // and the values are ListValues containing StringValue versions of entity |
| 37 // names. | 50 // names. |
| 38 scoped_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue(); | 51 scoped_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue(); |
| 39 | 52 |
| 53 // Adds |observer| to FakeServer's observer list. This should be called |
| 54 // before the Profile associated with |observer| is connected to the server. |
| 55 void AddObserver(Observer* observer); |
| 56 |
| 57 // Removes |observer| from the FakeServer's observer list. This method |
| 58 // must be called if AddObserver was ever called with |observer|. |
| 59 void RemoveObserver(Observer* observer); |
| 60 |
| 40 private: | 61 private: |
| 41 typedef std::map<std::string, FakeServerEntity*> EntityMap; | 62 typedef std::map<std::string, FakeServerEntity*> EntityMap; |
| 42 | 63 |
| 43 // Processes a GetUpdates call. | 64 // Processes a GetUpdates call. |
| 44 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, | 65 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, |
| 45 sync_pb::GetUpdatesResponse* response); | 66 sync_pb::GetUpdatesResponse* response); |
| 46 | 67 |
| 47 // Processes a Commit call. | 68 // Processes a Commit call. |
| 48 bool HandleCommitRequest(const sync_pb::CommitMessage& commit, | 69 bool HandleCommitRequest(const sync_pb::CommitMessage& commit, |
| 49 sync_pb::CommitResponse* response); | 70 sync_pb::CommitResponse* response); |
| 50 | 71 |
| 51 // Inserts the appropriate permanent items in |entities_|. | 72 // Inserts the appropriate permanent items in |entities_|. |
| 52 bool CreateDefaultPermanentItems( | 73 bool CreateDefaultPermanentItems( |
| 53 const std::vector<syncer::ModelType>& first_time_types); | 74 const std::vector<syncer::ModelType>& first_time_types); |
| 54 | 75 |
| 55 // Inserts the mobile bookmarks folder entity in |entities_|. | 76 // Inserts the mobile bookmarks folder entity in |entities_|. |
| 56 bool CreateMobileBookmarksPermanentItem(); | 77 bool CreateMobileBookmarksPermanentItem(); |
| 57 | 78 |
| 58 // Saves a |entity| to |entities_|. | 79 // Saves a |entity| to |entities_|. |
| 59 void SaveEntity(FakeServerEntity* entity); | 80 void SaveEntity(FakeServerEntity* entity); |
| 60 | 81 |
| 61 // Commits a client-side SyncEntity to the server as a FakeServerEntity. | 82 // Commits a client-side SyncEntity to the server as a FakeServerEntity. |
| 62 // The client that sent the commit is identified via |client_guid| and all | 83 // The client that sent the commit is identified via |client_guid|. The |
| 63 // entity ID renaming is tracked with |client_to_server_ids|. If the commit | 84 // parent ID string present in |client_entity| should be ignored in favor |
| 64 // is successful, true is returned and the server's version of the SyncEntity | 85 // of |parent_id|. If the commit is successful, the entity's server ID string |
| 65 // is stored in |server_entity|. | 86 // is returned and a new FakeServerEntity is saved in |entities_|. |
| 66 bool CommitEntity(const sync_pb::SyncEntity& client_entity, | 87 std::string CommitEntity( |
| 67 sync_pb::CommitResponse_EntryResponse* entry_response, | 88 const sync_pb::SyncEntity& client_entity, |
| 68 std::string client_guid, | 89 sync_pb::CommitResponse_EntryResponse* entry_response, |
| 69 std::map<std::string, std::string>* client_to_server_ids); | 90 std::string client_guid, |
| 91 std::string parent_id); |
| 70 | 92 |
| 71 // Populates |entry_response| based on |entity|. It is assumed that | 93 // Populates |entry_response| based on |entity|. It is assumed that |
| 72 // SaveEntity has already been called on |entity|. | 94 // SaveEntity has already been called on |entity|. |
| 73 void BuildEntryResponseForSuccessfulCommit( | 95 void BuildEntryResponseForSuccessfulCommit( |
| 74 sync_pb::CommitResponse_EntryResponse* entry_response, | 96 sync_pb::CommitResponse_EntryResponse* entry_response, |
| 75 FakeServerEntity* entity); | 97 FakeServerEntity* entity); |
| 76 | 98 |
| 77 // Determines whether the SyncEntity with id_string |id| is a child of an | 99 // Determines whether the SyncEntity with id_string |id| is a child of an |
| 78 // entity with id_string |potential_parent_id|. | 100 // entity with id_string |potential_parent_id|. |
| 79 bool IsChild(const std::string& id, const std::string& potential_parent_id); | 101 bool IsChild(const std::string& id, const std::string& potential_parent_id); |
| 80 | 102 |
| 81 // Creates and saves tombstones for all children of the entity with the given | 103 // Creates and saves tombstones for all children of the entity with the given |
| 82 // |id|. A tombstone is not created for the entity itself. | 104 // |id|. A tombstone is not created for the entity itself. |
| 83 bool DeleteChildren(const std::string& id); | 105 bool DeleteChildren(const std::string& id); |
| 84 | 106 |
| 85 // The lock used to ensure that only one client is communicating with server | |
| 86 // at any given time. | |
| 87 // | |
| 88 // It is probably preferable to have FakeServer operate on its own thread and | |
| 89 // communicate with it via PostTask, but clients would still need to wait for | |
| 90 // requests to finish before proceeding. | |
| 91 base::Lock lock_; | |
| 92 | |
| 93 // This is the last version number assigned to an entity. The next entity will | 107 // This is the last version number assigned to an entity. The next entity will |
| 94 // have a version number of version_ + 1. | 108 // have a version number of version_ + 1. |
| 95 int64 version_; | 109 int64 version_; |
| 96 | 110 |
| 97 // The current birthday value. | 111 // The current birthday value. |
| 98 std::string birthday_; | 112 std::string birthday_; |
| 99 | 113 |
| 100 // All SyncEntity objects saved by the server. The key value is the entity's | 114 // All SyncEntity objects saved by the server. The key value is the entity's |
| 101 // id string. | 115 // id string. |
| 102 EntityMap entities_; | 116 EntityMap entities_; |
| 103 | 117 |
| 104 // All Keystore keys known to the server. | 118 // All Keystore keys known to the server. |
| 105 std::vector<std::string> keystore_keys_; | 119 std::vector<std::string> keystore_keys_; |
| 106 | 120 |
| 107 // All ModelTypes for which permanent entities have been created. These types | 121 // All ModelTypes for which permanent entities have been created. These types |
| 108 // are kept track of so that permanent entities are not recreated for new | 122 // are kept track of so that permanent entities are not recreated for new |
| 109 // clients. | 123 // clients. |
| 110 syncer::ModelTypeSet created_permanent_entity_types_; | 124 syncer::ModelTypeSet created_permanent_entity_types_; |
| 125 |
| 126 // FakeServer's observers. |
| 127 ObserverList<Observer, true> observers_; |
| 111 }; | 128 }; |
| 112 | 129 |
| 113 } // namespace fake_server | 130 } // namespace fake_server |
| 114 | 131 |
| 115 #endif // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ | 132 #endif // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ |
| OLD | NEW |