1 // Copyright 2018 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 CHROMEOS_SERVICES_SECURE_CHANNEL_PUBLIC_CPP_CLIENT_CLIENT_CHANNEL_H_ 6 #define CHROMEOS_SERVICES_SECURE_CHANNEL_PUBLIC_CPP_CLIENT_CLIENT_CHANNEL_H_ 7 8 #include <string> 9 10 #include "base/callback_forward.h" 11 #include "base/macros.h" 12 #include "base/observer_list.h" 13 #include "chromeos/services/secure_channel/public/mojom/secure_channel.mojom.h" 14 15 namespace chromeos { 16 17 namespace secure_channel { 18 19 // A full-duplex communication channel which is guaranteed to be authenticated 20 // (i.e., the two sides of the channel both belong to the same underlying user). 21 // All messages sent and received over the channel are encrypted. 22 // 23 // If clients wish to disconnect the channel, they simply need to delete the 24 // object. 25 class ClientChannel { 26 public: 27 class Observer { 28 public: 29 virtual ~Observer(); 30 virtual void OnDisconnected() = 0; 31 virtual void OnMessageReceived(const std::string& payload) = 0; 32 }; 33 34 virtual ~ClientChannel(); 35 36 bool GetConnectionMetadata( 37 base::OnceCallback<void(mojom::ConnectionMetadataPtr)> callback); 38 39 // Sends a message with the specified |payload|. Once the message has been 40 // sent, |on_sent_callback| will be invoked. Returns whether this 41 // ClientChannel was able to start sending the message; this function only 42 // fails if the underlying connection has been disconnected. 43 bool SendMessage(const std::string& payload, 44 base::OnceClosure on_sent_callback); 45 is_disconnected()46 bool is_disconnected() const { return is_disconnected_; } 47 48 void AddObserver(Observer* observer); 49 void RemoveObserver(Observer* observer); 50 51 protected: 52 ClientChannel(); 53 54 // Performs the actual logic of sending the message. By the time this function 55 // is called, it has already been confirmed that the channel has not been 56 // disconnected. 57 virtual void PerformSendMessage(const std::string& payload, 58 base::OnceClosure on_sent_callback) = 0; 59 60 virtual void PerformGetConnectionMetadata( 61 base::OnceCallback<void(mojom::ConnectionMetadataPtr)> callback) = 0; 62 63 void NotifyDisconnected(); 64 void NotifyMessageReceived(const std::string& payload); 65 66 private: 67 base::ObserverList<Observer>::Unchecked observer_list_; 68 bool is_disconnected_ = false; 69 70 DISALLOW_COPY_AND_ASSIGN(ClientChannel); 71 }; 72 73 } // namespace secure_channel 74 75 } // namespace chromeos 76 77 #endif // CHROMEOS_SERVICES_SECURE_CHANNEL_PUBLIC_CPP_CLIENT_CLIENT_CHANNEL_H_ 78