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