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