OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 CHROME_BROWSER_SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ | |
6 #define CHROME_BROWSER_SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/gtest_prod_util.h" | |
12 #include "base/time.h" | |
13 #include "chrome/browser/sync/internal_api/includes/syncer_error.h" | |
14 #include "chrome/browser/sync/sessions/sync_session.h" | |
15 #include "chrome/browser/sync/syncable/blob.h" | |
16 #include "chrome/browser/sync/syncable/model_type.h" | |
17 | |
18 namespace syncable { | |
19 class Directory; | |
20 class Entry; | |
21 } // namespace syncable | |
22 | |
23 namespace sync_pb { | |
24 class ClientToServerResponse; | |
25 class EntitySpecifics; | |
26 } // namespace sync_pb | |
27 | |
28 namespace browser_sync { | |
29 | |
30 namespace sessions { | |
31 class SyncProtocolError; | |
32 class SyncSessionContext; | |
33 } | |
34 | |
35 class ClientToServerMessage; | |
36 class ServerConnectionManager; | |
37 class SyncEntity; | |
38 class CommitResponse_EntryResponse; | |
39 | |
40 class SyncerProtoUtil { | |
41 public: | |
42 // Posts the given message and fills the buffer with the returned value. | |
43 // Returns true on success. Also handles store birthday verification: will | |
44 // produce a SyncError if the birthday is incorrect. | |
45 static SyncerError PostClientToServerMessage( | |
46 const ClientToServerMessage& msg, | |
47 sync_pb::ClientToServerResponse* response, | |
48 sessions::SyncSession* session); | |
49 | |
50 // Compares a syncable Entry to SyncEntity, returns true iff the data is | |
51 // identical. | |
52 // | |
53 // TODO(sync): The places where this function is used are arguable big causes | |
54 // of the fragility, because there's a tendency to freak out the moment the | |
55 // local and server values diverge. However, this almost always indicates a | |
56 // sync bug somewhere earlier in the sync cycle. | |
57 static bool Compare(const syncable::Entry& local_entry, | |
58 const SyncEntity& server_entry); | |
59 | |
60 // Utility methods for converting between syncable::Blobs and protobuf byte | |
61 // fields. | |
62 static void CopyProtoBytesIntoBlob(const std::string& proto_bytes, | |
63 syncable::Blob* blob); | |
64 static bool ProtoBytesEqualsBlob(const std::string& proto_bytes, | |
65 const syncable::Blob& blob); | |
66 static void CopyBlobIntoProtoBytes(const syncable::Blob& blob, | |
67 std::string* proto_bytes); | |
68 | |
69 // Extract the name field from a sync entity. | |
70 static const std::string& NameFromSyncEntity( | |
71 const sync_pb::SyncEntity& entry); | |
72 | |
73 // Extract the name field from a commit entry response. | |
74 static const std::string& NameFromCommitEntryResponse( | |
75 const CommitResponse_EntryResponse& entry); | |
76 | |
77 // EntitySpecifics is used as a filter for the GetUpdates message to tell | |
78 // the server which datatypes to send back. This adds a datatype so that | |
79 // it's included in the filter. | |
80 static void AddToEntitySpecificDatatypesFilter(syncable::ModelType datatype, | |
81 sync_pb::EntitySpecifics* filter); | |
82 | |
83 // Get a debug string representation of the client to server response. | |
84 static std::string ClientToServerResponseDebugString( | |
85 const sync_pb::ClientToServerResponse& response); | |
86 | |
87 // Get update contents as a string. Intended for logging, and intended | |
88 // to have a smaller footprint than the protobuf's built-in pretty printer. | |
89 static std::string SyncEntityDebugString(const sync_pb::SyncEntity& entry); | |
90 | |
91 // Pull the birthday from the dir and put it into the msg. | |
92 static void AddRequestBirthday(syncable::Directory* dir, | |
93 ClientToServerMessage* msg); | |
94 | |
95 private: | |
96 SyncerProtoUtil() {} | |
97 | |
98 // Helper functions for PostClientToServerMessage. | |
99 | |
100 // Verifies the store birthday, alerting/resetting as appropriate if there's a | |
101 // mismatch. Return false if the syncer should be stuck. | |
102 static bool VerifyResponseBirthday(syncable::Directory* dir, | |
103 const sync_pb::ClientToServerResponse* response); | |
104 | |
105 // Builds and sends a SyncEngineEvent to begin migration for types (specified | |
106 // in notification). | |
107 static void HandleMigrationDoneResponse( | |
108 const sync_pb::ClientToServerResponse* response, | |
109 sessions::SyncSession* session); | |
110 | |
111 // Post the message using the scm, and do some processing on the returned | |
112 // headers. Decode the server response. | |
113 static bool PostAndProcessHeaders(browser_sync::ServerConnectionManager* scm, | |
114 sessions::SyncSession* session, | |
115 const ClientToServerMessage& msg, | |
116 sync_pb::ClientToServerResponse* response); | |
117 | |
118 static base::TimeDelta GetThrottleDelay( | |
119 const sync_pb::ClientToServerResponse& response); | |
120 | |
121 static void HandleThrottleError(const SyncProtocolError& error, | |
122 const base::TimeTicks& throttled_until, | |
123 sessions::SyncSessionContext* context, | |
124 sessions::SyncSession::Delegate* delegate); | |
125 | |
126 friend class SyncerProtoUtilTest; | |
127 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, AddRequestBirthday); | |
128 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, PostAndProcessHeaders); | |
129 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, VerifyResponseBirthday); | |
130 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, HandleThrottlingNoDatatypes); | |
131 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, HandleThrottlingWithDatatypes); | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(SyncerProtoUtil); | |
134 }; | |
135 | |
136 } // namespace browser_sync | |
137 | |
138 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ | |
OLD | NEW |