1 // Copyright (c) 2012 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.
5 #ifndef JINGLE_NOTIFIER_LISTENER_NON_BLOCKING_PUSH_CLIENT_H_
6 #define JINGLE_NOTIFIER_LISTENER_NON_BLOCKING_PUSH_CLIENT_H_
8 #include "base/basictypes.h"
9 #include "base/callback_forward.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/threading/thread_checker.h"
16 #include "jingle/notifier/listener/push_client.h"
17 #include "jingle/notifier/listener/push_client_observer.h"
20 class SingleThreadTaskRunner;
25 // This class implements a PushClient that doesn't block; it delegates
26 // to another blocking PushClient on a separate thread.
28 // This class must be used on a single thread.
29 class NonBlockingPushClient : public PushClient {
31 // The type for a function that creates a (blocking) PushClient.
32 // Will be called on the delegate task runner.
33 typedef base::Callback<scoped_ptr<PushClient>()>
34 CreateBlockingPushClientCallback;
36 // Runs the given callback on the given task runner, and delegates
37 // to that PushClient.
38 explicit NonBlockingPushClient(
39 const scoped_refptr<base::SingleThreadTaskRunner>& delegate_task_runner,
40 const CreateBlockingPushClientCallback&
41 create_blocking_push_client_callback);
42 virtual ~NonBlockingPushClient();
44 // PushClient implementation.
45 virtual void AddObserver(PushClientObserver* observer) OVERRIDE;
46 virtual void RemoveObserver(PushClientObserver* observer) OVERRIDE;
47 virtual void UpdateSubscriptions(
48 const SubscriptionList& subscriptions) OVERRIDE;
49 virtual void UpdateCredentials(
50 const std::string& email, const std::string& token) OVERRIDE;
51 virtual void SendNotification(const Notification& notification) OVERRIDE;
52 virtual void SendPing() OVERRIDE;
57 void OnNotificationsEnabled();
58 void OnNotificationsDisabled(NotificationsDisabledReason reason);
59 void OnIncomingNotification(const Notification& notification);
60 void OnPingResponse();
62 base::ThreadChecker thread_checker_;
63 base::WeakPtrFactory<NonBlockingPushClient> weak_ptr_factory_;
64 const scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_;
65 const scoped_refptr<Core> core_;
67 ObserverList<PushClientObserver> observers_;
69 DISALLOW_COPY_AND_ASSIGN(NonBlockingPushClient);
72 } // namespace notifier
74 #endif // JINGLE_NOTIFIER_LISTENER_NON_BLOCKING_PUSH_CLIENT_H_