Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / sync / test / fake_server / fake_server.h
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 SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
6 #define SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
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"
20
21 namespace fake_server {
22
23 // A fake version of the Sync server used for testing.
24 class FakeServer {
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
38   FakeServer();
39   virtual ~FakeServer();
40
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);
46
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
50   // names.
51   scoped_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue();
52
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);
57
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);
61
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);
65
66  private:
67   typedef std::map<std::string, FakeServerEntity*> EntityMap;
68
69   // Processes a GetUpdates call.
70   bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates,
71                                sync_pb::GetUpdatesResponse* response);
72
73   // Processes a Commit call.
74   bool HandleCommitRequest(const sync_pb::CommitMessage& commit,
75                            sync_pb::CommitResponse* response);
76
77   // Inserts the default permanent items in |entities_|.
78   bool CreateDefaultPermanentItems();
79
80   // Inserts the mobile bookmarks folder entity in |entities_|.
81   bool CreateMobileBookmarksPermanentItem();
82
83   // Saves a |entity| to |entities_|.
84   void SaveEntity(FakeServerEntity* entity);
85
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);
96
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);
102
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);
106
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);
110
111   // This is the last version number assigned to an entity. The next entity will
112   // have a version number of version_ + 1.
113   int64 version_;
114
115   // The current birthday value.
116   std::string birthday_;
117
118   // All SyncEntity objects saved by the server. The key value is the entity's
119   // id string.
120   EntityMap entities_;
121
122   // All Keystore keys known to the server.
123   std::vector<std::string> keystore_keys_;
124
125   // FakeServer's observers.
126   ObserverList<Observer, true> observers_;
127 };
128
129 }  // namespace fake_server
130
131 #endif  // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_