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
InvalidationServiceAndroid(Profile * profile,InvalidationControllerAndroid * invalidation_controller)14 InvalidationServiceAndroid::InvalidationServiceAndroid(
15 Profile* profile,
16 InvalidationControllerAndroid* invalidation_controller)
17 : invalidator_state_(syncer::INVALIDATIONS_ENABLED),
18 invalidation_controller_(invalidation_controller) {
19 DCHECK(CalledOnValidThread());
20 DCHECK(invalidation_controller);
21 registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
22 content::Source<Profile>(profile));
23 }
24
~InvalidationServiceAndroid()25 InvalidationServiceAndroid::~InvalidationServiceAndroid() { }
26
RegisterInvalidationHandler(syncer::InvalidationHandler * handler)27 void InvalidationServiceAndroid::RegisterInvalidationHandler(
28 syncer::InvalidationHandler* handler) {
29 DCHECK(CalledOnValidThread());
30 invalidator_registrar_.RegisterHandler(handler);
31 }
32
UpdateRegisteredInvalidationIds(syncer::InvalidationHandler * handler,const syncer::ObjectIdSet & ids)33 void InvalidationServiceAndroid::UpdateRegisteredInvalidationIds(
34 syncer::InvalidationHandler* handler,
35 const syncer::ObjectIdSet& ids) {
36 DCHECK(CalledOnValidThread());
37 invalidator_registrar_.UpdateRegisteredIds(handler, ids);
38 invalidation_controller_->SetRegisteredObjectIds(
39 invalidator_registrar_.GetAllRegisteredIds());
40 }
41
UnregisterInvalidationHandler(syncer::InvalidationHandler * handler)42 void InvalidationServiceAndroid::UnregisterInvalidationHandler(
43 syncer::InvalidationHandler* handler) {
44 DCHECK(CalledOnValidThread());
45 invalidator_registrar_.UnregisterHandler(handler);
46 }
47
48 syncer::InvalidatorState
GetInvalidatorState() const49 InvalidationServiceAndroid::GetInvalidatorState() const {
50 DCHECK(CalledOnValidThread());
51 return invalidator_state_;
52 }
53
GetInvalidatorClientId() const54 std::string InvalidationServiceAndroid::GetInvalidatorClientId() const {
55 DCHECK(CalledOnValidThread());
56 return invalidation_controller_->GetInvalidatorClientId();
57 }
58
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)59 void InvalidationServiceAndroid::Observe(
60 int type,
61 const content::NotificationSource& source,
62 const content::NotificationDetails& details) {
63 DCHECK(CalledOnValidThread());
64 DCHECK_EQ(type, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE);
65
66 content::Details<const syncer::ObjectIdInvalidationMap>
67 state_details(details);
68 const syncer::ObjectIdInvalidationMap object_invalidation_map =
69 *(state_details.ptr());
70
71 // An empty map implies that we should invalidate all.
72 const syncer::ObjectIdInvalidationMap& effective_invalidation_map =
73 object_invalidation_map.Empty() ?
74 syncer::ObjectIdInvalidationMap::InvalidateAll(
75 invalidator_registrar_.GetAllRegisteredIds()) :
76 object_invalidation_map;
77
78 invalidator_registrar_.DispatchInvalidationsToHandlers(
79 effective_invalidation_map);
80 }
81
TriggerStateChangeForTest(syncer::InvalidatorState state)82 void InvalidationServiceAndroid::TriggerStateChangeForTest(
83 syncer::InvalidatorState state) {
84 DCHECK(CalledOnValidThread());
85 invalidator_state_ = state;
86 invalidator_registrar_.UpdateInvalidatorState(invalidator_state_);
87 }
88
89 } // namespace invalidation
90