Home | History | Annotate | Download | only in notifier
      1 // Copyright (c) 2012 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 "sync/notifier/invalidation_notifier.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/logging.h"
      9 #include "base/message_loop/message_loop_proxy.h"
     10 #include "base/metrics/histogram.h"
     11 #include "google/cacheinvalidation/include/invalidation-client-factory.h"
     12 #include "jingle/notifier/listener/push_client.h"
     13 #include "net/url_request/url_request_context.h"
     14 #include "sync/notifier/invalidation_handler.h"
     15 #include "talk/xmpp/jid.h"
     16 #include "talk/xmpp/xmppclientsettings.h"
     17 
     18 namespace syncer {
     19 
     20 InvalidationNotifier::InvalidationNotifier(
     21     scoped_ptr<notifier::PushClient> push_client,
     22     const std::string& invalidator_client_id,
     23     const UnackedInvalidationsMap& saved_invalidations,
     24     const std::string& invalidation_bootstrap_data,
     25     const WeakHandle<InvalidationStateTracker>& invalidation_state_tracker,
     26     const std::string& client_info)
     27     : state_(STOPPED),
     28       saved_invalidations_(saved_invalidations),
     29       invalidation_state_tracker_(invalidation_state_tracker),
     30       client_info_(client_info),
     31       invalidator_client_id_(invalidator_client_id),
     32       invalidation_bootstrap_data_(invalidation_bootstrap_data),
     33       invalidation_listener_(push_client.Pass()) {
     34 }
     35 
     36 InvalidationNotifier::~InvalidationNotifier() {
     37   DCHECK(CalledOnValidThread());
     38 }
     39 
     40 void InvalidationNotifier::RegisterHandler(InvalidationHandler* handler) {
     41   DCHECK(CalledOnValidThread());
     42   registrar_.RegisterHandler(handler);
     43 }
     44 
     45 void InvalidationNotifier::UpdateRegisteredIds(InvalidationHandler* handler,
     46                                                const ObjectIdSet& ids) {
     47   DCHECK(CalledOnValidThread());
     48   registrar_.UpdateRegisteredIds(handler, ids);
     49   invalidation_listener_.UpdateRegisteredIds(registrar_.GetAllRegisteredIds());
     50 }
     51 
     52 void InvalidationNotifier::UnregisterHandler(InvalidationHandler* handler) {
     53   DCHECK(CalledOnValidThread());
     54   registrar_.UnregisterHandler(handler);
     55 }
     56 
     57 InvalidatorState InvalidationNotifier::GetInvalidatorState() const {
     58   DCHECK(CalledOnValidThread());
     59   return registrar_.GetInvalidatorState();
     60 }
     61 
     62 void InvalidationNotifier::UpdateCredentials(
     63     const std::string& email, const std::string& token) {
     64   if (state_ == STOPPED) {
     65     invalidation_listener_.Start(
     66         base::Bind(&invalidation::CreateInvalidationClient),
     67         invalidator_client_id_, client_info_, invalidation_bootstrap_data_,
     68         saved_invalidations_,
     69         invalidation_state_tracker_,
     70         this);
     71     state_ = STARTED;
     72   }
     73   invalidation_listener_.UpdateCredentials(email, token);
     74 }
     75 
     76 void InvalidationNotifier::OnInvalidate(
     77     const ObjectIdInvalidationMap& invalidation_map) {
     78   DCHECK(CalledOnValidThread());
     79   registrar_.DispatchInvalidationsToHandlers(invalidation_map);
     80 }
     81 
     82 void InvalidationNotifier::OnInvalidatorStateChange(InvalidatorState state) {
     83   DCHECK(CalledOnValidThread());
     84   registrar_.UpdateInvalidatorState(state);
     85 }
     86 
     87 }  // namespace syncer
     88