Roll src/third_party/WebKit bbcd5f6:83d0ae8 (svn 201827:201828)
[chromium-blink-merge.git] / content / child / web_data_consumer_handle_impl.cc
blobfff3110ce801b7adbe6d423cf555afe03a25673f
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 using Result = blink::WebDataConsumerHandle::Result;
16 class WebDataConsumerHandleImpl::Context
17 : public base::RefCountedThreadSafe<Context> {
18 public:
19 explicit Context(Handle handle) : handle_(handle.Pass()) {}
21 const Handle& handle() { return handle_; }
23 private:
24 friend class base::RefCountedThreadSafe<Context>;
25 ~Context() {}
26 Handle handle_;
28 DISALLOW_COPY_AND_ASSIGN(Context);
31 WebDataConsumerHandleImpl::ReaderImpl::ReaderImpl(
32 scoped_refptr<Context> context,
33 Client* client)
34 : context_(context), client_(client) {
35 if (client_)
36 StartWatching();
39 WebDataConsumerHandleImpl::ReaderImpl::~ReaderImpl() {
42 Result WebDataConsumerHandleImpl::ReaderImpl::read(void* data,
43 size_t size,
44 Flags flags,
45 size_t* read_size) {
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());
51 *read_size = 0;
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,
64 Flags flags,
65 size_t* available) {
66 // We need this variable definition to avoid a link error.
67 const Flags kNone = FlagNone;
68 DCHECK_EQ(flags, kNone);
70 *buffer = nullptr;
71 *available = 0;
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) {
91 case MOJO_RESULT_OK:
92 return Ok;
93 case MOJO_RESULT_FAILED_PRECONDITION:
94 return Done;
95 case MOJO_RESULT_BUSY:
96 return Busy;
97 case MOJO_RESULT_SHOULD_WAIT:
98 if (client_)
99 StartWatching();
100 return ShouldWait;
101 case MOJO_RESULT_RESOURCE_EXHAUSTED:
102 return ResourceExhausted;
103 default:
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) {
116 DCHECK(client_);
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 const char* WebDataConsumerHandleImpl::debugName() const {
138 return "WebDataConsumerHandleImpl";
141 } // namespace content