| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 MOJO_SERVICES_NETWORK_NET_ADAPTERS_H_ | |
| 6 #define MOJO_SERVICES_NETWORK_NET_ADAPTERS_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "mojo/services/network/public/interfaces/network_error.mojom.h" | |
| 13 #include "net/base/io_buffer.h" | |
| 14 | |
| 15 namespace mojo { | |
| 16 | |
| 17 // These adapters are used to transfer data between a Mojo pipe and the net | |
| 18 // library. There are four adapters, one for each end in each direction: | |
| 19 // | |
| 20 // Mojo pipe Data flow Network library | |
| 21 // ---------------------------------------------------------- | |
| 22 // MojoToNetPendingBuffer ---> MojoToNetIOBuffer | |
| 23 // NetToMojoPendingBuffer <--- NetToMojoIOBuffer | |
| 24 // | |
| 25 // While the operation is in progress, the Mojo-side objects keep ownership | |
| 26 // of the Mojo pipe, which in turn is kept alive by the IOBuffer. This allows | |
| 27 // the request to potentially outlive the object managing the translation. | |
| 28 | |
| 29 // Mojo side of a Net -> Mojo copy. The buffer is allocated by Mojo. | |
| 30 class NetToMojoPendingBuffer | |
| 31 : public base::RefCountedThreadSafe<NetToMojoPendingBuffer> { | |
| 32 public: | |
| 33 // Begins a two-phase write to the data pipe. | |
| 34 // | |
| 35 // On success, MOJO_RESULT_OK will be returned. The ownership of the given | |
| 36 // producer handle will be transferred to the new NetToMojoPendingBuffer that | |
| 37 // will be placed into *pending, and the size of the buffer will be in | |
| 38 // *num_bytes. | |
| 39 // | |
| 40 // On failure or MOJO_RESULT_SHOULD_WAIT, there will be no change to the | |
| 41 // handle, and *pending and *num_bytes will be unused. | |
| 42 static MojoResult BeginWrite(ScopedDataPipeProducerHandle* handle, | |
| 43 scoped_refptr<NetToMojoPendingBuffer>* pending, | |
| 44 uint32_t* num_bytes); | |
| 45 | |
| 46 // Called to indicate the buffer is done being written to. Passes ownership | |
| 47 // of the pipe back to the caller. | |
| 48 ScopedDataPipeProducerHandle Complete(uint32_t num_bytes); | |
| 49 | |
| 50 char* buffer() { return static_cast<char*>(buffer_); } | |
| 51 | |
| 52 private: | |
| 53 friend class base::RefCountedThreadSafe<NetToMojoPendingBuffer>; | |
| 54 | |
| 55 // Takes ownership of the handle. | |
| 56 NetToMojoPendingBuffer(ScopedDataPipeProducerHandle handle, void* buffer); | |
| 57 ~NetToMojoPendingBuffer(); | |
| 58 | |
| 59 ScopedDataPipeProducerHandle handle_; | |
| 60 void* buffer_; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(NetToMojoPendingBuffer); | |
| 63 }; | |
| 64 | |
| 65 // Net side of a Net -> Mojo copy. The data will be read from the network and | |
| 66 // copied into the buffer associated with the pending mojo write. | |
| 67 class NetToMojoIOBuffer : public net::WrappedIOBuffer { | |
| 68 public: | |
| 69 explicit NetToMojoIOBuffer(NetToMojoPendingBuffer* pending_buffer); | |
| 70 | |
| 71 private: | |
| 72 ~NetToMojoIOBuffer() override; | |
| 73 | |
| 74 scoped_refptr<NetToMojoPendingBuffer> pending_buffer_; | |
| 75 }; | |
| 76 | |
| 77 // Mojo side of a Mojo -> Net copy. | |
| 78 class MojoToNetPendingBuffer | |
| 79 : public base::RefCountedThreadSafe<MojoToNetPendingBuffer> { | |
| 80 public: | |
| 81 // Starts reading from Mojo. | |
| 82 // | |
| 83 // On success, MOJO_RESULT_OK will be returned. The ownership of the given | |
| 84 // consumer handle will be transferred to the new MojoToNetPendingBuffer that | |
| 85 // will be placed into *pending, and the size of the buffer will be in | |
| 86 // *num_bytes. | |
| 87 // | |
| 88 // On failure or MOJO_RESULT_SHOULD_WAIT, there will be no change to the | |
| 89 // handle, and *pending and *num_bytes will be unused. | |
| 90 static MojoResult BeginRead(ScopedDataPipeConsumerHandle* handle, | |
| 91 scoped_refptr<MojoToNetPendingBuffer>* pending, | |
| 92 uint32_t* num_bytes); | |
| 93 | |
| 94 // Indicates the buffer is done being read from. Passes ownership of the pipe | |
| 95 // back to the caller. The argument is the number of bytes actually read, | |
| 96 // since net may do partial writes, which will result in partial reads from | |
| 97 // the Mojo pipe's perspective. | |
| 98 ScopedDataPipeConsumerHandle Complete(uint32_t num_bytes); | |
| 99 | |
| 100 const char* buffer() { return static_cast<const char*>(buffer_); } | |
| 101 | |
| 102 private: | |
| 103 friend class base::RefCountedThreadSafe<MojoToNetPendingBuffer>; | |
| 104 | |
| 105 // Takes ownership of the handle. | |
| 106 explicit MojoToNetPendingBuffer(ScopedDataPipeConsumerHandle handle, | |
| 107 const void* buffer); | |
| 108 ~MojoToNetPendingBuffer(); | |
| 109 | |
| 110 ScopedDataPipeConsumerHandle handle_; | |
| 111 const void* buffer_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(MojoToNetPendingBuffer); | |
| 114 }; | |
| 115 | |
| 116 // Net side of a Mojo -> Net copy. The data will already be in the | |
| 117 // MojoToNetPendingBuffer's buffer. | |
| 118 class MojoToNetIOBuffer : public net::WrappedIOBuffer { | |
| 119 public: | |
| 120 explicit MojoToNetIOBuffer(MojoToNetPendingBuffer* pending_buffer); | |
| 121 | |
| 122 private: | |
| 123 ~MojoToNetIOBuffer() override; | |
| 124 | |
| 125 scoped_refptr<MojoToNetPendingBuffer> pending_buffer_; | |
| 126 }; | |
| 127 | |
| 128 // Creates a new Mojo network error object from a net error code. | |
| 129 NetworkErrorPtr MakeNetworkError(int error_code); | |
| 130 | |
| 131 } // namespace mojo | |
| 132 | |
| 133 #endif // MOJO_SERVICES_NETWORK_NET_ADAPTERS_H_ | |
| OLD | NEW |