Home | History | Annotate | Download | only in invalidation
      1 // Copyright (c) 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/invalidation_service_android.h"
      6 
      7 #include "chrome/browser/chrome_notification_types.h"
      8 #include "chrome/browser/invalidation/invalidation_controller_android.h"
      9 #include "content/public/browser/notification_service.h"
     10 #include "sync/notifier/object_id_invalidation_map.h"
     11 
     12 namespace invalidation {
     13 
     14 InvalidationServiceAndroid::InvalidationServiceAndroid(
     15     Profile* profile,
     16     InvalidationControllerAndroid* invalidation_controller)
     17     : invalidator_state_(syncer::INVALIDATIONS_ENABLED),
     18       invalidation_controller_(invalidation_controller),
     19       logger_() {
     20   DCHECK(CalledOnValidThread());
     21   DCHECK(invalidation_controller);
     22   registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
     23                  content::Source<Profile>(profile));
     24 }
     25 
     26 InvalidationServiceAndroid::~InvalidationServiceAndroid() { }
     27 
     28 void InvalidationServiceAndroid::RegisterInvalidationHandler(
     29     syncer::InvalidationHandler* handler) {
     30   DCHECK(CalledOnValidThread());
     31   invalidator_registrar_.RegisterHandler(handler);
     32   logger_.OnRegistration(handler->GetOwnerName());
     33 }
     34 
     35 void InvalidationServiceAndroid::UpdateRegisteredInvalidationIds(
     36     syncer::InvalidationHandler* handler,
     37     const syncer::ObjectIdSet& ids) {
     38   DCHECK(CalledOnValidThread());
     39   invalidator_registrar_.UpdateRegisteredIds(handler, ids);
     40   invalidation_controller_->SetRegisteredObjectIds(
     41       invalidator_registrar_.GetAllRegisteredIds());
     42   logger_.OnUpdateIds(invalidator_registrar_.GetSanitizedHandlersIdsMap());
     43 }
     44 
     45 void InvalidationServiceAndroid::UnregisterInvalidationHandler(
     46     syncer::InvalidationHandler* handler) {
     47   DCHECK(CalledOnValidThread());
     48   invalidator_registrar_.UnregisterHandler(handler);
     49   logger_.OnUnregistration(handler->GetOwnerName());
     50 }
     51 
     52 syncer::InvalidatorState
     53 InvalidationServiceAndroid::GetInvalidatorState() const {
     54   DCHECK(CalledOnValidThread());
     55   return invalidator_state_;
     56 }
     57 
     58 std::string InvalidationServiceAndroid::GetInvalidatorClientId() const {
     59   DCHECK(CalledOnValidThread());
     60   return invalidation_controller_->GetInvalidatorClientId();
     61 }
     62 
     63 InvalidationLogger* InvalidationServiceAndroid::GetInvalidationLogger() {
     64   return &logger_;
     65 }
     66 
     67 void InvalidationServiceAndroid::RequestDetailedStatus(
     68     base::Callback<void(const base::DictionaryValue&)> return_callback) const {
     69 }
     70 
     71 IdentityProvider* InvalidationServiceAndroid::GetIdentityProvider() {
     72   return NULL;
     73 }
     74 
     75 void InvalidationServiceAndroid::Observe(
     76     int type,
     77     const content::NotificationSource& source,
     78     const content::NotificationDetails& details) {
     79   DCHECK(CalledOnValidThread());
     80   DCHECK_EQ(type, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE);
     81 
     82   content::Details<const syncer::ObjectIdInvalidationMap>
     83       state_details(details);
     84   const syncer::ObjectIdInvalidationMap object_invalidation_map =
     85       *(state_details.ptr());
     86 
     87   // An empty map implies that we should invalidate all.
     88   const syncer::ObjectIdInvalidationMap& effective_invalidation_map =
     89       object_invalidation_map.Empty() ?
     90       syncer::ObjectIdInvalidationMap::InvalidateAll(
     91           invalidator_registrar_.GetAllRegisteredIds()) :
     92       object_invalidation_map;
     93 
     94   invalidator_registrar_.DispatchInvalidationsToHandlers(
     95       effective_invalidation_map);
     96   logger_.OnInvalidation(effective_invalidation_map);
     97 }
     98 
     99 void InvalidationServiceAndroid::TriggerStateChangeForTest(
    100     syncer::InvalidatorState state) {
    101   DCHECK(CalledOnValidThread());
    102   invalidator_state_ = state;
    103   invalidator_registrar_.UpdateInvalidatorState(invalidator_state_);
    104 }
    105 
    106 } //  namespace invalidation
    107