Home | History | Annotate | Download | only in sessions
      1 // Copyright (c) 2011 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 "chrome/browser/sync/sessions/sync_session.h"
      6 #include "chrome/browser/sync/syncable/directory_manager.h"
      7 #include "chrome/browser/sync/syncable/model_type.h"
      8 
      9 namespace browser_sync {
     10 namespace sessions {
     11 
     12 SyncSession::SyncSession(SyncSessionContext* context, Delegate* delegate,
     13     const SyncSourceInfo& source,
     14     const ModelSafeRoutingInfo& routing_info,
     15     const std::vector<ModelSafeWorker*>& workers)
     16     : context_(context),
     17       source_(source),
     18       write_transaction_(NULL),
     19       delegate_(delegate),
     20       workers_(workers),
     21       routing_info_(routing_info) {
     22   status_controller_.reset(new StatusController(routing_info_));
     23 }
     24 
     25 SyncSession::~SyncSession() {}
     26 
     27 void SyncSession::Coalesce(const SyncSession& session) {
     28   if (context_ != session.context() || delegate_ != session.delegate_) {
     29     NOTREACHED();
     30     return;
     31   }
     32 
     33   // When we coalesce sessions, the sync update source gets overwritten with the
     34   // most recent, while the type/payload map gets merged.
     35   CoalescePayloads(&source_.types, session.source_.types);
     36   source_.updates_source = session.source_.updates_source;
     37 
     38   std::vector<ModelSafeWorker*> temp;
     39   std::set_union(workers_.begin(), workers_.end(),
     40                  session.workers_.begin(), session.workers_.end(),
     41                  std::back_inserter(temp));
     42   workers_.swap(temp);
     43 
     44   // We have to update the model safe routing info to the union. In case the
     45   // same key is present in both pick the one from session.
     46   for (ModelSafeRoutingInfo::const_iterator it =
     47        session.routing_info_.begin();
     48        it != session.routing_info_.end();
     49        ++it) {
     50     routing_info_[it->first] = it->second;
     51   }
     52 }
     53 
     54 void SyncSession::ResetTransientState() {
     55   status_controller_.reset(new StatusController(routing_info_));
     56 }
     57 
     58 SyncSessionSnapshot SyncSession::TakeSnapshot() const {
     59   syncable::ScopedDirLookup dir(context_->directory_manager(),
     60                                 context_->account_name());
     61   if (!dir.good())
     62     LOG(ERROR) << "Scoped dir lookup failed!";
     63 
     64   bool is_share_useable = true;
     65   syncable::ModelTypeBitSet initial_sync_ended;
     66   std::string download_progress_markers[syncable::MODEL_TYPE_COUNT];
     67   for (int i = 0; i < syncable::MODEL_TYPE_COUNT; ++i) {
     68     syncable::ModelType type(syncable::ModelTypeFromInt(i));
     69     if (routing_info_.count(type) != 0) {
     70       if (dir->initial_sync_ended_for_type(type))
     71         initial_sync_ended.set(type);
     72       else
     73         is_share_useable = false;
     74       dir->GetDownloadProgressAsString(type, &download_progress_markers[i]);
     75     }
     76   }
     77 
     78   return SyncSessionSnapshot(
     79       status_controller_->syncer_status(),
     80       status_controller_->error_counters(),
     81       status_controller_->num_server_changes_remaining(),
     82       is_share_useable,
     83       initial_sync_ended,
     84       download_progress_markers,
     85       HasMoreToSync(),
     86       delegate_->IsSyncingCurrentlySilenced(),
     87       status_controller_->unsynced_handles().size(),
     88       status_controller_->TotalNumConflictingItems(),
     89       status_controller_->did_commit_items(),
     90       source_);
     91 }
     92 
     93 SyncSourceInfo SyncSession::TestAndSetSource() {
     94   SyncSourceInfo old_source = source_;
     95   source_ = SyncSourceInfo(
     96       sync_pb::GetUpdatesCallerInfo::SYNC_CYCLE_CONTINUATION,
     97       source_.types);
     98   return old_source;
     99 }
    100 
    101 bool SyncSession::HasMoreToSync() const {
    102   const StatusController* status = status_controller_.get();
    103   return ((status->commit_ids().size() < status->unsynced_handles().size()) &&
    104       status->syncer_status().num_successful_commits > 0) ||
    105       status->conflict_sets_built() ||
    106       status->conflicts_resolved();
    107       // Or, we have conflicting updates, but we're making progress on
    108       // resolving them...
    109   }
    110 
    111 }  // namespace sessions
    112 }  // namespace browser_sync
    113