12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// An invalidator that uses p2p invalidations based on XMPP push 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notifications. Used only for sync integration tests. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef SYNC_NOTIFIER_P2P_INVALIDATOR_H_ 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SYNC_NOTIFIER_P2P_INVALIDATOR_H_ 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/weak_ptr.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/observer_list.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread_checker.h" 19eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "jingle/notifier/base/notifier_options.h" 20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "jingle/notifier/listener/push_client.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "jingle/notifier/listener/push_client_observer.h" 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "sync/base/sync_export.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sync/internal_api/public/base/model_type.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sync/notifier/invalidator.h" 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sync/notifier/invalidator_registrar.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sync/notifier/invalidator_state.h" 274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "sync/notifier/object_id_invalidation_map.h" 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace notifier { 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PushClient; 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace notifier 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace syncer { 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The channel to use for sync notifications. 362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SYNC_EXPORT extern const char kSyncP2PNotificationChannel[]; 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The intended recipient(s) of a P2P notification. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum P2PNotificationTarget { 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NOTIFY_SELF, 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FIRST_NOTIFICATION_TARGET = NOTIFY_SELF, 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NOTIFY_OTHERS, 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NOTIFY_ALL, 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) LAST_NOTIFICATION_TARGET = NOTIFY_ALL 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SYNC_EXPORT_PRIVATE std::string P2PNotificationTargetToString( 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) P2PNotificationTarget target); 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If |target_str| can't be parsed, assumes NOTIFY_SELF. 512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)SYNC_EXPORT_PRIVATE P2PNotificationTarget P2PNotificationTargetFromString( 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& target_str); 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Helper notification data class that can be serialized to and 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// deserialized from a string. 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SYNC_EXPORT_PRIVATE P2PNotificationData { 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Initializes with an empty sender ID, target set to NOTIFY_SELF, 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and empty changed types. 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) P2PNotificationData(); 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) P2PNotificationData(const std::string& sender_id, 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) P2PNotificationTarget target, 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const ObjectIdInvalidationMap& invalidation_map); 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~P2PNotificationData(); 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if the given ID is targeted by this notification. 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool IsTargeted(const std::string& id) const; 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ObjectIdInvalidationMap& GetIdInvalidationMap() const; 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool Equals(const P2PNotificationData& other) const; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string ToString() const; 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns whether parsing |str| was successful. If parsing was 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // unsuccessful, the state of the notification is undefined. 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool ResetFromString(const std::string& str); 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The unique ID of the client that sent the notification. 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string sender_id_; 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The intendent recipient(s) of the notification. 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) P2PNotificationTarget target_; 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The invalidation map for the notification. 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ObjectIdInvalidationMap invalidation_map_; 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SYNC_EXPORT_PRIVATE P2PInvalidator 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : public Invalidator, 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public NON_EXPORTED_BASE(notifier::PushClientObserver) { 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The |send_notification_target| parameter was added to allow us to send 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // self-notifications in some cases, but not others. The value should be 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // either NOTIFY_ALL to send notifications to all clients, or NOTIFY_OTHERS 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to send notifications to all clients except for the one that triggered the 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // notification. See crbug.com/97780. 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) P2PInvalidator(scoped_ptr<notifier::PushClient> push_client, 99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const std::string& invalidator_client_id, 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) P2PNotificationTarget send_notification_target); 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~P2PInvalidator(); 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Invalidator implementation. 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void RegisterHandler(InvalidationHandler* handler) OVERRIDE; 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void UpdateRegisteredIds(InvalidationHandler* handler, 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const ObjectIdSet& ids) OVERRIDE; 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void UnregisterHandler(InvalidationHandler* handler) OVERRIDE; 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual InvalidatorState GetInvalidatorState() const OVERRIDE; 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void UpdateCredentials( 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& email, const std::string& token) OVERRIDE; 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // PushClientObserver implementation. 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void OnNotificationsEnabled() OVERRIDE; 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void OnNotificationsDisabled( 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) notifier::NotificationsDisabledReason reason) OVERRIDE; 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void OnIncomingNotification( 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const notifier::Notification& notification) OVERRIDE; 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) void SendInvalidation(const ObjectIdSet& ids); 121eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void SendNotificationDataForTest( 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const P2PNotificationData& notification_data); 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void SendNotificationData(const P2PNotificationData& notification_data); 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::ThreadChecker thread_checker_; 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) InvalidatorRegistrar registrar_; 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The push client. 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<notifier::PushClient> push_client_; 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Our unique ID. 135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::string invalidator_client_id_; 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Whether we have called UpdateCredentials() yet. 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool logged_in_; 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool notifications_enabled_; 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Which set of clients should be sent notifications. 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) P2PNotificationTarget send_notification_target_; 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(P2PInvalidator); 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace syncer 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // SYNC_NOTIFIER_P2P_INVALIDATOR_H_ 148