Standardize usage of virtual/override/final in mojo/
[chromium-blink-merge.git] / mojo / services / network / net_adapters.h
blobea6317ecf8754d0659a27eb7d48e630962d6cdcb
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.
5 #ifndef MOJO_SERVICES_NETWORK_NET_ADAPTERS_H_
6 #define MOJO_SERVICES_NETWORK_NET_ADAPTERS_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "mojo/services/public/interfaces/network/network_error.mojom.h"
11 #include "net/base/io_buffer.h"
13 namespace mojo {
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:
18 // Mojo pipe Data flow Network library
19 // ----------------------------------------------------------
20 // MojoToNetPendingBuffer ---> MojoToNetIOBuffer
21 // NetToMojoPendingBuffer <--- NetToMojoIOBuffer
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.
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.
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.
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);
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);
48 char* buffer() { return static_cast<char*>(buffer_); }
50 private:
51 friend class base::RefCountedThreadSafe<NetToMojoPendingBuffer>;
53 // Takes ownership of the handle.
54 NetToMojoPendingBuffer(ScopedDataPipeProducerHandle handle, void* buffer);
55 ~NetToMojoPendingBuffer();
57 ScopedDataPipeProducerHandle handle_;
58 void* buffer_;
60 DISALLOW_COPY_AND_ASSIGN(NetToMojoPendingBuffer);
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);
69 private:
70 ~NetToMojoIOBuffer() override;
72 scoped_refptr<NetToMojoPendingBuffer> pending_buffer_;
75 // Mojo side of a Mojo -> Net copy.
76 class MojoToNetPendingBuffer
77 : public base::RefCountedThreadSafe<MojoToNetPendingBuffer> {
78 public:
79 // Starts reading from Mojo.
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.
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);
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);
98 const char* buffer() { return static_cast<const char*>(buffer_); }
100 private:
101 friend class base::RefCountedThreadSafe<MojoToNetPendingBuffer>;
103 // Takes ownership of the handle.
104 explicit MojoToNetPendingBuffer(ScopedDataPipeConsumerHandle handle,
105 const void* buffer);
106 ~MojoToNetPendingBuffer();
108 ScopedDataPipeConsumerHandle handle_;
109 const void* buffer_;
111 DISALLOW_COPY_AND_ASSIGN(MojoToNetPendingBuffer);
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);
120 private:
121 ~MojoToNetIOBuffer() override;
123 scoped_refptr<MojoToNetPendingBuffer> pending_buffer_;
126 // Creates a new Mojo network error object from a net error code.
127 NetworkErrorPtr MakeNetworkError(int error_code);
129 } // namespace mojo
131 #endif // MOJO_SERVICES_NETWORK_NET_ADAPTERS_H_