OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/notifier/registration_manager.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "chrome/browser/sync/notifier/invalidation_util.h" |
| 10 #include "chrome/browser/sync/syncable/model_type.h" |
| 11 #include "google/cacheinvalidation/invalidation-client.h" |
| 12 |
| 13 namespace sync_notifier { |
| 14 |
| 15 RegistrationManager::RegistrationManager( |
| 16 invalidation::InvalidationClient* invalidation_client) |
| 17 : invalidation_client_(invalidation_client) { |
| 18 DCHECK(invalidation_client_); |
| 19 } |
| 20 |
| 21 RegistrationManager::~RegistrationManager() { |
| 22 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 23 } |
| 24 |
| 25 bool RegistrationManager::RegisterType(syncable::ModelType model_type) { |
| 26 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 27 invalidation::ObjectId object_id; |
| 28 if (!RealModelTypeToObjectId(model_type, &object_id)) { |
| 29 LOG(ERROR) << "Invalid model type: " << model_type; |
| 30 return false; |
| 31 } |
| 32 RegistrationStatusMap::iterator it = |
| 33 registration_status_.insert( |
| 34 std::make_pair(model_type, UNREGISTERED)).first; |
| 35 if (it->second == UNREGISTERED) { |
| 36 RegisterObject(object_id, it); |
| 37 } |
| 38 return true; |
| 39 } |
| 40 |
| 41 bool RegistrationManager::IsRegistered( |
| 42 syncable::ModelType model_type) const { |
| 43 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 44 RegistrationStatusMap::const_iterator it = |
| 45 registration_status_.find(model_type); |
| 46 if (it == registration_status_.end()) { |
| 47 return false; |
| 48 } |
| 49 return it->second == REGISTERED; |
| 50 } |
| 51 |
| 52 void RegistrationManager::MarkRegistrationLost( |
| 53 syncable::ModelType model_type) { |
| 54 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 55 invalidation::ObjectId object_id; |
| 56 if (!RealModelTypeToObjectId(model_type, &object_id)) { |
| 57 LOG(ERROR) << "Invalid model type: " << model_type; |
| 58 return; |
| 59 } |
| 60 RegistrationStatusMap::iterator it = |
| 61 registration_status_.find(model_type); |
| 62 if (it == registration_status_.end()) { |
| 63 LOG(ERROR) << "Unknown model type: " |
| 64 << syncable::ModelTypeToString(model_type); |
| 65 return; |
| 66 } |
| 67 it->second = UNREGISTERED; |
| 68 RegisterObject(object_id, it); |
| 69 } |
| 70 |
| 71 void RegistrationManager::MarkAllRegistrationsLost() { |
| 72 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 73 for (RegistrationStatusMap::iterator it = |
| 74 registration_status_.begin(); |
| 75 it != registration_status_.end(); ++it) { |
| 76 invalidation::ObjectId object_id; |
| 77 if (!RealModelTypeToObjectId(it->first, &object_id)) { |
| 78 LOG(DFATAL) << "Invalid model type: " << it->first; |
| 79 continue; |
| 80 } |
| 81 it->second = UNREGISTERED; |
| 82 RegisterObject(object_id, it); |
| 83 } |
| 84 } |
| 85 |
| 86 void RegistrationManager::RegisterObject( |
| 87 const invalidation::ObjectId& object_id, |
| 88 RegistrationStatusMap::iterator it) { |
| 89 DCHECK_EQ(it->second, UNREGISTERED); |
| 90 invalidation_client_->Register( |
| 91 object_id, |
| 92 invalidation::NewPermanentCallback( |
| 93 this, &RegistrationManager::OnRegister)); |
| 94 it->second = PENDING; |
| 95 } |
| 96 |
| 97 void RegistrationManager::OnRegister( |
| 98 const invalidation::RegistrationUpdateResult& result) { |
| 99 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 100 LOG(INFO) << "OnRegister: " << RegistrationUpdateResultToString(result); |
| 101 if (result.operation().type() != |
| 102 invalidation::RegistrationUpdate::REGISTER) { |
| 103 LOG(ERROR) << "Got non-register result"; |
| 104 return; |
| 105 } |
| 106 syncable::ModelType model_type; |
| 107 if (!ObjectIdToRealModelType(result.operation().object_id(), |
| 108 &model_type)) { |
| 109 LOG(ERROR) << "Could not get model type"; |
| 110 return; |
| 111 } |
| 112 RegistrationStatusMap::iterator it = |
| 113 registration_status_.find(model_type); |
| 114 if (it == registration_status_.end()) { |
| 115 LOG(ERROR) << "Unknown model type: " << model_type; |
| 116 return; |
| 117 } |
| 118 invalidation::Status::Code code = result.status().code(); |
| 119 switch (code) { |
| 120 case invalidation::Status::SUCCESS: |
| 121 it->second = REGISTERED; |
| 122 break; |
| 123 case invalidation::Status::TRANSIENT_FAILURE: |
| 124 // TODO(akalin): Add retry logic. For now, just fall through. |
| 125 default: |
| 126 // Treat everything else as a permanent failure. |
| 127 LOG(ERROR) << "Registration failed with code: " << code; |
| 128 break; |
| 129 } |
| 130 } |
| 131 |
| 132 } // namespace sync_notifier |
OLD | NEW |