OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #ifndef CONTENT_NETWORK_NET_ADAPTERS_ |
| 6 #define CONTENT_NETWORK_NET_ADAPTERS_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "mojo/public/cpp/system/data_pipe.h" |
| 12 #include "net/base/io_buffer.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 // These adapters are used to transfer data between a Mojo pipe and the net |
| 17 // library. |
| 18 // |
| 19 // Mojo pipe Data flow Network library |
| 20 // ---------------------------------------------------------- |
| 21 // NetToMojoPendingBuffer <--- NetToMojoIOBuffer |
| 22 // |
| 23 // While the operation is in progress, the Mojo-side objects keep ownership |
| 24 // of the Mojo pipe, which in turn is kept alive by the IOBuffer. This allows |
| 25 // the request to potentially outlive the object managing the translation. |
| 26 // Mojo side of a Net -> Mojo copy. The buffer is allocated by Mojo. |
| 27 class NetToMojoPendingBuffer |
| 28 : public base::RefCountedThreadSafe<NetToMojoPendingBuffer> { |
| 29 public: |
| 30 // Begins a two-phase write to the data pipe. |
| 31 // |
| 32 // On success, MOJO_RESULT_OK will be returned. The ownership of the given |
| 33 // producer handle will be transferred to the new NetToMojoPendingBuffer that |
| 34 // will be placed into *pending, and the size of the buffer will be in |
| 35 // *num_bytes. |
| 36 // |
| 37 // On failure or MOJO_RESULT_SHOULD_WAIT, there will be no change to the |
| 38 // handle, and *pending and *num_bytes will be unused. |
| 39 static MojoResult BeginWrite(mojo::ScopedDataPipeProducerHandle* handle, |
| 40 scoped_refptr<NetToMojoPendingBuffer>* pending, |
| 41 uint32_t* num_bytes); |
| 42 // Called to indicate the buffer is done being written to. Passes ownership |
| 43 // of the pipe back to the caller. |
| 44 mojo::ScopedDataPipeProducerHandle Complete(uint32_t num_bytes); |
| 45 char* buffer() { return static_cast<char*>(buffer_); } |
| 46 |
| 47 private: |
| 48 friend class base::RefCountedThreadSafe<NetToMojoPendingBuffer>; |
| 49 // Takes ownership of the handle. |
| 50 NetToMojoPendingBuffer(mojo::ScopedDataPipeProducerHandle handle, |
| 51 void* buffer); |
| 52 ~NetToMojoPendingBuffer(); |
| 53 mojo::ScopedDataPipeProducerHandle handle_; |
| 54 void* buffer_; |
| 55 DISALLOW_COPY_AND_ASSIGN(NetToMojoPendingBuffer); |
| 56 }; |
| 57 |
| 58 // Net side of a Net -> Mojo copy. The data will be read from the network and |
| 59 // copied into the buffer associated with the pending mojo write. |
| 60 class NetToMojoIOBuffer : public net::WrappedIOBuffer { |
| 61 public: |
| 62 explicit NetToMojoIOBuffer(NetToMojoPendingBuffer* pending_buffer); |
| 63 |
| 64 private: |
| 65 ~NetToMojoIOBuffer() override; |
| 66 scoped_refptr<NetToMojoPendingBuffer> pending_buffer_; |
| 67 }; |
| 68 |
| 69 } // namespace content |
| 70 |
| 71 #endif // CONTENT_NETWORK_NET_ADAPTERS_ |
OLD | NEW |