| 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/FetchBodyStream.h" | 6 #include "modules/serviceworkers/Body.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "bindings/core/v8/V8ThrowException.h" | 10 #include "bindings/core/v8/V8ThrowException.h" |
| 11 #include "core/dom/ExceptionCode.h" | |
| 12 #include "core/fileapi/Blob.h" | 11 #include "core/fileapi/Blob.h" |
| 13 #include "core/fileapi/FileReaderLoader.h" | 12 #include "core/fileapi/FileReaderLoader.h" |
| 14 #include "core/fileapi/FileReaderLoaderClient.h" | 13 #include "core/fileapi/FileReaderLoaderClient.h" |
| 15 #include "modules/serviceworkers/ResponseInit.h" | |
| 16 #include "platform/NotImplemented.h" | |
| 17 #include "public/platform/WebServiceWorkerResponse.h" | |
| 18 | 14 |
| 19 namespace blink { | 15 namespace blink { |
| 20 | 16 |
| 21 FetchBodyStream* FetchBodyStream::create(ExecutionContext* context, PassRefPtr<B
lobDataHandle> blobDataHandle) | 17 ScriptPromise Body::readAsync(ScriptState* scriptState, ResponseType type) |
| 22 { | 18 { |
| 23 FetchBodyStream* fetchBodyStream = new FetchBodyStream(context, blobDataHand
le); | 19 if (m_bodyUsed) |
| 24 fetchBodyStream->suspendIfNeeded(); | |
| 25 return fetchBodyStream; | |
| 26 } | |
| 27 | |
| 28 ScriptPromise FetchBodyStream::readAsync(ScriptState* scriptState, ResponseType
type) | |
| 29 { | |
| 30 if (m_hasRead) | |
| 31 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror("Already read", scriptState->isolate())); | 20 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror("Already read", scriptState->isolate())); |
| 32 | 21 |
| 33 m_hasRead = true; | 22 m_bodyUsed = true; |
| 34 m_responseType = type; | 23 m_responseType = type; |
| 35 | 24 |
| 36 ASSERT(!m_resolver); | 25 ASSERT(!m_resolver); |
| 37 m_resolver = ScriptPromiseResolver::create(scriptState); | 26 m_resolver = ScriptPromiseResolver::create(scriptState); |
| 38 ScriptPromise promise = m_resolver->promise(); | 27 ScriptPromise promise = m_resolver->promise(); |
| 39 | 28 |
| 40 FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsText; | 29 FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsText; |
| 41 | 30 RefPtr<BlobDataHandle> blobHandle = blobDataHandle(); |
| 31 if (!blobHandle.get()) { |
| 32 blobHandle = BlobDataHandle::create(BlobData::create(), 0); |
| 33 } |
| 42 switch (type) { | 34 switch (type) { |
| 43 case ResponseAsArrayBuffer: | 35 case ResponseAsArrayBuffer: |
| 44 readType = FileReaderLoader::ReadAsArrayBuffer; | 36 readType = FileReaderLoader::ReadAsArrayBuffer; |
| 45 break; | 37 break; |
| 46 case ResponseAsBlob: | 38 case ResponseAsBlob: |
| 47 if (m_blobDataHandle->size() != kuint64max) { | 39 if (blobHandle->size() != kuint64max) { |
| 48 // If the size of |m_blobDataHandle| is set correctly, creates Blob
from it. | 40 // If the size of |blobHandle| is set correctly, creates Blob from |
| 49 m_resolver->resolve(Blob::create(m_blobDataHandle)); | 41 // it. |
| 42 m_resolver->resolve(Blob::create(blobHandle)); |
| 50 m_resolver.clear(); | 43 m_resolver.clear(); |
| 51 return promise; | 44 return promise; |
| 52 } | 45 } |
| 53 // If the size is not set, read as ArrayBuffer and create a new blob to
get the size. | 46 // If the size is not set, read as ArrayBuffer and create a new blob to |
| 47 // get the size. |
| 54 // FIXME: This workaround is not good for performance. | 48 // FIXME: This workaround is not good for performance. |
| 55 // When we will stop using Blob as a base system of FetchBodyStream to s
upport stream, this problem should be solved. | 49 // When we will stop using Blob as a base system of Body to support |
| 50 // stream, this problem should be solved. |
| 56 readType = FileReaderLoader::ReadAsArrayBuffer; | 51 readType = FileReaderLoader::ReadAsArrayBuffer; |
| 57 break; | 52 break; |
| 58 case ResponseAsFormData: | 53 case ResponseAsFormData: |
| 59 // FIXME: Implement this. | 54 // FIXME: Implement this. |
| 60 ASSERT_NOT_REACHED(); | 55 ASSERT_NOT_REACHED(); |
| 61 break; | 56 break; |
| 62 case ResponseAsJSON: | 57 case ResponseAsJSON: |
| 63 case ResponseAsText: | 58 case ResponseAsText: |
| 64 break; | 59 break; |
| 65 default: | 60 default: |
| 66 ASSERT_NOT_REACHED(); | 61 ASSERT_NOT_REACHED(); |
| 67 } | 62 } |
| 68 | 63 |
| 69 m_loader = adoptPtr(new FileReaderLoader(readType, this)); | 64 m_loader = adoptPtr(new FileReaderLoader(readType, this)); |
| 70 m_loader->start(scriptState->executionContext(), m_blobDataHandle); | 65 m_loader->start(scriptState->executionContext(), blobHandle); |
| 71 | 66 |
| 72 return promise; | 67 return promise; |
| 73 } | 68 } |
| 74 | 69 |
| 75 ScriptPromise FetchBodyStream::asArrayBuffer(ScriptState* scriptState) | 70 ScriptPromise Body::arrayBuffer(ScriptState* scriptState) |
| 76 { | 71 { |
| 77 return readAsync(scriptState, ResponseAsArrayBuffer); | 72 return readAsync(scriptState, ResponseAsArrayBuffer); |
| 78 } | 73 } |
| 79 | 74 |
| 80 ScriptPromise FetchBodyStream::asBlob(ScriptState* scriptState) | 75 ScriptPromise Body::blob(ScriptState* scriptState) |
| 81 { | 76 { |
| 82 return readAsync(scriptState, ResponseAsBlob); | 77 return readAsync(scriptState, ResponseAsBlob); |
| 83 } | 78 } |
| 84 | 79 |
| 85 ScriptPromise FetchBodyStream::asFormData(ScriptState* scriptState) | 80 ScriptPromise Body::formData(ScriptState* scriptState) |
| 86 { | 81 { |
| 87 return readAsync(scriptState, ResponseAsFormData); | 82 return readAsync(scriptState, ResponseAsFormData); |
| 88 } | 83 } |
| 89 | 84 |
| 90 ScriptPromise FetchBodyStream::asJSON(ScriptState* scriptState) | 85 ScriptPromise Body::json(ScriptState* scriptState) |
| 91 { | 86 { |
| 92 return readAsync(scriptState, ResponseAsJSON); | 87 return readAsync(scriptState, ResponseAsJSON); |
| 93 } | 88 } |
| 94 | 89 |
| 95 ScriptPromise FetchBodyStream::asText(ScriptState* scriptState) | 90 ScriptPromise Body::text(ScriptState* scriptState) |
| 96 { | 91 { |
| 97 return readAsync(scriptState, ResponseAsText); | 92 return readAsync(scriptState, ResponseAsText); |
| 98 } | 93 } |
| 99 | 94 |
| 100 void FetchBodyStream::stop() | 95 bool Body::bodyUsed() const |
| 96 { |
| 97 return m_bodyUsed; |
| 98 } |
| 99 |
| 100 void Body::stop() |
| 101 { | 101 { |
| 102 // Canceling the load will call didFail which will remove the resolver. | 102 // Canceling the load will call didFail which will remove the resolver. |
| 103 if (m_resolver) | 103 if (m_resolver) |
| 104 m_loader->cancel(); | 104 m_loader->cancel(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 bool FetchBodyStream::hasPendingActivity() const | 107 bool Body::hasPendingActivity() const |
| 108 { | 108 { |
| 109 return m_resolver; | 109 return m_resolver; |
| 110 } | 110 } |
| 111 | 111 |
| 112 FetchBodyStream::FetchBodyStream(ExecutionContext* context, PassRefPtr<BlobDataH
andle> blobDataHandle) | 112 Body::Body(ExecutionContext* context) |
| 113 : ActiveDOMObject(context) | 113 : ActiveDOMObject(context) |
| 114 , m_blobDataHandle(blobDataHandle) | 114 , m_bodyUsed(false) |
| 115 , m_hasRead(false) | |
| 116 { | 115 { |
| 117 if (!m_blobDataHandle) { | |
| 118 m_blobDataHandle = BlobDataHandle::create(BlobData::create(), 0); | |
| 119 } | |
| 120 } | 116 } |
| 121 | 117 |
| 122 void FetchBodyStream::resolveJSON() | 118 void Body::resolveJSON() |
| 123 { | 119 { |
| 124 ASSERT(m_responseType == ResponseAsJSON); | 120 ASSERT(m_responseType == ResponseAsJSON); |
| 125 ScriptState::Scope scope(m_resolver->scriptState()); | 121 ScriptState::Scope scope(m_resolver->scriptState()); |
| 126 v8::Isolate* isolate = m_resolver->scriptState()->isolate(); | 122 v8::Isolate* isolate = m_resolver->scriptState()->isolate(); |
| 127 v8::Local<v8::String> inputString = v8String(isolate, m_loader->stringResult
()); | 123 v8::Local<v8::String> inputString = v8String(isolate, m_loader->stringResult
()); |
| 128 v8::TryCatch trycatch; | 124 v8::TryCatch trycatch; |
| 129 v8::Local<v8::Value> parsed = v8::JSON::Parse(inputString); | 125 v8::Local<v8::Value> parsed = v8::JSON::Parse(inputString); |
| 130 if (parsed.IsEmpty()) { | 126 if (parsed.IsEmpty()) { |
| 131 if (trycatch.HasCaught()) | 127 if (trycatch.HasCaught()) |
| 132 m_resolver->reject(trycatch.Exception()); | 128 m_resolver->reject(trycatch.Exception()); |
| 133 else | 129 else |
| 134 m_resolver->reject(v8::Exception::Error(v8::String::NewFromUtf8(isol
ate, "JSON parse error"))); | 130 m_resolver->reject(v8::Exception::Error(v8::String::NewFromUtf8(isol
ate, "JSON parse error"))); |
| 135 return; | 131 return; |
| 136 } | 132 } |
| 137 m_resolver->resolve(parsed); | 133 m_resolver->resolve(parsed); |
| 138 } | 134 } |
| 139 | 135 |
| 140 // FileReaderLoaderClient functions. | 136 // FileReaderLoaderClient functions. |
| 141 void FetchBodyStream::didStartLoading() { } | 137 void Body::didStartLoading() { } |
| 142 void FetchBodyStream::didReceiveData() { } | 138 void Body::didReceiveData() { } |
| 143 void FetchBodyStream::didFinishLoading() | 139 void Body::didFinishLoading() |
| 144 { | 140 { |
| 145 if (!m_resolver->executionContext() || m_resolver->executionContext()->activ
eDOMObjectsAreStopped()) | 141 if (!m_resolver->executionContext() || m_resolver->executionContext()->activ
eDOMObjectsAreStopped()) |
| 146 return; | 142 return; |
| 147 | 143 |
| 148 switch (m_responseType) { | 144 switch (m_responseType) { |
| 149 case ResponseAsArrayBuffer: | 145 case ResponseAsArrayBuffer: |
| 150 m_resolver->resolve(m_loader->arrayBufferResult()); | 146 m_resolver->resolve(m_loader->arrayBufferResult()); |
| 151 break; | 147 break; |
| 152 case ResponseAsBlob: { | 148 case ResponseAsBlob: { |
| 153 ASSERT(m_blobDataHandle->size() == kuint64max); | 149 ASSERT(blobDataHandle()->size() == kuint64max); |
| 154 OwnPtr<BlobData> blobData = BlobData::create(); | 150 OwnPtr<BlobData> blobData = BlobData::create(); |
| 155 RefPtr<ArrayBuffer> buffer = m_loader->arrayBufferResult(); | 151 RefPtr<ArrayBuffer> buffer = m_loader->arrayBufferResult(); |
| 156 blobData->appendArrayBuffer(buffer.get()); | 152 blobData->appendArrayBuffer(buffer.get()); |
| 157 const size_t length = blobData->length(); | 153 const size_t length = blobData->length(); |
| 158 m_resolver->resolve(Blob::create(BlobDataHandle::create(blobData.release
(), length))); | 154 m_resolver->resolve(Blob::create(BlobDataHandle::create(blobData.release
(), length))); |
| 159 break; | 155 break; |
| 160 } | 156 } |
| 161 case ResponseAsFormData: | 157 case ResponseAsFormData: |
| 162 ASSERT_NOT_REACHED(); | 158 ASSERT_NOT_REACHED(); |
| 163 break; | 159 break; |
| 164 case ResponseAsJSON: | 160 case ResponseAsJSON: |
| 165 resolveJSON(); | 161 resolveJSON(); |
| 166 break; | 162 break; |
| 167 case ResponseAsText: | 163 case ResponseAsText: |
| 168 m_resolver->resolve(m_loader->stringResult()); | 164 m_resolver->resolve(m_loader->stringResult()); |
| 169 break; | 165 break; |
| 170 default: | 166 default: |
| 171 ASSERT_NOT_REACHED(); | 167 ASSERT_NOT_REACHED(); |
| 172 } | 168 } |
| 173 m_resolver.clear(); | 169 m_resolver.clear(); |
| 174 } | 170 } |
| 175 | 171 |
| 176 void FetchBodyStream::didFail(FileError::ErrorCode code) | 172 void Body::didFail(FileError::ErrorCode code) |
| 177 { | 173 { |
| 178 ASSERT(m_resolver); | 174 ASSERT(m_resolver); |
| 179 if (!m_resolver->executionContext() || m_resolver->executionContext()->activ
eDOMObjectsAreStopped()) | 175 if (!m_resolver->executionContext() || m_resolver->executionContext()->activ
eDOMObjectsAreStopped()) |
| 180 return; | 176 return; |
| 181 | 177 |
| 182 m_resolver->resolve(""); | 178 m_resolver->resolve(""); |
| 183 m_resolver.clear(); | 179 m_resolver.clear(); |
| 184 } | 180 } |
| 185 | 181 |
| 186 } // namespace blink | 182 } // namespace blink |
| OLD | NEW |