Chromium Code Reviews| OLD | NEW |
|---|---|
| 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::BlobHandleResolver final : public BodyStreamBuffer::BlobHandleCreato rClient { | |
|
yhirano
2014/12/12 04:31:16
I don't understand why this class is named as "Res
horo
2014/12/12 05:30:47
I think BlobHandle"Receiver" is a better name.
Cha
| |
| 23 public: | |
| 24 explicit BlobHandleResolver(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->didBlobHandleResolveError(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 Loading... | |
| 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(contentType(), new BlobHandleResolv er(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 } |
| 96 FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsArrayBuffer; | |
|
yhirano
2014/12/12 04:31:16
You can call readAsyncFromBlob here.
horo
2014/12/12 05:30:47
Done.
| |
| 62 m_loader = adoptPtr(new FileReaderLoader(readType, this)); | 97 m_loader = adoptPtr(new FileReaderLoader(readType, this)); |
| 63 m_loader->start(executionContext(), blobHandle); | 98 m_loader->start(executionContext(), blobHandle); |
| 64 } | 99 } |
| 65 | 100 |
| 66 ScriptPromise Body::readAsync(ScriptState* scriptState, ResponseType type) | 101 ScriptPromise Body::readAsync(ScriptState* scriptState, ResponseType type) |
| 67 { | 102 { |
| 68 if (m_bodyUsed) | 103 if (m_bodyUsed) |
| 69 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); | 104 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); |
| 70 | 105 |
| 71 // When the main thread sends a V8::TerminateExecution() signal to a worker | 106 // When the main thread sends a V8::TerminateExecution() signal to a worker |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 98 case ReadableStream::Closed: | 133 case ReadableStream::Closed: |
| 99 case ReadableStream::Errored: | 134 case ReadableStream::Errored: |
| 100 m_resolver->resolve(m_stream->closed(scriptState).v8Value()); | 135 m_resolver->resolve(m_stream->closed(scriptState).v8Value()); |
| 101 return promise; | 136 return promise; |
| 102 break; | 137 break; |
| 103 } | 138 } |
| 104 ASSERT_NOT_REACHED(); | 139 ASSERT_NOT_REACHED(); |
| 105 return promise; | 140 return promise; |
| 106 } | 141 } |
| 107 | 142 |
| 143 if (buffer()) { | |
| 144 buffer()->readAllAndCreateBlobHandle(contentType(), new BlobHandleResolv er(this)); | |
| 145 return promise; | |
| 146 } | |
| 147 readAsyncFromBlob(blobDataHandle()); | |
| 148 return promise; | |
| 149 } | |
| 150 | |
| 151 void Body::readAsyncFromBlob(PassRefPtr<BlobDataHandle> handle) | |
| 152 { | |
| 153 if (m_streamAccessed) { | |
| 154 FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsArrayBuffe r; | |
| 155 m_loader = adoptPtr(new FileReaderLoader(readType, this)); | |
| 156 m_loader->start(executionContext(), handle); | |
| 157 return; | |
| 158 } | |
| 108 FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsText; | 159 FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsText; |
| 109 RefPtr<BlobDataHandle> blobHandle = blobDataHandle(); | 160 RefPtr<BlobDataHandle> blobHandle = handle; |
| 110 if (!blobHandle.get()) { | 161 if (!blobHandle.get()) { |
| 111 blobHandle = BlobDataHandle::create(BlobData::create(), 0); | 162 blobHandle = BlobDataHandle::create(BlobData::create(), 0); |
| 112 } | 163 } |
| 113 switch (type) { | 164 switch (m_responseType) { |
| 114 case ResponseAsArrayBuffer: | 165 case ResponseAsArrayBuffer: |
| 115 readType = FileReaderLoader::ReadAsArrayBuffer; | 166 readType = FileReaderLoader::ReadAsArrayBuffer; |
| 116 break; | 167 break; |
| 117 case ResponseAsBlob: | 168 case ResponseAsBlob: |
| 118 if (blobHandle->size() != kuint64max) { | 169 if (blobHandle->size() != kuint64max) { |
| 119 // If the size of |blobHandle| is set correctly, creates Blob from | 170 // If the size of |blobHandle| is set correctly, creates Blob from |
| 120 // it. | 171 // it. |
| 121 m_resolver->resolve(Blob::create(blobHandle)); | 172 m_resolver->resolve(Blob::create(blobHandle)); |
| 122 m_resolver.clear(); | 173 m_resolver.clear(); |
| 123 return promise; | 174 return; |
| 124 } | 175 } |
| 125 // If the size is not set, read as ArrayBuffer and create a new blob to | 176 // If the size is not set, read as ArrayBuffer and create a new blob to |
| 126 // get the size. | 177 // get the size. |
| 127 // FIXME: This workaround is not good for performance. | 178 // FIXME: This workaround is not good for performance. |
| 128 // When we will stop using Blob as a base system of Body to support | 179 // When we will stop using Blob as a base system of Body to support |
| 129 // stream, this problem should be solved. | 180 // stream, this problem should be solved. |
| 130 readType = FileReaderLoader::ReadAsArrayBuffer; | 181 readType = FileReaderLoader::ReadAsArrayBuffer; |
| 131 break; | 182 break; |
| 132 case ResponseAsFormData: | 183 case ResponseAsFormData: |
| 133 // FIXME: Implement this. | 184 // FIXME: Implement this. |
| 134 ASSERT_NOT_REACHED(); | 185 ASSERT_NOT_REACHED(); |
| 135 break; | 186 break; |
| 136 case ResponseAsJSON: | 187 case ResponseAsJSON: |
| 137 case ResponseAsText: | 188 case ResponseAsText: |
| 138 break; | 189 break; |
| 139 default: | 190 default: |
| 140 ASSERT_NOT_REACHED(); | 191 ASSERT_NOT_REACHED(); |
| 141 } | 192 } |
| 142 | 193 |
| 143 m_loader = adoptPtr(new FileReaderLoader(readType, this)); | 194 m_loader = adoptPtr(new FileReaderLoader(readType, this)); |
| 144 m_loader->start(executionContext, blobHandle); | 195 m_loader->start(m_resolver->scriptState()->executionContext(), blobHandle); |
| 145 | 196 |
| 146 return promise; | 197 return; |
| 147 } | 198 } |
| 148 | 199 |
| 149 void Body::readAllFromStream(ScriptState* scriptState) | 200 void Body::readAllFromStream(ScriptState* scriptState) |
| 150 { | 201 { |
| 151 // With the current loading mechanism, the data is loaded atomically. | 202 // With the current loading mechanism, the data is loaded atomically. |
| 152 ASSERT(m_stream->isDraining()); | 203 ASSERT(m_stream->isDraining()); |
| 153 TrackExceptionState es; | 204 TrackExceptionState es; |
| 154 // FIXME: Implement and use another |read| method that doesn't | 205 // FIXME: Implement and use another |read| method that doesn't |
| 155 // need an exception state and V8ArrayBuffer. | 206 // need an exception state and V8ArrayBuffer. |
| 156 ScriptValue value = m_stream->read(scriptState, es); | 207 ScriptValue value = m_stream->read(scriptState, es); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 bool Body::bodyUsed() const | 257 bool Body::bodyUsed() const |
| 207 { | 258 { |
| 208 return m_bodyUsed; | 259 return m_bodyUsed; |
| 209 } | 260 } |
| 210 | 261 |
| 211 void Body::setBodyUsed() | 262 void Body::setBodyUsed() |
| 212 { | 263 { |
| 213 m_bodyUsed = true; | 264 m_bodyUsed = true; |
| 214 } | 265 } |
| 215 | 266 |
| 267 bool Body::streamAccessed() const | |
| 268 { | |
| 269 return m_streamAccessed; | |
| 270 } | |
| 271 | |
| 216 void Body::stop() | 272 void Body::stop() |
| 217 { | 273 { |
| 218 // Canceling the load will call didFail which will remove the resolver. | 274 // Canceling the load will call didFail which will remove the resolver. |
| 219 if (m_resolver) | 275 if (m_resolver && m_loader) |
|
yhirano
2014/12/12 04:31:16
Just |if (m_loader)| is fine, I think.
horo
2014/12/12 05:30:47
Done.
| |
| 220 m_loader->cancel(); | 276 m_loader->cancel(); |
| 221 } | 277 } |
| 222 | 278 |
| 223 bool Body::hasPendingActivity() const | 279 bool Body::hasPendingActivity() const |
| 224 { | 280 { |
| 225 if (m_resolver) | 281 if (m_resolver) |
| 226 return true; | 282 return true; |
| 227 if (m_streamAccessed && (m_stream->state() == ReadableStream::Readable || m_ stream->state() == ReadableStream::Waiting)) | 283 if (m_streamAccessed && (m_stream->state() == ReadableStream::Readable || m_ stream->state() == ReadableStream::Waiting)) |
| 228 return true; | 284 return true; |
| 229 return false; | 285 return false; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 367 return; | 423 return; |
| 368 | 424 |
| 369 if (m_resolver) { | 425 if (m_resolver) { |
| 370 // FIXME: We should reject the promise. | 426 // FIXME: We should reject the promise. |
| 371 m_resolver->resolve(""); | 427 m_resolver->resolve(""); |
| 372 m_resolver.clear(); | 428 m_resolver.clear(); |
| 373 } | 429 } |
| 374 m_stream->error(DOMException::create(NetworkError, "network error")); | 430 m_stream->error(DOMException::create(NetworkError, "network error")); |
| 375 } | 431 } |
| 376 | 432 |
| 433 void Body::didBlobHandleResolveError(PassRefPtrWillBeRawPtr<DOMException> except ion) | |
| 434 { | |
| 435 if (!m_resolver) | |
| 436 return; | |
| 437 m_resolver->reject(exception); | |
| 438 m_resolver.clear(); | |
| 439 } | |
| 440 | |
| 377 } // namespace blink | 441 } // namespace blink |
| OLD | NEW |