Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / sync / engine / commit.cc
blob743996d026ba062c777f01f5a0615a506cc40b3f
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.
5 #include "sync/engine/commit.h"
7 #include "base/metrics/sparse_histogram.h"
8 #include "base/trace_event/trace_event.h"
9 #include "sync/engine/commit_contribution.h"
10 #include "sync/engine/commit_processor.h"
11 #include "sync/engine/commit_util.h"
12 #include "sync/engine/syncer.h"
13 #include "sync/engine/syncer_proto_util.h"
14 #include "sync/internal_api/public/events/commit_request_event.h"
15 #include "sync/internal_api/public/events/commit_response_event.h"
16 #include "sync/sessions/sync_session.h"
17 #include "sync/util/data_type_histogram.h"
19 namespace syncer {
21 Commit::Commit(ContributionMap contributions,
22 const sync_pb::ClientToServerMessage& message,
23 ExtensionsActivity::Records extensions_activity_buffer)
24 : contributions_(contributions.Pass()),
25 message_(message),
26 extensions_activity_buffer_(extensions_activity_buffer),
27 cleaned_up_(false) {
30 Commit::~Commit() {
31 DCHECK(cleaned_up_);
34 Commit* Commit::Init(
35 ModelTypeSet requested_types,
36 ModelTypeSet enabled_types,
37 size_t max_entries,
38 const std::string& account_name,
39 const std::string& cache_guid,
40 CommitProcessor* commit_processor,
41 ExtensionsActivity* extensions_activity) {
42 // Gather per-type contributions.
43 ContributionMap contributions;
44 commit_processor->GatherCommitContributions(
45 requested_types,
46 max_entries,
47 &contributions);
49 // Give up if no one had anything to commit.
50 if (contributions.empty())
51 return NULL;
53 sync_pb::ClientToServerMessage message;
54 message.set_message_contents(sync_pb::ClientToServerMessage::COMMIT);
55 message.set_share(account_name);
57 sync_pb::CommitMessage* commit_message = message.mutable_commit();
58 commit_message->set_cache_guid(cache_guid);
60 // Set extensions activity if bookmark commits are present.
61 ExtensionsActivity::Records extensions_activity_buffer;
62 ContributionMap::const_iterator it = contributions.find(syncer::BOOKMARKS);
63 if (it != contributions.end() && it->second->GetNumEntries() != 0) {
64 commit_util::AddExtensionsActivityToMessage(
65 extensions_activity,
66 &extensions_activity_buffer,
67 commit_message);
70 // Set the client config params.
71 commit_util::AddClientConfigParamsToMessage(
72 enabled_types,
73 commit_message);
75 int previous_message_size = message.ByteSize();
76 // Finally, serialize all our contributions.
77 for (const auto& contribution : contributions) {
78 contribution.second->AddToCommitMessage(&message);
79 int current_entry_size = message.ByteSize() - previous_message_size;
80 previous_message_size = message.ByteSize();
81 int local_integer_model_type = ModelTypeToHistogramInt(contribution.first);
82 if (current_entry_size > 0) {
83 SyncRecordDatatypeBin("DataUse.Sync.Upload.Bytes",
84 local_integer_model_type, current_entry_size);
86 UMA_HISTOGRAM_SPARSE_SLOWLY("DataUse.Sync.Upload.Count",
87 local_integer_model_type);
90 // If we made it this far, then we've successfully prepared a commit message.
91 return new Commit(contributions.Pass(), message, extensions_activity_buffer);
94 SyncerError Commit::PostAndProcessResponse(
95 sessions::NudgeTracker* nudge_tracker,
96 sessions::SyncSession* session,
97 sessions::StatusController* status,
98 ExtensionsActivity* extensions_activity) {
99 ModelTypeSet request_types;
100 for (ContributionMap::const_iterator it = contributions_.begin();
101 it != contributions_.end(); ++it) {
102 request_types.Put(it->first);
104 session->mutable_status_controller()->set_commit_request_types(request_types);
106 if (session->context()->debug_info_getter()) {
107 sync_pb::DebugInfo* debug_info = message_.mutable_debug_info();
108 session->context()->debug_info_getter()->GetDebugInfo(debug_info);
111 DVLOG(1) << "Sending commit message.";
113 CommitRequestEvent request_event(
114 base::Time::Now(),
115 message_.commit().entries_size(),
116 request_types,
117 message_);
118 session->SendProtocolEvent(request_event);
120 TRACE_EVENT_BEGIN0("sync", "PostCommit");
121 const SyncerError post_result = SyncerProtoUtil::PostClientToServerMessage(
122 &message_, &response_, session, NULL);
123 TRACE_EVENT_END0("sync", "PostCommit");
125 // TODO(rlarocque): Use result that includes errors captured later?
126 CommitResponseEvent response_event(
127 base::Time::Now(),
128 post_result,
129 response_);
130 session->SendProtocolEvent(response_event);
132 if (post_result != SYNCER_OK) {
133 LOG(WARNING) << "Post commit failed";
134 return post_result;
137 if (!response_.has_commit()) {
138 LOG(WARNING) << "Commit response has no commit body!";
139 return SERVER_RESPONSE_VALIDATION_FAILED;
142 size_t message_entries = message_.commit().entries_size();
143 size_t response_entries = response_.commit().entryresponse_size();
144 if (message_entries != response_entries) {
145 LOG(ERROR)
146 << "Commit response has wrong number of entries! "
147 << "Expected: " << message_entries << ", "
148 << "Got: " << response_entries;
149 return SERVER_RESPONSE_VALIDATION_FAILED;
152 if (session->context()->debug_info_getter()) {
153 // Clear debug info now that we have successfully sent it to the server.
154 DVLOG(1) << "Clearing client debug info.";
155 session->context()->debug_info_getter()->ClearDebugInfo();
158 // Let the contributors process the responses to each of their requests.
159 SyncerError processing_result = SYNCER_OK;
160 for (ContributionMap::const_iterator it = contributions_.begin();
161 it != contributions_.end(); ++it) {
162 TRACE_EVENT1("sync", "ProcessCommitResponse",
163 "type", ModelTypeToString(it->first));
164 SyncerError type_result =
165 it->second->ProcessCommitResponse(response_, status);
166 if (type_result == SERVER_RETURN_CONFLICT) {
167 nudge_tracker->RecordCommitConflict(it->first);
169 if (processing_result == SYNCER_OK && type_result != SYNCER_OK) {
170 processing_result = type_result;
174 // Handle bookmarks' special extensions activity stats.
175 if (session->status_controller().
176 model_neutral_state().num_successful_bookmark_commits == 0) {
177 extensions_activity->PutRecords(extensions_activity_buffer_);
180 return processing_result;
183 void Commit::CleanUp() {
184 for (ContributionMap::const_iterator it = contributions_.begin();
185 it != contributions_.end(); ++it) {
186 it->second->CleanUp();
188 cleaned_up_ = true;
191 } // namespace syncer