1 // Copyright 2014 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 REMOTING_HOST_GNUBBY_SOCKET_H_ 6 #define REMOTING_HOST_GNUBBY_SOCKET_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/callback.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/threading/non_thread_safe.h" 14 15 namespace base { 16 class Timer; 17 } // namespace base 18 19 namespace net { 20 class StreamListenSocket; 21 } // namespace net 22 23 namespace remoting { 24 25 // Class that manages a socket used for gnubby requests. 26 class GnubbySocket : public base::NonThreadSafe { 27 public: 28 GnubbySocket(scoped_ptr<net::StreamListenSocket> socket, 29 const base::Closure& timeout_callback); 30 ~GnubbySocket(); 31 32 // Adds data to the current request. 33 void AddRequestData(const char* data, int data_len); 34 35 // Gets the current request data and clears it. 36 void GetAndClearRequestData(std::string* data_out); 37 38 // Returns true if the current request is complete. 39 bool IsRequestComplete() const; 40 41 // Returns true if the stated request size is larger than the allowed maximum. 42 bool IsRequestTooLarge() const; 43 44 // Sends response data to the socket. 45 void SendResponse(const std::string& data); 46 47 // Sends an SSH error code to the socket. 48 void SendSshError(); 49 50 // Returns true if |socket| is the same one owned by this object. 51 bool IsSocket(net::StreamListenSocket* socket) const; 52 53 // Sets a timer for testing. 54 void SetTimerForTesting(scoped_ptr<base::Timer> timer); 55 56 private: 57 // Returns the stated request length. 58 size_t GetRequestLength() const; 59 60 // Returns the response length bytes. 61 std::string GetResponseLengthAsBytes(const std::string& response) const; 62 63 // Resets the socket activity timer. 64 void ResetTimer(); 65 66 // The socket. 67 scoped_ptr<net::StreamListenSocket> socket_; 68 69 // Request data. 70 std::vector<char> request_data_; 71 72 // The activity timer. 73 scoped_ptr<base::Timer> timer_; 74 75 DISALLOW_COPY_AND_ASSIGN(GnubbySocket); 76 }; 77 78 } // namespace remoting 79 80 #endif // REMOTING_HOST_GNUBBY_SOCKET_H_ 81