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.
4
5#ifndef SYNC_ENGINE_SYNCER_PROTO_UTIL_H_
6#define SYNC_ENGINE_SYNCER_PROTO_UTIL_H_
7
8#include <string>
9
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"
17
18namespace sync_pb {
19class ClientToServerMessage;
20class ClientToServerResponse;
21class ClientToServerResponse_Error;
22class CommitResponse_EntryResponse;
23class EntitySpecifics;
24class SyncEntity;
25}
26
27namespace syncer {
28
29class ServerConnectionManager;
30
31namespace sessions {
32class SyncProtocolError;
33class SyncSessionContext;
34}
35
36namespace syncable {
37class Directory;
38class Entry;
39}
40
41// Returns the types to migrate from the data in |response|.
42SYNC_EXPORT_PRIVATE ModelTypeSet GetTypesToMigrate(
43    const sync_pb::ClientToServerResponse& response);
44
45// Builds a SyncProtocolError from the data in |error|.
46SYNC_EXPORT_PRIVATE SyncProtocolError ConvertErrorPBToLocalType(
47    const sync_pb::ClientToServerResponse_Error& error);
48
49class SYNC_EXPORT_PRIVATE SyncerProtoUtil {
50 public:
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
61  static bool ShouldMaintainPosition(const sync_pb::SyncEntity& sync_entity);
62
63  // Utility methods for converting between syncable::Blobs and protobuf byte
64  // fields.
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);
71
72  // Extract the name field from a sync entity.
73  static const std::string& NameFromSyncEntity(
74      const sync_pb::SyncEntity& entry);
75
76  // Extract the name field from a commit entry response.
77  static const std::string& NameFromCommitEntryResponse(
78      const sync_pb::CommitResponse_EntryResponse& entry);
79
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);
84
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);
90
91  // Get a debug string representation of the client to server response.
92  static std::string ClientToServerResponseDebugString(
93      const sync_pb::ClientToServerResponse& response);
94
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);
98
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);
102
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);
106
107
108  // Set the protocol version field in the outgoing message.
109  static void SetProtocolVersion(sync_pb::ClientToServerMessage* msg);
110
111 private:
112  SyncerProtoUtil() {}
113
114  // Helper functions for PostClientToServerMessage.
115
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);
121
122  // Returns true if sync is disabled by admin for a dasher account.
123  static bool IsSyncDisabledByAdmin(
124      const sync_pb::ClientToServerResponse& response);
125
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);
132
133  static base::TimeDelta GetThrottleDelay(
134      const sync_pb::ClientToServerResponse& response);
135
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);
143
144  DISALLOW_COPY_AND_ASSIGN(SyncerProtoUtil);
145};
146
147}  // namespace syncer
148
149#endif  // SYNC_ENGINE_SYNCER_PROTO_UTIL_H_
150