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 #include "mojo/services/network/net_adapters.h"
7 #include "net/base/net_errors.h"
13 const uint32_t kMaxBufSize
= 64 * 1024;
17 NetToMojoPendingBuffer::NetToMojoPendingBuffer(
18 ScopedDataPipeProducerHandle handle
,
20 : handle_(handle
.Pass()),
24 NetToMojoPendingBuffer::~NetToMojoPendingBuffer() {
25 if (handle_
.is_valid())
26 EndWriteDataRaw(handle_
.get(), 0);
30 MojoResult
NetToMojoPendingBuffer::BeginWrite(
31 ScopedDataPipeProducerHandle
* handle
,
32 scoped_refptr
<NetToMojoPendingBuffer
>* pending
,
33 uint32_t* num_bytes
) {
37 MojoResult result
= BeginWriteDataRaw(handle
->get(), &buf
, num_bytes
,
38 MOJO_WRITE_DATA_FLAG_NONE
);
39 if (result
== MOJO_RESULT_OK
) {
40 if (*num_bytes
> kMaxBufSize
)
41 *num_bytes
= kMaxBufSize
;
42 *pending
= new NetToMojoPendingBuffer(handle
->Pass(), buf
);
47 ScopedDataPipeProducerHandle
NetToMojoPendingBuffer::Complete(
49 EndWriteDataRaw(handle_
.get(), num_bytes
);
51 return handle_
.Pass();
54 // -----------------------------------------------------------------------------
56 NetToMojoIOBuffer::NetToMojoIOBuffer(
57 NetToMojoPendingBuffer
* pending_buffer
)
58 : net::WrappedIOBuffer(pending_buffer
->buffer()),
59 pending_buffer_(pending_buffer
) {
62 NetToMojoIOBuffer::~NetToMojoIOBuffer() {
65 // -----------------------------------------------------------------------------
67 MojoToNetPendingBuffer::MojoToNetPendingBuffer(
68 ScopedDataPipeConsumerHandle handle
,
70 : handle_(handle
.Pass()),
74 MojoToNetPendingBuffer::~MojoToNetPendingBuffer() {
78 MojoResult
MojoToNetPendingBuffer::BeginRead(
79 ScopedDataPipeConsumerHandle
* handle
,
80 scoped_refptr
<MojoToNetPendingBuffer
>* pending
,
81 uint32_t* num_bytes
) {
82 const void* buffer
= NULL
;
84 MojoResult result
= BeginReadDataRaw(handle
->get(), &buffer
, num_bytes
,
85 MOJO_READ_DATA_FLAG_NONE
);
86 if (result
== MOJO_RESULT_OK
)
87 *pending
= new MojoToNetPendingBuffer(handle
->Pass(), buffer
);
91 ScopedDataPipeConsumerHandle
MojoToNetPendingBuffer::Complete(
93 EndReadDataRaw(handle_
.get(), num_bytes
);
95 return handle_
.Pass();
98 // -----------------------------------------------------------------------------
100 MojoToNetIOBuffer::MojoToNetIOBuffer(MojoToNetPendingBuffer
* pending_buffer
)
101 : net::WrappedIOBuffer(pending_buffer
->buffer()),
102 pending_buffer_(pending_buffer
) {
105 MojoToNetIOBuffer::~MojoToNetIOBuffer() {
108 // -----------------------------------------------------------------------------
110 NetworkErrorPtr
MakeNetworkError(int error_code
) {
111 NetworkErrorPtr error
= NetworkError::New();
112 error
->code
= error_code
;
114 error
->description
= net::ErrorToString(error_code
);