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 "base/profiler/scoped_tracker.h"
11 #include "third_party/mojo/src/mojo/public/c/system/types.h"
15 typedef blink::WebDataConsumerHandle::Result Result
;
17 WebDataConsumerHandleImpl::WebDataConsumerHandleImpl(Handle handle
)
18 : handle_(handle
.Pass()), client_(nullptr) {}
20 WebDataConsumerHandleImpl::~WebDataConsumerHandleImpl() {}
22 Result
WebDataConsumerHandleImpl::read(
27 // We need this variable definition to avoid a link error.
28 const Flags kNone
= FlagNone
;
29 DCHECK_EQ(flags
, kNone
);
30 DCHECK_LE(size
, std::numeric_limits
<uint32_t>::max());
34 uint32_t size_to_pass
= size
;
35 MojoReadDataFlags flags_to_pass
= MOJO_READ_DATA_FLAG_NONE
;
37 mojo::ReadDataRaw(handle_
.get(), data
, &size_to_pass
, flags_to_pass
);
38 if (rv
== MOJO_RESULT_OK
)
39 *read_size
= size_to_pass
;
41 return HandleReadResult(rv
);
44 Result
WebDataConsumerHandleImpl::beginRead(
45 const void** buffer
, Flags flags
, size_t* available
) {
46 // We need this variable definition to avoid a link error.
47 const Flags kNone
= FlagNone
;
48 DCHECK_EQ(flags
, kNone
);
53 uint32_t size_to_pass
= 0;
54 MojoReadDataFlags flags_to_pass
= MOJO_READ_DATA_FLAG_NONE
;
56 MojoResult rv
= mojo::BeginReadDataRaw(handle_
.get(), buffer
,
57 &size_to_pass
, flags_to_pass
);
58 if (rv
== MOJO_RESULT_OK
)
59 *available
= size_to_pass
;
60 return HandleReadResult(rv
);
63 Result
WebDataConsumerHandleImpl::endRead(size_t read_size
) {
64 MojoResult rv
= mojo::EndReadDataRaw(handle_
.get(), read_size
);
66 rv
== MOJO_RESULT_OK
? Ok
: UnexpectedError
;
69 void WebDataConsumerHandleImpl::registerClient(Client
* client
) {
74 handle_watcher_
.Start(
76 MOJO_HANDLE_SIGNAL_READABLE
,
77 MOJO_DEADLINE_INDEFINITE
,
78 base::Bind(&WebDataConsumerHandleImpl::OnHandleGotReadable
,
79 base::Unretained(this)));
82 void WebDataConsumerHandleImpl::unregisterClient() {
84 handle_watcher_
.Stop();
87 Result
WebDataConsumerHandleImpl::HandleReadResult(MojoResult mojo_result
) {
88 switch (mojo_result
) {
91 case MOJO_RESULT_FAILED_PRECONDITION
:
93 case MOJO_RESULT_BUSY
:
95 case MOJO_RESULT_SHOULD_WAIT
:
97 handle_watcher_
.Start(
99 MOJO_HANDLE_SIGNAL_READABLE
,
100 MOJO_DEADLINE_INDEFINITE
,
101 base::Bind(&WebDataConsumerHandleImpl::OnHandleGotReadable
,
102 base::Unretained(this)));
105 case MOJO_RESULT_RESOURCE_EXHAUSTED
:
106 return ResourceExhausted
;
108 return UnexpectedError
;
112 void WebDataConsumerHandleImpl::OnHandleGotReadable(MojoResult
) {
113 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455434 is
115 tracked_objects::ScopedTracker
tracking_profile(
116 FROM_HERE_WITH_EXPLICIT_FUNCTION(
117 "455434 WebDataConsumerHandleImpl::OnHandleGotReadable"));
119 client_
->didGetReadable();
122 } // namespace content