OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // A notifier that uses p2p notifications based on XMPP push | 5 // A notifier that uses p2p notifications based on XMPP push |
6 // notifications. Used only for sync integration tests. | 6 // notifications. Used only for sync integration tests. |
7 | 7 |
8 #ifndef CHROME_BROWSER_SYNC_NOTIFIER_P2P_NOTIFIER_H_ | 8 #ifndef CHROME_BROWSER_SYNC_NOTIFIER_P2P_NOTIFIER_H_ |
9 #define CHROME_BROWSER_SYNC_NOTIFIER_P2P_NOTIFIER_H_ | 9 #define CHROME_BROWSER_SYNC_NOTIFIER_P2P_NOTIFIER_H_ |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
17 #include "chrome/browser/sync/notifier/sync_notifier.h" | 17 #include "chrome/browser/sync/notifier/sync_notifier.h" |
18 #include "chrome/browser/sync/syncable/model_type.h" | 18 #include "chrome/browser/sync/syncable/model_type.h" |
19 #include "jingle/notifier/listener/talk_mediator.h" | 19 #include "jingle/notifier/listener/talk_mediator.h" |
20 | 20 |
21 namespace base { | 21 namespace base { |
22 class MessageLoopProxy; | 22 class MessageLoopProxy; |
23 } | 23 } |
24 | 24 |
25 | 25 |
26 namespace notifier { | 26 namespace sync_notifier { |
27 struct NotifierOptions; | |
28 } // namespace | |
29 | 27 |
30 namespace sync_notifier { | 28 // The intended recipient(s) of a P2P notification. |
| 29 enum P2PNotificationTarget { |
| 30 NOTIFY_SELF, |
| 31 FIRST_NOTIFICATION_TARGET = NOTIFY_SELF, |
| 32 NOTIFY_OTHERS, |
| 33 NOTIFY_ALL, |
| 34 LAST_NOTIFICATION_TARGET = NOTIFY_ALL |
| 35 }; |
| 36 |
| 37 std::string P2PNotificationTargetToString( |
| 38 P2PNotificationTarget target); |
| 39 |
| 40 // If |target_str| can't be parsed, assumes NOTIFY_SELF. |
| 41 P2PNotificationTarget P2PNotificationTargetFromString( |
| 42 const std::string& target_str); |
| 43 |
| 44 // Helper notification data class that can be serialized to and |
| 45 // deserialized from a string. |
| 46 class P2PNotificationData { |
| 47 public: |
| 48 // Initializes with an empty sender ID, target set to NOTIFY_SELF, |
| 49 // and empty changed types. |
| 50 P2PNotificationData(); |
| 51 P2PNotificationData(const std::string& sender_id, |
| 52 P2PNotificationTarget target, |
| 53 const syncable::ModelTypeSet& changed_types); |
| 54 |
| 55 ~P2PNotificationData(); |
| 56 |
| 57 // Returns true if the given ID is targeted by this notification. |
| 58 bool IsTargeted(const std::string& id) const; |
| 59 |
| 60 const syncable::ModelTypeSet& GetChangedTypes() const; |
| 61 |
| 62 bool Equals(const P2PNotificationData& other) const; |
| 63 |
| 64 std::string ToString() const; |
| 65 |
| 66 // Returns whether parsing |str| was successful. If parsing was |
| 67 // unsuccessful, the state of the notification is undefined. |
| 68 bool ResetFromString(const std::string& str); |
| 69 |
| 70 private: |
| 71 // The unique ID of the client that sent the notification. |
| 72 std::string sender_id_; |
| 73 // The intendent recipient(s) of the notification. |
| 74 P2PNotificationTarget target_; |
| 75 // The types the notification is for. |
| 76 syncable::ModelTypeSet changed_types_; |
| 77 }; |
31 | 78 |
32 class P2PNotifier | 79 class P2PNotifier |
33 : public SyncNotifier, | 80 : public SyncNotifier, |
34 public notifier::TalkMediator::Delegate { | 81 public notifier::TalkMediator::Delegate { |
35 public: | 82 public: |
36 explicit P2PNotifier(const notifier::NotifierOptions& notifier_options); | 83 // Takes ownership of |talk_mediator|, but it is guaranteed that |
| 84 // |talk_mediator| is destroyed only when this object is destroyed. |
| 85 explicit P2PNotifier(notifier::TalkMediator* talk_mediator); |
37 | 86 |
38 virtual ~P2PNotifier(); | 87 virtual ~P2PNotifier(); |
39 | 88 |
40 // SyncNotifier implementation | 89 // SyncNotifier implementation |
41 virtual void AddObserver(SyncNotifierObserver* observer) OVERRIDE; | 90 virtual void AddObserver(SyncNotifierObserver* observer) OVERRIDE; |
42 virtual void RemoveObserver(SyncNotifierObserver* observer) OVERRIDE; | 91 virtual void RemoveObserver(SyncNotifierObserver* observer) OVERRIDE; |
43 virtual void SetUniqueId(const std::string& unique_id) OVERRIDE; | 92 virtual void SetUniqueId(const std::string& unique_id) OVERRIDE; |
44 virtual void SetState(const std::string& state) OVERRIDE; | 93 virtual void SetState(const std::string& state) OVERRIDE; |
45 virtual void UpdateCredentials( | 94 virtual void UpdateCredentials( |
46 const std::string& email, const std::string& token) OVERRIDE; | 95 const std::string& email, const std::string& token) OVERRIDE; |
47 virtual void UpdateEnabledTypes( | 96 virtual void UpdateEnabledTypes( |
48 const syncable::ModelTypeSet& types) OVERRIDE; | 97 const syncable::ModelTypeSet& enabled_types) OVERRIDE; |
49 virtual void SendNotification() OVERRIDE; | 98 virtual void SendNotification( |
| 99 const syncable::ModelTypeSet& changed_types) OVERRIDE; |
50 | 100 |
51 // TalkMediator::Delegate implementation. | 101 // TalkMediator::Delegate implementation. |
52 virtual void OnNotificationStateChange(bool notifications_enabled); | 102 virtual void OnNotificationStateChange(bool notifications_enabled); |
53 virtual void OnIncomingNotification( | 103 virtual void OnIncomingNotification( |
54 const notifier::Notification& notification); | 104 const notifier::Notification& notification); |
55 virtual void OnOutgoingNotification(); | 105 virtual void OnOutgoingNotification(); |
56 | 106 |
| 107 // For testing. |
| 108 void SendNotificationDataForTest( |
| 109 const P2PNotificationData& notification_data); |
| 110 |
57 private: | 111 private: |
58 // Call OnIncomingNotification() on observers if we have a non-empty | 112 void SendNotificationData(const P2PNotificationData& notification_data); |
59 // set of enabled types. | |
60 void MaybeEmitNotification(); | |
61 | 113 |
62 ObserverList<SyncNotifierObserver> observer_list_; | 114 ObserverList<SyncNotifierObserver> observer_list_; |
63 | 115 |
64 // The actual notification listener. | 116 // The actual notification listener. |
65 scoped_ptr<notifier::TalkMediator> talk_mediator_; | 117 scoped_ptr<notifier::TalkMediator> talk_mediator_; |
| 118 // Our unique ID. |
| 119 std::string unique_id_; |
66 // Whether we called Login() on |talk_mediator_| yet. | 120 // Whether we called Login() on |talk_mediator_| yet. |
67 bool logged_in_; | 121 bool logged_in_; |
68 // Whether |talk_mediator_| has notified us that notifications are | 122 // Whether |talk_mediator_| has notified us that notifications are |
69 // enabled. | 123 // enabled. |
70 bool notifications_enabled_; | 124 bool notifications_enabled_; |
71 | 125 |
72 syncable::ModelTypeSet enabled_types_; | 126 syncable::ModelTypeSet enabled_types_; |
73 scoped_refptr<base::MessageLoopProxy> parent_message_loop_proxy_; | 127 scoped_refptr<base::MessageLoopProxy> parent_message_loop_proxy_; |
74 }; | 128 }; |
75 | 129 |
76 } // namespace sync_notifier | 130 } // namespace sync_notifier |
77 #endif // CHROME_BROWSER_SYNC_NOTIFIER_P2P_NOTIFIER_H_ | 131 #endif // CHROME_BROWSER_SYNC_NOTIFIER_P2P_NOTIFIER_H_ |
OLD | NEW |