| 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> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <utility> | 12 #include <utility> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
| 18 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 20 #include "net/socket/client_socket_handle.h" | 21 #include "net/socket/client_socket_handle.h" |
| 21 #include "net/websockets/websocket_errors.h" | 22 #include "net/websockets/websocket_errors.h" |
| 22 #include "net/websockets/websocket_frame.h" | 23 #include "net/websockets/websocket_frame.h" |
| 23 #include "net/websockets/websocket_frame_parser.h" | 24 #include "net/websockets/websocket_frame_parser.h" |
| 24 | 25 |
| 25 namespace net { | 26 namespace net { |
| 26 | 27 |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 // The use of base::Unretained() here is safe because on destruction we | 205 // The use of base::Unretained() here is safe because on destruction we |
| 205 // disconnect the socket, preventing any further callbacks. | 206 // disconnect the socket, preventing any further callbacks. |
| 206 int result = connection_->socket()->Write( | 207 int result = connection_->socket()->Write( |
| 207 buffer.get(), | 208 buffer.get(), |
| 208 buffer->BytesRemaining(), | 209 buffer->BytesRemaining(), |
| 209 base::Bind(&WebSocketBasicStream::OnWriteComplete, | 210 base::Bind(&WebSocketBasicStream::OnWriteComplete, |
| 210 base::Unretained(this), | 211 base::Unretained(this), |
| 211 buffer, | 212 buffer, |
| 212 callback)); | 213 callback)); |
| 213 if (result > 0) { | 214 if (result > 0) { |
| 215 UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Upstream", result); |
| 214 buffer->DidConsume(result); | 216 buffer->DidConsume(result); |
| 215 } else { | 217 } else { |
| 216 return result; | 218 return result; |
| 217 } | 219 } |
| 218 } | 220 } |
| 219 return OK; | 221 return OK; |
| 220 } | 222 } |
| 221 | 223 |
| 222 void WebSocketBasicStream::OnWriteComplete( | 224 void WebSocketBasicStream::OnWriteComplete( |
| 223 const scoped_refptr<DrainableIOBuffer>& buffer, | 225 const scoped_refptr<DrainableIOBuffer>& buffer, |
| 224 const CompletionCallback& callback, | 226 const CompletionCallback& callback, |
| 225 int result) { | 227 int result) { |
| 226 if (result < 0) { | 228 if (result < 0) { |
| 227 DCHECK_NE(ERR_IO_PENDING, result); | 229 DCHECK_NE(ERR_IO_PENDING, result); |
| 228 callback.Run(result); | 230 callback.Run(result); |
| 229 return; | 231 return; |
| 230 } | 232 } |
| 231 | 233 |
| 232 DCHECK_NE(0, result); | 234 DCHECK_NE(0, result); |
| 235 UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Upstream", result); |
| 236 |
| 233 buffer->DidConsume(result); | 237 buffer->DidConsume(result); |
| 234 result = WriteEverything(buffer, callback); | 238 result = WriteEverything(buffer, callback); |
| 235 if (result != ERR_IO_PENDING) | 239 if (result != ERR_IO_PENDING) |
| 236 callback.Run(result); | 240 callback.Run(result); |
| 237 } | 241 } |
| 238 | 242 |
| 239 int WebSocketBasicStream::HandleReadResult( | 243 int WebSocketBasicStream::HandleReadResult( |
| 240 int result, | 244 int result, |
| 241 std::vector<std::unique_ptr<WebSocketFrame>>* frames) { | 245 std::vector<std::unique_ptr<WebSocketFrame>>* frames) { |
| 242 DCHECK_NE(ERR_IO_PENDING, result); | 246 DCHECK_NE(ERR_IO_PENDING, result); |
| 243 DCHECK(frames->empty()); | 247 DCHECK(frames->empty()); |
| 244 if (result < 0) | 248 if (result < 0) |
| 245 return result; | 249 return result; |
| 246 if (result == 0) | 250 if (result == 0) |
| 247 return ERR_CONNECTION_CLOSED; | 251 return ERR_CONNECTION_CLOSED; |
| 252 |
| 253 UMA_HISTOGRAM_COUNTS_100000("Net.WebSocket.DataUse.Downstream", result); |
| 254 |
| 248 std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks; | 255 std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks; |
| 249 if (!parser_.Decode(read_buffer_->data(), result, &frame_chunks)) | 256 if (!parser_.Decode(read_buffer_->data(), result, &frame_chunks)) |
| 250 return WebSocketErrorToNetError(parser_.websocket_error()); | 257 return WebSocketErrorToNetError(parser_.websocket_error()); |
| 251 if (frame_chunks.empty()) | 258 if (frame_chunks.empty()) |
| 252 return ERR_IO_PENDING; | 259 return ERR_IO_PENDING; |
| 253 return ConvertChunksToFrames(&frame_chunks, frames); | 260 return ConvertChunksToFrames(&frame_chunks, frames); |
| 254 } | 261 } |
| 255 | 262 |
| 256 int WebSocketBasicStream::ConvertChunksToFrames( | 263 int WebSocketBasicStream::ConvertChunksToFrames( |
| 257 std::vector<std::unique_ptr<WebSocketFrameChunk>>* frame_chunks, | 264 std::vector<std::unique_ptr<WebSocketFrameChunk>>* frame_chunks, |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 const CompletionCallback& callback, | 420 const CompletionCallback& callback, |
| 414 int result) { | 421 int result) { |
| 415 result = HandleReadResult(result, frames); | 422 result = HandleReadResult(result, frames); |
| 416 if (result == ERR_IO_PENDING) | 423 if (result == ERR_IO_PENDING) |
| 417 result = ReadFrames(frames, callback); | 424 result = ReadFrames(frames, callback); |
| 418 if (result != ERR_IO_PENDING) | 425 if (result != ERR_IO_PENDING) |
| 419 callback.Run(result); | 426 callback.Run(result); |
| 420 } | 427 } |
| 421 | 428 |
| 422 } // namespace net | 429 } // namespace net |
| OLD | NEW |