summaryrefslogtreecommitdiff
path: root/chromium/jingle/notifier/listener/non_blocking_push_client.h
blob: b49ae03b5d8ad2102a05c133bc03d52e4552aca4 (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
// 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 JINGLE_NOTIFIER_LISTENER_NON_BLOCKING_PUSH_CLIENT_H_
#define JINGLE_NOTIFIER_LISTENER_NON_BLOCKING_PUSH_CLIENT_H_

#include "base/basictypes.h"
#include "base/callback_forward.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/threading/thread_checker.h"
#include "jingle/notifier/listener/push_client.h"
#include "jingle/notifier/listener/push_client_observer.h"

namespace base {
class SingleThreadTaskRunner;
} // namespace base

namespace notifier {

// This class implements a PushClient that doesn't block; it delegates
// to another blocking PushClient on a separate thread.
//
// This class must be used on a single thread.
class NonBlockingPushClient : public PushClient {
 public:
  // The type for a function that creates a (blocking) PushClient.
  // Will be called on the delegate task runner.
  typedef base::Callback<scoped_ptr<PushClient>()>
      CreateBlockingPushClientCallback;

  // Runs the given callback on the given task runner, and delegates
  // to that PushClient.
  explicit NonBlockingPushClient(
      const scoped_refptr<base::SingleThreadTaskRunner>& delegate_task_runner,
      const CreateBlockingPushClientCallback&
          create_blocking_push_client_callback);
  virtual ~NonBlockingPushClient();

  // PushClient implementation.
  virtual void AddObserver(PushClientObserver* observer) OVERRIDE;
  virtual void RemoveObserver(PushClientObserver* observer) OVERRIDE;
  virtual void UpdateSubscriptions(
      const SubscriptionList& subscriptions) OVERRIDE;
  virtual void UpdateCredentials(
      const std::string& email, const std::string& token) OVERRIDE;
  virtual void SendNotification(const Notification& notification) OVERRIDE;
  virtual void SendPing() OVERRIDE;

 private:
  class Core;

  void OnNotificationsEnabled();
  void OnNotificationsDisabled(NotificationsDisabledReason reason);
  void OnIncomingNotification(const Notification& notification);
  void OnPingResponse();

  base::ThreadChecker thread_checker_;
  const scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_;
  scoped_refptr<Core> core_;

  ObserverList<PushClientObserver> observers_;

  base::WeakPtrFactory<NonBlockingPushClient> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(NonBlockingPushClient);
};

}  // namespace notifier

#endif  // JINGLE_NOTIFIER_LISTENER_NON_BLOCKING_PUSH_CLIENT_H_