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)
76e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/callback.h"
87dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/invalidation/invalidation_controller_android.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/invalidation/object_id_invalidation_map.h"
11a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "content/public/browser/notification_service.h"
12a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
13a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace invalidation {
14a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)InvalidationServiceAndroid::InvalidationServiceAndroid(
1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    Profile* profile,
1758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    InvalidationControllerAndroid* invalidation_controller)
1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    : invalidator_state_(syncer::INVALIDATIONS_ENABLED),
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      invalidation_controller_(invalidation_controller),
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      logger_() {
21a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
2258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK(invalidation_controller);
23a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  registrar_.Add(this, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
24a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                 content::Source<Profile>(profile));
25a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
26a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
27a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)InvalidationServiceAndroid::~InvalidationServiceAndroid() { }
28a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
29a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void InvalidationServiceAndroid::RegisterInvalidationHandler(
30a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    syncer::InvalidationHandler* handler) {
31a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
32a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_registrar_.RegisterHandler(handler);
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  logger_.OnRegistration(handler->GetOwnerName());
34a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
35a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
36a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void InvalidationServiceAndroid::UpdateRegisteredInvalidationIds(
37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    syncer::InvalidationHandler* handler,
38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const syncer::ObjectIdSet& ids) {
39a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_registrar_.UpdateRegisteredIds(handler, ids);
4158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  invalidation_controller_->SetRegisteredObjectIds(
4258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      invalidator_registrar_.GetAllRegisteredIds());
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  logger_.OnUpdateIds(invalidator_registrar_.GetSanitizedHandlersIdsMap());
44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
45a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void InvalidationServiceAndroid::UnregisterInvalidationHandler(
47a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    syncer::InvalidationHandler* handler) {
48a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
49a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_registrar_.UnregisterHandler(handler);
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  logger_.OnUnregistration(handler->GetOwnerName());
51a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
52a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
53a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)syncer::InvalidatorState
54a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)InvalidationServiceAndroid::GetInvalidatorState() const {
55a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
56a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return invalidator_state_;
57a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
58a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)std::string InvalidationServiceAndroid::GetInvalidatorClientId() const {
60a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return invalidation_controller_->GetInvalidatorClientId();
62a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
63a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)InvalidationLogger* InvalidationServiceAndroid::GetInvalidationLogger() {
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return &logger_;
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void InvalidationServiceAndroid::RequestDetailedStatus(
69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    base::Callback<void(const base::DictionaryValue&)> return_callback) const {
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
720529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochIdentityProvider* InvalidationServiceAndroid::GetIdentityProvider() {
7323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  return NULL;
7423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
7523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
76a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void InvalidationServiceAndroid::Observe(
77a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    int type,
78a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const content::NotificationSource& source,
79a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const content::NotificationDetails& details) {
80a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
81a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK_EQ(type, chrome::NOTIFICATION_SYNC_REFRESH_REMOTE);
82a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  content::Details<const syncer::ObjectIdInvalidationMap>
84a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      state_details(details);
857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  const syncer::ObjectIdInvalidationMap object_invalidation_map =
86a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      *(state_details.ptr());
87a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
88a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // An empty map implies that we should invalidate all.
89a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  const syncer::ObjectIdInvalidationMap& effective_invalidation_map =
904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      object_invalidation_map.Empty() ?
914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      syncer::ObjectIdInvalidationMap::InvalidateAll(
924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          invalidator_registrar_.GetAllRegisteredIds()) :
93a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      object_invalidation_map;
94a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
95a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_registrar_.DispatchInvalidationsToHandlers(
96a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      effective_invalidation_map);
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  logger_.OnInvalidation(effective_invalidation_map);
98a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
99a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
100a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void InvalidationServiceAndroid::TriggerStateChangeForTest(
101a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    syncer::InvalidatorState state) {
102a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(CalledOnValidThread());
103a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_state_ = state;
104a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  invalidator_registrar_.UpdateInvalidatorState(invalidator_state_);
105a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
106a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
107a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)} //  namespace invalidation
108