Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/security_key/security_key_socket.h" | 5 #include "remoting/host/security_key/security_key_socket.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 | 23 |
| 24 // SSH Failure Code | 24 // SSH Failure Code |
| 25 const char kSshError[] = {0x05}; | 25 const char kSshError[] = {0x05}; |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 SecurityKeySocket::SecurityKeySocket(std::unique_ptr<net::StreamSocket> socket, | 29 SecurityKeySocket::SecurityKeySocket(std::unique_ptr<net::StreamSocket> socket, |
| 30 base::TimeDelta timeout, | 30 base::TimeDelta timeout, |
| 31 const base::Closure& timeout_callback) | 31 const base::Closure& timeout_callback) |
| 32 : socket_(std::move(socket)), | 32 : socket_(std::move(socket)), |
| 33 read_completed_(false), | |
| 34 read_buffer_(new net::IOBufferWithSize(kRequestReadBufferLength)) { | 33 read_buffer_(new net::IOBufferWithSize(kRequestReadBufferLength)) { |
| 35 timer_.reset(new base::Timer(false, false)); | 34 timer_.reset(new base::Timer(false, false)); |
| 36 timer_->Start(FROM_HERE, timeout, timeout_callback); | 35 timer_->Start(FROM_HERE, timeout, timeout_callback); |
| 37 } | 36 } |
| 38 | 37 |
| 39 SecurityKeySocket::~SecurityKeySocket() {} | 38 SecurityKeySocket::~SecurityKeySocket() {} |
| 40 | 39 |
| 41 bool SecurityKeySocket::GetAndClearRequestData(std::string* data_out) { | 40 bool SecurityKeySocket::GetAndClearRequestData(std::string* data_out) { |
| 42 DCHECK(thread_checker_.CalledOnValidThread()); | 41 DCHECK(thread_checker_.CalledOnValidThread()); |
| 43 DCHECK(read_completed_); | 42 DCHECK(!waiting_for_request_); |
| 44 | 43 |
| 45 if (!read_completed_) { | |
| 46 return false; | |
| 47 } | |
| 48 if (!IsRequestComplete() || IsRequestTooLarge()) { | 44 if (!IsRequestComplete() || IsRequestTooLarge()) { |
| 49 return false; | 45 return false; |
| 50 } | 46 } |
| 51 // The request size is not part of the data; don't send it. | 47 // The request size is not part of the data; don't send it. |
| 52 data_out->assign(request_data_.begin() + kRequestSizeBytes, | 48 data_out->assign(request_data_.begin() + kRequestSizeBytes, |
| 53 request_data_.end()); | 49 request_data_.end()); |
| 54 request_data_.clear(); | 50 request_data_.clear(); |
| 55 return true; | 51 return true; |
| 56 } | 52 } |
| 57 | 53 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 74 DCHECK(thread_checker_.CalledOnValidThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 75 | 71 |
| 76 SendResponse(std::string(kSshError, arraysize(kSshError))); | 72 SendResponse(std::string(kSshError, arraysize(kSshError))); |
| 77 } | 73 } |
| 78 | 74 |
| 79 void SecurityKeySocket::StartReadingRequest( | 75 void SecurityKeySocket::StartReadingRequest( |
| 80 const base::Closure& request_received_callback) { | 76 const base::Closure& request_received_callback) { |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | 77 DCHECK(thread_checker_.CalledOnValidThread()); |
| 82 DCHECK(request_received_callback_.is_null()); | 78 DCHECK(request_received_callback_.is_null()); |
| 83 | 79 |
| 80 waiting_for_request_ = true; | |
| 84 request_received_callback_ = request_received_callback; | 81 request_received_callback_ = request_received_callback; |
| 82 | |
| 85 DoRead(); | 83 DoRead(); |
| 86 } | 84 } |
| 87 | 85 |
| 88 void SecurityKeySocket::OnDataWritten(int result) { | 86 void SecurityKeySocket::OnDataWritten(int result) { |
| 89 DCHECK(thread_checker_.CalledOnValidThread()); | 87 DCHECK(thread_checker_.CalledOnValidThread()); |
| 90 DCHECK(write_buffer_); | 88 DCHECK(write_buffer_); |
| 91 | 89 |
| 92 if (result < 0) { | 90 if (result < 0) { |
| 93 LOG(ERROR) << "Error sending response: " << result; | 91 LOG(ERROR) << "Error sending response: " << result; |
| 94 return; | 92 return; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 115 OnDataWritten(result); | 113 OnDataWritten(result); |
| 116 } | 114 } |
| 117 } | 115 } |
| 118 | 116 |
| 119 void SecurityKeySocket::OnDataRead(int result) { | 117 void SecurityKeySocket::OnDataRead(int result) { |
| 120 DCHECK(thread_checker_.CalledOnValidThread()); | 118 DCHECK(thread_checker_.CalledOnValidThread()); |
| 121 | 119 |
| 122 if (result <= 0) { | 120 if (result <= 0) { |
| 123 if (result < 0) { | 121 if (result < 0) { |
| 124 LOG(ERROR) << "Error reading request: " << result; | 122 LOG(ERROR) << "Error reading request: " << result; |
| 123 socket_read_error_ = true; | |
| 125 } | 124 } |
| 126 read_completed_ = true; | 125 waiting_for_request_ = false; |
| 127 base::ResetAndReturn(&request_received_callback_).Run(); | 126 base::ResetAndReturn(&request_received_callback_).Run(); |
| 128 return; | 127 return; |
| 129 } | 128 } |
| 130 | 129 |
| 131 ResetTimer(); | 130 ResetTimer(); |
| 131 // TODO(joedow): If there are multiple requests in a burst, it is possible | |
| 132 // that we could read too many bytes from the buffer (e.g. all of request #1 | |
| 133 // and some of request #2). We should consider using the request header to | |
| 134 // determine the request length and only read that amount from buffer. | |
|
Sergey Ulanov
2016/12/20 00:54:23
I think it would be easier to keep the current rea
joedow
2016/12/20 02:24:03
That's reasonable. I'll work on that tomorrow and
| |
| 132 request_data_.insert(request_data_.end(), read_buffer_->data(), | 135 request_data_.insert(request_data_.end(), read_buffer_->data(), |
| 133 read_buffer_->data() + result); | 136 read_buffer_->data() + result); |
| 134 if (IsRequestComplete()) { | 137 if (IsRequestComplete()) { |
| 135 read_completed_ = true; | 138 waiting_for_request_ = false; |
| 136 base::ResetAndReturn(&request_received_callback_).Run(); | 139 base::ResetAndReturn(&request_received_callback_).Run(); |
| 137 return; | 140 return; |
| 138 } | 141 } |
| 139 | 142 |
| 140 DoRead(); | 143 DoRead(); |
| 141 } | 144 } |
| 142 | 145 |
| 143 void SecurityKeySocket::DoRead() { | 146 void SecurityKeySocket::DoRead() { |
| 144 DCHECK(thread_checker_.CalledOnValidThread()); | 147 DCHECK(thread_checker_.CalledOnValidThread()); |
| 145 | 148 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 return response_len; | 194 return response_len; |
| 192 } | 195 } |
| 193 | 196 |
| 194 void SecurityKeySocket::ResetTimer() { | 197 void SecurityKeySocket::ResetTimer() { |
| 195 if (timer_->IsRunning()) { | 198 if (timer_->IsRunning()) { |
| 196 timer_->Reset(); | 199 timer_->Reset(); |
| 197 } | 200 } |
| 198 } | 201 } |
| 199 | 202 |
| 200 } // namespace remoting | 203 } // namespace remoting |
| OLD | NEW |