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 typedef blink::WebDataConsumerHandle::Result Result
;
16 WebDataConsumerHandleImpl::WebDataConsumerHandleImpl(Handle handle
)
17 : handle_(handle
.Pass()), client_(nullptr) {}
19 WebDataConsumerHandleImpl::~WebDataConsumerHandleImpl() {}
21 Result
WebDataConsumerHandleImpl::read(
26 // We need this variable definition to avoid a link error.
27 const Flags kNone
= FlagNone
;
28 DCHECK_EQ(flags
, kNone
);
29 DCHECK_LE(size
, std::numeric_limits
<uint32_t>::max());
33 uint32_t size_to_pass
= size
;
34 MojoReadDataFlags flags_to_pass
= MOJO_READ_DATA_FLAG_NONE
;
36 mojo::ReadDataRaw(handle_
.get(), data
, &size_to_pass
, flags_to_pass
);
37 if (rv
== MOJO_RESULT_OK
)
38 *read_size
= size_to_pass
;
40 return HandleReadResult(rv
);
43 Result
WebDataConsumerHandleImpl::beginRead(
44 const void** buffer
, Flags flags
, size_t* available
) {
45 // We need this variable definition to avoid a link error.
46 const Flags kNone
= FlagNone
;
47 DCHECK_EQ(flags
, kNone
);
52 uint32_t size_to_pass
= 0;
53 MojoReadDataFlags flags_to_pass
= MOJO_READ_DATA_FLAG_NONE
;
55 MojoResult rv
= mojo::BeginReadDataRaw(handle_
.get(), buffer
,
56 &size_to_pass
, flags_to_pass
);
57 if (rv
== MOJO_RESULT_OK
)
58 *available
= size_to_pass
;
59 return HandleReadResult(rv
);
62 Result
WebDataConsumerHandleImpl::endRead(size_t read_size
) {
63 MojoResult rv
= mojo::EndReadDataRaw(handle_
.get(), read_size
);
65 rv
== MOJO_RESULT_OK
? Ok
: UnexpectedError
;
68 void WebDataConsumerHandleImpl::registerClient(Client
* client
) {
73 handle_watcher_
.Start(
75 MOJO_HANDLE_SIGNAL_READABLE
,
76 MOJO_DEADLINE_INDEFINITE
,
77 base::Bind(&WebDataConsumerHandleImpl::OnHandleGotReadable
,
78 base::Unretained(this)));
81 void WebDataConsumerHandleImpl::unregisterClient() {
83 handle_watcher_
.Stop();
86 Result
WebDataConsumerHandleImpl::HandleReadResult(MojoResult mojo_result
) {
87 switch (mojo_result
) {
90 case MOJO_RESULT_FAILED_PRECONDITION
:
92 case MOJO_RESULT_BUSY
:
94 case MOJO_RESULT_SHOULD_WAIT
:
96 handle_watcher_
.Start(
98 MOJO_HANDLE_SIGNAL_READABLE
,
99 MOJO_DEADLINE_INDEFINITE
,
100 base::Bind(&WebDataConsumerHandleImpl::OnHandleGotReadable
,
101 base::Unretained(this)));
104 case MOJO_RESULT_RESOURCE_EXHAUSTED
:
105 return ResourceExhausted
;
107 return UnexpectedError
;
111 void WebDataConsumerHandleImpl::OnHandleGotReadable(MojoResult
) {
113 client_
->didGetReadable();
116 } // namespace content