Chromium Code Reviews| Index: blimp/net/message_port_util.cc |
| diff --git a/blimp/net/message_port_util.cc b/blimp/net/message_port_util.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..45cfcc37ac95bf520f17a727d871c187f447b9c6 |
| --- /dev/null |
| +++ b/blimp/net/message_port_util.cc |
| @@ -0,0 +1,53 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "blimp/net/message_port_util.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "blimp/net/compressed_packet_reader.h" |
| +#include "blimp/net/compressed_packet_writer.h" |
| +#include "blimp/net/message_port.h" |
| +#include "blimp/net/stream_packet_reader.h" |
| +#include "blimp/net/stream_packet_writer.h" |
| +#include "net/socket/stream_socket.h" |
| + |
| +namespace blimp { |
| +namespace { |
| + |
| +const char kMessagePortUtilAttachment[] = "message_port_util_attachment"; |
|
Wez
2016/08/20 00:09:10
nit: Suggest naming this to match the user-data cl
|
| + |
| +// Used to bind a Socket lifetime to an object which extends |
| +// base::SupportsUserData. |
| +class StreamSocketUserData : public base::SupportsUserData::Data { |
| + public: |
| + explicit StreamSocketUserData(std::unique_ptr<net::StreamSocket> socket) |
| + : socket_(std::move(socket)) {} |
| + ~StreamSocketUserData() override = default; |
| + |
| + private: |
| + std::unique_ptr<net::StreamSocket> socket_; |
| + DISALLOW_COPY_AND_ASSIGN(StreamSocketUserData); |
| +}; |
| + |
| +} // namespace |
| + |
| +std::unique_ptr<MessagePort> CreateMessagePortForStreamSocket( |
| + std::unique_ptr<net::StreamSocket> socket) { |
| + // Use compressed packet readers and writers. |
| + std::unique_ptr<MessagePort> output( |
| + new MessagePort(base::MakeUnique<CompressedPacketReader>( |
| + base::MakeUnique<StreamPacketReader>(socket.get())), |
| + base::MakeUnique<CompressedPacketWriter>( |
| + base::MakeUnique<StreamPacketWriter>(socket.get())))); |
| + |
| + // Bind |socket|'s lifetime to the MessagePort, so that ownership doesn't need |
| + // to be coordinated with the reader or the writer objects. |
| + output->SetUserData(kMessagePortUtilAttachment, |
| + new StreamSocketUserData(std::move(socket))); |
| + return output; |
| +} |
| + |
| +} // namespace blimp |