Update V8 to version 4.5.92.
[chromium-blink-merge.git] / sync / engine / commit.cc
blobf607be4c8b4e91499e2d1184c9c8d491d6b0d7ba
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/trace_event/trace_event.h"
8 #include "sync/engine/commit_contribution.h"
9 #include "sync/engine/commit_processor.h"
10 #include "sync/engine/commit_util.h"
11 #include "sync/engine/syncer.h"
12 #include "sync/engine/syncer_proto_util.h"
13 #include "sync/internal_api/public/events/commit_request_event.h"
14 #include "sync/internal_api/public/events/commit_response_event.h"
15 #include "sync/sessions/sync_session.h"
17 namespace syncer {
19 Commit::Commit(ContributionMap contributions,
20 const sync_pb::ClientToServerMessage& message,
21 ExtensionsActivity::Records extensions_activity_buffer)
22 : contributions_(contributions.Pass()),
23 message_(message),
24 extensions_activity_buffer_(extensions_activity_buffer),
25 cleaned_up_(false) {
28 Commit::~Commit() {
29 DCHECK(cleaned_up_);
32 Commit* Commit::Init(
33 ModelTypeSet requested_types,
34 ModelTypeSet enabled_types,
35 size_t max_entries,
36 const std::string& account_name,
37 const std::string& cache_guid,
38 CommitProcessor* commit_processor,
39 ExtensionsActivity* extensions_activity) {
40 // Gather per-type contributions.
41 ContributionMap contributions;
42 commit_processor->GatherCommitContributions(
43 requested_types,
44 max_entries,
45 &contributions);
47 // Give up if no one had anything to commit.
48 if (contributions.empty())
49 return NULL;
51 sync_pb::ClientToServerMessage message;
52 message.set_message_contents(sync_pb::ClientToServerMessage::COMMIT);
53 message.set_share(account_name);
55 sync_pb::CommitMessage* commit_message = message.mutable_commit();
56 commit_message->set_cache_guid(cache_guid);
58 // Set extensions activity if bookmark commits are present.
59 ExtensionsActivity::Records extensions_activity_buffer;
60 ContributionMap::const_iterator it = contributions.find(syncer::BOOKMARKS);
61 if (it != contributions.end() && it->second->GetNumEntries() != 0) {
62 commit_util::AddExtensionsActivityToMessage(
63 extensions_activity,
64 &extensions_activity_buffer,
65 commit_message);
68 // Set the client config params.
69 commit_util::AddClientConfigParamsToMessage(
70 enabled_types,
71 commit_message);
73 // Finally, serialize all our contributions.
74 for (std::map<ModelType, CommitContribution*>::const_iterator it =
75 contributions.begin();
76 it != contributions.end(); ++it) {
77 it->second->AddToCommitMessage(&message);
80 // If we made it this far, then we've successfully prepared a commit message.
81 return new Commit(contributions.Pass(), message, extensions_activity_buffer);
84 SyncerError Commit::PostAndProcessResponse(
85 sessions::NudgeTracker* nudge_tracker,
86 sessions::SyncSession* session,
87 sessions::StatusController* status,
88 ExtensionsActivity* extensions_activity) {
89 ModelTypeSet request_types;
90 for (ContributionMap::const_iterator it = contributions_.begin();
91 it != contributions_.end(); ++it) {
92 request_types.Put(it->first);
94 session->mutable_status_controller()->set_commit_request_types(request_types);
96 if (session->context()->debug_info_getter()) {
97 sync_pb::DebugInfo* debug_info = message_.mutable_debug_info();
98 session->context()->debug_info_getter()->GetDebugInfo(debug_info);
101 DVLOG(1) << "Sending commit message.";
103 CommitRequestEvent request_event(
104 base::Time::Now(),
105 message_.commit().entries_size(),
106 request_types,
107 message_);
108 session->SendProtocolEvent(request_event);
110 TRACE_EVENT_BEGIN0("sync", "PostCommit");
111 const SyncerError post_result = SyncerProtoUtil::PostClientToServerMessage(
112 &message_, &response_, session, NULL);
113 TRACE_EVENT_END0("sync", "PostCommit");
115 // TODO(rlarocque): Use result that includes errors captured later?
116 CommitResponseEvent response_event(
117 base::Time::Now(),
118 post_result,
119 response_);
120 session->SendProtocolEvent(response_event);
122 if (post_result != SYNCER_OK) {
123 LOG(WARNING) << "Post commit failed";
124 return post_result;
127 if (!response_.has_commit()) {
128 LOG(WARNING) << "Commit response has no commit body!";
129 return SERVER_RESPONSE_VALIDATION_FAILED;
132 size_t message_entries = message_.commit().entries_size();
133 size_t response_entries = response_.commit().entryresponse_size();
134 if (message_entries != response_entries) {
135 LOG(ERROR)
136 << "Commit response has wrong number of entries! "
137 << "Expected: " << message_entries << ", "
138 << "Got: " << response_entries;
139 return SERVER_RESPONSE_VALIDATION_FAILED;
142 if (session->context()->debug_info_getter()) {
143 // Clear debug info now that we have successfully sent it to the server.
144 DVLOG(1) << "Clearing client debug info.";
145 session->context()->debug_info_getter()->ClearDebugInfo();
148 // Let the contributors process the responses to each of their requests.
149 SyncerError processing_result = SYNCER_OK;
150 for (ContributionMap::const_iterator it = contributions_.begin();
151 it != contributions_.end(); ++it) {
152 TRACE_EVENT1("sync", "ProcessCommitResponse",
153 "type", ModelTypeToString(it->first));
154 SyncerError type_result =
155 it->second->ProcessCommitResponse(response_, status);
156 if (type_result == SERVER_RETURN_CONFLICT) {
157 nudge_tracker->RecordCommitConflict(it->first);
159 if (processing_result == SYNCER_OK && type_result != SYNCER_OK) {
160 processing_result = type_result;
164 // Handle bookmarks' special extensions activity stats.
165 if (session->status_controller().
166 model_neutral_state().num_successful_bookmark_commits == 0) {
167 extensions_activity->PutRecords(extensions_activity_buffer_);
170 return processing_result;
173 void Commit::CleanUp() {
174 for (ContributionMap::const_iterator it = contributions_.begin();
175 it != contributions_.end(); ++it) {
176 it->second->CleanUp();
178 cleaned_up_ = true;
181 } // namespace syncer