Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / host / gnubby_socket.h
blob50702de89f8683db7b6915bc5f7c57c9c613bbf3
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.
5 #ifndef REMOTING_HOST_GNUBBY_SOCKET_H_
6 #define REMOTING_HOST_GNUBBY_SOCKET_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
16 namespace base {
17 class Timer;
18 } // namespace base
20 namespace net {
21 class DrainableIOBuffer;
22 class IOBufferWithSize;
23 class StreamSocket;
24 } // namespace net
26 namespace remoting {
28 // Class that manages reading requests and sending responses. The socket can
29 // only handle receiving one request at a time. It expects to receive no extra
30 // bytes over the wire, which is checked by IsRequestTooLarge method.
31 class GnubbySocket : public base::NonThreadSafe {
32 public:
33 GnubbySocket(scoped_ptr<net::StreamSocket> socket,
34 const base::TimeDelta& timeout,
35 const base::Closure& timeout_callback);
36 ~GnubbySocket();
38 // Returns false if the request has not yet completed, or is too large to be
39 // processed. Otherwise, the cached request data is copied into |data_out| and
40 // the internal buffer resets and is ready for the next request.
41 bool GetAndClearRequestData(std::string* data_out);
43 // Sends response data to the socket.
44 void SendResponse(const std::string& data);
46 // Sends an SSH error code to the socket.
47 void SendSshError();
49 // |request_received_callback| is used to notify the caller that request data
50 // has been fully read, and caller is to use GetAndClearRequestData method to
51 // get the request data.
52 void StartReadingRequest(const base::Closure& request_received_callback);
54 private:
55 // Called when bytes are written to |socket_|.
56 void OnDataWritten(int result);
58 // Continues writing to |socket_| if needed.
59 void DoWrite();
61 // Called when bytes are read from |socket_|.
62 void OnDataRead(int bytes_read);
64 // Continues to read.
65 void DoRead();
67 // Returns true if the current request is complete.
68 bool IsRequestComplete() const;
70 // Returns true if the stated request size is larger than the allowed maximum.
71 bool IsRequestTooLarge() const;
73 // Returns the stated request length.
74 size_t GetRequestLength() const;
76 // Returns the response length bytes.
77 std::string GetResponseLengthAsBytes(const std::string& response) const;
79 // Resets the socket activity timer.
80 void ResetTimer();
82 // The socket.
83 scoped_ptr<net::StreamSocket> socket_;
85 // Invoked when request data has been read.
86 base::Closure request_received_callback_;
88 // Indicates whether read has completed and |request_received_callback_| is
89 // about to be run.
90 bool read_completed_;
92 // Request data.
93 std::vector<char> request_data_;
95 scoped_refptr<net::DrainableIOBuffer> write_buffer_;
97 scoped_refptr<net::IOBufferWithSize> read_buffer_;
99 // The activity timer.
100 scoped_ptr<base::Timer> timer_;
102 DISALLOW_COPY_AND_ASSIGN(GnubbySocket);
105 } // namespace remoting
107 #endif // REMOTING_HOST_GNUBBY_SOCKET_H_