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