| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ | 5 #ifndef SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ |
| 6 #define SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ | 6 #define SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "sync/internal_api/public/base/model_type.h" | 12 #include "sync/internal_api/public/base/model_type.h" |
| 13 #include "sync/internal_api/public/util/syncer_error.h" | 13 #include "sync/internal_api/public/util/syncer_error.h" |
| 14 #include "sync/sessions/sync_session.h" | 14 #include "sync/sessions/sync_session.h" |
| 15 #include "sync/syncable/blob.h" | 15 #include "sync/syncable/blob.h" |
| 16 | 16 |
| 17 namespace sync_pb { | 17 namespace sync_pb { |
| 18 class ClientToServerMessage; |
| 18 class ClientToServerResponse; | 19 class ClientToServerResponse; |
| 20 class CommitResponse_EntryResponse; |
| 19 class EntitySpecifics; | 21 class EntitySpecifics; |
| 22 class SyncEntity; |
| 20 } | 23 } |
| 21 | 24 |
| 22 namespace syncer { | 25 namespace syncer { |
| 23 | 26 |
| 24 class ClientToServerMessage; | |
| 25 class ThrottledDataTypeTracker; | 27 class ThrottledDataTypeTracker; |
| 26 class ServerConnectionManager; | 28 class ServerConnectionManager; |
| 27 class SyncEntity; | |
| 28 class CommitResponse_EntryResponse; | |
| 29 | 29 |
| 30 namespace sessions { | 30 namespace sessions { |
| 31 class SyncProtocolError; | 31 class SyncProtocolError; |
| 32 class SyncSessionContext; | 32 class SyncSessionContext; |
| 33 } | 33 } |
| 34 | 34 |
| 35 namespace syncable { | 35 namespace syncable { |
| 36 class Directory; | 36 class Directory; |
| 37 class Entry; | 37 class Entry; |
| 38 } | 38 } |
| 39 | 39 |
| 40 class SyncerProtoUtil { | 40 class SyncerProtoUtil { |
| 41 public: | 41 public: |
| 42 // Posts the given message and fills the buffer with the returned value. | 42 // Posts the given message and fills the buffer with the returned value. |
| 43 // Returns true on success. Also handles store birthday verification: will | 43 // Returns true on success. Also handles store birthday verification: will |
| 44 // produce a SyncError if the birthday is incorrect. | 44 // produce a SyncError if the birthday is incorrect. |
| 45 static SyncerError PostClientToServerMessage( | 45 static SyncerError PostClientToServerMessage( |
| 46 const ClientToServerMessage& msg, | 46 const sync_pb::ClientToServerMessage& msg, |
| 47 sync_pb::ClientToServerResponse* response, | 47 sync_pb::ClientToServerResponse* response, |
| 48 sessions::SyncSession* session); | 48 sessions::SyncSession* session); |
| 49 | 49 |
| 50 // Compares a syncable Entry to SyncEntity, returns true iff the data is | 50 // Compares a syncable Entry to SyncEntity, returns true iff the data is |
| 51 // identical. | 51 // identical. |
| 52 // | 52 // |
| 53 // TODO(sync): The places where this function is used are arguable big causes | 53 // TODO(sync): The places where this function is used are arguable big causes |
| 54 // of the fragility, because there's a tendency to freak out the moment the | 54 // of the fragility, because there's a tendency to freak out the moment the |
| 55 // local and server values diverge. However, this almost always indicates a | 55 // local and server values diverge. However, this almost always indicates a |
| 56 // sync bug somewhere earlier in the sync cycle. | 56 // sync bug somewhere earlier in the sync cycle. |
| 57 static bool Compare(const syncable::Entry& local_entry, | 57 static bool Compare(const syncable::Entry& local_entry, |
| 58 const SyncEntity& server_entry); | 58 const sync_pb::SyncEntity& server_entry); |
| 59 | 59 |
| 60 // Utility methods for converting between syncable::Blobs and protobuf byte | 60 // Utility methods for converting between syncable::Blobs and protobuf byte |
| 61 // fields. | 61 // fields. |
| 62 static void CopyProtoBytesIntoBlob(const std::string& proto_bytes, | 62 static void CopyProtoBytesIntoBlob(const std::string& proto_bytes, |
| 63 syncable::Blob* blob); | 63 syncable::Blob* blob); |
| 64 static bool ProtoBytesEqualsBlob(const std::string& proto_bytes, | 64 static bool ProtoBytesEqualsBlob(const std::string& proto_bytes, |
| 65 const syncable::Blob& blob); | 65 const syncable::Blob& blob); |
| 66 static void CopyBlobIntoProtoBytes(const syncable::Blob& blob, | 66 static void CopyBlobIntoProtoBytes(const syncable::Blob& blob, |
| 67 std::string* proto_bytes); | 67 std::string* proto_bytes); |
| 68 | 68 |
| 69 // Extract the name field from a sync entity. | 69 // Extract the name field from a sync entity. |
| 70 static const std::string& NameFromSyncEntity( | 70 static const std::string& NameFromSyncEntity( |
| 71 const sync_pb::SyncEntity& entry); | 71 const sync_pb::SyncEntity& entry); |
| 72 | 72 |
| 73 // Extract the name field from a commit entry response. | 73 // Extract the name field from a commit entry response. |
| 74 static const std::string& NameFromCommitEntryResponse( | 74 static const std::string& NameFromCommitEntryResponse( |
| 75 const CommitResponse_EntryResponse& entry); | 75 const sync_pb::CommitResponse_EntryResponse& entry); |
| 76 | 76 |
| 77 // EntitySpecifics is used as a filter for the GetUpdates message to tell | 77 // EntitySpecifics is used as a filter for the GetUpdates message to tell |
| 78 // the server which datatypes to send back. This adds a datatype so that | 78 // the server which datatypes to send back. This adds a datatype so that |
| 79 // it's included in the filter. | 79 // it's included in the filter. |
| 80 static void AddToEntitySpecificDatatypesFilter(syncer::ModelType datatype, | 80 static void AddToEntitySpecificDatatypesFilter(syncer::ModelType datatype, |
| 81 sync_pb::EntitySpecifics* filter); | 81 sync_pb::EntitySpecifics* filter); |
| 82 | 82 |
| 83 // Get a debug string representation of the client to server response. | 83 // Get a debug string representation of the client to server response. |
| 84 static std::string ClientToServerResponseDebugString( | 84 static std::string ClientToServerResponseDebugString( |
| 85 const sync_pb::ClientToServerResponse& response); | 85 const sync_pb::ClientToServerResponse& response); |
| 86 | 86 |
| 87 // Get update contents as a string. Intended for logging, and intended | 87 // Get update contents as a string. Intended for logging, and intended |
| 88 // to have a smaller footprint than the protobuf's built-in pretty printer. | 88 // to have a smaller footprint than the protobuf's built-in pretty printer. |
| 89 static std::string SyncEntityDebugString(const sync_pb::SyncEntity& entry); | 89 static std::string SyncEntityDebugString(const sync_pb::SyncEntity& entry); |
| 90 | 90 |
| 91 // Pull the birthday from the dir and put it into the msg. | 91 // Pull the birthday from the dir and put it into the msg. |
| 92 static void AddRequestBirthday(syncable::Directory* dir, | 92 static void AddRequestBirthday(syncable::Directory* dir, |
| 93 ClientToServerMessage* msg); | 93 sync_pb::ClientToServerMessage* msg); |
| 94 |
| 95 // Set the protocol version field in the outgoing message. |
| 96 static void SetProtocolVersion(sync_pb::ClientToServerMessage* msg); |
| 94 | 97 |
| 95 private: | 98 private: |
| 96 SyncerProtoUtil() {} | 99 SyncerProtoUtil() {} |
| 97 | 100 |
| 98 // Helper functions for PostClientToServerMessage. | 101 // Helper functions for PostClientToServerMessage. |
| 99 | 102 |
| 100 // Verifies the store birthday, alerting/resetting as appropriate if there's a | 103 // Verifies the store birthday, alerting/resetting as appropriate if there's a |
| 101 // mismatch. Return false if the syncer should be stuck. | 104 // mismatch. Return false if the syncer should be stuck. |
| 102 static bool VerifyResponseBirthday(syncable::Directory* dir, | 105 static bool VerifyResponseBirthday(syncable::Directory* dir, |
| 103 const sync_pb::ClientToServerResponse* response); | 106 const sync_pb::ClientToServerResponse* response); |
| 104 | 107 |
| 105 // Builds and sends a SyncEngineEvent to begin migration for types (specified | 108 // Builds and sends a SyncEngineEvent to begin migration for types (specified |
| 106 // in notification). | 109 // in notification). |
| 107 static void HandleMigrationDoneResponse( | 110 static void HandleMigrationDoneResponse( |
| 108 const sync_pb::ClientToServerResponse* response, | 111 const sync_pb::ClientToServerResponse* response, |
| 109 sessions::SyncSession* session); | 112 sessions::SyncSession* session); |
| 110 | 113 |
| 111 // Post the message using the scm, and do some processing on the returned | 114 // Post the message using the scm, and do some processing on the returned |
| 112 // headers. Decode the server response. | 115 // headers. Decode the server response. |
| 113 static bool PostAndProcessHeaders(syncer::ServerConnectionManager* scm, | 116 static bool PostAndProcessHeaders(syncer::ServerConnectionManager* scm, |
| 114 sessions::SyncSession* session, | 117 sessions::SyncSession* session, |
| 115 const ClientToServerMessage& msg, | 118 const sync_pb::ClientToServerMessage& msg, |
| 116 sync_pb::ClientToServerResponse* response); | 119 sync_pb::ClientToServerResponse* response); |
| 117 | 120 |
| 118 static base::TimeDelta GetThrottleDelay( | 121 static base::TimeDelta GetThrottleDelay( |
| 119 const sync_pb::ClientToServerResponse& response); | 122 const sync_pb::ClientToServerResponse& response); |
| 120 | 123 |
| 121 static void HandleThrottleError( | 124 static void HandleThrottleError( |
| 122 const SyncProtocolError& error, | 125 const SyncProtocolError& error, |
| 123 const base::TimeTicks& throttled_until, | 126 const base::TimeTicks& throttled_until, |
| 124 syncer::ThrottledDataTypeTracker* tracker, | 127 syncer::ThrottledDataTypeTracker* tracker, |
| 125 sessions::SyncSession::Delegate* delegate); | 128 sessions::SyncSession::Delegate* delegate); |
| 126 | 129 |
| 127 friend class SyncerProtoUtilTest; | 130 friend class SyncerProtoUtilTest; |
| 128 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, AddRequestBirthday); | 131 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, AddRequestBirthday); |
| 129 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, PostAndProcessHeaders); | 132 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, PostAndProcessHeaders); |
| 130 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, VerifyResponseBirthday); | 133 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, VerifyResponseBirthday); |
| 131 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, HandleThrottlingNoDatatypes); | 134 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, HandleThrottlingNoDatatypes); |
| 132 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, HandleThrottlingWithDatatypes); | 135 FRIEND_TEST_ALL_PREFIXES(SyncerProtoUtilTest, HandleThrottlingWithDatatypes); |
| 133 | 136 |
| 134 DISALLOW_COPY_AND_ASSIGN(SyncerProtoUtil); | 137 DISALLOW_COPY_AND_ASSIGN(SyncerProtoUtil); |
| 135 }; | 138 }; |
| 136 | 139 |
| 137 } // namespace syncer | 140 } // namespace syncer |
| 138 | 141 |
| 139 #endif // SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ | 142 #endif // SYNC_ENGINE_SYNCER_PROTO_UTIL_H_ |
| OLD | NEW |