| OLD | NEW |
| (Empty) |
| 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/macros.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "sync/base/sync_export.h" | |
| 14 #include "sync/internal_api/public/base/model_type.h" | |
| 15 #include "sync/internal_api/public/util/syncer_error.h" | |
| 16 #include "sync/sessions/sync_session.h" | |
| 17 | |
| 18 namespace sync_pb { | |
| 19 class ClientToServerMessage; | |
| 20 class ClientToServerResponse; | |
| 21 class ClientToServerResponse_Error; | |
| 22 class CommitResponse_EntryResponse; | |
| 23 class EntitySpecifics; | |
| 24 class SyncEntity; | |
| 25 } | |
| 26 | |
| 27 namespace syncer { | |
| 28 | |
| 29 class ServerConnectionManager; | |
| 30 | |
| 31 namespace sessions { | |
| 32 class SyncProtocolError; | |
| 33 class SyncSessionContext; | |
| 34 } | |
| 35 | |
| 36 namespace syncable { | |
| 37 class Directory; | |
| 38 class Entry; | |
| 39 } | |
| 40 | |
| 41 // Returns the types to migrate from the data in |response|. | |
| 42 SYNC_EXPORT ModelTypeSet | |
| 43 GetTypesToMigrate(const sync_pb::ClientToServerResponse& response); | |
| 44 | |
| 45 // Builds a SyncProtocolError from the data in |error|. | |
| 46 SYNC_EXPORT SyncProtocolError ConvertErrorPBToSyncProtocolError( | |
| 47 const sync_pb::ClientToServerResponse_Error& error); | |
| 48 | |
| 49 class SYNC_EXPORT 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 ModelTypeSet* partial_failure_data_types); | |
| 61 | |
| 62 // Specifies where entity's position should be updated from the data in | |
| 63 // GetUpdates message. | |
| 64 static bool ShouldMaintainPosition(const sync_pb::SyncEntity& sync_entity); | |
| 65 | |
| 66 // Specifies where entity's parent ID should be updated from the data in | |
| 67 // GetUpdates message. | |
| 68 static bool ShouldMaintainHierarchy(const sync_pb::SyncEntity& sync_entity); | |
| 69 | |
| 70 // Extract the name field from a sync entity. | |
| 71 static const std::string& NameFromSyncEntity( | |
| 72 const sync_pb::SyncEntity& entry); | |
| 73 | |
| 74 // Extract the name field from a commit entry response. | |
| 75 static const std::string& NameFromCommitEntryResponse( | |
| 76 const sync_pb::CommitResponse_EntryResponse& entry); | |
| 77 | |
| 78 // Persist the bag of chips if it is present in the response. | |
| 79 static void PersistBagOfChips( | |
| 80 syncable::Directory* dir, | |
| 81 const sync_pb::ClientToServerResponse& response); | |
| 82 | |
| 83 // EntitySpecifics is used as a filter for the GetUpdates message to tell | |
| 84 // the server which datatypes to send back. This adds a datatype so that | |
| 85 // it's included in the filter. | |
| 86 static void AddToEntitySpecificDatatypesFilter(ModelType datatype, | |
| 87 sync_pb::EntitySpecifics* filter); | |
| 88 | |
| 89 // Get a debug string representation of the client to server response. | |
| 90 static std::string ClientToServerResponseDebugString( | |
| 91 const sync_pb::ClientToServerResponse& response); | |
| 92 | |
| 93 // Get update contents as a string. Intended for logging, and intended | |
| 94 // to have a smaller footprint than the protobuf's built-in pretty printer. | |
| 95 static std::string SyncEntityDebugString(const sync_pb::SyncEntity& entry); | |
| 96 | |
| 97 // Pull the birthday from the dir and put it into the msg. | |
| 98 static void AddRequestBirthday(syncable::Directory* dir, | |
| 99 sync_pb::ClientToServerMessage* msg); | |
| 100 | |
| 101 // Pull the bag of chips from the dir and put it into the msg. | |
| 102 static void AddBagOfChips(syncable::Directory* dir, | |
| 103 sync_pb::ClientToServerMessage* msg); | |
| 104 | |
| 105 | |
| 106 // Set the protocol version field in the outgoing message. | |
| 107 static void SetProtocolVersion(sync_pb::ClientToServerMessage* msg); | |
| 108 | |
| 109 private: | |
| 110 SyncerProtoUtil() {} | |
| 111 | |
| 112 // Helper functions for PostClientToServerMessage. | |
| 113 | |
| 114 // Analyzes error fields and store birthday in response message, compares | |
| 115 // store birthday with value in directory and returns corresponding | |
| 116 // SyncProtocolError. If needed updates store birthday in directory. | |
| 117 // This function makes it easier to test error handling. | |
| 118 static SyncProtocolError GetProtocolErrorFromResponse( | |
| 119 const sync_pb::ClientToServerResponse& response, | |
| 120 syncable::Directory* dir); | |
| 121 | |
| 122 // Verifies the store birthday, alerting/resetting as appropriate if there's a | |
| 123 // mismatch. Return false if the syncer should be stuck. | |
| 124 static bool VerifyResponseBirthday( | |
| 125 const sync_pb::ClientToServerResponse& response, | |
| 126 syncable::Directory* dir); | |
| 127 | |
| 128 // Returns true if sync is disabled by admin for a dasher account. | |
| 129 static bool IsSyncDisabledByAdmin( | |
| 130 const sync_pb::ClientToServerResponse& response); | |
| 131 | |
| 132 // Post the message using the scm, and do some processing on the returned | |
| 133 // headers. Decode the server response. | |
| 134 static bool PostAndProcessHeaders(ServerConnectionManager* scm, | |
| 135 sessions::SyncSession* session, | |
| 136 const sync_pb::ClientToServerMessage& msg, | |
| 137 sync_pb::ClientToServerResponse* response); | |
| 138 | |
| 139 static base::TimeDelta GetThrottleDelay( | |
| 140 const sync_pb::ClientToServerResponse& response); | |
| 141 | |
| 142 friend class SyncerProtoUtilTest; | |
| 143 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, AddRequestBirthday); | |
| 144 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, PostAndProcessHeaders); | |
| 145 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, HandleThrottlingNoDatatypes); | |
| 146 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, HandleThrottlingWithDatatypes); | |
| 147 | |
| 148 DISALLOW_COPY_AND_ASSIGN(SyncerProtoUtil); | |
| 149 }; | |
| 150 | |
| 151 } // namespace syncer | |
| 152 | |
| 153 #endif // SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ | |
| OLD | NEW |