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
);
61 static bool ShouldMaintainPosition(const sync_pb::SyncEntity
& sync_entity
);
63 // Utility methods for converting between syncable::Blobs and protobuf byte
65 static void CopyProtoBytesIntoBlob(const std::string
& proto_bytes
,
66 syncable::Blob
* blob
);
67 static bool ProtoBytesEqualsBlob(const std::string
& proto_bytes
,
68 const syncable::Blob
& blob
);
69 static void CopyBlobIntoProtoBytes(const syncable::Blob
& blob
,
70 std::string
* proto_bytes
);
72 // Extract the name field from a sync entity.
73 static const std::string
& NameFromSyncEntity(
74 const sync_pb::SyncEntity
& entry
);
76 // Extract the name field from a commit entry response.
77 static const std::string
& NameFromCommitEntryResponse(
78 const sync_pb::CommitResponse_EntryResponse
& entry
);
80 // Persist the bag of chips if it is present in the response.
81 static void PersistBagOfChips(
82 syncable::Directory
* dir
,
83 const sync_pb::ClientToServerResponse
& response
);
85 // EntitySpecifics is used as a filter for the GetUpdates message to tell
86 // the server which datatypes to send back. This adds a datatype so that
87 // it's included in the filter.
88 static void AddToEntitySpecificDatatypesFilter(ModelType datatype
,
89 sync_pb::EntitySpecifics
* filter
);
91 // Get a debug string representation of the client to server response.
92 static std::string
ClientToServerResponseDebugString(
93 const sync_pb::ClientToServerResponse
& response
);
95 // Get update contents as a string. Intended for logging, and intended
96 // to have a smaller footprint than the protobuf's built-in pretty printer.
97 static std::string
SyncEntityDebugString(const sync_pb::SyncEntity
& entry
);
99 // Pull the birthday from the dir and put it into the msg.
100 static void AddRequestBirthday(syncable::Directory
* dir
,
101 sync_pb::ClientToServerMessage
* msg
);
103 // Pull the bag of chips from the dir and put it into the msg.
104 static void AddBagOfChips(syncable::Directory
* dir
,
105 sync_pb::ClientToServerMessage
* msg
);
108 // Set the protocol version field in the outgoing message.
109 static void SetProtocolVersion(sync_pb::ClientToServerMessage
* msg
);
114 // Helper functions for PostClientToServerMessage.
116 // Verifies the store birthday, alerting/resetting as appropriate if there's a
117 // mismatch. Return false if the syncer should be stuck.
118 static bool VerifyResponseBirthday(
119 const sync_pb::ClientToServerResponse
& response
,
120 syncable::Directory
* dir
);
122 // Returns true if sync is disabled by admin for a dasher account.
123 static bool IsSyncDisabledByAdmin(
124 const sync_pb::ClientToServerResponse
& response
);
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 friend class SyncerProtoUtilTest
;
137 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, AddRequestBirthday
);
138 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, PostAndProcessHeaders
);
139 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, VerifyDisabledByAdmin
);
140 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, VerifyResponseBirthday
);
141 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, HandleThrottlingNoDatatypes
);
142 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, HandleThrottlingWithDatatypes
);
144 DISALLOW_COPY_AND_ASSIGN(SyncerProtoUtil
);
147 } // namespace syncer
149 #endif // SYNC_ENGINE_SYNCER_PROTO_UTIL_H_