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/invalidation/fake_invalidation_service.h" 6 7 #include "chrome/browser/invalidation/invalidation_service_util.h" 8 #include "sync/notifier/object_id_invalidation_map.h" 9 10 namespace invalidation { 11 FakeInvalidationService()12FakeInvalidationService::FakeInvalidationService() 13 : client_id_(GenerateInvalidatorClientId()) { 14 invalidator_registrar_.UpdateInvalidatorState(syncer::INVALIDATIONS_ENABLED); 15 } 16 ~FakeInvalidationService()17FakeInvalidationService::~FakeInvalidationService() { 18 } 19 RegisterInvalidationHandler(syncer::InvalidationHandler * handler)20void FakeInvalidationService::RegisterInvalidationHandler( 21 syncer::InvalidationHandler* handler) { 22 invalidator_registrar_.RegisterHandler(handler); 23 } 24 UpdateRegisteredInvalidationIds(syncer::InvalidationHandler * handler,const syncer::ObjectIdSet & ids)25void FakeInvalidationService::UpdateRegisteredInvalidationIds( 26 syncer::InvalidationHandler* handler, 27 const syncer::ObjectIdSet& ids) { 28 invalidator_registrar_.UpdateRegisteredIds(handler, ids); 29 } 30 UnregisterInvalidationHandler(syncer::InvalidationHandler * handler)31void FakeInvalidationService::UnregisterInvalidationHandler( 32 syncer::InvalidationHandler* handler) { 33 invalidator_registrar_.UnregisterHandler(handler); 34 } 35 GetInvalidatorState() const36syncer::InvalidatorState FakeInvalidationService::GetInvalidatorState() const { 37 return invalidator_registrar_.GetInvalidatorState(); 38 } 39 GetInvalidatorClientId() const40std::string FakeInvalidationService::GetInvalidatorClientId() const { 41 return client_id_; 42 } 43 SetInvalidatorState(syncer::InvalidatorState state)44void FakeInvalidationService::SetInvalidatorState( 45 syncer::InvalidatorState state) { 46 invalidator_registrar_.UpdateInvalidatorState(state); 47 } 48 EmitInvalidationForTest(const syncer::Invalidation & invalidation)49void FakeInvalidationService::EmitInvalidationForTest( 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); 66 syncer::ObjectIdInvalidationMap invalidation_map; 67 invalidation_map.Insert(invalidation_copy); 68 invalidator_registrar_.DispatchInvalidationsToHandlers(invalidation_map); 69 } 70 GetMockAckHandler()71syncer::MockAckHandler* FakeInvalidationService::GetMockAckHandler() { 72 return &mock_ack_handler_; 73 } 74 75 } // namespace invalidation 76