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 "base/macros.h"
8 #include "components/invalidation/invalidation_service_util.h"
9 #include "sync/notifier/object_id_invalidation_map.h"
11 namespace invalidation
{
13 FakeInvalidationService::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);
20 FakeInvalidationService::~FakeInvalidationService() {
24 KeyedService
* FakeInvalidationService::Build(content::BrowserContext
* context
) {
25 return new FakeInvalidationService();
28 void FakeInvalidationService::RegisterInvalidationHandler(
29 syncer::InvalidationHandler
* handler
) {
30 invalidator_registrar_
.RegisterHandler(handler
);
33 void FakeInvalidationService::UpdateRegisteredInvalidationIds(
34 syncer::InvalidationHandler
* handler
,
35 const syncer::ObjectIdSet
& ids
) {
36 invalidator_registrar_
.UpdateRegisteredIds(handler
, ids
);
39 void FakeInvalidationService::UnregisterInvalidationHandler(
40 syncer::InvalidationHandler
* handler
) {
41 invalidator_registrar_
.UnregisterHandler(handler
);
44 syncer::InvalidatorState
FakeInvalidationService::GetInvalidatorState() const {
45 return invalidator_registrar_
.GetInvalidatorState();
48 std::string
FakeInvalidationService::GetInvalidatorClientId() const {
52 InvalidationLogger
* FakeInvalidationService::GetInvalidationLogger() {
56 void FakeInvalidationService::RequestDetailedStatus(
57 base::Callback
<void(const base::DictionaryValue
&)> caller
) const {
58 base::DictionaryValue value
;
62 IdentityProvider
* FakeInvalidationService::GetIdentityProvider() {
63 return &identity_provider_
;
66 void FakeInvalidationService::SetInvalidatorState(
67 syncer::InvalidatorState state
) {
68 invalidator_registrar_
.UpdateInvalidatorState(state
);
71 void FakeInvalidationService::EmitInvalidationForTest(
72 const syncer::Invalidation
& invalidation
) {
73 // This function might need to modify the invalidator, so we start by making
74 // an identical copy of it.
75 syncer::Invalidation
invalidation_copy(invalidation
);
77 // If no one is listening to this invalidation, do not send it out.
78 syncer::ObjectIdSet registered_ids
=
79 invalidator_registrar_
.GetAllRegisteredIds();
80 if (registered_ids
.find(invalidation
.object_id()) == registered_ids
.end()) {
81 mock_ack_handler_
.RegisterUnsentInvalidation(&invalidation_copy
);
85 // Otherwise, register the invalidation with the mock_ack_handler_ and deliver
86 // it to the appropriate consumer.
87 mock_ack_handler_
.RegisterInvalidation(&invalidation_copy
);
88 syncer::ObjectIdInvalidationMap invalidation_map
;
89 invalidation_map
.Insert(invalidation_copy
);
90 invalidator_registrar_
.DispatchInvalidationsToHandlers(invalidation_map
);
93 syncer::MockAckHandler
* FakeInvalidationService::GetMockAckHandler() {
94 return &mock_ack_handler_
;
97 } // namespace invalidation