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