| Index: content/child/web_data_producer_handle_impl.cc
|
| diff --git a/content/child/web_data_producer_handle_impl.cc b/content/child/web_data_producer_handle_impl.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..411cf5343fc6809440261df4beac16731adb1611
|
| --- /dev/null
|
| +++ b/content/child/web_data_producer_handle_impl.cc
|
| @@ -0,0 +1,113 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/child/web_data_producer_handle_impl.h"
|
| +
|
| +#include <limits>
|
| +#include "base/bind.h"
|
| +#include "base/logging.h"
|
| +#include "mojo/public/c/system/types.h"
|
| +
|
| +namespace content {
|
| +
|
| +using Result = blink::WebDataProducerHandle::Result;
|
| +
|
| +WebDataProducerHandleImpl::WebDataProducerHandleImpl(Handle handle)
|
| + : handle_(handle.Pass()), client_(nullptr) {}
|
| +
|
| +WebDataProducerHandleImpl::~WebDataProducerHandleImpl() {}
|
| +
|
| +Result WebDataProducerHandleImpl::write(const void* data,
|
| + size_t size,
|
| + Flags flags,
|
| + size_t* written_size) {
|
| + // We need this variable definition to avoid a link error.
|
| + const Flags kNone = FlagNone;
|
| + DCHECK_EQ(flags, kNone);
|
| + DCHECK_LE(size, std::numeric_limits<uint32_t>::max());
|
| +
|
| + *written_size = 0;
|
| +
|
| + uint32_t size_to_pass = size;
|
| + MojoReadDataFlags flags_to_pass = MOJO_WRITE_DATA_FLAG_NONE;
|
| + MojoResult rv =
|
| + mojo::WriteDataRaw(handle_.get(), data, &size_to_pass, flags_to_pass);
|
| + if (rv == MOJO_RESULT_OK)
|
| + *written_size = size_to_pass;
|
| +
|
| + return HandleWriteResult(rv);
|
| +}
|
| +
|
| +Result WebDataProducerHandleImpl::beginWrite(void** buffer,
|
| + Flags flags,
|
| + size_t* available) {
|
| + // We need this variable definition to avoid a link error.
|
| + const Flags kNone = FlagNone;
|
| + DCHECK_EQ(flags, kNone);
|
| +
|
| + *buffer = nullptr;
|
| + *available = 0;
|
| +
|
| + uint32_t size_to_pass = 0;
|
| + MojoReadDataFlags flags_to_pass = MOJO_WRITE_DATA_FLAG_NONE;
|
| +
|
| + MojoResult rv = mojo::BeginWriteDataRaw(handle_.get(), buffer,
|
| + &size_to_pass, flags_to_pass);
|
| + if (rv == MOJO_RESULT_OK)
|
| + *available = size_to_pass;
|
| + return HandleWriteResult(rv);
|
| +}
|
| +
|
| +Result WebDataProducerHandleImpl::endWrite(size_t written_size) {
|
| + MojoResult rv = mojo::EndWriteDataRaw(handle_.get(), written_size);
|
| + return
|
| + rv == MOJO_RESULT_OK ? Ok : UnexpectedError;
|
| +}
|
| +
|
| +void WebDataProducerHandleImpl::registerClient(Client* client) {
|
| + DCHECK(!client_);
|
| + DCHECK(client);
|
| + client_ = client;
|
| +
|
| + handle_watcher_.Start(
|
| + handle_.get(),
|
| + MOJO_HANDLE_SIGNAL_WRITABLE,
|
| + MOJO_DEADLINE_INDEFINITE,
|
| + base::Bind(&WebDataProducerHandleImpl::OnHandleGotWritable,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void WebDataProducerHandleImpl::unregisterClient() {
|
| + DCHECK(client_);
|
| + client_ = nullptr;
|
| + handle_watcher_.Stop();
|
| +}
|
| +
|
| +Result WebDataProducerHandleImpl::HandleWriteResult(MojoResult mojo_result) {
|
| + switch (mojo_result) {
|
| + case MOJO_RESULT_OK:
|
| + return Ok;
|
| + case MOJO_RESULT_FAILED_PRECONDITION:
|
| + return AlreadyClosed;
|
| + case MOJO_RESULT_SHOULD_WAIT:
|
| + if (client_) {
|
| + handle_watcher_.Start(
|
| + handle_.get(),
|
| + MOJO_HANDLE_SIGNAL_WRITABLE,
|
| + MOJO_DEADLINE_INDEFINITE,
|
| + base::Bind(&WebDataProducerHandleImpl::OnHandleGotWritable,
|
| + base::Unretained(this)));
|
| + }
|
| + return ShouldWait;
|
| + default:
|
| + return UnexpectedError;
|
| + }
|
| +}
|
| +
|
| +void WebDataProducerHandleImpl::OnHandleGotWritable(MojoResult) {
|
| + DCHECK(client_);
|
| + client_->didGetWritable();
|
| +}
|
| +
|
| +} // namespace content
|
|
|