Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: components/sync/engine_impl/loopback_server/loopback_server.h

Issue 2360703002: [Sync] Implements the loopback sync server. (Closed)
Patch Set: Address comment. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_
6 #define COMPONENTS_SYNC_ENGINE_IMPL_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/files/file_path.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/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 local profile serialization.
28 class LoopbackServer {
29 public:
30 LoopbackServer(const base::FilePath& persistent_file);
31 virtual ~LoopbackServer();
32
33 // Handles a /command POST (with the given |request|) to the server. Three
34 // output arguments, |server_status|, |response_code|, and |response|, are
35 // used to pass data back to the caller. The command has failed if the value
36 // pointed to by |error_code| is nonzero.
37 void HandleCommand(const std::string& request,
38 HttpResponse::ServerConnectionCode* server_status,
39 int64_t* response_code,
40 std::string* response);
41
42 private:
43 using EntityMap =
44 std::map<std::string, std::unique_ptr<LoopbackServerEntity>>;
45
46 // Gets LoopbackServer ready for syncing.
47 void Init();
48
49 // Processes a GetUpdates call.
50 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates,
51 sync_pb::GetUpdatesResponse* response);
52
53 // Processes a Commit call.
54 bool HandleCommitRequest(const sync_pb::CommitMessage& message,
55 const std::string& invalidator_client_id,
56 sync_pb::CommitResponse* response);
57
58 void ClearServerData();
59
60 // Creates and saves a permanent folder for Bookmarks (e.g., Bookmark Bar).
61 bool CreatePermanentBookmarkFolder(const std::string& server_tag,
62 const std::string& name);
63
64 // Inserts the default permanent items in |entities_|.
65 bool CreateDefaultPermanentItems();
66
67 std::string GenerateNewKeystoreKey() const;
68
69 // Saves a |entity| to |entities_|.
70 void SaveEntity(std::unique_ptr<LoopbackServerEntity> entity);
71
72 // Commits a client-side SyncEntity to the server as a LoopbackServerEntity.
73 // The client that sent the commit is identified via |client_guid|. The
74 // parent ID string present in |client_entity| should be ignored in favor
75 // of |parent_id|. If the commit is successful, the entity's server ID string
76 // is returned and a new LoopbackServerEntity is saved in |entities_|.
77 std::string CommitEntity(
78 const sync_pb::SyncEntity& client_entity,
79 sync_pb::CommitResponse_EntryResponse* entry_response,
80 const std::string& client_guid,
81 const std::string& parent_id);
82
83 // Populates |entry_response| based on the stored entity identified by
84 // |entity_id|. It is assumed that the entity identified by |entity_id| has
85 // already been stored using SaveEntity.
86 void BuildEntryResponseForSuccessfulCommit(
87 const std::string& entity_id,
88 sync_pb::CommitResponse_EntryResponse* entry_response);
89
90 // Determines whether the SyncEntity with id_string |id| is a child of an
91 // entity with id_string |potential_parent_id|.
92 bool IsChild(const std::string& id, const std::string& potential_parent_id);
93
94 // Creates and saves tombstones for all children of the entity with the given
95 // |id|. A tombstone is not created for the entity itself.
96 void DeleteChildren(const std::string& id);
97
98 // Updates the |entity| to a new version and increments the version counter
99 // that the server uses to assign versions.
100 void UpdateEntityVersion(LoopbackServerEntity* entity);
101
102 // Returns the store birthday.
103 std::string GetStoreBirthday() const;
104
105 // Serializes the server state to |proto|.
106 void SerializeState(sync_pb::LoopbackServerProto* proto) const;
107
108 // Populates the server state from |proto|. Returns true iff successful.
109 bool DeSerializeState(const sync_pb::LoopbackServerProto& proto);
110
111 // Saves all entities and server state to a protobuf file in |filename|.
112 bool SaveStateToFile(const base::FilePath& filename) const;
113
114 // Loads all entities and server state from a protobuf file in |filename|.
115 bool LoadStateFromFile(const base::FilePath& filename);
116
117 // This is the last version number assigned to an entity. The next entity will
118 // have a version number of version_ + 1.
119 int64_t version_;
120
121 int64_t store_birthday_;
122
123 EntityMap entities_;
124 std::vector<std::string> keystore_keys_;
125
126 // The file used to store the local sync data.
127 base::FilePath persistent_file_;
128
129 // Used to verify that LoopbackServer is only used from one thread.
130 base::ThreadChecker thread_checker_;
131 };
132
133 } // namespace syncer
134
135 #endif // COMPONENTS_SYNC_ENGINE_IMPL_LOOPBACK_SERVER_LOOPBACK_SERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698