Skip to content

Commit

Permalink
Change scoped_ptr to std::unique_ptr in //net/websockets.
Browse files Browse the repository at this point in the history
R=agl
BUG=554298

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

Cr-Commit-Position: refs/heads/master@{#387771}
  • Loading branch information
danakj authored and Commit bot committed Apr 16, 2016
1 parent 655b66c commit 9c5cab5
Show file tree
Hide file tree
Showing 38 changed files with 656 additions and 632 deletions.
18 changes: 8 additions & 10 deletions net/websockets/websocket_basic_handshake_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ bool ValidateExtensions(const HttpResponseHeaders* headers,
} // namespace

WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream(
scoped_ptr<ClientSocketHandle> connection,
std::unique_ptr<ClientSocketHandle> connection,
WebSocketStream::ConnectDelegate* connect_delegate,
bool using_proxy,
std::vector<std::string> requested_sub_protocols,
Expand Down Expand Up @@ -355,7 +355,7 @@ int WebSocketBasicHandshakeStream::SendRequest(
ComputeSecWebSocketAccept(handshake_challenge);

DCHECK(connect_delegate_);
scoped_ptr<WebSocketHandshakeRequestInfo> request(
std::unique_ptr<WebSocketHandshakeRequestInfo> request(
new WebSocketHandshakeRequestInfo(url_, base::Time::Now()));
request->headers.CopyFrom(enriched_headers);
connect_delegate_->OnStartOpeningHandshake(std::move(request));
Expand Down Expand Up @@ -471,26 +471,24 @@ HttpStream* WebSocketBasicHandshakeStream::RenewStreamForAuth() {
return nullptr;
}

scoped_ptr<WebSocketStream> WebSocketBasicHandshakeStream::Upgrade() {
std::unique_ptr<WebSocketStream> WebSocketBasicHandshakeStream::Upgrade() {
// The HttpStreamParser object has a pointer to our ClientSocketHandle. Make
// sure it does not touch it again before it is destroyed.
state_.DeleteParser();
WebSocketTransportClientSocketPool::UnlockEndpoint(state_.connection());
scoped_ptr<WebSocketStream> basic_stream(
new WebSocketBasicStream(state_.ReleaseConnection(),
state_.read_buf(),
sub_protocol_,
extensions_));
std::unique_ptr<WebSocketStream> basic_stream(
new WebSocketBasicStream(state_.ReleaseConnection(), state_.read_buf(),
sub_protocol_, extensions_));
DCHECK(extension_params_.get());
if (extension_params_->deflate_enabled) {
UMA_HISTOGRAM_ENUMERATION(
"Net.WebSocket.DeflateMode",
extension_params_->deflate_parameters.client_context_take_over_mode(),
WebSocketDeflater::NUM_CONTEXT_TAKEOVER_MODE_TYPES);

return scoped_ptr<WebSocketStream>(new WebSocketDeflateStream(
return std::unique_ptr<WebSocketStream>(new WebSocketDeflateStream(
std::move(basic_stream), extension_params_->deflate_parameters,
scoped_ptr<WebSocketDeflatePredictor>(
std::unique_ptr<WebSocketDeflatePredictor>(
new WebSocketDeflatePredictorImpl)));
} else {
return basic_stream;
Expand Down
10 changes: 5 additions & 5 deletions net/websockets/websocket_basic_handshake_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

#include <stdint.h>

#include <memory>
#include <string>
#include <vector>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "net/base/net_export.h"
#include "net/http/http_basic_state.h"
#include "net/websockets/websocket_handshake_stream_base.h"
Expand All @@ -32,7 +32,7 @@ class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream
public:
// |connect_delegate| and |failure_message| must out-live this object.
WebSocketBasicHandshakeStream(
scoped_ptr<ClientSocketHandle> connection,
std::unique_ptr<ClientSocketHandle> connection,
WebSocketStream::ConnectDelegate* connect_delegate,
bool using_proxy,
std::vector<std::string> requested_sub_protocols,
Expand Down Expand Up @@ -77,7 +77,7 @@ class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream
// have been received. It creates an appropriate subclass of WebSocketStream
// depending on what extensions were negotiated. This object is unusable after
// Upgrade() has been called and should be disposed of as soon as possible.
scoped_ptr<WebSocketStream> Upgrade() override;
std::unique_ptr<WebSocketStream> Upgrade() override;

// Set the value used for the next Sec-WebSocket-Key header
// deterministically. The key is only used once, and then discarded.
Expand Down Expand Up @@ -118,7 +118,7 @@ class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream

// The key to be sent in the next Sec-WebSocket-Key header. Usually NULL (the
// key is generated on the fly).
scoped_ptr<std::string> handshake_challenge_for_testing_;
std::unique_ptr<std::string> handshake_challenge_for_testing_;

// The required value for the Sec-WebSocket-Accept header.
std::string handshake_challenge_response_;
Expand All @@ -137,7 +137,7 @@ class NET_EXPORT_PRIVATE WebSocketBasicHandshakeStream

// The extension parameters. The class is defined in the implementation file
// to avoid including extension-related header files here.
scoped_ptr<WebSocketExtensionParams> extension_params_;
std::unique_ptr<WebSocketExtensionParams> extension_params_;

std::string* failure_message_;

Expand Down
36 changes: 18 additions & 18 deletions net/websockets/websocket_basic_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const int kReadBufferSize = 32 * 1024;
// |frames| will be serialized with mask field. This function forces the
// masked bit of the frames on.
int CalculateSerializedSizeAndTurnOnMaskBit(
std::vector<scoped_ptr<WebSocketFrame>>* frames) {
std::vector<std::unique_ptr<WebSocketFrame>>* frames) {
const uint64_t kMaximumTotalSize = std::numeric_limits<int>::max();

uint64_t total_size = 0;
Expand All @@ -67,7 +67,7 @@ int CalculateSerializedSizeAndTurnOnMaskBit(
} // namespace

WebSocketBasicStream::WebSocketBasicStream(
scoped_ptr<ClientSocketHandle> connection,
std::unique_ptr<ClientSocketHandle> connection,
const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
const std::string& sub_protocol,
const std::string& extensions)
Expand All @@ -86,7 +86,7 @@ WebSocketBasicStream::WebSocketBasicStream(
WebSocketBasicStream::~WebSocketBasicStream() { Close(); }

int WebSocketBasicStream::ReadFrames(
std::vector<scoped_ptr<WebSocketFrame>>* frames,
std::vector<std::unique_ptr<WebSocketFrame>>* frames,
const CompletionCallback& callback) {
DCHECK(frames->empty());
// If there is data left over after parsing the HTTP headers, attempt to parse
Expand All @@ -98,7 +98,7 @@ int WebSocketBasicStream::ReadFrames(
scoped_refptr<GrowableIOBuffer> buffered_data;
buffered_data.swap(http_read_buffer_);
DCHECK(http_read_buffer_.get() == NULL);
std::vector<scoped_ptr<WebSocketFrameChunk>> frame_chunks;
std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks;
if (!parser_.Decode(buffered_data->StartOfBuffer(),
buffered_data->offset(),
&frame_chunks))
Expand Down Expand Up @@ -133,7 +133,7 @@ int WebSocketBasicStream::ReadFrames(
}

int WebSocketBasicStream::WriteFrames(
std::vector<scoped_ptr<WebSocketFrame>>* frames,
std::vector<std::unique_ptr<WebSocketFrame>>* frames,
const CompletionCallback& callback) {
// This function always concatenates all frames into a single buffer.
// TODO(ricea): Investigate whether it would be better in some cases to
Expand Down Expand Up @@ -184,14 +184,14 @@ std::string WebSocketBasicStream::GetSubProtocol() const {
std::string WebSocketBasicStream::GetExtensions() const { return extensions_; }

/*static*/
scoped_ptr<WebSocketBasicStream>
std::unique_ptr<WebSocketBasicStream>
WebSocketBasicStream::CreateWebSocketBasicStreamForTesting(
scoped_ptr<ClientSocketHandle> connection,
std::unique_ptr<ClientSocketHandle> connection,
const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
const std::string& sub_protocol,
const std::string& extensions,
WebSocketMaskingKeyGeneratorFunction key_generator_function) {
scoped_ptr<WebSocketBasicStream> stream(new WebSocketBasicStream(
std::unique_ptr<WebSocketBasicStream> stream(new WebSocketBasicStream(
std::move(connection), http_read_buffer, sub_protocol, extensions));
stream->generate_websocket_masking_key_ = key_generator_function;
return stream;
Expand Down Expand Up @@ -238,14 +238,14 @@ void WebSocketBasicStream::OnWriteComplete(

int WebSocketBasicStream::HandleReadResult(
int result,
std::vector<scoped_ptr<WebSocketFrame>>* frames) {
std::vector<std::unique_ptr<WebSocketFrame>>* frames) {
DCHECK_NE(ERR_IO_PENDING, result);
DCHECK(frames->empty());
if (result < 0)
return result;
if (result == 0)
return ERR_CONNECTION_CLOSED;
std::vector<scoped_ptr<WebSocketFrameChunk>> frame_chunks;
std::vector<std::unique_ptr<WebSocketFrameChunk>> frame_chunks;
if (!parser_.Decode(read_buffer_->data(), result, &frame_chunks))
return WebSocketErrorToNetError(parser_.websocket_error());
if (frame_chunks.empty())
Expand All @@ -254,10 +254,10 @@ int WebSocketBasicStream::HandleReadResult(
}

int WebSocketBasicStream::ConvertChunksToFrames(
std::vector<scoped_ptr<WebSocketFrameChunk>>* frame_chunks,
std::vector<scoped_ptr<WebSocketFrame>>* frames) {
std::vector<std::unique_ptr<WebSocketFrameChunk>>* frame_chunks,
std::vector<std::unique_ptr<WebSocketFrame>>* frames) {
for (size_t i = 0; i < frame_chunks->size(); ++i) {
scoped_ptr<WebSocketFrame> frame;
std::unique_ptr<WebSocketFrame> frame;
int result = ConvertChunkToFrame(std::move((*frame_chunks)[i]), &frame);
if (result != OK)
return result;
Expand All @@ -271,8 +271,8 @@ int WebSocketBasicStream::ConvertChunksToFrames(
}

int WebSocketBasicStream::ConvertChunkToFrame(
scoped_ptr<WebSocketFrameChunk> chunk,
scoped_ptr<WebSocketFrame>* frame) {
std::unique_ptr<WebSocketFrameChunk> chunk,
std::unique_ptr<WebSocketFrame>* frame) {
DCHECK(frame->get() == NULL);
bool is_first_chunk = false;
if (chunk->header) {
Expand Down Expand Up @@ -356,10 +356,10 @@ int WebSocketBasicStream::ConvertChunkToFrame(
return OK;
}

scoped_ptr<WebSocketFrame> WebSocketBasicStream::CreateFrame(
std::unique_ptr<WebSocketFrame> WebSocketBasicStream::CreateFrame(
bool is_final_chunk,
const scoped_refptr<IOBufferWithSize>& data) {
scoped_ptr<WebSocketFrame> result_frame;
std::unique_ptr<WebSocketFrame> result_frame;
const bool is_final_chunk_in_message =
is_final_chunk && current_frame_header_->final;
const int data_size = data.get() ? data->size() : 0;
Expand Down Expand Up @@ -409,7 +409,7 @@ void WebSocketBasicStream::AddToIncompleteControlFrameBody(
}

void WebSocketBasicStream::OnReadComplete(
std::vector<scoped_ptr<WebSocketFrame>>* frames,
std::vector<std::unique_ptr<WebSocketFrame>>* frames,
const CompletionCallback& callback,
int result) {
result = HandleReadResult(result, frames);
Expand Down
38 changes: 19 additions & 19 deletions net/websockets/websocket_basic_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#ifndef NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_
#define NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_

#include <memory>
#include <string>
#include <vector>

#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "net/websockets/websocket_frame_parser.h"
#include "net/websockets/websocket_stream.h"

Expand All @@ -32,21 +32,20 @@ class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream {
// This class should not normally be constructed directly; see
// WebSocketStream::CreateAndConnectStream() and
// WebSocketBasicHandshakeStream::Upgrade().
WebSocketBasicStream(
scoped_ptr<ClientSocketHandle> connection,
const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
const std::string& sub_protocol,
const std::string& extensions);
WebSocketBasicStream(std::unique_ptr<ClientSocketHandle> connection,
const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
const std::string& sub_protocol,
const std::string& extensions);

// The destructor has to make sure the connection is closed when we finish so
// that it does not get returned to the pool.
~WebSocketBasicStream() override;

// WebSocketStream implementation.
int ReadFrames(std::vector<scoped_ptr<WebSocketFrame>>* frames,
int ReadFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames,
const CompletionCallback& callback) override;

int WriteFrames(std::vector<scoped_ptr<WebSocketFrame>>* frames,
int WriteFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames,
const CompletionCallback& callback) override;

void Close() override;
Expand All @@ -58,8 +57,9 @@ class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream {
////////////////////////////////////////////////////////////////////////////
// Methods for testing only.

static scoped_ptr<WebSocketBasicStream> CreateWebSocketBasicStreamForTesting(
scoped_ptr<ClientSocketHandle> connection,
static std::unique_ptr<WebSocketBasicStream>
CreateWebSocketBasicStreamForTesting(
std::unique_ptr<ClientSocketHandle> connection,
const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
const std::string& sub_protocol,
const std::string& extensions,
Expand All @@ -79,23 +79,23 @@ class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream {
// Attempts to parse the output of a read as WebSocket frames. On success,
// returns OK and places the frame(s) in |frames|.
int HandleReadResult(int result,
std::vector<scoped_ptr<WebSocketFrame>>* frames);
std::vector<std::unique_ptr<WebSocketFrame>>* frames);

// Converts the chunks in |frame_chunks| into frames and writes them to
// |frames|. |frame_chunks| is destroyed in the process. Returns
// ERR_WS_PROTOCOL_ERROR if an invalid chunk was found. If one or more frames
// was added to |frames|, then returns OK, otherwise returns ERR_IO_PENDING.
int ConvertChunksToFrames(
std::vector<scoped_ptr<WebSocketFrameChunk>>* frame_chunks,
std::vector<scoped_ptr<WebSocketFrame>>* frames);
std::vector<std::unique_ptr<WebSocketFrameChunk>>* frame_chunks,
std::vector<std::unique_ptr<WebSocketFrame>>* frames);

// Converts a |chunk| to a |frame|. |*frame| should be NULL on entry to this
// method. If |chunk| is an incomplete control frame, or an empty middle
// frame, then |*frame| may still be NULL on exit. If an invalid control frame
// is found, returns ERR_WS_PROTOCOL_ERROR and the stream is no longer
// usable. Otherwise returns OK (even if frame is still NULL).
int ConvertChunkToFrame(scoped_ptr<WebSocketFrameChunk> chunk,
scoped_ptr<WebSocketFrame>* frame);
int ConvertChunkToFrame(std::unique_ptr<WebSocketFrameChunk> chunk,
std::unique_ptr<WebSocketFrame>* frame);

// Creates a frame based on the value of |is_final_chunk|, |data| and
// |current_frame_header_|. Clears |current_frame_header_| if |is_final_chunk|
Expand All @@ -104,7 +104,7 @@ class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream {
// returned frame will be NULL. Otherwise, |current_frame_header_->opcode| is
// set to Continuation after use if it was Text or Binary, in accordance with
// WebSocket RFC6455 section 5.4.
scoped_ptr<WebSocketFrame> CreateFrame(
std::unique_ptr<WebSocketFrame> CreateFrame(
bool is_final_chunk,
const scoped_refptr<IOBufferWithSize>& data);

Expand All @@ -115,7 +115,7 @@ class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream {

// Called when a read completes. Parses the result and (unless no complete
// header has been received) calls |callback|.
void OnReadComplete(std::vector<scoped_ptr<WebSocketFrame>>* frames,
void OnReadComplete(std::vector<std::unique_ptr<WebSocketFrame>>* frames,
const CompletionCallback& callback,
int result);

Expand All @@ -126,14 +126,14 @@ class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream {

// The connection, wrapped in a ClientSocketHandle so that we can prevent it
// from being returned to the pool.
scoped_ptr<ClientSocketHandle> connection_;
std::unique_ptr<ClientSocketHandle> connection_;

// Frame header for the frame currently being received. Only non-NULL while we
// are processing the frame. If the frame arrives in multiple chunks, it can
// remain non-NULL until additional chunks arrive. If the header of the frame
// was invalid, this is set to NULL, the channel is failed, and subsequent
// chunks of the same frame will be ignored.
scoped_ptr<WebSocketFrameHeader> current_frame_header_;
std::unique_ptr<WebSocketFrameHeader> current_frame_header_;

// Although it should rarely happen in practice, a control frame can arrive
// broken into chunks. This variable provides storage for a partial control
Expand Down
Loading

0 comments on commit 9c5cab5

Please sign in to comment.