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