1 // Copyright 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/time.h"
12 #include "sync/base/sync_export.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 ClientToServerMessage
;
20 class ClientToServerResponse
;
21 class ClientToServerResponse_Error
;
22 class CommitResponse_EntryResponse
;
23 class EntitySpecifics
;
29 class ServerConnectionManager
;
32 class SyncProtocolError
;
33 class SyncSessionContext
;
41 // Returns the types to migrate from the data in |response|.
42 SYNC_EXPORT_PRIVATE ModelTypeSet
GetTypesToMigrate(
43 const sync_pb::ClientToServerResponse
& response
);
45 // Builds a SyncProtocolError from the data in |error|.
46 SYNC_EXPORT_PRIVATE SyncProtocolError
ConvertErrorPBToLocalType(
47 const sync_pb::ClientToServerResponse_Error
& error
);
49 class SYNC_EXPORT_PRIVATE SyncerProtoUtil
{
51 // Posts the given message and fills the buffer with the returned value.
52 // Returns true on success. Also handles store birthday verification: will
53 // produce a SyncError if the birthday is incorrect.
54 // NOTE: This will add all fields that must be sent on every request, which
55 // includes store birthday, protocol version, client chips, api keys, etc.
56 static SyncerError
PostClientToServerMessage(
57 sync_pb::ClientToServerMessage
* msg
,
58 sync_pb::ClientToServerResponse
* response
,
59 sessions::SyncSession
* session
,
60 ModelTypeSet
* partial_failure_data_types
);
62 static bool ShouldMaintainPosition(const sync_pb::SyncEntity
& sync_entity
);
64 // Utility methods for converting between syncable::Blobs and protobuf byte
66 static void CopyProtoBytesIntoBlob(const std::string
& proto_bytes
,
67 syncable::Blob
* blob
);
68 static bool ProtoBytesEqualsBlob(const std::string
& proto_bytes
,
69 const syncable::Blob
& blob
);
70 static void CopyBlobIntoProtoBytes(const syncable::Blob
& blob
,
71 std::string
* proto_bytes
);
73 // Extract the name field from a sync entity.
74 static const std::string
& NameFromSyncEntity(
75 const sync_pb::SyncEntity
& entry
);
77 // Extract the name field from a commit entry response.
78 static const std::string
& NameFromCommitEntryResponse(
79 const sync_pb::CommitResponse_EntryResponse
& entry
);
81 // Persist the bag of chips if it is present in the response.
82 static void PersistBagOfChips(
83 syncable::Directory
* dir
,
84 const sync_pb::ClientToServerResponse
& response
);
86 // EntitySpecifics is used as a filter for the GetUpdates message to tell
87 // the server which datatypes to send back. This adds a datatype so that
88 // it's included in the filter.
89 static void AddToEntitySpecificDatatypesFilter(ModelType datatype
,
90 sync_pb::EntitySpecifics
* filter
);
92 // Get a debug string representation of the client to server response.
93 static std::string
ClientToServerResponseDebugString(
94 const sync_pb::ClientToServerResponse
& response
);
96 // Get update contents as a string. Intended for logging, and intended
97 // to have a smaller footprint than the protobuf's built-in pretty printer.
98 static std::string
SyncEntityDebugString(const sync_pb::SyncEntity
& entry
);
100 // Pull the birthday from the dir and put it into the msg.
101 static void AddRequestBirthday(syncable::Directory
* dir
,
102 sync_pb::ClientToServerMessage
* msg
);
104 // Pull the bag of chips from the dir and put it into the msg.
105 static void AddBagOfChips(syncable::Directory
* dir
,
106 sync_pb::ClientToServerMessage
* msg
);
109 // Set the protocol version field in the outgoing message.
110 static void SetProtocolVersion(sync_pb::ClientToServerMessage
* msg
);
115 // Helper functions for PostClientToServerMessage.
117 // Verifies the store birthday, alerting/resetting as appropriate if there's a
118 // mismatch. Return false if the syncer should be stuck.
119 static bool VerifyResponseBirthday(
120 const sync_pb::ClientToServerResponse
& response
,
121 syncable::Directory
* dir
);
123 // Returns true if sync is disabled by admin for a dasher account.
124 static bool IsSyncDisabledByAdmin(
125 const sync_pb::ClientToServerResponse
& response
);
127 // Post the message using the scm, and do some processing on the returned
128 // headers. Decode the server response.
129 static bool PostAndProcessHeaders(ServerConnectionManager
* scm
,
130 sessions::SyncSession
* session
,
131 const sync_pb::ClientToServerMessage
& msg
,
132 sync_pb::ClientToServerResponse
* response
);
134 static base::TimeDelta
GetThrottleDelay(
135 const sync_pb::ClientToServerResponse
& response
);
137 friend class SyncerProtoUtilTest
;
138 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, AddRequestBirthday
);
139 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, PostAndProcessHeaders
);
140 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, VerifyDisabledByAdmin
);
141 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, VerifyResponseBirthday
);
142 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, HandleThrottlingNoDatatypes
);
143 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, HandleThrottlingWithDatatypes
);
145 DISALLOW_COPY_AND_ASSIGN(SyncerProtoUtil
);
148 } // namespace syncer
150 #endif // SYNC_ENGINE_SYNCER_PROTO_UTIL_H_