// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_ #define REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_ #include #include #include "base/message_loop.h" #include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "remoting/protocol/message_reader.h" #include "remoting/protocol/session.h" #include "remoting/protocol/stream_writer.h" #include "remoting/protocol/video_writer.h" namespace remoting { namespace protocol { class ClientStub; // This class represents a remote viewer connected to the chromoting host // through a libjingle connection. A viewer object is responsible for sending // screen updates and other messages to the remote viewer. It is also // responsible for receiving and parsing data from the remote viewer and // delegating events to the event handler. class ConnectionToClient : public base::RefCountedThreadSafe { public: class EventHandler { public: virtual ~EventHandler() {} // Handles an event received by the ConnectionToClient. Receiver will own // the ClientMessages in ClientMessageList and needs to delete them. // Note that the sender of messages will not reference messages // again so it is okay to clear |messages| in this method. virtual void HandleMessage(ConnectionToClient* connection, ChromotingClientMessage* message) = 0; // Called when the network connection is opened. virtual void OnConnectionOpened(ConnectionToClient* connection) = 0; // Called when the network connection is closed. virtual void OnConnectionClosed(ConnectionToClient* connection) = 0; // Called when the network connection has failed. virtual void OnConnectionFailed(ConnectionToClient* connection) = 0; }; // Constructs a ConnectionToClient object. |message_loop| is the message loop // that this object runs on. A viewer object receives events and messages from // a libjingle channel, these events are delegated to |handler|. // It is guranteed that |handler| is called only on the |message_loop|. ConnectionToClient(MessageLoop* message_loop, EventHandler* handler); virtual ~ConnectionToClient(); virtual void Init(protocol::Session* session); // Returns the connection in use. virtual protocol::Session* session(); // Send encoded update stream data to the viewer. virtual void SendVideoPacket(const VideoPacket& packet); // Gets the number of update stream messages not yet transmitted. // Note that the value returned is an estimate using average size of the // most recent update streams. // TODO(hclam): Report this number accurately. virtual int GetPendingUpdateStreamMessages(); // Disconnect the client connection. This method is allowed to be called // more than once and calls after the first one will be ignored. // // After this method is called all the send method calls will be ignored. virtual void Disconnect(); // Return pointer to ClientStub. virtual ClientStub* client_stub() { return client_stub_.get(); } protected: // Protected constructor used by unit test. ConnectionToClient(); private: // Callback for protocol Session. void OnSessionStateChange(protocol::Session::State state); // Callback for MessageReader. void OnMessageReceived(ChromotingClientMessage* message); // Process a libjingle state change event on the |loop_|. void StateChangeTask(protocol::Session::State state); // Process a data buffer received from libjingle. void MessageReceivedTask(ChromotingClientMessage* message); void OnClosed(); // The libjingle channel used to send and receive data from the remote client. scoped_refptr session_; MessageReader event_reader_; scoped_ptr video_writer_; // ClientStub for sending messages to the client. scoped_ptr client_stub_; // The message loop that this object runs on. MessageLoop* loop_; // Event handler for handling events sent from this object. EventHandler* handler_; DISALLOW_COPY_AND_ASSIGN(ConnectionToClient); }; } // namespace protocol } // namespace remoting #endif // REMOTING_PROTOCOL_CONNECTION_TO_CLIENT_H_