Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(198)

Side by Side Diff: third_party/WebKit/Source/modules/fetch/BytesConsumerForDataConsumerHandle.cpp

Issue 2046203003: [Fetch API] Introduce BytesConsumer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include "modules/fetch/BytesConsumerForDataConsumerHandle.h"
6
7 #include <algorithm>
8 #include <string.h>
9
10 namespace blink {
11
12 BytesConsumerForDataConsumerHandle::BytesConsumerForDataConsumerHandle(std::uniq ue_ptr<FetchDataConsumerHandle> handle)
13 : m_reader(handle->obtainReader(this))
14 {
15 }
16
17 BytesConsumerForDataConsumerHandle::~BytesConsumerForDataConsumerHandle() {}
18
19 BytesConsumer::Result BytesConsumerForDataConsumerHandle::read(char* buffer, siz e_t size, size_t* readSize)
20 {
21 *readSize = 0;
22 if (m_state == InternalState::Closed)
23 return Result::Done;
24 if (m_state == InternalState::Errored)
25 return Result::Error;
26
27 WebDataConsumerHandle::Result r = m_reader->read(buffer, size, WebDataConsum erHandle::FlagNone, readSize);
28 switch (r) {
29 case WebDataConsumerHandle::Ok:
30 return Result::Ok;
31 case WebDataConsumerHandle::ShouldWait:
32 return Result::ShouldWait;
33 case WebDataConsumerHandle::Done:
34 close();
hiroshige 2016/07/26 06:48:12 Calling close() here can cause onStateChange() cal
yhirano 2016/07/27 13:19:52 Done.
35 return Result::Done;
36 default:
37 error();
hiroshige 2016/07/26 06:48:12 ditto.
yhirano 2016/07/27 13:19:52 Done.
38 return Result::Error;
39 }
40 }
41
42 BytesConsumer::Result BytesConsumerForDataConsumerHandle::beginRead(const char** buffer, size_t* available)
43 {
44 *buffer = nullptr;
45 *available = 0;
46 if (m_state == InternalState::Closed)
47 return Result::Done;
48 if (m_state == InternalState::Errored)
49 return Result::Error;
50
51 WebDataConsumerHandle::Result r = m_reader->beginRead(reinterpret_cast<const void**>(buffer), WebDataConsumerHandle::FlagNone, available);
52 switch (r) {
53 case WebDataConsumerHandle::Ok:
54 return Result::Ok;
55 case WebDataConsumerHandle::ShouldWait:
56 return Result::ShouldWait;
57 case WebDataConsumerHandle::Done:
58 close();
hiroshige 2016/07/26 06:48:11 ditto.
yhirano 2016/07/27 13:19:52 Done.
59 return Result::Done;
60 default:
61 error();
hiroshige 2016/07/26 06:48:12 ditto.
yhirano 2016/07/27 13:19:52 Done.
62 return Result::Error;
63 }
64 }
65
66 BytesConsumer::Result BytesConsumerForDataConsumerHandle::endRead(size_t read)
67 {
68 DCHECK(m_state == InternalState::Readable || m_state == InternalState::Waiti ng);
69 WebDataConsumerHandle::Result r = m_reader->endRead(read);
70 if (r == WebDataConsumerHandle::Ok)
71 return Result::Ok;
72 error();
hiroshige 2016/07/26 06:48:12 ditto.
yhirano 2016/07/27 13:19:52 Done.
73 return Result::Error;
74 }
75
76 PassRefPtr<BlobDataHandle> BytesConsumerForDataConsumerHandle::drainAsBlobDataHa ndle(BlobSizePolicy policy)
77 {
78 if (!m_reader)
79 return nullptr;
80 if (policy == BlobSizePolicy::DisallowBlobWithInvalidSize)
81 return m_reader->drainAsBlobDataHandle(FetchDataConsumerHandle::Reader:: DisallowBlobWithInvalidSize);
hiroshige 2016/07/26 06:48:12 Shouldn't we call close() if the returned value is
yhirano 2016/07/27 13:19:52 Done.
82 DCHECK_EQ(BlobSizePolicy::AllowBlobWithInvalidSize, policy);
83 return m_reader->drainAsBlobDataHandle(FetchDataConsumerHandle::Reader::Allo wBlobWithInvalidSize);
hiroshige 2016/07/26 06:48:11 ditto.
yhirano 2016/07/27 13:19:52 Done.
84 }
85
86 PassRefPtr<EncodedFormData> BytesConsumerForDataConsumerHandle::drainAsFormData( )
87 {
88 if (!m_reader)
89 return nullptr;
90 return m_reader->drainAsFormData();
hiroshige 2016/07/26 06:48:12 ditto.
yhirano 2016/07/27 13:19:52 Done.
91 }
92
93 void BytesConsumerForDataConsumerHandle::setClient(BytesConsumer::Client* client )
94 {
95 DCHECK(!m_client);
96 DCHECK(client);
97 m_client = client;
98 }
99
100 void BytesConsumerForDataConsumerHandle::clearClient()
101 {
102 DCHECK(m_client);
103 m_client = nullptr;
104 }
105
106 void BytesConsumerForDataConsumerHandle::cancel()
107 {
108 if (m_state == InternalState::Readable || m_state == InternalState::Waiting) {
109 // We don't want the client to be notified in this case.
110 BytesConsumer::Client* client = m_client;
111 m_client = nullptr;
112 close();
113 m_client = client;
114 }
115 }
116
117 BytesConsumer::PublicState BytesConsumerForDataConsumerHandle::getPublicState() const
118 {
119 return getPublicStateFromInternalState(m_state);
120 }
121
122 void BytesConsumerForDataConsumerHandle::didGetReadable()
123 {
124 DCHECK(m_state == InternalState::Readable || m_state == InternalState::Waiti ng);
125 // Perform zero-length read to call check handle's status.
126 size_t readSize;
127 WebDataConsumerHandle::Result result = m_reader->read(nullptr, 0, WebDataCon sumerHandle::FlagNone, &readSize);
128 switch (result) {
129 case WebDataConsumerHandle::Ok:
130 case WebDataConsumerHandle::ShouldWait:
131 if (m_client)
132 m_client->onStateChange();
133 return;
134 case WebDataConsumerHandle::Done:
135 close();
136 return;
137 case WebDataConsumerHandle::Busy:
138 case WebDataConsumerHandle::ResourceExhausted:
139 case WebDataConsumerHandle::UnexpectedError:
hiroshige 2016/07/26 06:48:12 nit: Can we make these lines "default:", as done i
yhirano 2016/07/27 13:19:52 My understanding is "default" is not preferred bec
hiroshige 2016/07/28 06:40:12 Agree. Then can we turn |default| in BytesConsumer
yhirano 2016/07/28 07:32:31 Thanks, done.
140 error();
141 return;
142 }
143 return;
144 }
145
146 DEFINE_TRACE(BytesConsumerForDataConsumerHandle)
147 {
148 visitor->trace(m_client);
149 BytesConsumer::trace(visitor);
150 }
151
152 void BytesConsumerForDataConsumerHandle::close()
153 {
154 if (m_state == InternalState::Closed)
155 return;
156 DCHECK(m_state == InternalState::Readable || m_state == InternalState::Waiti ng);
157 m_state = InternalState::Closed;
158 m_reader = nullptr;
159 if (m_client)
160 m_client->onStateChange();
161 }
162
163 void BytesConsumerForDataConsumerHandle::error()
164 {
165 if (m_state == InternalState::Errored)
166 return;
167 DCHECK(m_state == InternalState::Readable || m_state == InternalState::Waiti ng);
168 m_state = InternalState::Errored;
169 m_reader = nullptr;
170 m_error = Error("error");
171 if (m_client)
172 m_client->onStateChange();
173 }
174
175 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698