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

Side by Side Diff: Source/modules/serviceworkers/Body.cpp

Issue 786893004: [ServiceWorker] Use BodyStreamBuffer as the body data of the Response object. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years 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
« no previous file with comments | « Source/modules/serviceworkers/Body.h ('k') | Source/modules/serviceworkers/FetchResponseData.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "modules/serviceworkers/Body.h" 6 #include "modules/serviceworkers/Body.h"
7 7
8 #include "bindings/core/v8/ExceptionState.h" 8 #include "bindings/core/v8/ExceptionState.h"
9 #include "bindings/core/v8/ScriptPromiseResolver.h" 9 #include "bindings/core/v8/ScriptPromiseResolver.h"
10 #include "bindings/core/v8/ScriptState.h" 10 #include "bindings/core/v8/ScriptState.h"
11 #include "bindings/core/v8/V8ArrayBuffer.h" 11 #include "bindings/core/v8/V8ArrayBuffer.h"
12 #include "bindings/core/v8/V8ThrowException.h" 12 #include "bindings/core/v8/V8ThrowException.h"
13 #include "core/dom/DOMArrayBuffer.h" 13 #include "core/dom/DOMArrayBuffer.h"
14 #include "core/fileapi/Blob.h" 14 #include "core/fileapi/Blob.h"
15 #include "core/fileapi/FileReaderLoader.h" 15 #include "core/fileapi/FileReaderLoader.h"
16 #include "core/fileapi/FileReaderLoaderClient.h" 16 #include "core/fileapi/FileReaderLoaderClient.h"
17 #include "core/streams/UnderlyingSource.h" 17 #include "core/streams/UnderlyingSource.h"
18 #include "modules/serviceworkers/BodyStreamBuffer.h"
18 19
19 namespace blink { 20 namespace blink {
20 21
22 class Body::BlobHandleReceiver final : public BodyStreamBuffer::BlobHandleCreato rClient {
23 public:
24 explicit BlobHandleReceiver(Body* body)
25 : m_body(body)
26 {
27 }
28 void didCreateBlobHandle(PassRefPtr<BlobDataHandle> handle) override
29 {
30 ASSERT(m_body);
31 m_body->readAsyncFromBlob(handle);
32 m_body = nullptr;
33 }
34 void didFail(PassRefPtrWillBeRawPtr<DOMException> exception) override
35 {
36 ASSERT(m_body);
37 m_body->didBlobHandleReceiveError(exception);
38 m_body = nullptr;
39 }
40 void trace(Visitor* visitor) override
41 {
42 BodyStreamBuffer::BlobHandleCreatorClient::trace(visitor);
43 visitor->trace(m_body);
44 }
45 private:
46 Member<Body> m_body;
47 };
48
21 class Body::ReadableStreamSource : public GarbageCollectedFinalized<ReadableStre amSource>, public UnderlyingSource { 49 class Body::ReadableStreamSource : public GarbageCollectedFinalized<ReadableStre amSource>, public UnderlyingSource {
22 USING_GARBAGE_COLLECTED_MIXIN(ReadableStreamSource); 50 USING_GARBAGE_COLLECTED_MIXIN(ReadableStreamSource);
23 public: 51 public:
24 ReadableStreamSource(Body* body) : m_body(body) { } 52 ReadableStreamSource(Body* body) : m_body(body) { }
25 ~ReadableStreamSource() override { } 53 ~ReadableStreamSource() override { }
26 void pullSource() override { m_body->pullSource(); } 54 void pullSource() override { m_body->pullSource(); }
27 55
28 ScriptPromise cancelSource(ScriptState* scriptState, ScriptValue reason) ove rride 56 ScriptPromise cancelSource(ScriptState* scriptState, ScriptValue reason) ove rride
29 { 57 {
30 return ScriptPromise(); 58 return ScriptPromise();
(...skipping 16 matching lines...) Expand all
47 // ReadableStream object in order to avoid performance regression, 75 // ReadableStream object in order to avoid performance regression,
48 // because currently Chrome cannot handle Streams efficiently 76 // because currently Chrome cannot handle Streams efficiently
49 // especially with ServiceWorker or Blob. 77 // especially with ServiceWorker or Blob.
50 return; 78 return;
51 } 79 }
52 if (m_bodyUsed) { 80 if (m_bodyUsed) {
53 m_stream->error(DOMException::create(InvalidStateError, "The stream is l ocked.")); 81 m_stream->error(DOMException::create(InvalidStateError, "The stream is l ocked."));
54 return; 82 return;
55 } 83 }
56 ASSERT(!m_loader); 84 ASSERT(!m_loader);
57 FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsArrayBuffer; 85 if (buffer()) {
86 // If the body has a body buffer, we read all data from the buffer and
87 // create a blob and then put the data from the blob to |m_stream|.
88 // FIXME: Put the data directry from the buffer.
89 buffer()->readAllAndCreateBlobHandle(contentTypeForBuffer(), new BlobHan dleReceiver(this));
90 return;
91 }
58 RefPtr<BlobDataHandle> blobHandle = blobDataHandle(); 92 RefPtr<BlobDataHandle> blobHandle = blobDataHandle();
59 if (!blobHandle.get()) { 93 if (!blobHandle.get()) {
60 blobHandle = BlobDataHandle::create(BlobData::create(), 0); 94 blobHandle = BlobDataHandle::create(BlobData::create(), 0);
61 } 95 }
62 m_loader = adoptPtr(new FileReaderLoader(readType, this)); 96 readAsyncFromBlob(blobHandle);
63 m_loader->start(executionContext(), blobHandle);
64 } 97 }
65 98
66 ScriptPromise Body::readAsync(ScriptState* scriptState, ResponseType type) 99 ScriptPromise Body::readAsync(ScriptState* scriptState, ResponseType type)
67 { 100 {
68 if (m_bodyUsed) 101 if (m_bodyUsed)
69 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); 102 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read"));
70 103
71 // When the main thread sends a V8::TerminateExecution() signal to a worker 104 // When the main thread sends a V8::TerminateExecution() signal to a worker
72 // thread, any V8 API on the worker thread starts returning an empty 105 // thread, any V8 API on the worker thread starts returning an empty
73 // handle. This can happen in Body::readAsync. To avoid the situation, we 106 // handle. This can happen in Body::readAsync. To avoid the situation, we
(...skipping 24 matching lines...) Expand all
98 case ReadableStream::Closed: 131 case ReadableStream::Closed:
99 case ReadableStream::Errored: 132 case ReadableStream::Errored:
100 m_resolver->resolve(m_stream->closed(scriptState).v8Value()); 133 m_resolver->resolve(m_stream->closed(scriptState).v8Value());
101 return promise; 134 return promise;
102 break; 135 break;
103 } 136 }
104 ASSERT_NOT_REACHED(); 137 ASSERT_NOT_REACHED();
105 return promise; 138 return promise;
106 } 139 }
107 140
141 if (buffer()) {
142 buffer()->readAllAndCreateBlobHandle(contentTypeForBuffer(), new BlobHan dleReceiver(this));
143 return promise;
144 }
145 readAsyncFromBlob(blobDataHandle());
146 return promise;
147 }
148
149 void Body::readAsyncFromBlob(PassRefPtr<BlobDataHandle> handle)
150 {
151 if (m_streamAccessed) {
152 FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsArrayBuffe r;
153 m_loader = adoptPtr(new FileReaderLoader(readType, this));
154 m_loader->start(executionContext(), handle);
155 return;
156 }
108 FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsText; 157 FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsText;
109 RefPtr<BlobDataHandle> blobHandle = blobDataHandle(); 158 RefPtr<BlobDataHandle> blobHandle = handle;
110 if (!blobHandle.get()) { 159 if (!blobHandle.get()) {
111 blobHandle = BlobDataHandle::create(BlobData::create(), 0); 160 blobHandle = BlobDataHandle::create(BlobData::create(), 0);
112 } 161 }
113 switch (type) { 162 switch (m_responseType) {
114 case ResponseAsArrayBuffer: 163 case ResponseAsArrayBuffer:
115 readType = FileReaderLoader::ReadAsArrayBuffer; 164 readType = FileReaderLoader::ReadAsArrayBuffer;
116 break; 165 break;
117 case ResponseAsBlob: 166 case ResponseAsBlob:
118 if (blobHandle->size() != kuint64max) { 167 if (blobHandle->size() != kuint64max) {
119 // If the size of |blobHandle| is set correctly, creates Blob from 168 // If the size of |blobHandle| is set correctly, creates Blob from
120 // it. 169 // it.
121 m_resolver->resolve(Blob::create(blobHandle)); 170 m_resolver->resolve(Blob::create(blobHandle));
122 m_resolver.clear(); 171 m_resolver.clear();
123 return promise; 172 return;
124 } 173 }
125 // If the size is not set, read as ArrayBuffer and create a new blob to 174 // If the size is not set, read as ArrayBuffer and create a new blob to
126 // get the size. 175 // get the size.
127 // FIXME: This workaround is not good for performance. 176 // FIXME: This workaround is not good for performance.
128 // When we will stop using Blob as a base system of Body to support 177 // When we will stop using Blob as a base system of Body to support
129 // stream, this problem should be solved. 178 // stream, this problem should be solved.
130 readType = FileReaderLoader::ReadAsArrayBuffer; 179 readType = FileReaderLoader::ReadAsArrayBuffer;
131 break; 180 break;
132 case ResponseAsFormData: 181 case ResponseAsFormData:
133 // FIXME: Implement this. 182 // FIXME: Implement this.
134 ASSERT_NOT_REACHED(); 183 ASSERT_NOT_REACHED();
135 break; 184 break;
136 case ResponseAsJSON: 185 case ResponseAsJSON:
137 case ResponseAsText: 186 case ResponseAsText:
138 break; 187 break;
139 default: 188 default:
140 ASSERT_NOT_REACHED(); 189 ASSERT_NOT_REACHED();
141 } 190 }
142 191
143 m_loader = adoptPtr(new FileReaderLoader(readType, this)); 192 m_loader = adoptPtr(new FileReaderLoader(readType, this));
144 m_loader->start(executionContext, blobHandle); 193 m_loader->start(m_resolver->scriptState()->executionContext(), blobHandle);
145 194
146 return promise; 195 return;
147 } 196 }
148 197
149 void Body::readAllFromStream(ScriptState* scriptState) 198 void Body::readAllFromStream(ScriptState* scriptState)
150 { 199 {
151 // With the current loading mechanism, the data is loaded atomically. 200 // With the current loading mechanism, the data is loaded atomically.
152 ASSERT(m_stream->isDraining()); 201 ASSERT(m_stream->isDraining());
153 TrackExceptionState es; 202 TrackExceptionState es;
154 // FIXME: Implement and use another |read| method that doesn't 203 // FIXME: Implement and use another |read| method that doesn't
155 // need an exception state and V8ArrayBuffer. 204 // need an exception state and V8ArrayBuffer.
156 ScriptValue value = m_stream->read(scriptState, es); 205 ScriptValue value = m_stream->read(scriptState, es);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 bool Body::bodyUsed() const 255 bool Body::bodyUsed() const
207 { 256 {
208 return m_bodyUsed; 257 return m_bodyUsed;
209 } 258 }
210 259
211 void Body::setBodyUsed() 260 void Body::setBodyUsed()
212 { 261 {
213 m_bodyUsed = true; 262 m_bodyUsed = true;
214 } 263 }
215 264
265 bool Body::streamAccessed() const
266 {
267 return m_streamAccessed;
268 }
269
216 void Body::stop() 270 void Body::stop()
217 { 271 {
218 // Canceling the load will call didFail which will remove the resolver. 272 // Canceling the load will call didFail which will remove the resolver.
219 if (m_resolver) 273 if (m_loader)
220 m_loader->cancel(); 274 m_loader->cancel();
221 } 275 }
222 276
223 bool Body::hasPendingActivity() const 277 bool Body::hasPendingActivity() const
224 { 278 {
225 if (m_resolver) 279 if (m_resolver)
226 return true; 280 return true;
227 if (m_streamAccessed && (m_stream->state() == ReadableStream::Readable || m_ stream->state() == ReadableStream::Waiting)) 281 if (m_streamAccessed && (m_stream->state() == ReadableStream::Readable || m_ stream->state() == ReadableStream::Waiting))
228 return true; 282 return true;
229 return false; 283 return false;
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 return; 421 return;
368 422
369 if (m_resolver) { 423 if (m_resolver) {
370 // FIXME: We should reject the promise. 424 // FIXME: We should reject the promise.
371 m_resolver->resolve(""); 425 m_resolver->resolve("");
372 m_resolver.clear(); 426 m_resolver.clear();
373 } 427 }
374 m_stream->error(DOMException::create(NetworkError, "network error")); 428 m_stream->error(DOMException::create(NetworkError, "network error"));
375 } 429 }
376 430
431 void Body::didBlobHandleReceiveError(PassRefPtrWillBeRawPtr<DOMException> except ion)
432 {
433 if (!m_resolver)
434 return;
435 m_resolver->reject(exception);
436 m_resolver.clear();
437 }
438
377 } // namespace blink 439 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/serviceworkers/Body.h ('k') | Source/modules/serviceworkers/FetchResponseData.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698