1 // Copyright 2014 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/sync/test/integration/fake_server_invalidation_service.h" 6 7 #include <string> 8 9 #include "base/macros.h" 10 #include "chrome/browser/sync/glue/invalidation_helper.h" 11 #include "components/invalidation/invalidation.h" 12 #include "components/invalidation/invalidation_service_util.h" 13 #include "components/invalidation/object_id_invalidation_map.h" 14 #include "sync/internal_api/public/base/model_type.h" 15 16 namespace fake_server { 17 FakeServerInvalidationService()18FakeServerInvalidationService::FakeServerInvalidationService() 19 : client_id_(invalidation::GenerateInvalidatorClientId()), 20 self_notify_(true), 21 identity_provider_(&token_service_) { 22 invalidator_registrar_.UpdateInvalidatorState(syncer::INVALIDATIONS_ENABLED); 23 } 24 ~FakeServerInvalidationService()25FakeServerInvalidationService::~FakeServerInvalidationService() { 26 } 27 RegisterInvalidationHandler(syncer::InvalidationHandler * handler)28void FakeServerInvalidationService::RegisterInvalidationHandler( 29 syncer::InvalidationHandler* handler) { 30 invalidator_registrar_.RegisterHandler(handler); 31 } 32 UpdateRegisteredInvalidationIds(syncer::InvalidationHandler * handler,const syncer::ObjectIdSet & ids)33void FakeServerInvalidationService::UpdateRegisteredInvalidationIds( 34 syncer::InvalidationHandler* handler, 35 const syncer::ObjectIdSet& ids) { 36 invalidator_registrar_.UpdateRegisteredIds(handler, ids); 37 } 38 UnregisterInvalidationHandler(syncer::InvalidationHandler * handler)39void FakeServerInvalidationService::UnregisterInvalidationHandler( 40 syncer::InvalidationHandler* handler) { 41 invalidator_registrar_.UnregisterHandler(handler); 42 } 43 GetInvalidatorState() const44syncer::InvalidatorState FakeServerInvalidationService::GetInvalidatorState() 45 const { 46 return invalidator_registrar_.GetInvalidatorState(); 47 } 48 GetInvalidatorClientId() const49std::string FakeServerInvalidationService::GetInvalidatorClientId() const { 50 return client_id_; 51 } 52 53 invalidation::InvalidationLogger* GetInvalidationLogger()54FakeServerInvalidationService::GetInvalidationLogger() { 55 return NULL; 56 } 57 RequestDetailedStatus(base::Callback<void (const base::DictionaryValue &)> caller) const58void FakeServerInvalidationService::RequestDetailedStatus( 59 base::Callback<void(const base::DictionaryValue&)> caller) const { 60 base::DictionaryValue value; 61 caller.Run(value); 62 } 63 GetIdentityProvider()64IdentityProvider* FakeServerInvalidationService::GetIdentityProvider() { 65 return &identity_provider_; 66 } 67 EnableSelfNotifications()68void FakeServerInvalidationService::EnableSelfNotifications() { 69 self_notify_ = true; 70 } 71 DisableSelfNotifications()72void FakeServerInvalidationService::DisableSelfNotifications() { 73 self_notify_ = false; 74 } 75 OnCommit(const std::string & committer_id,syncer::ModelTypeSet committed_model_types)76void FakeServerInvalidationService::OnCommit( 77 const std::string& committer_id, 78 syncer::ModelTypeSet committed_model_types) { 79 syncer::ObjectIdSet object_ids = syncer::ModelTypeSetToObjectIdSet( 80 committed_model_types); 81 syncer::ObjectIdInvalidationMap invalidation_map; 82 for (syncer::ObjectIdSet::const_iterator it = object_ids.begin(); 83 it != object_ids.end(); ++it) { 84 // TODO(pvalenzuela): Create more refined invalidations instead of 85 // invalidating all items of a given type. 86 87 if (self_notify_ || client_id_ != committer_id) { 88 invalidation_map.Insert(syncer::Invalidation::InitUnknownVersion(*it)); 89 } 90 } 91 invalidator_registrar_.DispatchInvalidationsToHandlers(invalidation_map); 92 } 93 94 } // namespace fake_server 95