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.
5 #include "chrome/browser/invalidation/fake_invalidation_service.h"
7 #include "chrome/browser/invalidation/invalidation_service_util.h"
8 #include "sync/notifier/object_id_invalidation_map.h"
10 namespace invalidation
{
12 FakeInvalidationService::FakeInvalidationService()
13 : client_id_(GenerateInvalidatorClientId()) {
14 invalidator_registrar_
.UpdateInvalidatorState(syncer::INVALIDATIONS_ENABLED
);
17 FakeInvalidationService::~FakeInvalidationService() {
20 void FakeInvalidationService::RegisterInvalidationHandler(
21 syncer::InvalidationHandler
* handler
) {
22 invalidator_registrar_
.RegisterHandler(handler
);
25 void FakeInvalidationService::UpdateRegisteredInvalidationIds(
26 syncer::InvalidationHandler
* handler
,
27 const syncer::ObjectIdSet
& ids
) {
28 invalidator_registrar_
.UpdateRegisteredIds(handler
, ids
);
31 void FakeInvalidationService::UnregisterInvalidationHandler(
32 syncer::InvalidationHandler
* handler
) {
33 invalidator_registrar_
.UnregisterHandler(handler
);
36 syncer::InvalidatorState
FakeInvalidationService::GetInvalidatorState() const {
37 return invalidator_registrar_
.GetInvalidatorState();
40 std::string
FakeInvalidationService::GetInvalidatorClientId() const {
44 void FakeInvalidationService::SetInvalidatorState(
45 syncer::InvalidatorState state
) {
46 invalidator_registrar_
.UpdateInvalidatorState(state
);
49 void 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
);
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
);
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
);
71 syncer::MockAckHandler
* FakeInvalidationService::GetMockAckHandler() {
72 return &mock_ack_handler_
;
75 } // namespace invalidation