OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef JINGLE_NOTIFIER_LISTENER_PUSH_CLIENT_H_ | 5 #ifndef JINGLE_NOTIFIER_LISTENER_PUSH_CLIENT_H_ |
6 #define JINGLE_NOTIFIER_LISTENER_PUSH_CLIENT_H_ | 6 #define JINGLE_NOTIFIER_LISTENER_PUSH_CLIENT_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | |
10 | 9 |
11 #include "base/basictypes.h" | 10 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "jingle/notifier/base/notifier_options.h" | |
15 #include "jingle/notifier/listener/notification_defines.h" | 11 #include "jingle/notifier/listener/notification_defines.h" |
16 | 12 |
17 namespace base { | |
18 class MessageLoopProxy; | |
19 } // namespace base | |
20 | |
21 namespace buzz { | |
22 class XmppTaskParentInterface; | |
23 } // namespace buzz | |
24 | |
25 namespace notifier { | 13 namespace notifier { |
26 | 14 |
27 // This class implements a client for the XMPP google:push protocol. | 15 struct NotifierOptions; |
28 // | 16 class PushClientObserver; |
29 // This class must be used on a single thread. | 17 |
18 // A PushClient is an interface for classes that implement a push | |
19 // mechanism, where a client can push notifications to and receive | |
20 // notifications from other clients. | |
30 class PushClient { | 21 class PushClient { |
31 public: | 22 public: |
32 // An Observer is sent messages whenever a notification is received | 23 virtual ~PushClient(); |
33 // or when the state of the push client changes. | |
34 class Observer { | |
35 public: | |
36 // Called when the state of the push client changes. If | |
37 // |notifications_enabled| is true, that means notifications can | |
38 // be sent and received freely. If it is false, that means no | |
39 // notifications can be sent or received. | |
40 virtual void OnNotificationStateChange(bool notifications_enabled) = 0; | |
41 | 24 |
42 // Called when a notification is received. The details of the | 25 // If the current thread is the IO thread (according to |
43 // notification are in |notification|. | 26 // |notifier_options|) returns a blocking PushClient implementation. |
44 virtual void OnIncomingNotification(const Notification& notification) = 0; | 27 // Otherwise, returns a non-blocking PushClient implementation that |
28 // delegates to the IO thread. | |
29 static scoped_ptr<PushClient> CreateForCurrentThread( | |
rlarocque
2012/05/18 23:01:51
What's the use case for this? Shouldn't the calle
akalin
2012/05/18 23:21:57
I thought it was a more convenient API, but you're
| |
30 const NotifierOptions& notifier_options); | |
45 | 31 |
46 protected: | 32 // Manage the list of observers for incoming notifications. |
47 virtual ~Observer(); | 33 virtual void AddObserver(PushClientObserver* observer) = 0; |
48 }; | 34 virtual void RemoveObserver(PushClientObserver* observer) = 0; |
49 | 35 |
50 explicit PushClient(const NotifierOptions& notifier_options); | 36 // Implementors are required to have this take effect only on the |
51 ~PushClient(); | 37 // next (re-)connection. Therefore, clients should call this before |
52 | 38 // UpdateCredentials(). |
53 void AddObserver(Observer* observer); | 39 virtual void UpdateSubscriptions(const SubscriptionList& subscriptions) = 0; |
54 void RemoveObserver(Observer* observer); | |
55 | |
56 // Takes effect only on the next (re-)connection. Therefore, you | |
57 // probably want to call this before UpdateCredentials(). | |
58 void UpdateSubscriptions(const SubscriptionList& subscriptions); | |
59 | 40 |
60 // If not connected, connects with the given credentials. If | 41 // If not connected, connects with the given credentials. If |
61 // already connected, the next connection attempt will use the given | 42 // already connected, the next connection attempt will use the given |
62 // credentials. | 43 // credentials. |
63 void UpdateCredentials(const std::string& email, const std::string& token); | 44 virtual void UpdateCredentials( |
45 const std::string& email, const std::string& token) = 0; | |
64 | 46 |
65 // Sends a notification. Can be called when notifications are | 47 // Sends a notification (with no reliability guarantees). |
66 // disabled; the notification will be sent when notifications become | 48 virtual void SendNotification(const Notification& notification) = 0; |
67 // enabled. | |
68 void SendNotification(const Notification& notification); | |
69 | |
70 void SimulateOnNotificationReceivedForTest( | |
71 const Notification& notification); | |
72 | |
73 void SimulateConnectAndSubscribeForTest( | |
74 base::WeakPtr<buzz::XmppTaskParentInterface> base_task); | |
75 | |
76 void SimulateDisconnectForTest(); | |
77 | |
78 void SimulateSubscriptionErrorForTest(); | |
79 | |
80 // Any notifications sent after this is called will be reflected, | |
81 // i.e. will be treated as an incoming notification also. | |
82 void ReflectSentNotificationsForTest(); | |
83 | |
84 private: | |
85 class Core; | |
86 | |
87 // The real guts of PushClient, which allows this class to not be | |
88 // refcounted. | |
89 const scoped_refptr<Core> core_; | |
90 const scoped_refptr<base::MessageLoopProxy> parent_message_loop_proxy_; | |
91 const scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(PushClient); | |
94 }; | 49 }; |
95 | 50 |
96 } // namespace notifier | 51 } // namespace notifier |
97 | 52 |
98 #endif // JINGLE_NOTIFIER_LISTENER_PUSH_CLIENT_H_ | 53 #endif // JINGLE_NOTIFIER_LISTENER_PUSH_CLIENT_H_ |
OLD | NEW |