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"
18 class ClientToServerMessage
;
19 class ClientToServerResponse
;
20 class ClientToServerResponse_Error
;
21 class CommitResponse_EntryResponse
;
22 class EntitySpecifics
;
28 class ServerConnectionManager
;
31 class SyncProtocolError
;
32 class SyncSessionContext
;
40 // Returns the types to migrate from the data in |response|.
41 SYNC_EXPORT_PRIVATE ModelTypeSet
GetTypesToMigrate(
42 const sync_pb::ClientToServerResponse
& response
);
44 // Builds a SyncProtocolError from the data in |error|.
45 SYNC_EXPORT_PRIVATE SyncProtocolError
ConvertErrorPBToLocalType(
46 const sync_pb::ClientToServerResponse_Error
& error
);
48 class SYNC_EXPORT_PRIVATE SyncerProtoUtil
{
50 // Posts the given message and fills the buffer with the returned value.
51 // Returns true on success. Also handles store birthday verification: will
52 // produce a SyncError if the birthday is incorrect.
53 // NOTE: This will add all fields that must be sent on every request, which
54 // includes store birthday, protocol version, client chips, api keys, etc.
55 static SyncerError
PostClientToServerMessage(
56 sync_pb::ClientToServerMessage
* msg
,
57 sync_pb::ClientToServerResponse
* response
,
58 sessions::SyncSession
* session
,
59 ModelTypeSet
* partial_failure_data_types
);
61 static bool ShouldMaintainPosition(const sync_pb::SyncEntity
& sync_entity
);
63 // Extract the name field from a sync entity.
64 static const std::string
& NameFromSyncEntity(
65 const sync_pb::SyncEntity
& entry
);
67 // Extract the name field from a commit entry response.
68 static const std::string
& NameFromCommitEntryResponse(
69 const sync_pb::CommitResponse_EntryResponse
& entry
);
71 // Persist the bag of chips if it is present in the response.
72 static void PersistBagOfChips(
73 syncable::Directory
* dir
,
74 const sync_pb::ClientToServerResponse
& response
);
76 // EntitySpecifics is used as a filter for the GetUpdates message to tell
77 // the server which datatypes to send back. This adds a datatype so that
78 // it's included in the filter.
79 static void AddToEntitySpecificDatatypesFilter(ModelType datatype
,
80 sync_pb::EntitySpecifics
* filter
);
82 // Get a debug string representation of the client to server response.
83 static std::string
ClientToServerResponseDebugString(
84 const sync_pb::ClientToServerResponse
& response
);
86 // Get update contents as a string. Intended for logging, and intended
87 // to have a smaller footprint than the protobuf's built-in pretty printer.
88 static std::string
SyncEntityDebugString(const sync_pb::SyncEntity
& entry
);
90 // Pull the birthday from the dir and put it into the msg.
91 static void AddRequestBirthday(syncable::Directory
* dir
,
92 sync_pb::ClientToServerMessage
* msg
);
94 // Pull the bag of chips from the dir and put it into the msg.
95 static void AddBagOfChips(syncable::Directory
* dir
,
96 sync_pb::ClientToServerMessage
* msg
);
99 // Set the protocol version field in the outgoing message.
100 static void SetProtocolVersion(sync_pb::ClientToServerMessage
* msg
);
105 // Helper functions for PostClientToServerMessage.
107 // Verifies the store birthday, alerting/resetting as appropriate if there's a
108 // mismatch. Return false if the syncer should be stuck.
109 static bool VerifyResponseBirthday(
110 const sync_pb::ClientToServerResponse
& response
,
111 syncable::Directory
* dir
);
113 // Returns true if sync is disabled by admin for a dasher account.
114 static bool IsSyncDisabledByAdmin(
115 const sync_pb::ClientToServerResponse
& response
);
117 // Post the message using the scm, and do some processing on the returned
118 // headers. Decode the server response.
119 static bool PostAndProcessHeaders(ServerConnectionManager
* scm
,
120 sessions::SyncSession
* session
,
121 const sync_pb::ClientToServerMessage
& msg
,
122 sync_pb::ClientToServerResponse
* response
);
124 static base::TimeDelta
GetThrottleDelay(
125 const sync_pb::ClientToServerResponse
& response
);
127 friend class SyncerProtoUtilTest
;
128 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, AddRequestBirthday
);
129 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, PostAndProcessHeaders
);
130 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, VerifyDisabledByAdmin
);
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_