Home | History | Annotate | Download | only in sync_driver
      1 // Copyright 2014 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_driver/data_type_manager.h"
      6 #include "components/sync_driver/data_type_status_table.h"
      7 
      8 namespace sync_driver {
      9 
     10 namespace {
     11 
     12 syncer::ModelTypeSet GetTypesFromErrorMap(
     13     const DataTypeStatusTable::TypeErrorMap& errors) {
     14   syncer::ModelTypeSet result;
     15   for (DataTypeStatusTable::TypeErrorMap::const_iterator it = errors.begin();
     16        it != errors.end(); ++it) {
     17     DCHECK(!result.Has(it->first));
     18     result.Put(it->first);
     19   }
     20   return result;
     21 }
     22 
     23 }  // namespace
     24 
     25 DataTypeStatusTable::DataTypeStatusTable() {
     26 }
     27 
     28 DataTypeStatusTable::~DataTypeStatusTable() {
     29 }
     30 
     31 bool DataTypeStatusTable::UpdateFailedDataTypes(const TypeErrorMap& errors) {
     32   if (errors.empty())
     33     return false;
     34 
     35   DVLOG(1) << "Setting " << errors.size() << " new failed types.";
     36 
     37   for (TypeErrorMap::const_iterator iter = errors.begin(); iter != errors.end();
     38        ++iter) {
     39     syncer::SyncError::ErrorType failure_type = iter->second.error_type();
     40     switch (failure_type) {
     41       case syncer::SyncError::UNSET:
     42         NOTREACHED();
     43         break;
     44       case syncer::SyncError::UNRECOVERABLE_ERROR:
     45         unrecoverable_errors_.insert(*iter);
     46         break;
     47       case syncer::SyncError::DATATYPE_ERROR:
     48       case syncer::SyncError::DATATYPE_POLICY_ERROR:
     49         data_type_errors_.insert(*iter);
     50         break;
     51       case syncer::SyncError::CRYPTO_ERROR:
     52         crypto_errors_.insert(*iter);
     53         break;
     54       case syncer::SyncError::PERSISTENCE_ERROR:
     55         persistence_errors_.insert(*iter);
     56         break;
     57       case syncer::SyncError::UNREADY_ERROR:
     58         unready_errors_.insert(*iter);
     59         break;
     60     }
     61   }
     62   return true;
     63 }
     64 
     65 void DataTypeStatusTable::Reset() {
     66   DVLOG(1) << "Resetting data type errors.";
     67   unrecoverable_errors_.clear();
     68   data_type_errors_.clear();
     69   crypto_errors_.clear();
     70   persistence_errors_.clear();
     71   unready_errors_.clear();
     72 }
     73 
     74 void DataTypeStatusTable::ResetCryptoErrors() {
     75   crypto_errors_.clear();
     76 }
     77 
     78 void DataTypeStatusTable::ResetPersistenceErrorsFrom(
     79     syncer::ModelTypeSet purged_types) {
     80   for (syncer::ModelTypeSet::Iterator iter = purged_types.First(); iter.Good();
     81        iter.Inc()) {
     82     persistence_errors_.erase(iter.Get());
     83   }
     84 }
     85 
     86 bool DataTypeStatusTable::ResetDataTypeErrorFor(syncer::ModelType type) {
     87   return data_type_errors_.erase(type) > 0;
     88 }
     89 
     90 bool DataTypeStatusTable::ResetUnreadyErrorFor(syncer::ModelType type) {
     91   return unready_errors_.erase(type) > 0;
     92 }
     93 
     94 DataTypeStatusTable::TypeErrorMap DataTypeStatusTable::GetAllErrors()
     95     const {
     96   TypeErrorMap result;
     97   result.insert(data_type_errors_.begin(), data_type_errors_.end());
     98   result.insert(crypto_errors_.begin(), crypto_errors_.end());
     99   result.insert(persistence_errors_.begin(), persistence_errors_.end());
    100   result.insert(unready_errors_.begin(), unready_errors_.end());
    101   result.insert(unrecoverable_errors_.begin(), unrecoverable_errors_.end());
    102   return result;
    103 }
    104 
    105 syncer::ModelTypeSet DataTypeStatusTable::GetFailedTypes() const {
    106   syncer::ModelTypeSet result = GetFatalErrorTypes();
    107   result.PutAll(GetCryptoErrorTypes());
    108   result.PutAll(GetUnreadyErrorTypes());
    109   return result;
    110 }
    111 
    112 syncer::ModelTypeSet DataTypeStatusTable::GetFatalErrorTypes()
    113     const {
    114   syncer::ModelTypeSet result;
    115   result.PutAll(GetTypesFromErrorMap(data_type_errors_));
    116   result.PutAll(GetTypesFromErrorMap(unrecoverable_errors_));
    117   return result;
    118 }
    119 
    120 syncer::ModelTypeSet DataTypeStatusTable::GetCryptoErrorTypes() const {
    121   syncer::ModelTypeSet result = GetTypesFromErrorMap(crypto_errors_);
    122   return result;
    123 }
    124 
    125 syncer::ModelTypeSet DataTypeStatusTable::GetPersistenceErrorTypes() const {
    126   syncer::ModelTypeSet result = GetTypesFromErrorMap(persistence_errors_);
    127   return result;
    128 }
    129 
    130 syncer::ModelTypeSet DataTypeStatusTable::GetUnreadyErrorTypes() const {
    131   syncer::ModelTypeSet result = GetTypesFromErrorMap(unready_errors_);
    132   return result;
    133 }
    134 
    135 syncer::ModelTypeSet DataTypeStatusTable::GetUnrecoverableErrorTypes()
    136     const {
    137   syncer::ModelTypeSet result = GetTypesFromErrorMap(unrecoverable_errors_);
    138   return result;
    139 }
    140 
    141 syncer::SyncError DataTypeStatusTable::GetUnrecoverableError() const {
    142   // Just return the first one. It is assumed all the unrecoverable errors
    143   // have the same cause. The others are just tracked to know which types
    144   // were involved.
    145   return (unrecoverable_errors_.empty()
    146               ? syncer::SyncError()
    147               : unrecoverable_errors_.begin()->second);
    148 }
    149 
    150 bool DataTypeStatusTable::AnyFailedDataType() const {
    151   // Note: persistence errors are not failed types. They just trigger automatic
    152   // unapply + getupdates, at which point they are associated like normal.
    153   return unrecoverable_errors_.empty() ||
    154          !data_type_errors_.empty() ||
    155          !crypto_errors_.empty();
    156 }
    157 
    158 }  // namespace sync_driver
    159