Home | History | Annotate | Download | only in engine
      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 "sync/engine/commit.h"
      6 
      7 #include "base/debug/trace_event.h"
      8 #include "sync/engine/commit_util.h"
      9 #include "sync/engine/sync_directory_commit_contribution.h"
     10 #include "sync/engine/syncer.h"
     11 #include "sync/engine/syncer_proto_util.h"
     12 #include "sync/sessions/sync_session.h"
     13 
     14 namespace syncer {
     15 
     16 Commit::Commit(
     17     const std::map<ModelType, SyncDirectoryCommitContribution*>& contributions,
     18     const sync_pb::ClientToServerMessage& message,
     19     ExtensionsActivity::Records extensions_activity_buffer)
     20   : contributions_(contributions),
     21     deleter_(&contributions_),
     22     message_(message),
     23     extensions_activity_buffer_(extensions_activity_buffer),
     24     cleaned_up_(false) {
     25 }
     26 
     27 Commit::~Commit() {
     28   DCHECK(cleaned_up_);
     29 }
     30 
     31 Commit* Commit::Init(
     32     ModelTypeSet requested_types,
     33     size_t max_entries,
     34     const std::string& account_name,
     35     const std::string& cache_guid,
     36     CommitContributorMap* contributor_map,
     37     ExtensionsActivity* extensions_activity) {
     38   // Gather per-type contributions.
     39   ContributionMap contributions;
     40   size_t num_entries = 0;
     41   for (ModelTypeSet::Iterator it = requested_types.First();
     42        it.Good(); it.Inc()) {
     43     CommitContributorMap::iterator cm_it = contributor_map->find(it.Get());
     44     if (cm_it == contributor_map->end()) {
     45       NOTREACHED()
     46           << "Could not find requested type " << ModelTypeToString(it.Get())
     47           << " in contributor map.";
     48       continue;
     49     }
     50     size_t spaces_remaining = max_entries - num_entries;
     51     SyncDirectoryCommitContribution* contribution =
     52         cm_it->second->GetContribution(spaces_remaining);
     53     if (contribution) {
     54       num_entries += contribution->GetNumEntries();
     55       contributions.insert(std::make_pair(it.Get(), contribution));
     56     }
     57     if (num_entries == max_entries) {
     58       break;  // No point in continuting to iterate in this case.
     59     }
     60   }
     61 
     62   // Give up if no one had anything to commit.
     63   if (contributions.empty())
     64     return NULL;
     65 
     66   sync_pb::ClientToServerMessage message;
     67   message.set_message_contents(sync_pb::ClientToServerMessage::COMMIT);
     68   message.set_share(account_name);
     69 
     70   sync_pb::CommitMessage* commit_message = message.mutable_commit();
     71   commit_message->set_cache_guid(cache_guid);
     72 
     73   // Set extensions activity if bookmark commits are present.
     74   ExtensionsActivity::Records extensions_activity_buffer;
     75   ContributionMap::iterator it = contributions.find(syncer::BOOKMARKS);
     76   if (it != contributions.end() && it->second->GetNumEntries() != 0) {
     77     commit_util::AddExtensionsActivityToMessage(
     78         extensions_activity,
     79         &extensions_activity_buffer,
     80         commit_message);
     81   }
     82 
     83   // Set the client config params.
     84   ModelTypeSet enabled_types;
     85   for (CommitContributorMap::iterator it = contributor_map->begin();
     86        it != contributor_map->end(); ++it) {
     87     enabled_types.Put(it->first);
     88   }
     89   commit_util::AddClientConfigParamsToMessage(enabled_types,
     90                                                     commit_message);
     91 
     92   // Finally, serialize all our contributions.
     93   for (std::map<ModelType, SyncDirectoryCommitContribution*>::iterator it =
     94            contributions.begin(); it != contributions.end(); ++it) {
     95     it->second->AddToCommitMessage(&message);
     96   }
     97 
     98   // If we made it this far, then we've successfully prepared a commit message.
     99   return new Commit(contributions, message, extensions_activity_buffer);
    100 }
    101 
    102 SyncerError Commit::PostAndProcessResponse(
    103     sessions::SyncSession* session,
    104     sessions::StatusController* status,
    105     ExtensionsActivity* extensions_activity) {
    106   ModelTypeSet request_types;
    107   for (ContributionMap::const_iterator it = contributions_.begin();
    108        it != contributions_.end(); ++it) {
    109     request_types.Put(it->first);
    110   }
    111   session->mutable_status_controller()->set_commit_request_types(request_types);
    112 
    113   if (session->context()->debug_info_getter()) {
    114     sync_pb::DebugInfo* debug_info = message_.mutable_debug_info();
    115     session->context()->debug_info_getter()->GetDebugInfo(debug_info);
    116   }
    117 
    118   DVLOG(1) << "Sending commit message.";
    119   TRACE_EVENT_BEGIN0("sync", "PostCommit");
    120   const SyncerError post_result = SyncerProtoUtil::PostClientToServerMessage(
    121       &message_, &response_, session);
    122   TRACE_EVENT_END0("sync", "PostCommit");
    123 
    124   if (post_result != SYNCER_OK) {
    125     LOG(WARNING) << "Post commit failed";
    126     return post_result;
    127   }
    128 
    129   if (!response_.has_commit()) {
    130     LOG(WARNING) << "Commit response has no commit body!";
    131     return SERVER_RESPONSE_VALIDATION_FAILED;
    132   }
    133 
    134   size_t message_entries = message_.commit().entries_size();
    135   size_t response_entries = response_.commit().entryresponse_size();
    136   if (message_entries != response_entries) {
    137     LOG(ERROR)
    138        << "Commit response has wrong number of entries! "
    139        << "Expected: " << message_entries << ", "
    140        << "Got: " << response_entries;
    141     return SERVER_RESPONSE_VALIDATION_FAILED;
    142   }
    143 
    144   if (session->context()->debug_info_getter()) {
    145     // Clear debug info now that we have successfully sent it to the server.
    146     DVLOG(1) << "Clearing client debug info.";
    147     session->context()->debug_info_getter()->ClearDebugInfo();
    148   }
    149 
    150   // Let the contributors process the responses to each of their requests.
    151   SyncerError processing_result = SYNCER_OK;
    152   for (std::map<ModelType, SyncDirectoryCommitContribution*>::iterator it =
    153        contributions_.begin(); it != contributions_.end(); ++it) {
    154     TRACE_EVENT1("sync", "ProcessCommitResponse",
    155                  "type", ModelTypeToString(it->first));
    156     SyncerError type_result =
    157         it->second->ProcessCommitResponse(response_, status);
    158     if (processing_result == SYNCER_OK && type_result != SYNCER_OK) {
    159       processing_result = type_result;
    160     }
    161   }
    162 
    163   // Handle bookmarks' special extensions activity stats.
    164   if (session->status_controller().
    165           model_neutral_state().num_successful_bookmark_commits == 0) {
    166     extensions_activity->PutRecords(extensions_activity_buffer_);
    167   }
    168 
    169   return processing_result;
    170 }
    171 
    172 void Commit::CleanUp() {
    173   for (ContributionMap::iterator it = contributions_.begin();
    174        it != contributions_.end(); ++it) {
    175     it->second->CleanUp();
    176   }
    177   cleaned_up_ = true;
    178 }
    179 
    180 }  // namespace syncer
    181