summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/android_invalidator_bridge.h
blob: ec1baaedb40e83fe6782b3ae77a2e72e8d618ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_GLUE_ANDROID_INVALIDATOR_BRIDGE_H_
#define CHROME_BROWSER_SYNC_GLUE_ANDROID_INVALIDATOR_BRIDGE_H_

#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/sequenced_task_runner.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "sync/notifier/invalidator.h"

class Profile;

namespace syncer {
class InvalidationHandler;
}  // namespace

namespace browser_sync {

// A bridge that converts Chrome events on the UI thread to sync
// notifications on the sync task runner.
//
// Listens to NOTIFICATION_SYNC_REFRESH_REMOTE (on the UI thread) and triggers
// each observer's OnIncomingNotification method on these notifications (on the
// sync task runner).  Android clients receive invalidations through this
// mechanism exclusively, hence the name.
class AndroidInvalidatorBridge
    : public content::NotificationObserver, syncer::Invalidator {
 public:
  // Must be created and destroyed on the UI thread.
  AndroidInvalidatorBridge(
      const Profile* profile,
      const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner);
  virtual ~AndroidInvalidatorBridge();

  // Must be called on the UI thread while the sync task runner is still
  // around.  No other member functions on the sync thread may be called after
  // this is called.
  void StopForShutdown();

  // Invalidator implementation.  Must be called on the sync task runner.
  virtual void RegisterHandler(syncer::InvalidationHandler* handler) OVERRIDE;
  virtual void UpdateRegisteredIds(syncer::InvalidationHandler* handler,
                                   const syncer::ObjectIdSet& ids) OVERRIDE;
  virtual void UnregisterHandler(syncer::InvalidationHandler* handler) OVERRIDE;
  virtual void Acknowledge(const invalidation::ObjectId& id,
                           const syncer::AckHandle& ack_handle) OVERRIDE;
  virtual syncer::InvalidatorState GetInvalidatorState() const OVERRIDE;

  // The following members of the Invalidator interface are not applicable to
  // this invalidator and are implemented as no-ops.
  virtual void UpdateCredentials(
      const std::string& email, const std::string& token) OVERRIDE;
  virtual void SendInvalidation(
      const syncer::ObjectIdInvalidationMap& invalidation_map) OVERRIDE;

  bool IsHandlerRegisteredForTest(
      syncer::InvalidationHandler* handler) const;
  syncer::ObjectIdSet GetRegisteredIdsForTest(
      syncer::InvalidationHandler* handler) const;

  // NotificationObserver implementation. Called on UI thread.
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

 private:
  // Inner class to hold all the bits used on |sync_task_runner_|.
  class Core;

  const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_;

  // Created on the UI thread, used only on |sync_task_runner_|.
  const scoped_refptr<Core> core_;

  // Used only on the UI thread.
  content::NotificationRegistrar registrar_;
};

}  // namespace browser_sync

#endif  // CHROME_BROWSER_SYNC_GLUE_ANDROID_INVALIDATOR_BRIDGE_H_