Hide Google logo and custom launcher page in app list for non-Google search engines.
[chromium-blink-merge.git] / content / child / web_data_consumer_handle_impl.cc
bloba310238d67c00ca0b7bfca8427108b47d8673566
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 "base/profiler/scoped_tracker.h"
11 #include "third_party/mojo/src/mojo/public/c/system/types.h"
13 namespace content {
15 typedef blink::WebDataConsumerHandle::Result Result;
17 WebDataConsumerHandleImpl::WebDataConsumerHandleImpl(Handle handle)
18 : handle_(handle.Pass()), client_(nullptr) {}
20 WebDataConsumerHandleImpl::~WebDataConsumerHandleImpl() {}
22 Result WebDataConsumerHandleImpl::read(
23 void* data,
24 size_t size,
25 Flags flags,
26 size_t* read_size) {
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());
32 *read_size = 0;
34 uint32_t size_to_pass = size;
35 MojoReadDataFlags flags_to_pass = MOJO_READ_DATA_FLAG_NONE;
36 MojoResult rv =
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);
50 *buffer = nullptr;
51 *available = 0;
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);
65 return
66 rv == MOJO_RESULT_OK ? Ok : UnexpectedError;
69 void WebDataConsumerHandleImpl::registerClient(Client* client) {
70 DCHECK(!client_);
71 DCHECK(client);
72 client_ = client;
74 handle_watcher_.Start(
75 handle_.get(),
76 MOJO_HANDLE_SIGNAL_READABLE,
77 MOJO_DEADLINE_INDEFINITE,
78 base::Bind(&WebDataConsumerHandleImpl::OnHandleGotReadable,
79 base::Unretained(this)));
82 void WebDataConsumerHandleImpl::unregisterClient() {
83 client_ = nullptr;
84 handle_watcher_.Stop();
87 Result WebDataConsumerHandleImpl::HandleReadResult(MojoResult mojo_result) {
88 switch (mojo_result) {
89 case MOJO_RESULT_OK:
90 return Ok;
91 case MOJO_RESULT_FAILED_PRECONDITION:
92 return Done;
93 case MOJO_RESULT_BUSY:
94 return Busy;
95 case MOJO_RESULT_SHOULD_WAIT:
96 if (client_) {
97 handle_watcher_.Start(
98 handle_.get(),
99 MOJO_HANDLE_SIGNAL_READABLE,
100 MOJO_DEADLINE_INDEFINITE,
101 base::Bind(&WebDataConsumerHandleImpl::OnHandleGotReadable,
102 base::Unretained(this)));
104 return ShouldWait;
105 case MOJO_RESULT_RESOURCE_EXHAUSTED:
106 return ResourceExhausted;
107 default:
108 return UnexpectedError;
112 void WebDataConsumerHandleImpl::OnHandleGotReadable(MojoResult) {
113 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455434 is
114 // fixed.
115 tracked_objects::ScopedTracker tracking_profile(
116 FROM_HERE_WITH_EXPLICIT_FUNCTION(
117 "455434 WebDataConsumerHandleImpl::OnHandleGotReadable"));
118 DCHECK(client_);
119 client_->didGetReadable();
122 } // namespace content