OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "blimp/net/message_port_util.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "blimp/net/compressed_packet_reader.h" |
| 11 #include "blimp/net/compressed_packet_writer.h" |
| 12 #include "blimp/net/message_port.h" |
| 13 #include "blimp/net/stream_packet_reader.h" |
| 14 #include "blimp/net/stream_packet_writer.h" |
| 15 #include "net/socket/stream_socket.h" |
| 16 |
| 17 namespace blimp { |
| 18 namespace { |
| 19 |
| 20 const char kMessagePortUtilAttachment[] = "message_port_util_attachment"; |
| 21 |
| 22 // Used to bind a Socket lifetime to an object which extends |
| 23 // base::SupportsUserData. |
| 24 class StreamSocketUserData : public base::SupportsUserData::Data { |
| 25 public: |
| 26 explicit StreamSocketUserData(std::unique_ptr<net::StreamSocket> socket) |
| 27 : socket_(std::move(socket)) {} |
| 28 ~StreamSocketUserData() override = default; |
| 29 |
| 30 private: |
| 31 std::unique_ptr<net::StreamSocket> socket_; |
| 32 DISALLOW_COPY_AND_ASSIGN(StreamSocketUserData); |
| 33 }; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 std::unique_ptr<MessagePort> CreateMessagePortForStreamSocket( |
| 38 std::unique_ptr<net::StreamSocket> socket) { |
| 39 // Use compressed packet readers and writers. |
| 40 std::unique_ptr<MessagePort> output( |
| 41 new MessagePort(base::MakeUnique<CompressedPacketReader>( |
| 42 base::MakeUnique<StreamPacketReader>(socket.get())), |
| 43 base::MakeUnique<CompressedPacketWriter>( |
| 44 base::MakeUnique<StreamPacketWriter>(socket.get())))); |
| 45 |
| 46 // Bind |socket|'s lifetime to the MessagePort, so that ownership doesn't need |
| 47 // to be coordinated with the reader or the writer objects. |
| 48 output->SetUserData(kMessagePortUtilAttachment, |
| 49 new StreamSocketUserData(std::move(socket))); |
| 50 return output; |
| 51 } |
| 52 |
| 53 } // namespace blimp |
OLD | NEW |