| 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 // An invalidator that uses p2p notifications based on XMPP push | 5 // An invalidator 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 SYNC_NOTIFIER_P2P_INVALIDATOR_H_ | 8 #ifndef SYNC_NOTIFIER_P2P_INVALIDATOR_H_ |
| 9 #define SYNC_NOTIFIER_P2P_INVALIDATOR_H_ | 9 #define SYNC_NOTIFIER_P2P_INVALIDATOR_H_ |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "sync/notifier/invalidator_registrar.h" | 22 #include "sync/notifier/invalidator_registrar.h" |
| 23 #include "sync/notifier/notifications_disabled_reason.h" | 23 #include "sync/notifier/notifications_disabled_reason.h" |
| 24 | 24 |
| 25 namespace notifier { | 25 namespace notifier { |
| 26 class PushClient; | 26 class PushClient; |
| 27 } // namespace notifier | 27 } // namespace notifier |
| 28 | 28 |
| 29 namespace syncer { | 29 namespace syncer { |
| 30 | 30 |
| 31 // The channel to use for sync notifications. | 31 // The channel to use for sync notifications. |
| 32 extern const char* kSyncP2PNotificationChannel; | 32 extern const char kSyncP2PNotificationChannel[]; |
| 33 | 33 |
| 34 // The intended recipient(s) of a P2P notification. | 34 // The intended recipient(s) of a P2P notification. |
| 35 enum P2PNotificationTarget { | 35 enum P2PNotificationTarget { |
| 36 NOTIFY_SELF, | 36 NOTIFY_SELF, |
| 37 FIRST_NOTIFICATION_TARGET = NOTIFY_SELF, | 37 FIRST_NOTIFICATION_TARGET = NOTIFY_SELF, |
| 38 NOTIFY_OTHERS, | 38 NOTIFY_OTHERS, |
| 39 NOTIFY_ALL, | 39 NOTIFY_ALL, |
| 40 LAST_NOTIFICATION_TARGET = NOTIFY_ALL | 40 LAST_NOTIFICATION_TARGET = NOTIFY_ALL |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 std::string P2PNotificationTargetToString( | 43 std::string P2PNotificationTargetToString( |
| 44 P2PNotificationTarget target); | 44 P2PNotificationTarget target); |
| 45 | 45 |
| 46 // If |target_str| can't be parsed, assumes NOTIFY_SELF. | 46 // If |target_str| can't be parsed, assumes NOTIFY_SELF. |
| 47 P2PNotificationTarget P2PNotificationTargetFromString( | 47 P2PNotificationTarget P2PNotificationTargetFromString( |
| 48 const std::string& target_str); | 48 const std::string& target_str); |
| 49 | 49 |
| 50 // Helper notification data class that can be serialized to and | 50 // Helper notification data class that can be serialized to and |
| 51 // deserialized from a string. | 51 // deserialized from a string. |
| 52 class P2PNotificationData { | 52 class P2PNotificationData { |
| 53 public: | 53 public: |
| 54 // Initializes with an empty sender ID, target set to NOTIFY_SELF, | 54 // Initializes with an empty sender ID, target set to NOTIFY_SELF, |
| 55 // and empty changed types. | 55 // and empty changed types. |
| 56 P2PNotificationData(); | 56 P2PNotificationData(); |
| 57 P2PNotificationData(const std::string& sender_id, | 57 P2PNotificationData(const std::string& sender_id, |
| 58 P2PNotificationTarget target, | 58 P2PNotificationTarget target, |
| 59 ModelTypeSet changed_types); | 59 const ObjectIdStateMap& id_state_map, |
| 60 IncomingNotificationSource source); |
| 60 | 61 |
| 61 ~P2PNotificationData(); | 62 ~P2PNotificationData(); |
| 62 | 63 |
| 63 // Returns true if the given ID is targeted by this notification. | 64 // Returns true if the given ID is targeted by this notification. |
| 64 bool IsTargeted(const std::string& id) const; | 65 bool IsTargeted(const std::string& id) const; |
| 65 | 66 |
| 66 ModelTypeSet GetChangedTypes() const; | 67 const ObjectIdStateMap& GetIdStateMap() const; |
| 68 |
| 69 IncomingNotificationSource GetSource() const; |
| 67 | 70 |
| 68 bool Equals(const P2PNotificationData& other) const; | 71 bool Equals(const P2PNotificationData& other) const; |
| 69 | 72 |
| 70 std::string ToString() const; | 73 std::string ToString() const; |
| 71 | 74 |
| 72 // Returns whether parsing |str| was successful. If parsing was | 75 // Returns whether parsing |str| was successful. If parsing was |
| 73 // unsuccessful, the state of the notification is undefined. | 76 // unsuccessful, the state of the notification is undefined. |
| 74 bool ResetFromString(const std::string& str); | 77 bool ResetFromString(const std::string& str); |
| 75 | 78 |
| 76 private: | 79 private: |
| 77 // The unique ID of the client that sent the notification. | 80 // The unique ID of the client that sent the notification. |
| 78 std::string sender_id_; | 81 std::string sender_id_; |
| 79 // The intendent recipient(s) of the notification. | 82 // The intendent recipient(s) of the notification. |
| 80 P2PNotificationTarget target_; | 83 P2PNotificationTarget target_; |
| 81 // The types the notification is for. | 84 // The state map for the notification. |
| 82 ModelTypeSet changed_types_; | 85 ObjectIdStateMap id_state_map_; |
| 86 // The source of the notification. |
| 87 IncomingNotificationSource source_; |
| 83 }; | 88 }; |
| 84 | 89 |
| 85 class P2PInvalidator : public Invalidator, | 90 class P2PInvalidator : public Invalidator, |
| 86 public notifier::PushClientObserver { | 91 public notifier::PushClientObserver { |
| 87 public: | 92 public: |
| 88 // The |send_notification_target| parameter was added to allow us to send | 93 // The |send_notification_target| parameter was added to allow us to send |
| 89 // self-notifications in some cases, but not others. The value should be | 94 // self-notifications in some cases, but not others. The value should be |
| 90 // either NOTIFY_ALL to send notifications to all clients, or NOTIFY_OTHERS | 95 // either NOTIFY_ALL to send notifications to all clients, or NOTIFY_OTHERS |
| 91 // to send notifications to all clients except for the one that triggered the | 96 // to send notifications to all clients except for the one that triggered the |
| 92 // notification. See crbug.com/97780. | 97 // notification. See crbug.com/97780. |
| 93 P2PInvalidator(scoped_ptr<notifier::PushClient> push_client, | 98 P2PInvalidator(scoped_ptr<notifier::PushClient> push_client, |
| 94 P2PNotificationTarget send_notification_target); | 99 P2PNotificationTarget send_notification_target); |
| 95 | 100 |
| 96 virtual ~P2PInvalidator(); | 101 virtual ~P2PInvalidator(); |
| 97 | 102 |
| 98 // Invalidator implementation | 103 // Invalidator implementation |
| 99 virtual void RegisterHandler(InvalidationHandler* handler) OVERRIDE; | 104 virtual void RegisterHandler(InvalidationHandler* handler) OVERRIDE; |
| 100 virtual void UpdateRegisteredIds(InvalidationHandler* handler, | 105 virtual void UpdateRegisteredIds(InvalidationHandler* handler, |
| 101 const ObjectIdSet& ids) OVERRIDE; | 106 const ObjectIdSet& ids) OVERRIDE; |
| 102 virtual void UnregisterHandler(InvalidationHandler* handler) OVERRIDE; | 107 virtual void UnregisterHandler(InvalidationHandler* handler) OVERRIDE; |
| 103 virtual void SetUniqueId(const std::string& unique_id) OVERRIDE; | 108 virtual void SetUniqueId(const std::string& unique_id) OVERRIDE; |
| 104 virtual void SetStateDeprecated(const std::string& state) OVERRIDE; | 109 virtual void SetStateDeprecated(const std::string& state) OVERRIDE; |
| 105 virtual void UpdateCredentials( | 110 virtual void UpdateCredentials( |
| 106 const std::string& email, const std::string& token) OVERRIDE; | 111 const std::string& email, const std::string& token) OVERRIDE; |
| 107 virtual void SendNotification(ModelTypeSet changed_types) OVERRIDE; | 112 virtual void SendNotification(const ObjectIdStateMap& id_state_map) OVERRIDE; |
| 108 | 113 |
| 109 // PushClientObserver implementation. | 114 // PushClientObserver implementation. |
| 110 virtual void OnNotificationsEnabled() OVERRIDE; | 115 virtual void OnNotificationsEnabled() OVERRIDE; |
| 111 virtual void OnNotificationsDisabled( | 116 virtual void OnNotificationsDisabled( |
| 112 notifier::NotificationsDisabledReason reason) OVERRIDE; | 117 notifier::NotificationsDisabledReason reason) OVERRIDE; |
| 113 virtual void OnIncomingNotification( | 118 virtual void OnIncomingNotification( |
| 114 const notifier::Notification& notification) OVERRIDE; | 119 const notifier::Notification& notification) OVERRIDE; |
| 115 | 120 |
| 116 void SendNotificationDataForTest( | 121 void SendNotificationDataForTest( |
| 117 const P2PNotificationData& notification_data); | 122 const P2PNotificationData& notification_data); |
| 118 | 123 |
| 119 private: | 124 private: |
| 120 void SendNotificationData(const P2PNotificationData& notification_data); | 125 void SendNotificationData(const P2PNotificationData& notification_data); |
| 121 | 126 |
| 122 base::ThreadChecker thread_checker_; | 127 base::ThreadChecker thread_checker_; |
| 123 | 128 |
| 124 InvalidatorRegistrar registrar_; | 129 InvalidatorRegistrar registrar_; |
| 125 | 130 |
| 126 // The push client. | 131 // The push client. |
| 127 scoped_ptr<notifier::PushClient> push_client_; | 132 scoped_ptr<notifier::PushClient> push_client_; |
| 128 // Our unique ID. | 133 // Our unique ID. |
| 129 std::string unique_id_; | 134 std::string unique_id_; |
| 130 // Whether we have called UpdateCredentials() yet. | 135 // Whether we have called UpdateCredentials() yet. |
| 131 bool logged_in_; | 136 bool logged_in_; |
| 132 bool notifications_enabled_; | 137 bool notifications_enabled_; |
| 133 // Which set of clients should be sent notifications. | 138 // Which set of clients should be sent notifications. |
| 134 P2PNotificationTarget send_notification_target_; | 139 P2PNotificationTarget send_notification_target_; |
| 135 | 140 |
| 136 ModelTypeSet enabled_types_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(P2PInvalidator); | 141 DISALLOW_COPY_AND_ASSIGN(P2PInvalidator); |
| 139 }; | 142 }; |
| 140 | 143 |
| 141 } // namespace syncer | 144 } // namespace syncer |
| 142 | 145 |
| 143 #endif // SYNC_NOTIFIER_P2P_INVALIDATOR_H_ | 146 #endif // SYNC_NOTIFIER_P2P_INVALIDATOR_H_ |
| OLD | NEW |