| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "net/websockets/websocket_basic_stream.h" | 5 #include "net/websockets/websocket_basic_stream.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 } | 65 } |
| 66 | 66 |
| 67 } // namespace | 67 } // namespace |
| 68 | 68 |
| 69 WebSocketBasicStream::WebSocketBasicStream( | 69 WebSocketBasicStream::WebSocketBasicStream( |
| 70 scoped_ptr<ClientSocketHandle> connection, | 70 scoped_ptr<ClientSocketHandle> connection, |
| 71 const scoped_refptr<GrowableIOBuffer>& http_read_buffer, | 71 const scoped_refptr<GrowableIOBuffer>& http_read_buffer, |
| 72 const std::string& sub_protocol, | 72 const std::string& sub_protocol, |
| 73 const std::string& extensions) | 73 const std::string& extensions) |
| 74 : read_buffer_(new IOBufferWithSize(kReadBufferSize)), | 74 : read_buffer_(new IOBufferWithSize(kReadBufferSize)), |
| 75 connection_(connection.Pass()), | 75 connection_(std::move(connection)), |
| 76 http_read_buffer_(http_read_buffer), | 76 http_read_buffer_(http_read_buffer), |
| 77 sub_protocol_(sub_protocol), | 77 sub_protocol_(sub_protocol), |
| 78 extensions_(extensions), | 78 extensions_(extensions), |
| 79 generate_websocket_masking_key_(&GenerateWebSocketMaskingKey) { | 79 generate_websocket_masking_key_(&GenerateWebSocketMaskingKey) { |
| 80 // http_read_buffer_ should not be set if it contains no data. | 80 // http_read_buffer_ should not be set if it contains no data. |
| 81 if (http_read_buffer_.get() && http_read_buffer_->offset() == 0) | 81 if (http_read_buffer_.get() && http_read_buffer_->offset() == 0) |
| 82 http_read_buffer_ = NULL; | 82 http_read_buffer_ = NULL; |
| 83 DCHECK(connection_->is_initialized()); | 83 DCHECK(connection_->is_initialized()); |
| 84 } | 84 } |
| 85 | 85 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 | 185 |
| 186 /*static*/ | 186 /*static*/ |
| 187 scoped_ptr<WebSocketBasicStream> | 187 scoped_ptr<WebSocketBasicStream> |
| 188 WebSocketBasicStream::CreateWebSocketBasicStreamForTesting( | 188 WebSocketBasicStream::CreateWebSocketBasicStreamForTesting( |
| 189 scoped_ptr<ClientSocketHandle> connection, | 189 scoped_ptr<ClientSocketHandle> connection, |
| 190 const scoped_refptr<GrowableIOBuffer>& http_read_buffer, | 190 const scoped_refptr<GrowableIOBuffer>& http_read_buffer, |
| 191 const std::string& sub_protocol, | 191 const std::string& sub_protocol, |
| 192 const std::string& extensions, | 192 const std::string& extensions, |
| 193 WebSocketMaskingKeyGeneratorFunction key_generator_function) { | 193 WebSocketMaskingKeyGeneratorFunction key_generator_function) { |
| 194 scoped_ptr<WebSocketBasicStream> stream(new WebSocketBasicStream( | 194 scoped_ptr<WebSocketBasicStream> stream(new WebSocketBasicStream( |
| 195 connection.Pass(), http_read_buffer, sub_protocol, extensions)); | 195 std::move(connection), http_read_buffer, sub_protocol, extensions)); |
| 196 stream->generate_websocket_masking_key_ = key_generator_function; | 196 stream->generate_websocket_masking_key_ = key_generator_function; |
| 197 return stream.Pass(); | 197 return stream; |
| 198 } | 198 } |
| 199 | 199 |
| 200 int WebSocketBasicStream::WriteEverything( | 200 int WebSocketBasicStream::WriteEverything( |
| 201 const scoped_refptr<DrainableIOBuffer>& buffer, | 201 const scoped_refptr<DrainableIOBuffer>& buffer, |
| 202 const CompletionCallback& callback) { | 202 const CompletionCallback& callback) { |
| 203 while (buffer->BytesRemaining() > 0) { | 203 while (buffer->BytesRemaining() > 0) { |
| 204 // The use of base::Unretained() here is safe because on destruction we | 204 // The use of base::Unretained() here is safe because on destruction we |
| 205 // disconnect the socket, preventing any further callbacks. | 205 // disconnect the socket, preventing any further callbacks. |
| 206 int result = connection_->socket()->Write( | 206 int result = connection_->socket()->Write( |
| 207 buffer.get(), | 207 buffer.get(), |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 | 255 |
| 256 int WebSocketBasicStream::ConvertChunksToFrames( | 256 int WebSocketBasicStream::ConvertChunksToFrames( |
| 257 std::vector<scoped_ptr<WebSocketFrameChunk>>* frame_chunks, | 257 std::vector<scoped_ptr<WebSocketFrameChunk>>* frame_chunks, |
| 258 std::vector<scoped_ptr<WebSocketFrame>>* frames) { | 258 std::vector<scoped_ptr<WebSocketFrame>>* frames) { |
| 259 for (size_t i = 0; i < frame_chunks->size(); ++i) { | 259 for (size_t i = 0; i < frame_chunks->size(); ++i) { |
| 260 scoped_ptr<WebSocketFrame> frame; | 260 scoped_ptr<WebSocketFrame> frame; |
| 261 int result = ConvertChunkToFrame(std::move((*frame_chunks)[i]), &frame); | 261 int result = ConvertChunkToFrame(std::move((*frame_chunks)[i]), &frame); |
| 262 if (result != OK) | 262 if (result != OK) |
| 263 return result; | 263 return result; |
| 264 if (frame) | 264 if (frame) |
| 265 frames->push_back(frame.Pass()); | 265 frames->push_back(std::move(frame)); |
| 266 } | 266 } |
| 267 frame_chunks->clear(); | 267 frame_chunks->clear(); |
| 268 if (frames->empty()) | 268 if (frames->empty()) |
| 269 return ERR_IO_PENDING; | 269 return ERR_IO_PENDING; |
| 270 return OK; | 270 return OK; |
| 271 } | 271 } |
| 272 | 272 |
| 273 int WebSocketBasicStream::ConvertChunkToFrame( | 273 int WebSocketBasicStream::ConvertChunkToFrame( |
| 274 scoped_ptr<WebSocketFrameChunk> chunk, | 274 scoped_ptr<WebSocketFrameChunk> chunk, |
| 275 scoped_ptr<WebSocketFrame>* frame) { | 275 scoped_ptr<WebSocketFrame>* frame) { |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 current_frame_header_->opcode = WebSocketFrameHeader::kOpCodeContinuation; | 383 current_frame_header_->opcode = WebSocketFrameHeader::kOpCodeContinuation; |
| 384 current_frame_header_->reserved1 = false; | 384 current_frame_header_->reserved1 = false; |
| 385 current_frame_header_->reserved2 = false; | 385 current_frame_header_->reserved2 = false; |
| 386 current_frame_header_->reserved3 = false; | 386 current_frame_header_->reserved3 = false; |
| 387 } | 387 } |
| 388 } | 388 } |
| 389 // Make sure that a frame header is not applied to any chunks that do not | 389 // Make sure that a frame header is not applied to any chunks that do not |
| 390 // belong to it. | 390 // belong to it. |
| 391 if (is_final_chunk) | 391 if (is_final_chunk) |
| 392 current_frame_header_.reset(); | 392 current_frame_header_.reset(); |
| 393 return result_frame.Pass(); | 393 return result_frame; |
| 394 } | 394 } |
| 395 | 395 |
| 396 void WebSocketBasicStream::AddToIncompleteControlFrameBody( | 396 void WebSocketBasicStream::AddToIncompleteControlFrameBody( |
| 397 const scoped_refptr<IOBufferWithSize>& data_buffer) { | 397 const scoped_refptr<IOBufferWithSize>& data_buffer) { |
| 398 if (!data_buffer.get()) | 398 if (!data_buffer.get()) |
| 399 return; | 399 return; |
| 400 const int new_offset = | 400 const int new_offset = |
| 401 incomplete_control_frame_body_->offset() + data_buffer->size(); | 401 incomplete_control_frame_body_->offset() + data_buffer->size(); |
| 402 CHECK_GE(incomplete_control_frame_body_->capacity(), new_offset) | 402 CHECK_GE(incomplete_control_frame_body_->capacity(), new_offset) |
| 403 << "Control frame body larger than frame header indicates; frame parser " | 403 << "Control frame body larger than frame header indicates; frame parser " |
| 404 "bug?"; | 404 "bug?"; |
| 405 memcpy(incomplete_control_frame_body_->data(), | 405 memcpy(incomplete_control_frame_body_->data(), |
| 406 data_buffer->data(), | 406 data_buffer->data(), |
| 407 data_buffer->size()); | 407 data_buffer->size()); |
| 408 incomplete_control_frame_body_->set_offset(new_offset); | 408 incomplete_control_frame_body_->set_offset(new_offset); |
| 409 } | 409 } |
| 410 | 410 |
| 411 void WebSocketBasicStream::OnReadComplete( | 411 void WebSocketBasicStream::OnReadComplete( |
| 412 std::vector<scoped_ptr<WebSocketFrame>>* frames, | 412 std::vector<scoped_ptr<WebSocketFrame>>* frames, |
| 413 const CompletionCallback& callback, | 413 const CompletionCallback& callback, |
| 414 int result) { | 414 int result) { |
| 415 result = HandleReadResult(result, frames); | 415 result = HandleReadResult(result, frames); |
| 416 if (result == ERR_IO_PENDING) | 416 if (result == ERR_IO_PENDING) |
| 417 result = ReadFrames(frames, callback); | 417 result = ReadFrames(frames, callback); |
| 418 if (result != ERR_IO_PENDING) | 418 if (result != ERR_IO_PENDING) |
| 419 callback.Run(result); | 419 callback.Run(result); |
| 420 } | 420 } |
| 421 | 421 |
| 422 } // namespace net | 422 } // namespace net |
| OLD | NEW |