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.
5 #ifndef SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
6 #define SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/values.h"
17 #include "sync/internal_api/public/base/model_type.h"
18 #include "sync/protocol/sync.pb.h"
19 #include "sync/test/fake_server/fake_server_entity.h"
21 namespace fake_server {
23 // A fake version of the Sync server used for testing.
26 typedef base::Callback<void(int, int, const std::string&)>
27 HandleCommandCallback;
31 virtual ~Observer() {}
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;
39 virtual ~FakeServer();
41 // Asynchronously handles a /command POST to the server. If the error_code is
42 // passed to |callback| is 0 (success), the POST's response code and content
43 // will also be passed.
44 void HandleCommand(const std::string& request,
45 const HandleCommandCallback& callback);
47 // Creates a DicionaryValue representation of all entities present in the
48 // server. The dictionary keys are the strings generated by ModelTypeToString
49 // and the values are ListValues containing StringValue versions of entity
51 scoped_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue();
53 // Adds the FakeServerEntity* owned by |entity| to the server's collection
54 // of entities. This method makes no guarantees that the added entity will
55 // result in successful server operations.
56 void InjectEntity(scoped_ptr<FakeServerEntity> entity);
58 // Adds |observer| to FakeServer's observer list. This should be called
59 // before the Profile associated with |observer| is connected to the server.
60 void AddObserver(Observer* observer);
62 // Removes |observer| from the FakeServer's observer list. This method
63 // must be called if AddObserver was ever called with |observer|.
64 void RemoveObserver(Observer* observer);
67 typedef std::map<std::string, FakeServerEntity*> EntityMap;
69 // Processes a GetUpdates call.
70 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates,
71 sync_pb::GetUpdatesResponse* response);
73 // Processes a Commit call.
74 bool HandleCommitRequest(const sync_pb::CommitMessage& commit,
75 sync_pb::CommitResponse* response);
77 // Inserts the default permanent items in |entities_|.
78 bool CreateDefaultPermanentItems();
80 // Inserts the mobile bookmarks folder entity in |entities_|.
81 bool CreateMobileBookmarksPermanentItem();
83 // Saves a |entity| to |entities_|.
84 void SaveEntity(FakeServerEntity* entity);
86 // Commits a client-side SyncEntity to the server as a FakeServerEntity.
87 // The client that sent the commit is identified via |client_guid|. The
88 // parent ID string present in |client_entity| should be ignored in favor
89 // of |parent_id|. If the commit is successful, the entity's server ID string
90 // is returned and a new FakeServerEntity is saved in |entities_|.
91 std::string CommitEntity(
92 const sync_pb::SyncEntity& client_entity,
93 sync_pb::CommitResponse_EntryResponse* entry_response,
94 std::string client_guid,
95 std::string parent_id);
97 // Populates |entry_response| based on |entity|. It is assumed that
98 // SaveEntity has already been called on |entity|.
99 void BuildEntryResponseForSuccessfulCommit(
100 sync_pb::CommitResponse_EntryResponse* entry_response,
101 FakeServerEntity* entity);
103 // Determines whether the SyncEntity with id_string |id| is a child of an
104 // entity with id_string |potential_parent_id|.
105 bool IsChild(const std::string& id, const std::string& potential_parent_id);
107 // Creates and saves tombstones for all children of the entity with the given
108 // |id|. A tombstone is not created for the entity itself.
109 bool DeleteChildren(const std::string& id);
111 // This is the last version number assigned to an entity. The next entity will
112 // have a version number of version_ + 1.
115 // The current birthday value.
116 std::string birthday_;
118 // All SyncEntity objects saved by the server. The key value is the entity's
122 // All Keystore keys known to the server.
123 std::vector<std::string> keystore_keys_;
125 // FakeServer's observers.
126 ObserverList<Observer, true> observers_;
129 } // namespace fake_server
131 #endif // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_