Chromium Code Reviews| 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 #include "mojo/services/network/net_adapters.h" | 5 #include "mojo/services/network/net_adapters.h" |
| 6 | 6 |
| 7 #include "net/base/net_errors.h" | 7 #include "net/base/net_errors.h" |
| 8 | 8 |
| 9 namespace mojo { | 9 namespace mojo { |
| 10 | 10 |
| 11 namespace { | |
| 12 | |
| 13 const uint32_t kMaxReadSize = 64 * 1024; | |
| 14 | |
| 15 } // namespace | |
| 16 | |
| 17 NetToMojoPendingBuffer::NetToMojoPendingBuffer( | |
| 18 ScopedDataPipeProducerHandle handle, | |
| 19 void* buffer) | |
| 20 : handle_(handle.Pass()), | |
| 21 buffer_(buffer) { | |
| 22 } | |
| 23 | |
| 24 NetToMojoPendingBuffer::~NetToMojoPendingBuffer() { | |
| 25 if (handle_.is_valid()) | |
| 26 EndWriteDataRaw(handle_.get(), 0); | |
| 27 } | |
| 28 | |
| 29 // static | |
| 30 MojoResult NetToMojoPendingBuffer::BeginWrite( | |
| 31 ScopedDataPipeProducerHandle* handle, | |
| 32 scoped_refptr<NetToMojoPendingBuffer>* pending, | |
| 33 uint32_t* num_bytes) { | |
| 34 void* buf; | |
| 35 *num_bytes = 0; | |
| 36 | |
| 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 > kMaxReadSize) | |
| 41 *num_bytes = kMaxReadSize; | |
|
yzshen1
2014/10/10 21:18:59
It seems a little confusing why we use kMaxReadSiz
| |
| 42 *pending = new NetToMojoPendingBuffer(handle->Pass(), buf); | |
| 43 } | |
| 44 return result; | |
| 45 } | |
| 46 | |
| 47 ScopedDataPipeProducerHandle NetToMojoPendingBuffer::Complete( | |
| 48 uint32_t num_bytes) { | |
| 49 EndWriteDataRaw(handle_.get(), num_bytes); | |
| 50 buffer_ = NULL; | |
| 51 return handle_.Pass(); | |
| 52 } | |
| 53 | |
| 54 // ----------------------------------------------------------------------------- | |
| 55 | |
| 56 NetToMojoIOBuffer::NetToMojoIOBuffer( | |
| 57 NetToMojoPendingBuffer* pending_buffer) | |
| 58 : net::WrappedIOBuffer(pending_buffer->buffer()), | |
| 59 pending_buffer_(pending_buffer) { | |
| 60 } | |
| 61 | |
| 62 NetToMojoIOBuffer::~NetToMojoIOBuffer() { | |
| 63 } | |
| 64 | |
| 65 // ----------------------------------------------------------------------------- | |
| 66 | |
| 67 MojoToNetPendingBuffer::MojoToNetPendingBuffer( | |
| 68 ScopedDataPipeConsumerHandle handle, | |
| 69 const void* buffer) | |
| 70 : buffer_(buffer) { | |
|
yzshen1
2014/10/10 21:18:59
|handle| should be used to initialize the correspo
| |
| 71 } | |
| 72 | |
| 73 MojoToNetPendingBuffer::~MojoToNetPendingBuffer() { | |
| 74 } | |
| 75 | |
| 76 // static | |
| 77 MojoResult MojoToNetPendingBuffer::BeginRead( | |
| 78 ScopedDataPipeConsumerHandle* handle, | |
| 79 scoped_refptr<MojoToNetPendingBuffer>* pending, | |
| 80 uint32_t* num_bytes) { | |
| 81 const void* buffer = NULL; | |
| 82 *num_bytes = 0; | |
| 83 MojoResult result = BeginReadDataRaw(handle->get(), &buffer, num_bytes, | |
| 84 MOJO_READ_DATA_FLAG_NONE); | |
| 85 if (result == MOJO_RESULT_OK) | |
| 86 *pending = new MojoToNetPendingBuffer(handle->Pass(), buffer); | |
| 87 return result; | |
| 88 } | |
| 89 | |
| 90 ScopedDataPipeConsumerHandle MojoToNetPendingBuffer::Complete( | |
| 91 uint32_t num_bytes) { | |
| 92 EndReadDataRaw(handle_.get(), num_bytes); | |
| 93 buffer_ = NULL; | |
| 94 return handle_.Pass(); | |
| 95 } | |
| 96 | |
| 97 // ----------------------------------------------------------------------------- | |
| 98 | |
| 99 MojoToNetIOBuffer::MojoToNetIOBuffer(MojoToNetPendingBuffer* pending_buffer) | |
| 100 : net::WrappedIOBuffer(pending_buffer->buffer()), | |
| 101 pending_buffer_(pending_buffer) { | |
| 102 } | |
| 103 | |
| 104 MojoToNetIOBuffer::~MojoToNetIOBuffer() { | |
| 105 } | |
| 106 | |
| 107 // ----------------------------------------------------------------------------- | |
| 108 | |
| 11 NetworkErrorPtr MakeNetworkError(int error_code) { | 109 NetworkErrorPtr MakeNetworkError(int error_code) { |
| 12 NetworkErrorPtr error = NetworkError::New(); | 110 NetworkErrorPtr error = NetworkError::New(); |
| 13 error->code = error_code; | 111 error->code = error_code; |
| 14 if (error_code <= 0) | 112 if (error_code <= 0) |
| 15 error->description = net::ErrorToString(error_code); | 113 error->description = net::ErrorToString(error_code); |
| 16 return error.Pass(); | 114 return error.Pass(); |
| 17 } | 115 } |
| 18 | 116 |
| 19 } // namespace mojo | 117 } // namespace mojo |
| OLD | NEW |