invalidation_service_android.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
1a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// found in the LICENSE file.
4a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
5a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "chrome/browser/invalidation/invalidation_service_android.h"
6a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
77dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
8a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "content/public/browser/notification_service.h"
9a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
10a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace invalidation {
11a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
12a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)InvalidationServiceAndroid::InvalidationServiceAndroid(Profile* profile)
13a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    : invalidator_state_(syncer::INVALIDATIONS_ENABLED) {
14a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
15a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
16a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                 content::Source<Profile>(profile));
17a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
18a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
19a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)InvalidationServiceAndroid::~InvalidationServiceAndroid() { }
20a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
21a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void InvalidationServiceAndroid::RegisterInvalidationHandler(
22a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    syncer::InvalidationHandler* handler) {
23a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
24a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_registrar_.RegisterHandler(handler);
25a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
26a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
27a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void InvalidationServiceAndroid::UpdateRegisteredInvalidationIds(
28a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    syncer::InvalidationHandler* handler,
29a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const syncer::ObjectIdSet& ids) {
30a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
31a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_registrar_.UpdateRegisteredIds(handler, ids);
32a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
33a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
34a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void InvalidationServiceAndroid::UnregisterInvalidationHandler(
35a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    syncer::InvalidationHandler* handler) {
36a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_registrar_.UnregisterHandler(handler);
38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
39a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void InvalidationServiceAndroid::AcknowledgeInvalidation(
41a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const invalidation::ObjectId& id,
42a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const syncer::AckHandle& ack_handle) {
43a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Do nothing.  The Android invalidator does not support ack tracking.
45a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
47a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)syncer::InvalidatorState
48a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)InvalidationServiceAndroid::GetInvalidatorState() const {
49a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
50a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return invalidator_state_;
51a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
52a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
53a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)std::string InvalidationServiceAndroid::GetInvalidatorClientId() const {
54a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
55a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // TODO: Return a valid ID here.  See crbug.com/172391.
56a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return "Bogus";
57a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
58a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void InvalidationServiceAndroid::Observe(
60a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    int type,
61a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const content::NotificationSource& source,
62a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const content::NotificationDetails& details) {
63a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
64a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK_EQ(type, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE);
65a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  content::Details<const syncer::ObjectIdInvalidationMap>
67a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      state_details(details);
687dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const syncer::ObjectIdInvalidationMap object_invalidation_map =
69a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      *(state_details.ptr());
70a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
71a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // An empty map implies that we should invalidate all.
72a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  const syncer::ObjectIdInvalidationMap& effective_invalidation_map =
73a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      object_invalidation_map.empty() ?
74a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      ObjectIdSetToInvalidationMap(
75a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)          invalidator_registrar_.GetAllRegisteredIds(), std::string()) :
76a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      object_invalidation_map;
77a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
78a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_registrar_.DispatchInvalidationsToHandlers(
79a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      effective_invalidation_map);
80a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
81a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
82a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void InvalidationServiceAndroid::TriggerStateChangeForTest(
83a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    syncer::InvalidatorState state) {
84a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
85a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_state_ = state;
86a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_registrar_.UpdateInvalidatorState(invalidator_state_);
87a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
88a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
89a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)} //  namespace invalidation
90