| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/invalidation/fake_invalidation_service.h" | 5 #include "chrome/browser/invalidation/fake_invalidation_service.h" |
| 6 | 6 |
| 7 #include "chrome/browser/invalidation/invalidation_service_util.h" | 7 #include "chrome/browser/invalidation/invalidation_service_util.h" |
| 8 #include "sync/notifier/object_id_invalidation_map.h" |
| 8 | 9 |
| 9 namespace invalidation { | 10 namespace invalidation { |
| 10 | 11 |
| 11 FakeInvalidationService::FakeInvalidationService() | 12 FakeInvalidationService::FakeInvalidationService() |
| 12 : client_id_(GenerateInvalidatorClientId()), | 13 : client_id_(GenerateInvalidatorClientId()) { |
| 13 received_invalid_acknowledgement_(false) { | |
| 14 invalidator_registrar_.UpdateInvalidatorState(syncer::INVALIDATIONS_ENABLED); | 14 invalidator_registrar_.UpdateInvalidatorState(syncer::INVALIDATIONS_ENABLED); |
| 15 } | 15 } |
| 16 | 16 |
| 17 FakeInvalidationService::~FakeInvalidationService() { | 17 FakeInvalidationService::~FakeInvalidationService() { |
| 18 } | 18 } |
| 19 | 19 |
| 20 void FakeInvalidationService::RegisterInvalidationHandler( | 20 void FakeInvalidationService::RegisterInvalidationHandler( |
| 21 syncer::InvalidationHandler* handler) { | 21 syncer::InvalidationHandler* handler) { |
| 22 invalidator_registrar_.RegisterHandler(handler); | 22 invalidator_registrar_.RegisterHandler(handler); |
| 23 } | 23 } |
| 24 | 24 |
| 25 void FakeInvalidationService::UpdateRegisteredInvalidationIds( | 25 void FakeInvalidationService::UpdateRegisteredInvalidationIds( |
| 26 syncer::InvalidationHandler* handler, | 26 syncer::InvalidationHandler* handler, |
| 27 const syncer::ObjectIdSet& ids) { | 27 const syncer::ObjectIdSet& ids) { |
| 28 invalidator_registrar_.UpdateRegisteredIds(handler, ids); | 28 invalidator_registrar_.UpdateRegisteredIds(handler, ids); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void FakeInvalidationService::UnregisterInvalidationHandler( | 31 void FakeInvalidationService::UnregisterInvalidationHandler( |
| 32 syncer::InvalidationHandler* handler) { | 32 syncer::InvalidationHandler* handler) { |
| 33 invalidator_registrar_.UnregisterHandler(handler); | 33 invalidator_registrar_.UnregisterHandler(handler); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void FakeInvalidationService::AcknowledgeInvalidation( | |
| 37 const invalidation::ObjectId& id, | |
| 38 const syncer::AckHandle& ack_handle) { | |
| 39 // Try to find the given handle and object id in the unacknowledged list. | |
| 40 AckHandleList::iterator handle; | |
| 41 AckHandleList::iterator begin = unacknowledged_handles_.begin(); | |
| 42 AckHandleList::iterator end = unacknowledged_handles_.end(); | |
| 43 for (handle = begin; handle != end; ++handle) | |
| 44 if (handle->first.Equals(ack_handle) && handle->second == id) | |
| 45 break; | |
| 46 if (handle == end) | |
| 47 received_invalid_acknowledgement_ = false; | |
| 48 else | |
| 49 unacknowledged_handles_.erase(handle); | |
| 50 | |
| 51 // Add to the acknowledged list. | |
| 52 acknowledged_handles_.push_back(AckHandleList::value_type(ack_handle, id)); | |
| 53 } | |
| 54 | |
| 55 syncer::InvalidatorState FakeInvalidationService::GetInvalidatorState() const { | 36 syncer::InvalidatorState FakeInvalidationService::GetInvalidatorState() const { |
| 56 return invalidator_registrar_.GetInvalidatorState(); | 37 return invalidator_registrar_.GetInvalidatorState(); |
| 57 } | 38 } |
| 58 | 39 |
| 59 std::string FakeInvalidationService::GetInvalidatorClientId() const { | 40 std::string FakeInvalidationService::GetInvalidatorClientId() const { |
| 60 return client_id_; | 41 return client_id_; |
| 61 } | 42 } |
| 62 | 43 |
| 63 void FakeInvalidationService::SetInvalidatorState( | 44 void FakeInvalidationService::SetInvalidatorState( |
| 64 syncer::InvalidatorState state) { | 45 syncer::InvalidatorState state) { |
| 65 invalidator_registrar_.UpdateInvalidatorState(state); | 46 invalidator_registrar_.UpdateInvalidatorState(state); |
| 66 } | 47 } |
| 67 | 48 |
| 68 void FakeInvalidationService::EmitInvalidationForTest( | 49 void FakeInvalidationService::EmitInvalidationForTest( |
| 69 const syncer::Invalidation& invalidation) { | 50 const syncer::Invalidation& invalidation) { |
| 51 // This function might need to modify the invalidator, so we start by making |
| 52 // an identical copy of it. |
| 53 syncer::Invalidation invalidation_copy(invalidation); |
| 54 |
| 55 // If no one is listening to this invalidation, do not send it out. |
| 56 syncer::ObjectIdSet registered_ids = |
| 57 invalidator_registrar_.GetAllRegisteredIds(); |
| 58 if (registered_ids.find(invalidation.object_id()) == registered_ids.end()) { |
| 59 mock_ack_handler_.RegisterUnsentInvalidation(&invalidation_copy); |
| 60 return; |
| 61 } |
| 62 |
| 63 // Otherwise, register the invalidation with the mock_ack_handler_ and deliver |
| 64 // it to the appropriate consumer. |
| 65 mock_ack_handler_.RegisterInvalidation(&invalidation_copy); |
| 70 syncer::ObjectIdInvalidationMap invalidation_map; | 66 syncer::ObjectIdInvalidationMap invalidation_map; |
| 71 | 67 invalidation_map.Insert(invalidation_copy); |
| 72 invalidation_map.Insert(invalidation); | |
| 73 unacknowledged_handles_.push_back( | |
| 74 AckHandleList::value_type( | |
| 75 invalidation.ack_handle(), | |
| 76 invalidation.object_id())); | |
| 77 | |
| 78 invalidator_registrar_.DispatchInvalidationsToHandlers(invalidation_map); | 68 invalidator_registrar_.DispatchInvalidationsToHandlers(invalidation_map); |
| 79 } | 69 } |
| 80 | 70 |
| 81 bool FakeInvalidationService::IsInvalidationAcknowledged( | 71 syncer::MockAckHandler* FakeInvalidationService::GetMockAckHandler() { |
| 82 const syncer::AckHandle& ack_handle) const { | 72 return &mock_ack_handler_; |
| 83 // Try to find the given handle in the acknowledged list. | |
| 84 AckHandleList::const_iterator begin = acknowledged_handles_.begin(); | |
| 85 AckHandleList::const_iterator end = acknowledged_handles_.end(); | |
| 86 for (AckHandleList::const_iterator handle = begin; handle != end; ++handle) | |
| 87 if (handle->first.Equals(ack_handle)) | |
| 88 return true; | |
| 89 return false; | |
| 90 } | 73 } |
| 91 | 74 |
| 92 } // namespace invalidation | 75 } // namespace invalidation |
| OLD | NEW |