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