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 "base/macros.h" 8 #include "components/invalidation/invalidation_service_util.h" 9 #include "sync/notifier/object_id_invalidation_map.h" 10 11 namespace invalidation { 12 FakeInvalidationService()13FakeInvalidationService::FakeInvalidationService() 14 : client_id_(GenerateInvalidatorClientId()), 15 identity_provider_(&token_service_) { 16 invalidator_registrar_.UpdateInvalidatorState(syncer::INVALIDATIONS_ENABLED); 17 token_service_.set_auto_post_fetch_response_on_message_loop(true); 18 } 19 ~FakeInvalidationService()20FakeInvalidationService::~FakeInvalidationService() { 21 } 22 RegisterInvalidationHandler(syncer::InvalidationHandler * handler)23void FakeInvalidationService::RegisterInvalidationHandler( 24 syncer::InvalidationHandler* handler) { 25 invalidator_registrar_.RegisterHandler(handler); 26 } 27 UpdateRegisteredInvalidationIds(syncer::InvalidationHandler * handler,const syncer::ObjectIdSet & ids)28void FakeInvalidationService::UpdateRegisteredInvalidationIds( 29 syncer::InvalidationHandler* handler, 30 const syncer::ObjectIdSet& ids) { 31 invalidator_registrar_.UpdateRegisteredIds(handler, ids); 32 } 33 UnregisterInvalidationHandler(syncer::InvalidationHandler * handler)34void FakeInvalidationService::UnregisterInvalidationHandler( 35 syncer::InvalidationHandler* handler) { 36 invalidator_registrar_.UnregisterHandler(handler); 37 } 38 GetInvalidatorState() const39syncer::InvalidatorState FakeInvalidationService::GetInvalidatorState() const { 40 return invalidator_registrar_.GetInvalidatorState(); 41 } 42 GetInvalidatorClientId() const43std::string FakeInvalidationService::GetInvalidatorClientId() const { 44 return client_id_; 45 } 46 GetInvalidationLogger()47InvalidationLogger* FakeInvalidationService::GetInvalidationLogger() { 48 return NULL; 49 } 50 RequestDetailedStatus(base::Callback<void (const base::DictionaryValue &)> caller) const51void FakeInvalidationService::RequestDetailedStatus( 52 base::Callback<void(const base::DictionaryValue&)> caller) const { 53 base::DictionaryValue value; 54 caller.Run(value); 55 } 56 GetIdentityProvider()57IdentityProvider* FakeInvalidationService::GetIdentityProvider() { 58 return &identity_provider_; 59 } 60 SetInvalidatorState(syncer::InvalidatorState state)61void FakeInvalidationService::SetInvalidatorState( 62 syncer::InvalidatorState state) { 63 invalidator_registrar_.UpdateInvalidatorState(state); 64 } 65 EmitInvalidationForTest(const syncer::Invalidation & invalidation)66void FakeInvalidationService::EmitInvalidationForTest( 67 const syncer::Invalidation& invalidation) { 68 // This function might need to modify the invalidator, so we start by making 69 // an identical copy of it. 70 syncer::Invalidation invalidation_copy(invalidation); 71 72 // If no one is listening to this invalidation, do not send it out. 73 syncer::ObjectIdSet registered_ids = 74 invalidator_registrar_.GetAllRegisteredIds(); 75 if (registered_ids.find(invalidation.object_id()) == registered_ids.end()) { 76 mock_ack_handler_.RegisterUnsentInvalidation(&invalidation_copy); 77 return; 78 } 79 80 // Otherwise, register the invalidation with the mock_ack_handler_ and deliver 81 // it to the appropriate consumer. 82 mock_ack_handler_.RegisterInvalidation(&invalidation_copy); 83 syncer::ObjectIdInvalidationMap invalidation_map; 84 invalidation_map.Insert(invalidation_copy); 85 invalidator_registrar_.DispatchInvalidationsToHandlers(invalidation_map); 86 } 87 GetMockAckHandler()88syncer::MockAckHandler* FakeInvalidationService::GetMockAckHandler() { 89 return &mock_ack_handler_; 90 } 91 92 } // namespace invalidation 93