1 // Copyright 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. 4 5 #ifndef SYNC_NOTIFIER_PUSH_CLIENT_CHANNEL_H_ 6 #define SYNC_NOTIFIER_PUSH_CLIENT_CHANNEL_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/compiler_specific.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "google/cacheinvalidation/include/system-resources.h" 15 #include "jingle/notifier/listener/push_client_observer.h" 16 #include "sync/base/sync_export.h" 17 18 namespace notifier { 19 class PushClient; 20 } // namespace notifier 21 22 namespace syncer { 23 24 // A PushClientChannel is an implementation of NetworkChannel that 25 // routes messages through a PushClient. 26 class SYNC_EXPORT_PRIVATE PushClientChannel 27 : public NON_EXPORTED_BASE(invalidation::NetworkChannel), 28 public NON_EXPORTED_BASE(notifier::PushClientObserver) { 29 public: 30 // |push_client| is guaranteed to be destroyed only when this object 31 // is destroyed. 32 explicit PushClientChannel(scoped_ptr<notifier::PushClient> push_client); 33 34 virtual ~PushClientChannel(); 35 36 // If not connected, connects with the given credentials. If 37 // already connected, the next connection attempt will use the given 38 // credentials. 39 void UpdateCredentials(const std::string& email, const std::string& token); 40 41 // invalidation::NetworkChannel implementation. 42 virtual void SendMessage(const std::string& outgoing_message) OVERRIDE; 43 virtual void SetMessageReceiver( 44 invalidation::MessageCallback* incoming_receiver) OVERRIDE; 45 virtual void AddNetworkStatusReceiver( 46 invalidation::NetworkStatusCallback* network_status_receiver) OVERRIDE; 47 virtual void SetSystemResources( 48 invalidation::SystemResources* resources) OVERRIDE; 49 50 // notifier::PushClient::Observer implementation. 51 virtual void OnNotificationsEnabled() OVERRIDE; 52 virtual void OnNotificationsDisabled( 53 notifier::NotificationsDisabledReason reason) OVERRIDE; 54 virtual void OnIncomingNotification( 55 const notifier::Notification& notification) OVERRIDE; 56 57 const std::string& GetServiceContextForTest() const; 58 59 int64 GetSchedulingHashForTest() const; 60 61 static notifier::Notification EncodeMessageForTest( 62 const std::string& message, 63 const std::string& service_context, 64 int64 scheduling_hash); 65 66 static bool DecodeMessageForTest( 67 const notifier::Notification& notification, 68 std::string* message, 69 std::string* service_context, 70 int64* scheduling_hash); 71 72 private: 73 typedef std::vector<invalidation::NetworkStatusCallback*> 74 NetworkStatusReceiverList; 75 76 static notifier::Notification EncodeMessage( 77 const std::string& message, 78 const std::string& service_context, 79 int64 scheduling_hash); 80 81 static bool DecodeMessage( 82 const notifier::Notification& notification, 83 std::string* message, 84 std::string* service_context, 85 int64* scheduling_hash); 86 87 scoped_ptr<notifier::PushClient> push_client_; 88 scoped_ptr<invalidation::MessageCallback> incoming_receiver_; 89 NetworkStatusReceiverList network_status_receivers_; 90 91 bool notifications_enabled_; 92 93 // Service context. 94 std::string service_context_; 95 96 // Scheduling hash. 97 int64 scheduling_hash_; 98 99 DISALLOW_COPY_AND_ASSIGN(PushClientChannel); 100 }; 101 102 } // namespace syncer 103 104 #endif // SYNC_NOTIFIER_PUSH_CLIENT_CHANNEL_H_ 105