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.
5 #include "components/invalidation/invalidation_notifier.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram.h"
10 #include "components/invalidation/invalidation_handler.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"
17 InvalidationNotifier::InvalidationNotifier(
18 scoped_ptr
<SyncNetworkChannel
> network_channel
,
19 const std::string
& invalidator_client_id
,
20 const UnackedInvalidationsMap
& saved_invalidations
,
21 const std::string
& invalidation_bootstrap_data
,
22 const base::WeakPtr
<InvalidationStateTracker
>& invalidation_state_tracker
,
23 scoped_refptr
<base::SingleThreadTaskRunner
>
24 invalidation_state_tracker_task_runner
,
25 const std::string
& client_info
)
27 saved_invalidations_(saved_invalidations
),
28 invalidation_state_tracker_(invalidation_state_tracker
),
29 invalidation_state_tracker_task_runner_(
30 invalidation_state_tracker_task_runner
),
31 client_info_(client_info
),
32 invalidator_client_id_(invalidator_client_id
),
33 invalidation_bootstrap_data_(invalidation_bootstrap_data
),
34 invalidation_listener_(network_channel
.Pass()) {
37 InvalidationNotifier::~InvalidationNotifier() {
38 DCHECK(CalledOnValidThread());
41 void InvalidationNotifier::RegisterHandler(InvalidationHandler
* handler
) {
42 DCHECK(CalledOnValidThread());
43 registrar_
.RegisterHandler(handler
);
46 bool InvalidationNotifier::UpdateRegisteredIds(InvalidationHandler
* handler
,
47 const ObjectIdSet
& ids
) {
48 DCHECK(CalledOnValidThread());
49 if (!registrar_
.UpdateRegisteredIds(handler
, ids
))
51 invalidation_listener_
.UpdateRegisteredIds(registrar_
.GetAllRegisteredIds());
55 void InvalidationNotifier::UnregisterHandler(InvalidationHandler
* handler
) {
56 DCHECK(CalledOnValidThread());
57 registrar_
.UnregisterHandler(handler
);
60 InvalidatorState
InvalidationNotifier::GetInvalidatorState() const {
61 DCHECK(CalledOnValidThread());
62 return registrar_
.GetInvalidatorState();
65 void InvalidationNotifier::UpdateCredentials(
66 const std::string
& email
, const std::string
& token
) {
67 if (state_
== STOPPED
) {
68 invalidation_listener_
.Start(
69 base::Bind(&invalidation::CreateInvalidationClient
),
70 invalidator_client_id_
,
72 invalidation_bootstrap_data_
,
74 invalidation_state_tracker_
,
75 invalidation_state_tracker_task_runner_
,
79 invalidation_listener_
.UpdateCredentials(email
, token
);
82 void InvalidationNotifier::RequestDetailedStatus(
83 base::Callback
<void(const base::DictionaryValue
&)> callback
) const {
84 DCHECK(CalledOnValidThread());
85 invalidation_listener_
.RequestDetailedStatus(callback
);
88 void InvalidationNotifier::OnInvalidate(
89 const ObjectIdInvalidationMap
& invalidation_map
) {
90 DCHECK(CalledOnValidThread());
91 registrar_
.DispatchInvalidationsToHandlers(invalidation_map
);
94 void InvalidationNotifier::OnInvalidatorStateChange(InvalidatorState state
) {
95 DCHECK(CalledOnValidThread());
96 registrar_
.UpdateInvalidatorState(state
);