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