Skip to content

Commit

Permalink
net/websockets: Convert int types from basictypes.h to the ones from …
Browse files Browse the repository at this point in the history
…stdint.h

Now that the supported toolchain has stdint.h, use the integer types
from the standard header file.

Patch generated by the following command lines:

$ git grep -l '\<u\?int[1-9]\+\>' net/websockets/ | xargs sed -i -e 's/\(\<u\?int[1-9]\+\>\)/\1_t/g'
$ git cl format net/websockets

Plus manual inclusion of stdint.h where necessary/appropriate.

BUG=138541,488550,539734
TEST=net_unittests
R=ricea@chromium.org,eroman@chromium.org,wtc@chromium.org

Review URL: https://codereview.chromium.org/1399303002

Cr-Commit-Position: refs/heads/master@{#353814}
  • Loading branch information
tfarina authored and Commit bot committed Oct 13, 2015
1 parent f38d77e commit 8a2c66c
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 135 deletions.
20 changes: 11 additions & 9 deletions net/websockets/websocket_basic_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "net/websockets/websocket_basic_stream.h"

#include <stdint.h>
#include <algorithm>
#include <limits>
#include <string>
Expand All @@ -24,9 +25,9 @@ namespace net {

namespace {

// This uses type uint64 to match the definition of
// This uses type uint64_t to match the definition of
// WebSocketFrameHeader::payload_length in websocket_frame.h.
const uint64 kMaxControlFramePayload = 125;
const uint64_t kMaxControlFramePayload = 125;

// The number of bytes to attempt to read at a time.
// TODO(ricea): See if there is a better number or algorithm to fulfill our
Expand All @@ -47,17 +48,17 @@ typedef ScopedVector<WebSocketFrame>::const_iterator WebSocketFrameIterator;
// masked bit of the frames on.
int CalculateSerializedSizeAndTurnOnMaskBit(
ScopedVector<WebSocketFrame>* frames) {
const uint64 kMaximumTotalSize = std::numeric_limits<int>::max();
const uint64_t kMaximumTotalSize = std::numeric_limits<int>::max();

uint64 total_size = 0;
uint64_t total_size = 0;
for (WebSocketFrameIterator it = frames->begin(); it != frames->end(); ++it) {
WebSocketFrame* frame = *it;
// Force the masked bit on.
frame->header.masked = true;
// We enforce flow control so the renderer should never be able to force us
// to cache anywhere near 2GB of frames.
uint64 frame_size = frame->header.payload_length +
GetWebSocketFrameHeaderSize(frame->header);
uint64_t frame_size = frame->header.payload_length +
GetWebSocketFrameHeaderSize(frame->header);
CHECK_LE(frame_size, kMaximumTotalSize - total_size)
<< "Aborting to prevent overflow";
total_size += frame_size;
Expand Down Expand Up @@ -157,7 +158,8 @@ int WebSocketBasicStream::WriteFrames(ScopedVector<WebSocketFrame>* frames,
dest += result;
remaining_size -= result;

CHECK_LE(frame->header.payload_length, static_cast<uint64>(remaining_size));
CHECK_LE(frame->header.payload_length,
static_cast<uint64_t>(remaining_size));
const int frame_size = static_cast<int>(frame->header.payload_length);
if (frame_size > 0) {
const char* const frame_data = frame->data->data();
Expand Down Expand Up @@ -349,10 +351,10 @@ int WebSocketBasicStream::ConvertChunkToFrame(
// header. A check for exact equality can only be used when the whole frame
// arrives in one chunk.
DCHECK_GE(current_frame_header_->payload_length,
base::checked_cast<uint64>(chunk_size));
base::checked_cast<uint64_t>(chunk_size));
DCHECK(!is_first_chunk || !is_final_chunk ||
current_frame_header_->payload_length ==
base::checked_cast<uint64>(chunk_size));
base::checked_cast<uint64_t>(chunk_size));

// Convert the chunk to a complete frame.
*frame = CreateFrame(is_final_chunk, data_buffer);
Expand Down
2 changes: 1 addition & 1 deletion net/websockets/websocket_basic_stream_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ TEST_F(WebSocketBasicStreamSocketChunkedReadTest, OneMegFrame) {
// This should be equal to the definition of kReadBufferSize in
// websocket_basic_stream.cc.
const int kReadBufferSize = 32 * 1024;
const uint64 kPayloadSize = 1 << 20;
const uint64_t kPayloadSize = 1 << 20;
const size_t kWireSize = kPayloadSize + kLargeFrameHeaderSize;
const size_t kExpectedFrameCount =
(kWireSize + kReadBufferSize - 1) / kReadBufferSize;
Expand Down
44 changes: 22 additions & 22 deletions net/websockets/websocket_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const size_t kMaximumCloseReasonLength = 125 - kWebSocketCloseCodeLength;
// used for close codes received from a renderer that we are intending to send
// out over the network. See ParseClose() for the restrictions on incoming close
// codes. The |code| parameter is type int for convenience of implementation;
// the real type is uint16. Code 1005 is treated specially; it cannot be set
// the real type is uint16_t. Code 1005 is treated specially; it cannot be set
// explicitly by Javascript but the renderer uses it to indicate we should send
// a Close frame with no payload.
bool IsStrictlyValidCloseStatusCode(int code) {
Expand Down Expand Up @@ -147,7 +147,7 @@ class WebSocketChannel::SendBuffer {
// The total size of the payload data in |frames_|. This will be used to
// measure the throughput of the link.
// TODO(ricea): Measure the throughput of the link.
uint64 total_bytes_;
uint64_t total_bytes_;
};

void WebSocketChannel::SendBuffer::AddFrame(scoped_ptr<WebSocketFrame> frame) {
Expand Down Expand Up @@ -275,8 +275,8 @@ WebSocketChannel::PendingReceivedFrame::PendingReceivedFrame(
bool final,
WebSocketFrameHeader::OpCode opcode,
const scoped_refptr<IOBuffer>& data,
uint64 offset,
uint64 size)
uint64_t offset,
uint64_t size)
: final_(final),
opcode_(opcode),
data_(data),
Expand All @@ -290,7 +290,7 @@ void WebSocketChannel::PendingReceivedFrame::ResetOpcode() {
opcode_ = WebSocketFrameHeader::kOpCodeContinuation;
}

void WebSocketChannel::PendingReceivedFrame::DidConsume(uint64 bytes) {
void WebSocketChannel::PendingReceivedFrame::DidConsume(uint64_t bytes) {
DCHECK_LE(offset_, size_);
DCHECK_LE(bytes, size_ - offset_);
offset_ += bytes;
Expand Down Expand Up @@ -424,7 +424,7 @@ void WebSocketChannel::SendFrame(bool fin,
// |this| may have been deleted.
}

void WebSocketChannel::SendFlowControl(int64 quota) {
void WebSocketChannel::SendFlowControl(int64_t quota) {
DCHECK(state_ == CONNECTING || state_ == CONNECTED || state_ == SEND_CLOSED ||
state_ == CLOSE_WAIT);
// TODO(ricea): Kill the renderer if it tries to send us a negative quota
Expand All @@ -436,9 +436,9 @@ void WebSocketChannel::SendFlowControl(int64 quota) {
}
while (!pending_received_frames_.empty() && quota > 0) {
PendingReceivedFrame& front = pending_received_frames_.front();
const uint64 data_size = front.size() - front.offset();
const uint64 bytes_to_send =
std::min(base::checked_cast<uint64>(quota), data_size);
const uint64_t data_size = front.size() - front.offset();
const uint64_t bytes_to_send =
std::min(base::checked_cast<uint64_t>(quota), data_size);
const bool final = front.final() && data_size == bytes_to_send;
const char* data =
front.data().get() ? front.data()->data() + front.offset() : NULL;
Expand Down Expand Up @@ -470,7 +470,7 @@ void WebSocketChannel::SendFlowControl(int64 quota) {
// |this| may have been deleted.
}

void WebSocketChannel::StartClosingHandshake(uint16 code,
void WebSocketChannel::StartClosingHandshake(uint16_t code,
const std::string& reason) {
if (InClosingState()) {
// When the associated renderer process is killed while the channel is in
Expand Down Expand Up @@ -768,7 +768,7 @@ ChannelState WebSocketChannel::OnReadDone(bool synchronous, int result) {
stream_->Close();
SetState(CLOSED);

uint16 code = kWebSocketErrorAbnormalClosure;
uint16_t code = kWebSocketErrorAbnormalClosure;
std::string reason = "";
bool was_clean = false;
if (has_received_close_frame_) {
Expand Down Expand Up @@ -815,7 +815,7 @@ ChannelState WebSocketChannel::HandleFrameByState(
const WebSocketFrameHeader::OpCode opcode,
bool final,
const scoped_refptr<IOBuffer>& data_buffer,
uint64 size) {
uint64_t size) {
DCHECK_NE(RECV_CLOSED, state_)
<< "HandleFrame() does not support being called re-entrantly from within "
"SendClose()";
Expand Down Expand Up @@ -852,7 +852,7 @@ ChannelState WebSocketChannel::HandleFrameByState(
// the renderer, then the renderer should not receive an
// OnClosingHandshake or OnDropChannel IPC until the queued message has
// been completedly transmitted.
uint16 code = kWebSocketNormalClosure;
uint16_t code = kWebSocketNormalClosure;
std::string reason;
std::string message;
if (!ParseClose(data_buffer, size, &code, &reason, &message)) {
Expand Down Expand Up @@ -925,7 +925,7 @@ ChannelState WebSocketChannel::HandleDataFrame(
WebSocketFrameHeader::OpCode opcode,
bool final,
const scoped_refptr<IOBuffer>& data_buffer,
uint64 size) {
uint64_t size) {
if (state_ != CONNECTED) {
DVLOG(3) << "Ignored data packet received in state " << state_;
return CHANNEL_ALIVE;
Expand Down Expand Up @@ -1001,7 +1001,7 @@ ChannelState WebSocketChannel::SendFrameFromIOBuffer(
bool fin,
WebSocketFrameHeader::OpCode op_code,
const scoped_refptr<IOBuffer>& buffer,
uint64 size) {
uint64_t size) {
DCHECK(state_ == CONNECTED || state_ == RECV_CLOSED);
DCHECK(stream_);

Expand Down Expand Up @@ -1029,7 +1029,7 @@ ChannelState WebSocketChannel::SendFrameFromIOBuffer(
}

ChannelState WebSocketChannel::FailChannel(const std::string& message,
uint16 code,
uint16_t code,
const std::string& reason) {
DCHECK_NE(FRESHLY_CONSTRUCTED, state_);
DCHECK_NE(CONNECTING, state_);
Expand All @@ -1051,12 +1051,12 @@ ChannelState WebSocketChannel::FailChannel(const std::string& message,
return result;
}

ChannelState WebSocketChannel::SendClose(uint16 code,
ChannelState WebSocketChannel::SendClose(uint16_t code,
const std::string& reason) {
DCHECK(state_ == CONNECTED || state_ == RECV_CLOSED);
DCHECK_LE(reason.size(), kMaximumCloseReasonLength);
scoped_refptr<IOBuffer> body;
uint64 size = 0;
uint64_t size = 0;
if (code == kWebSocketErrorNoStatusReceived) {
// Special case: translate kWebSocketErrorNoStatusReceived into a Close
// frame with no payload.
Expand All @@ -1080,8 +1080,8 @@ ChannelState WebSocketChannel::SendClose(uint16 code,
}

bool WebSocketChannel::ParseClose(const scoped_refptr<IOBuffer>& buffer,
uint64 size,
uint16* code,
uint64_t size,
uint16_t* code,
std::string* reason,
std::string* message) {
reason->clear();
Expand All @@ -1101,7 +1101,7 @@ bool WebSocketChannel::ParseClose(const scoped_refptr<IOBuffer>& buffer,
}

const char* data = buffer->data();
uint16 unchecked_code = 0;
uint16_t unchecked_code = 0;
base::ReadBigEndian(data, &unchecked_code);
static_assert(sizeof(unchecked_code) == kWebSocketCloseCodeLength,
"they should both be two bytes");
Expand Down Expand Up @@ -1133,7 +1133,7 @@ bool WebSocketChannel::ParseClose(const scoped_refptr<IOBuffer>& buffer,
}

ChannelState WebSocketChannel::DoDropChannel(bool was_clean,
uint16 code,
uint16_t code,
const std::string& reason) {
if (CHANNEL_DELETED ==
notification_sender_->SendImmediately(event_interface_.get()))
Expand Down
46 changes: 23 additions & 23 deletions net/websockets/websocket_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_
#define NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_

#include <stdint.h>
#include <queue>
#include <string>
#include <vector>
Expand Down Expand Up @@ -84,7 +85,7 @@ class NET_EXPORT WebSocketChannel {
// Sends |quota| units of flow control to the remote side. If the underlying
// transport has a concept of |quota|, then it permits the remote server to
// send up to |quota| units of data.
void SendFlowControl(int64 quota);
void SendFlowControl(int64_t quota);

// Starts the closing handshake for a client-initiated shutdown of the
// connection. There is no API to close the connection without a closing
Expand All @@ -95,7 +96,7 @@ class NET_EXPORT WebSocketChannel {
// This does *not* trigger the event OnClosingHandshake(). The caller should
// assume that the closing handshake has started and perform the equivalent
// processing to OnClosingHandshake() if necessary.
void StartClosingHandshake(uint16 code, const std::string& reason);
void StartClosingHandshake(uint16_t code, const std::string& reason);

// Starts the connection process, using a specified creator callback rather
// than the default. This is exposed for testing.
Expand Down Expand Up @@ -135,19 +136,19 @@ class NET_EXPORT WebSocketChannel {
PendingReceivedFrame(bool final,
WebSocketFrameHeader::OpCode opcode,
const scoped_refptr<IOBuffer>& data,
uint64 offset,
uint64 size);
uint64_t offset,
uint64_t size);
~PendingReceivedFrame();

bool final() const { return final_; }
WebSocketFrameHeader::OpCode opcode() const { return opcode_; }
// ResetOpcode() to Continuation.
void ResetOpcode();
const scoped_refptr<IOBuffer>& data() const { return data_; }
uint64 offset() const { return offset_; }
uint64 size() const { return size_; }
uint64_t offset() const { return offset_; }
uint64_t size() const { return size_; }
// Increase |offset_| by |bytes|.
void DidConsume(uint64 bytes);
void DidConsume(uint64_t bytes);

// This object needs to be copyable and assignable, since it will be placed
// in a std::queue. The compiler-generated copy constructor and assignment
Expand All @@ -159,9 +160,9 @@ class NET_EXPORT WebSocketChannel {
scoped_refptr<IOBuffer> data_;
// Where to start reading from data_. Everything prior to offset_ has
// already been sent to the browser.
uint64 offset_;
uint64_t offset_;
// The size of data_.
uint64 size_;
uint64_t size_;
};

// Methods which return a value of type ChannelState may delete |this|. If the
Expand Down Expand Up @@ -260,19 +261,18 @@ class NET_EXPORT WebSocketChannel {

// Handles a single frame depending on the current state. It's used by the
// HandleFrame() method.
ChannelState HandleFrameByState(
const WebSocketFrameHeader::OpCode opcode,
bool final,
const scoped_refptr<IOBuffer>& data_buffer,
uint64 size) WARN_UNUSED_RESULT;
ChannelState HandleFrameByState(const WebSocketFrameHeader::OpCode opcode,
bool final,
const scoped_refptr<IOBuffer>& data_buffer,
uint64_t size) WARN_UNUSED_RESULT;

// Forward a received data frame to the renderer, if connected. If
// |expecting_continuation| is not equal to |expecting_to_read_continuation_|,
// will fail the channel. Also checks the UTF-8 validity of text frames.
ChannelState HandleDataFrame(WebSocketFrameHeader::OpCode opcode,
bool final,
const scoped_refptr<IOBuffer>& data_buffer,
uint64 size) WARN_UNUSED_RESULT;
uint64_t size) WARN_UNUSED_RESULT;

// Low-level method to send a single frame. Used for both data and control
// frames. Either sends the frame immediately or buffers it to be scheduled
Expand All @@ -282,7 +282,7 @@ class NET_EXPORT WebSocketChannel {
ChannelState SendFrameFromIOBuffer(bool fin,
WebSocketFrameHeader::OpCode op_code,
const scoped_refptr<IOBuffer>& buffer,
uint64 size) WARN_UNUSED_RESULT;
uint64_t size) WARN_UNUSED_RESULT;

// Performs the "Fail the WebSocket Connection" operation as defined in
// RFC6455. A NotifyFailure message is sent to the renderer with |message|.
Expand All @@ -294,14 +294,14 @@ class NET_EXPORT WebSocketChannel {
// returns CHANNEL_DELETED. It is not valid to access any member variables or
// methods after calling FailChannel().
ChannelState FailChannel(const std::string& message,
uint16 code,
uint16_t code,
const std::string& reason) WARN_UNUSED_RESULT;

// Sends a Close frame to Start the WebSocket Closing Handshake, or to respond
// to a Close frame from the server. As a special case, setting |code| to
// kWebSocketErrorNoStatusReceived will create a Close frame with no payload;
// this is symmetric with the behaviour of ParseClose.
ChannelState SendClose(uint16 code,
ChannelState SendClose(uint16_t code,
const std::string& reason) WARN_UNUSED_RESULT;

// Parses a Close frame payload. If no status code is supplied, then |code| is
Expand All @@ -311,8 +311,8 @@ class NET_EXPORT WebSocketChannel {
// then false is returned and |message| is set to an appropriate console
// message.
bool ParseClose(const scoped_refptr<IOBuffer>& buffer,
uint64 size,
uint16* code,
uint64_t size,
uint16_t* code,
std::string* reason,
std::string* message);

Expand All @@ -322,7 +322,7 @@ class NET_EXPORT WebSocketChannel {
//
// Always returns CHANNEL_DELETED.
ChannelState DoDropChannel(bool was_clean,
uint16 code,
uint16_t code,
const std::string& reason);

// Called if the closing handshake times out. Closes the connection and
Expand Down Expand Up @@ -373,7 +373,7 @@ class NET_EXPORT WebSocketChannel {
int current_send_quota_;
// The remaining amount of quota that the renderer will allow us to send on
// this logical channel (quota units).
uint64 current_receive_quota_;
uint64_t current_receive_quota_;

// Timer for the closing handshake.
base::OneShotTimer close_timer_;
Expand All @@ -389,7 +389,7 @@ class NET_EXPORT WebSocketChannel {
// arrives until the connection is closed and they are passed to
// OnDropChannel().
bool has_received_close_frame_;
uint16 received_close_code_;
uint16_t received_close_code_;
std::string received_close_reason_;

// The current state of the channel. Mainly used for sanity checking, but also
Expand Down
Loading

0 comments on commit 8a2c66c

Please sign in to comment.