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.
5 #ifndef SYNC_ENGINE_SYNCER_PROTO_UTIL_H_
6 #define SYNC_ENGINE_SYNCER_PROTO_UTIL_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/time.h"
12 #include "sync/internal_api/public/base/model_type.h"
13 #include "sync/internal_api/public/util/syncer_error.h"
14 #include "sync/sessions/sync_session.h"
15 #include "sync/syncable/blob.h"
18 class ClientToServerMessage
;
19 class ClientToServerResponse
;
20 class CommitResponse_EntryResponse
;
21 class EntitySpecifics
;
27 class ThrottledDataTypeTracker
;
28 class ServerConnectionManager
;
31 class SyncProtocolError
;
32 class SyncSessionContext
;
40 class SyncerProtoUtil
{
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 // NOTE: This will add all fields that must be sent on every request, which
46 // includes store birthday, protocol version, client chips, api keys, etc.
47 static SyncerError
PostClientToServerMessage(
48 sync_pb::ClientToServerMessage
* msg
,
49 sync_pb::ClientToServerResponse
* response
,
50 sessions::SyncSession
* session
);
52 // Compares a syncable Entry to SyncEntity, returns true iff the data is
55 // TODO(sync): The places where this function is used are arguable big causes
56 // of the fragility, because there's a tendency to freak out the moment the
57 // local and server values diverge. However, this almost always indicates a
58 // sync bug somewhere earlier in the sync cycle.
59 static bool Compare(const syncable::Entry
& local_entry
,
60 const sync_pb::SyncEntity
& server_entry
);
62 // Utility methods for converting between syncable::Blobs and protobuf byte
64 static void CopyProtoBytesIntoBlob(const std::string
& proto_bytes
,
65 syncable::Blob
* blob
);
66 static bool ProtoBytesEqualsBlob(const std::string
& proto_bytes
,
67 const syncable::Blob
& blob
);
68 static void CopyBlobIntoProtoBytes(const syncable::Blob
& blob
,
69 std::string
* proto_bytes
);
71 // Extract the name field from a sync entity.
72 static const std::string
& NameFromSyncEntity(
73 const sync_pb::SyncEntity
& entry
);
75 // Extract the name field from a commit entry response.
76 static const std::string
& NameFromCommitEntryResponse(
77 const sync_pb::CommitResponse_EntryResponse
& entry
);
79 // Persist the bag of chips if it is present in the response.
80 static void PersistBagOfChips(
81 syncable::Directory
* dir
,
82 const sync_pb::ClientToServerResponse
& response
);
84 // EntitySpecifics is used as a filter for the GetUpdates message to tell
85 // the server which datatypes to send back. This adds a datatype so that
86 // it's included in the filter.
87 static void AddToEntitySpecificDatatypesFilter(ModelType datatype
,
88 sync_pb::EntitySpecifics
* filter
);
90 // Get a debug string representation of the client to server response.
91 static std::string
ClientToServerResponseDebugString(
92 const sync_pb::ClientToServerResponse
& response
);
94 // Get update contents as a string. Intended for logging, and intended
95 // to have a smaller footprint than the protobuf's built-in pretty printer.
96 static std::string
SyncEntityDebugString(const sync_pb::SyncEntity
& entry
);
98 // Pull the birthday from the dir and put it into the msg.
99 static void AddRequestBirthday(syncable::Directory
* dir
,
100 sync_pb::ClientToServerMessage
* msg
);
102 // Pull the bag of chips from the dir and put it into the msg.
103 static void AddBagOfChips(syncable::Directory
* dir
,
104 sync_pb::ClientToServerMessage
* msg
);
107 // Set the protocol version field in the outgoing message.
108 static void SetProtocolVersion(sync_pb::ClientToServerMessage
* msg
);
113 // Helper functions for PostClientToServerMessage.
115 // Verifies the store birthday, alerting/resetting as appropriate if there's a
116 // mismatch. Return false if the syncer should be stuck.
117 static bool VerifyResponseBirthday(syncable::Directory
* dir
,
118 const sync_pb::ClientToServerResponse
* response
);
120 // Builds and sends a SyncEngineEvent to begin migration for types (specified
122 static void HandleMigrationDoneResponse(
123 const sync_pb::ClientToServerResponse
* response
,
124 sessions::SyncSession
* session
);
126 // Post the message using the scm, and do some processing on the returned
127 // headers. Decode the server response.
128 static bool PostAndProcessHeaders(ServerConnectionManager
* scm
,
129 sessions::SyncSession
* session
,
130 const sync_pb::ClientToServerMessage
& msg
,
131 sync_pb::ClientToServerResponse
* response
);
133 static base::TimeDelta
GetThrottleDelay(
134 const sync_pb::ClientToServerResponse
& response
);
136 static void HandleThrottleError(
137 const SyncProtocolError
& error
,
138 const base::TimeTicks
& throttled_until
,
139 ThrottledDataTypeTracker
* tracker
,
140 sessions::SyncSession::Delegate
* delegate
);
142 friend class SyncerProtoUtilTest
;
143 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, AddRequestBirthday
);
144 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, PostAndProcessHeaders
);
145 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, VerifyResponseBirthday
);
146 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, HandleThrottlingNoDatatypes
);
147 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, HandleThrottlingWithDatatypes
);
149 DISALLOW_COPY_AND_ASSIGN(SyncerProtoUtil
);
152 } // namespace syncer
154 #endif // SYNC_ENGINE_SYNCER_PROTO_UTIL_H_