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 "content/child/web_data_consumer_handle_impl.h"
9 #include "base/logging.h"
10 #include "third_party/mojo/src/mojo/public/c/system/types.h"
14 using Result
= blink::WebDataConsumerHandle::Result
;
16 class WebDataConsumerHandleImpl::Context
17 : public base::RefCountedThreadSafe
<Context
> {
19 explicit Context(Handle handle
) : handle_(handle
.Pass()) {}
21 const Handle
& handle() { return handle_
; }
24 friend class base::RefCountedThreadSafe
<Context
>;
28 DISALLOW_COPY_AND_ASSIGN(Context
);
31 WebDataConsumerHandleImpl::ReaderImpl::ReaderImpl(
32 scoped_refptr
<Context
> context
,
34 : context_(context
), client_(client
) {
39 WebDataConsumerHandleImpl::ReaderImpl::~ReaderImpl() {
42 Result
WebDataConsumerHandleImpl::ReaderImpl::read(void* data
,
46 // We need this variable definition to avoid a link error.
47 const Flags kNone
= FlagNone
;
48 DCHECK_EQ(flags
, kNone
);
49 DCHECK_LE(size
, std::numeric_limits
<uint32_t>::max());
53 uint32_t size_to_pass
= size
;
54 MojoReadDataFlags flags_to_pass
= MOJO_READ_DATA_FLAG_NONE
;
55 MojoResult rv
= mojo::ReadDataRaw(context_
->handle().get(), data
,
56 &size_to_pass
, flags_to_pass
);
57 if (rv
== MOJO_RESULT_OK
)
58 *read_size
= size_to_pass
;
60 return HandleReadResult(rv
);
63 Result
WebDataConsumerHandleImpl::ReaderImpl::beginRead(const void** buffer
,
66 // We need this variable definition to avoid a link error.
67 const Flags kNone
= FlagNone
;
68 DCHECK_EQ(flags
, kNone
);
73 uint32_t size_to_pass
= 0;
74 MojoReadDataFlags flags_to_pass
= MOJO_READ_DATA_FLAG_NONE
;
76 MojoResult rv
= mojo::BeginReadDataRaw(context_
->handle().get(), buffer
,
77 &size_to_pass
, flags_to_pass
);
78 if (rv
== MOJO_RESULT_OK
)
79 *available
= size_to_pass
;
80 return HandleReadResult(rv
);
83 Result
WebDataConsumerHandleImpl::ReaderImpl::endRead(size_t read_size
) {
84 MojoResult rv
= mojo::EndReadDataRaw(context_
->handle().get(), read_size
);
85 return rv
== MOJO_RESULT_OK
? Ok
: UnexpectedError
;
88 Result
WebDataConsumerHandleImpl::ReaderImpl::HandleReadResult(
89 MojoResult mojo_result
) {
90 switch (mojo_result
) {
93 case MOJO_RESULT_FAILED_PRECONDITION
:
95 case MOJO_RESULT_BUSY
:
97 case MOJO_RESULT_SHOULD_WAIT
:
101 case MOJO_RESULT_RESOURCE_EXHAUSTED
:
102 return ResourceExhausted
;
104 return UnexpectedError
;
108 void WebDataConsumerHandleImpl::ReaderImpl::StartWatching() {
109 handle_watcher_
.Start(
110 context_
->handle().get(), MOJO_HANDLE_SIGNAL_READABLE
,
111 MOJO_DEADLINE_INDEFINITE
,
112 base::Bind(&ReaderImpl::OnHandleGotReadable
, base::Unretained(this)));
115 void WebDataConsumerHandleImpl::ReaderImpl::OnHandleGotReadable(MojoResult
) {
117 client_
->didGetReadable();
120 WebDataConsumerHandleImpl::WebDataConsumerHandleImpl(Handle handle
)
121 : context_(new Context(handle
.Pass())) {
124 WebDataConsumerHandleImpl::~WebDataConsumerHandleImpl() {
127 scoped_ptr
<blink::WebDataConsumerHandle::Reader
>
128 WebDataConsumerHandleImpl::ObtainReader(Client
* client
) {
129 return make_scoped_ptr(obtainReaderInternal(client
));
132 WebDataConsumerHandleImpl::ReaderImpl
*
133 WebDataConsumerHandleImpl::obtainReaderInternal(Client
* client
) {
134 return new ReaderImpl(context_
, client
);
137 } // namespace content