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 #include "components/sync/engine_impl/commit.h"
6
7 #include <utility>
8
9 #include "base/metrics/histogram_macros.h"
10 #include "base/notreached.h"
11 #include "base/rand_util.h"
12 #include "base/trace_event/trace_event.h"
13 #include "components/sync/base/data_type_histogram.h"
14 #include "components/sync/engine_impl/commit_processor.h"
15 #include "components/sync/engine_impl/commit_util.h"
16 #include "components/sync/engine_impl/cycle/sync_cycle.h"
17 #include "components/sync/engine_impl/events/commit_request_event.h"
18 #include "components/sync/engine_impl/events/commit_response_event.h"
19 #include "components/sync/engine_impl/syncer.h"
20 #include "components/sync/engine_impl/syncer_proto_util.h"
21
22 namespace syncer {
23
24 namespace {
25
26 // The number of random ASCII bytes we'll add to CommitMessage. We choose 256
27 // because it is not too large (to hurt performance and compression ratio), but
28 // it is not too small to easily be canceled out using statistical analysis.
29 const size_t kPaddingSize = 256;
30
RandASCIIString(size_t length)31 std::string RandASCIIString(size_t length) {
32 std::string result;
33 const int kMin = static_cast<int>(' ');
34 const int kMax = static_cast<int>('~');
35 result.reserve(length);
36 for (size_t i = 0; i < length; ++i)
37 result.push_back(static_cast<char>(base::RandInt(kMin, kMax)));
38 return result;
39 }
40
GetSyncCommitError(SyncerError syncer_error)41 SyncCommitError GetSyncCommitError(SyncerError syncer_error) {
42 switch (syncer_error.value()) {
43 case SyncerError::UNSET:
44 case SyncerError::CANNOT_DO_WORK:
45 case SyncerError::SYNCER_OK:
46 case SyncerError::DATATYPE_TRIGGERED_RETRY:
47 case SyncerError::SERVER_MORE_TO_DOWNLOAD:
48 NOTREACHED();
49 break;
50 case SyncerError::NETWORK_CONNECTION_UNAVAILABLE:
51 case SyncerError::NETWORK_IO_ERROR:
52 return SyncCommitError::kNetworkError;
53 case SyncerError::SYNC_AUTH_ERROR:
54 return SyncCommitError::kAuthError;
55 case SyncerError::SYNC_SERVER_ERROR:
56 case SyncerError::SERVER_RETURN_UNKNOWN_ERROR:
57 case SyncerError::SERVER_RETURN_THROTTLED:
58 case SyncerError::SERVER_RETURN_TRANSIENT_ERROR:
59 case SyncerError::SERVER_RETURN_MIGRATION_DONE:
60 case SyncerError::SERVER_RETURN_CLEAR_PENDING:
61 case SyncerError::SERVER_RETURN_NOT_MY_BIRTHDAY:
62 case SyncerError::SERVER_RETURN_CONFLICT:
63 case SyncerError::SERVER_RETURN_PARTIAL_FAILURE:
64 case SyncerError::SERVER_RETURN_CLIENT_DATA_OBSOLETE:
65 case SyncerError::SERVER_RETURN_ENCRYPTION_OBSOLETE:
66 case SyncerError::SERVER_RETURN_DISABLED_BY_ADMIN:
67 return SyncCommitError::kServerError;
68 case SyncerError::SERVER_RESPONSE_VALIDATION_FAILED:
69 return SyncCommitError::kBadServerResponse;
70 }
71
72 NOTREACHED();
73 return SyncCommitError::kServerError;
74 }
75
76 } // namespace
77
Commit(ContributionMap contributions,const sync_pb::ClientToServerMessage & message,ExtensionsActivity::Records extensions_activity_buffer)78 Commit::Commit(ContributionMap contributions,
79 const sync_pb::ClientToServerMessage& message,
80 ExtensionsActivity::Records extensions_activity_buffer)
81 : contributions_(std::move(contributions)),
82 message_(message),
83 extensions_activity_buffer_(extensions_activity_buffer) {}
84
85 Commit::~Commit() = default;
86
87 // static
Init(ModelTypeSet enabled_types,size_t max_entries,const std::string & account_name,const std::string & cache_guid,bool cookie_jar_mismatch,bool cookie_jar_empty,bool single_client,CommitProcessor * commit_processor,ExtensionsActivity * extensions_activity)88 std::unique_ptr<Commit> Commit::Init(ModelTypeSet enabled_types,
89 size_t max_entries,
90 const std::string& account_name,
91 const std::string& cache_guid,
92 bool cookie_jar_mismatch,
93 bool cookie_jar_empty,
94 bool single_client,
95 CommitProcessor* commit_processor,
96 ExtensionsActivity* extensions_activity) {
97 // Gather per-type contributions.
98 ContributionMap contributions = commit_processor->GatherCommitContributions(
99 max_entries, cookie_jar_mismatch, cookie_jar_empty);
100
101 // Give up if no one had anything to commit.
102 if (contributions.empty())
103 return nullptr;
104
105 sync_pb::ClientToServerMessage message;
106 message.set_message_contents(sync_pb::ClientToServerMessage::COMMIT);
107 message.set_share(account_name);
108
109 sync_pb::CommitMessage* commit_message = message.mutable_commit();
110 commit_message->set_cache_guid(cache_guid);
111
112 // Set padding to mitigate CRIME attack.
113 commit_message->set_padding(RandASCIIString(kPaddingSize));
114
115 // Set extensions activity if bookmark commits are present.
116 ExtensionsActivity::Records extensions_activity_buffer;
117 if (extensions_activity != nullptr) {
118 ContributionMap::const_iterator it = contributions.find(BOOKMARKS);
119 if (it != contributions.end() && it->second->GetNumEntries() != 0) {
120 commit_util::AddExtensionsActivityToMessage(
121 extensions_activity, &extensions_activity_buffer, commit_message);
122 }
123 }
124
125 // Set the client config params.
126 commit_util::AddClientConfigParamsToMessage(
127 enabled_types, cookie_jar_mismatch, single_client, commit_message);
128
129 // Finally, serialize all our contributions.
130 for (const auto& contribution : contributions) {
131 contribution.second->AddToCommitMessage(&message);
132 }
133
134 // If we made it this far, then we've successfully prepared a commit message.
135 return std::make_unique<Commit>(std::move(contributions), message,
136 extensions_activity_buffer);
137 }
138
PostAndProcessResponse(NudgeTracker * nudge_tracker,SyncCycle * cycle,StatusController * status,ExtensionsActivity * extensions_activity)139 SyncerError Commit::PostAndProcessResponse(
140 NudgeTracker* nudge_tracker,
141 SyncCycle* cycle,
142 StatusController* status,
143 ExtensionsActivity* extensions_activity) {
144 ModelTypeSet request_types;
145 for (ContributionMap::const_iterator it = contributions_.begin();
146 it != contributions_.end(); ++it) {
147 ModelType request_type = it->first;
148 request_types.Put(request_type);
149 UMA_HISTOGRAM_ENUMERATION("Sync.PostedDataTypeCommitRequest",
150 ModelTypeHistogramValue(request_type));
151 }
152
153 if (cycle->context()->debug_info_getter()) {
154 *message_.mutable_debug_info() =
155 cycle->context()->debug_info_getter()->GetDebugInfo();
156 }
157
158 DVLOG(1) << "Sending commit message.";
159 SyncerProtoUtil::AddRequiredFieldsToClientToServerMessage(cycle, &message_);
160
161 CommitRequestEvent request_event(base::Time::Now(),
162 message_.commit().entries_size(),
163 request_types, message_);
164 cycle->SendProtocolEvent(request_event);
165
166 TRACE_EVENT_BEGIN0("sync", "PostCommit");
167 sync_pb::ClientToServerResponse response;
168 const SyncerError post_result = SyncerProtoUtil::PostClientToServerMessage(
169 message_, &response, cycle, nullptr);
170 TRACE_EVENT_END0("sync", "PostCommit");
171
172 // TODO(rlarocque): Use result that includes errors captured later?
173 CommitResponseEvent response_event(base::Time::Now(), post_result, response);
174 cycle->SendProtocolEvent(response_event);
175
176 if (post_result.value() != SyncerError::SYNCER_OK) {
177 LOG(WARNING) << "Post commit failed";
178 ReportFullCommitFailure(post_result);
179 return post_result;
180 }
181
182 if (!response.has_commit()) {
183 LOG(WARNING) << "Commit response has no commit body!";
184 const SyncerError syncer_error(
185 SyncerError::SERVER_RESPONSE_VALIDATION_FAILED);
186 ReportFullCommitFailure(syncer_error);
187 return syncer_error;
188 }
189
190 size_t message_entries = message_.commit().entries_size();
191 size_t response_entries = response.commit().entryresponse_size();
192 if (message_entries != response_entries) {
193 LOG(ERROR) << "Commit response has wrong number of entries! "
194 << "Expected: " << message_entries << ", "
195 << "Got: " << response_entries;
196 const SyncerError syncer_error(
197 SyncerError::SERVER_RESPONSE_VALIDATION_FAILED);
198 ReportFullCommitFailure(syncer_error);
199 return syncer_error;
200 }
201
202 if (cycle->context()->debug_info_getter()) {
203 // Clear debug info now that we have successfully sent it to the server.
204 DVLOG(1) << "Clearing client debug info.";
205 cycle->context()->debug_info_getter()->ClearDebugInfo();
206 }
207
208 // Let the contributors process the responses to each of their requests.
209 SyncerError processing_result = SyncerError(SyncerError::SYNCER_OK);
210 for (ContributionMap::const_iterator it = contributions_.begin();
211 it != contributions_.end(); ++it) {
212 TRACE_EVENT1("sync", "ProcessCommitResponse", "type",
213 ModelTypeToString(it->first));
214 SyncerError type_result =
215 it->second->ProcessCommitResponse(response, status);
216 if (type_result.value() == SyncerError::SERVER_RETURN_CONFLICT) {
217 nudge_tracker->RecordCommitConflict(it->first);
218 }
219 if (processing_result.value() == SyncerError::SYNCER_OK &&
220 type_result.value() != SyncerError::SYNCER_OK) {
221 processing_result = type_result;
222 }
223 }
224
225 // Handle bookmarks' special extensions activity stats.
226 if (extensions_activity != nullptr &&
227 cycle->status_controller()
228 .model_neutral_state()
229 .num_successful_bookmark_commits == 0) {
230 extensions_activity->PutRecords(extensions_activity_buffer_);
231 }
232
233 return processing_result;
234 }
235
GetContributingDataTypes() const236 ModelTypeSet Commit::GetContributingDataTypes() const {
237 ModelTypeSet contributed_data_types;
238 for (const auto& model_type_and_contribution : contributions_) {
239 contributed_data_types.Put(model_type_and_contribution.first);
240 }
241 return contributed_data_types;
242 }
243
ReportFullCommitFailure(SyncerError syncer_error)244 void Commit::ReportFullCommitFailure(SyncerError syncer_error) {
245 const SyncCommitError commit_error = GetSyncCommitError(syncer_error);
246 for (auto& model_type_and_contribution : contributions_) {
247 model_type_and_contribution.second->ProcessCommitFailure(commit_error);
248 }
249 }
250
251 } // namespace syncer
252