Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / web_data_consumer_handle_impl.cc
bloba26656ed3101a4bb855d7225651a2be38befd68b
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"
7 #include <limits>
8 #include "base/bind.h"
9 #include "base/logging.h"
10 #include "third_party/mojo/src/mojo/public/c/system/types.h"
12 namespace content {
14 typedef blink::WebDataConsumerHandle::Result Result;
16 WebDataConsumerHandleImpl::WebDataConsumerHandleImpl(Handle handle)
17 : handle_(handle.Pass()), client_(nullptr) {}
19 WebDataConsumerHandleImpl::~WebDataConsumerHandleImpl() {}
21 Result WebDataConsumerHandleImpl::read(
22 void* data,
23 size_t size,
24 Flags flags,
25 size_t* read_size) {
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());
31 *read_size = 0;
33 uint32_t size_to_pass = size;
34 MojoReadDataFlags flags_to_pass = MOJO_READ_DATA_FLAG_NONE;
35 MojoResult rv =
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);
49 *buffer = nullptr;
50 *available = 0;
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);
64 return
65 rv == MOJO_RESULT_OK ? Ok : UnexpectedError;
68 void WebDataConsumerHandleImpl::registerClient(Client* client) {
69 DCHECK(!client_);
70 DCHECK(client);
71 client_ = client;
73 handle_watcher_.Start(
74 handle_.get(),
75 MOJO_HANDLE_SIGNAL_READABLE,
76 MOJO_DEADLINE_INDEFINITE,
77 base::Bind(&WebDataConsumerHandleImpl::OnHandleGotReadable,
78 base::Unretained(this)));
81 void WebDataConsumerHandleImpl::unregisterClient() {
82 client_ = nullptr;
83 handle_watcher_.Stop();
86 Result WebDataConsumerHandleImpl::HandleReadResult(MojoResult mojo_result) {
87 switch (mojo_result) {
88 case MOJO_RESULT_OK:
89 return Ok;
90 case MOJO_RESULT_FAILED_PRECONDITION:
91 return Done;
92 case MOJO_RESULT_BUSY:
93 return Busy;
94 case MOJO_RESULT_SHOULD_WAIT:
95 if (client_) {
96 handle_watcher_.Start(
97 handle_.get(),
98 MOJO_HANDLE_SIGNAL_READABLE,
99 MOJO_DEADLINE_INDEFINITE,
100 base::Bind(&WebDataConsumerHandleImpl::OnHandleGotReadable,
101 base::Unretained(this)));
103 return ShouldWait;
104 case MOJO_RESULT_RESOURCE_EXHAUSTED:
105 return ResourceExhausted;
106 default:
107 return UnexpectedError;
111 void WebDataConsumerHandleImpl::OnHandleGotReadable(MojoResult) {
112 DCHECK(client_);
113 client_->didGetReadable();
116 } // namespace content