Home | History | Annotate | Download | only in glue
      1 // Copyright 2013 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/glue/data_type_manager.h"
      6 #include "chrome/browser/sync/glue/failed_data_types_handler.h"
      7 
      8 using browser_sync::DataTypeManager;
      9 
     10 namespace browser_sync {
     11 
     12 namespace {
     13 
     14 syncer::ModelTypeSet GetTypesFromErrorMap(
     15     const FailedDataTypesHandler::TypeErrorMap& errors) {
     16   syncer::ModelTypeSet result;
     17   for (FailedDataTypesHandler::TypeErrorMap::const_iterator it = errors.begin();
     18        it != errors.end(); ++it) {
     19     DCHECK(!result.Has(it->first));
     20     result.Put(it->first);
     21   }
     22   return result;
     23 }
     24 
     25 }  // namespace
     26 
     27 FailedDataTypesHandler::FailedDataTypesHandler() {
     28 }
     29 
     30 FailedDataTypesHandler::~FailedDataTypesHandler() {
     31 }
     32 
     33 bool FailedDataTypesHandler::UpdateFailedDataTypes(const TypeErrorMap& errors) {
     34   if (errors.empty())
     35     return false;
     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::UNRECOVERABLE_ERROR:
     42       case syncer::SyncError::DATATYPE_ERROR:
     43         fatal_errors_.insert(*iter);
     44         break;
     45       case syncer::SyncError::CRYPTO_ERROR:
     46         crypto_errors_.insert(*iter);
     47         break;
     48       case syncer::SyncError::PERSISTENCE_ERROR:
     49         persistence_errors_.insert(*iter);
     50         break;
     51       default:
     52         NOTREACHED();
     53     }
     54   }
     55   return true;
     56 }
     57 
     58 void FailedDataTypesHandler::Reset() {
     59   fatal_errors_.clear();
     60   crypto_errors_.clear();
     61   persistence_errors_.clear();
     62 }
     63 
     64 void FailedDataTypesHandler::ResetCryptoErrors() {
     65   crypto_errors_.clear();
     66 }
     67 
     68 void FailedDataTypesHandler::ResetPersistenceErrorsFrom(
     69     syncer::ModelTypeSet purged_types) {
     70   for (syncer::ModelTypeSet::Iterator iter = purged_types.First(); iter.Good();
     71        iter.Inc()) {
     72     persistence_errors_.erase(iter.Get());
     73   }
     74 }
     75 
     76 FailedDataTypesHandler::TypeErrorMap FailedDataTypesHandler::GetAllErrors()
     77     const {
     78   TypeErrorMap result;
     79 
     80   if (AnyFailedDataType()) {
     81     result = fatal_errors_;
     82     result.insert(crypto_errors_.begin(), crypto_errors_.end());
     83     result.insert(persistence_errors_.begin(), persistence_errors_.end());
     84   }
     85   return result;
     86 }
     87 
     88 syncer::ModelTypeSet FailedDataTypesHandler::GetFailedTypes() const {
     89   syncer::ModelTypeSet result = GetFatalErrorTypes();
     90   result.PutAll(GetCryptoErrorTypes());
     91   return result;
     92 }
     93 
     94 syncer::ModelTypeSet FailedDataTypesHandler::GetFatalErrorTypes() const {
     95   return GetTypesFromErrorMap(fatal_errors_);;
     96 }
     97 
     98 syncer::ModelTypeSet FailedDataTypesHandler::GetCryptoErrorTypes() const {
     99   syncer::ModelTypeSet result = GetTypesFromErrorMap(crypto_errors_);
    100   return result;
    101 }
    102 
    103 syncer::ModelTypeSet FailedDataTypesHandler::GetPersistenceErrorTypes() const {
    104   syncer::ModelTypeSet result = GetTypesFromErrorMap(persistence_errors_);
    105   return result;
    106 }
    107 
    108 bool FailedDataTypesHandler::AnyFailedDataType() const {
    109   // Note: persistence errors are not failed types. They just trigger automatic
    110   // unapply + getupdates, at which point they are associated like normal.
    111   return !fatal_errors_.empty() || !crypto_errors_.empty();
    112 }
    113 
    114 }  // namespace browser_sync
    115