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 // Specifies where entity's position should be updated from the data in
62 // GetUpdates message.
63 static bool ShouldMaintainPosition(const sync_pb::SyncEntity
& sync_entity
);
65 // Specifies where entity's parent ID should be updated from the data in
66 // GetUpdates message.
67 static bool ShouldMaintainHierarchy(const sync_pb::SyncEntity
& sync_entity
);
69 // Extract the name field from a sync entity.
70 static const std::string
& NameFromSyncEntity(
71 const sync_pb::SyncEntity
& entry
);
73 // Extract the name field from a commit entry response.
74 static const std::string
& NameFromCommitEntryResponse(
75 const sync_pb::CommitResponse_EntryResponse
& entry
);
77 // Persist the bag of chips if it is present in the response.
78 static void PersistBagOfChips(
79 syncable::Directory
* dir
,
80 const sync_pb::ClientToServerResponse
& response
);
82 // EntitySpecifics is used as a filter for the GetUpdates message to tell
83 // the server which datatypes to send back. This adds a datatype so that
84 // it's included in the filter.
85 static void AddToEntitySpecificDatatypesFilter(ModelType datatype
,
86 sync_pb::EntitySpecifics
* filter
);
88 // Get a debug string representation of the client to server response.
89 static std::string
ClientToServerResponseDebugString(
90 const sync_pb::ClientToServerResponse
& response
);
92 // Get update contents as a string. Intended for logging, and intended
93 // to have a smaller footprint than the protobuf's built-in pretty printer.
94 static std::string
SyncEntityDebugString(const sync_pb::SyncEntity
& entry
);
96 // Pull the birthday from the dir and put it into the msg.
97 static void AddRequestBirthday(syncable::Directory
* dir
,
98 sync_pb::ClientToServerMessage
* msg
);
100 // Pull the bag of chips from the dir and put it into the msg.
101 static void AddBagOfChips(syncable::Directory
* dir
,
102 sync_pb::ClientToServerMessage
* msg
);
105 // Set the protocol version field in the outgoing message.
106 static void SetProtocolVersion(sync_pb::ClientToServerMessage
* msg
);
111 // Helper functions for PostClientToServerMessage.
113 // Verifies the store birthday, alerting/resetting as appropriate if there's a
114 // mismatch. Return false if the syncer should be stuck.
115 static bool VerifyResponseBirthday(
116 const sync_pb::ClientToServerResponse
& response
,
117 syncable::Directory
* dir
);
119 // Returns true if sync is disabled by admin for a dasher account.
120 static bool IsSyncDisabledByAdmin(
121 const sync_pb::ClientToServerResponse
& response
);
123 // Post the message using the scm, and do some processing on the returned
124 // headers. Decode the server response.
125 static bool PostAndProcessHeaders(ServerConnectionManager
* scm
,
126 sessions::SyncSession
* session
,
127 const sync_pb::ClientToServerMessage
& msg
,
128 sync_pb::ClientToServerResponse
* response
);
130 static base::TimeDelta
GetThrottleDelay(
131 const sync_pb::ClientToServerResponse
& response
);
133 friend class SyncerProtoUtilTest
;
134 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, AddRequestBirthday
);
135 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, PostAndProcessHeaders
);
136 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, VerifyDisabledByAdmin
);
137 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, VerifyResponseBirthday
);
138 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, HandleThrottlingNoDatatypes
);
139 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest
, HandleThrottlingWithDatatypes
);
141 DISALLOW_COPY_AND_ASSIGN(SyncerProtoUtil
);
144 } // namespace syncer
146 #endif // SYNC_ENGINE_SYNCER_PROTO_UTIL_H_