| 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 "Response.h" | 6 #include "Response.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/Dictionary.h" | 8 #include "bindings/core/v8/Dictionary.h" |
| 9 #include "core/fileapi/Blob.h" | 9 #include "core/fileapi/Blob.h" |
| 10 #include "modules/serviceworkers/FetchBodyStream.h" | 10 #include "modules/serviceworkers/FetchBodyStream.h" |
| 11 #include "modules/serviceworkers/ResponseInit.h" | 11 #include "modules/serviceworkers/ResponseInit.h" |
| 12 | 12 |
| 13 namespace blink { | 13 namespace blink { |
| 14 | 14 |
| 15 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(Response); | 15 Response* Response::create(Blob* body, const Dictionary& responseInit, Exception
State& exceptionState) |
| 16 | |
| 17 PassRefPtrWillBeRawPtr<Response> Response::create(Blob* body, const Dictionary&
responseInit, ExceptionState& exceptionState) | |
| 18 { | 16 { |
| 19 return create(body, ResponseInit(responseInit), exceptionState); | 17 return create(body, ResponseInit(responseInit), exceptionState); |
| 20 } | 18 } |
| 21 | 19 |
| 22 PassRefPtrWillBeRawPtr<Response> Response::create(const String& body, const Dict
ionary& responseInit, ExceptionState& exceptionState) | 20 Response* Response::create(const String& body, const Dictionary& responseInit, E
xceptionState& exceptionState) |
| 23 { | 21 { |
| 24 OwnPtr<BlobData> blobData = BlobData::create(); | 22 OwnPtr<BlobData> blobData = BlobData::create(); |
| 25 blobData->appendText(body, false); | 23 blobData->appendText(body, false); |
| 26 // "Set |Content-Type| to `text/plain;charset=UTF-8`." | 24 // "Set |Content-Type| to `text/plain;charset=UTF-8`." |
| 27 blobData->setContentType("text/plain;charset=UTF-8"); | 25 blobData->setContentType("text/plain;charset=UTF-8"); |
| 28 const long long length = blobData->length(); | 26 const long long length = blobData->length(); |
| 29 RefPtrWillBeRawPtr<Blob> blob = Blob::create(BlobDataHandle::create(blobData
.release(), length)); | 27 RefPtrWillBeRawPtr<Blob> blob = Blob::create(BlobDataHandle::create(blobData
.release(), length)); |
| 30 return create(blob.get(), ResponseInit(responseInit), exceptionState); | 28 return create(blob.get(), ResponseInit(responseInit), exceptionState); |
| 31 } | 29 } |
| 32 | 30 |
| 33 PassRefPtrWillBeRawPtr<Response> Response::create(Blob* body, const ResponseInit
& responseInit, ExceptionState& exceptionState) | 31 Response* Response::create(Blob* body, const ResponseInit& responseInit, Excepti
onState& exceptionState) |
| 34 { | 32 { |
| 35 // "1. If |init|'s status member is not in the range 200 to 599, throw a | 33 // "1. If |init|'s status member is not in the range 200 to 599, throw a |
| 36 // RangeError." | 34 // RangeError." |
| 37 if (responseInit.status < 200 || 599 < responseInit.status) { | 35 if (responseInit.status < 200 || 599 < responseInit.status) { |
| 38 exceptionState.throwRangeError("Invalid status"); | 36 exceptionState.throwRangeError("Invalid status"); |
| 39 return nullptr; | 37 return 0; |
| 40 } | 38 } |
| 41 | 39 |
| 42 // FIXME: "2. If |init|'s statusText member does not match the Reason-Phrase | 40 // FIXME: "2. If |init|'s statusText member does not match the Reason-Phrase |
| 43 // token production, throw a TypeError." | 41 // token production, throw a TypeError." |
| 44 | 42 |
| 45 // "3. Let |r| be a new Response object, associated with a new response, | 43 // "3. Let |r| be a new Response object, associated with a new response, |
| 46 // Headers object, and FetchBodyStream object." | 44 // Headers object, and FetchBodyStream object." |
| 47 RefPtrWillBeRawPtr<Response> r = adoptRefWillBeNoop(new Response()); | 45 Response* r = new Response(); |
| 48 | 46 |
| 49 // "4. Set |r|'s response's status to |init|'s status member." | 47 // "4. Set |r|'s response's status to |init|'s status member." |
| 50 r->m_response->setStatus(responseInit.status); | 48 r->m_response->setStatus(responseInit.status); |
| 51 | 49 |
| 52 // "5. Set |r|'s response's status message to |init|'s statusText member." | 50 // "5. Set |r|'s response's status message to |init|'s statusText member." |
| 53 r->m_response->setStatusMessage(AtomicString(responseInit.statusText)); | 51 r->m_response->setStatusMessage(AtomicString(responseInit.statusText)); |
| 54 | 52 |
| 55 // "6. If |init|'s headers member is present, run these substeps:" | 53 // "6. If |init|'s headers member is present, run these substeps:" |
| 56 if (responseInit.headers) { | 54 if (responseInit.headers) { |
| 57 // "1. Empty |r|'s response's header list." | 55 // "1. Empty |r|'s response's header list." |
| 58 r->m_response->headerList()->clearList(); | 56 r->m_response->headerList()->clearList(); |
| 59 // "2. Fill |r|'s Headers object with |init|'s headers member. Rethrow | 57 // "2. Fill |r|'s Headers object with |init|'s headers member. Rethrow |
| 60 // any exceptions." | 58 // any exceptions." |
| 61 r->m_headers->fillWith(responseInit.headers.get(), exceptionState); | 59 r->m_headers->fillWith(responseInit.headers.get(), exceptionState); |
| 62 if (exceptionState.hadException()) | 60 if (exceptionState.hadException()) |
| 63 return nullptr; | 61 return 0; |
| 64 } else if (!responseInit.headersDictionary.isUndefinedOrNull()) { | 62 } else if (!responseInit.headersDictionary.isUndefinedOrNull()) { |
| 65 // "1. Empty |r|'s response's header list." | 63 // "1. Empty |r|'s response's header list." |
| 66 r->m_response->headerList()->clearList(); | 64 r->m_response->headerList()->clearList(); |
| 67 // "2. Fill |r|'s Headers object with |init|'s headers member. Rethrow | 65 // "2. Fill |r|'s Headers object with |init|'s headers member. Rethrow |
| 68 // any exceptions." | 66 // any exceptions." |
| 69 r->m_headers->fillWith(responseInit.headersDictionary, exceptionState); | 67 r->m_headers->fillWith(responseInit.headersDictionary, exceptionState); |
| 70 if (exceptionState.hadException()) | 68 if (exceptionState.hadException()) |
| 71 return nullptr; | 69 return 0; |
| 72 } | 70 } |
| 73 // "7. If body is given, run these substeps:" | 71 // "7. If body is given, run these substeps:" |
| 74 if (body) { | 72 if (body) { |
| 75 // "1. Let |stream| and |Content-Type| be the result of extracting body.
" | 73 // "1. Let |stream| and |Content-Type| be the result of extracting body.
" |
| 76 // "2. Set |r|'s response's body to |stream|." | 74 // "2. Set |r|'s response's body to |stream|." |
| 77 // "3. If |r|'s response's header list contains no header named | 75 // "3. If |r|'s response's header list contains no header named |
| 78 // `Content-Type`, append `Content-Type`/|Content-Type| to |r|'s | 76 // `Content-Type`, append `Content-Type`/|Content-Type| to |r|'s |
| 79 // response's header list." | 77 // response's header list." |
| 80 r->m_response->setBlobDataHandle(body->blobDataHandle()); | 78 r->m_response->setBlobDataHandle(body->blobDataHandle()); |
| 81 if (!r->m_response->headerList()->has("Content-Type")) { | 79 if (!r->m_response->headerList()->has("Content-Type")) { |
| 82 if (body->type().isNull()) | 80 if (body->type().isNull()) |
| 83 r->m_response->headerList()->append("Content-Type", ""); | 81 r->m_response->headerList()->append("Content-Type", ""); |
| 84 else | 82 else |
| 85 r->m_response->headerList()->append("Content-Type", body->type()
); | 83 r->m_response->headerList()->append("Content-Type", body->type()
); |
| 86 } | 84 } |
| 87 } | 85 } |
| 88 | 86 |
| 89 // FIXME: "8. Set |r|'s FetchBodyStream object's MIME type to the result of | 87 // FIXME: "8. Set |r|'s FetchBodyStream object's MIME type to the result of |
| 90 // extracting a MIME type from |r|'s response's header list." | 88 // extracting a MIME type from |r|'s response's header list." |
| 91 | 89 |
| 92 // "9. Return |r|." | 90 // "9. Return |r|." |
| 93 return r.release(); | 91 return r; |
| 94 } | 92 } |
| 95 | 93 |
| 96 PassRefPtrWillBeRawPtr<Response> Response::create(PassRefPtrWillBeRawPtr<FetchRe
sponseData> response) | 94 Response* Response::create(FetchResponseData* response) |
| 97 { | 95 { |
| 98 return adoptRefWillBeNoop(new Response(response)); | 96 return new Response(response); |
| 99 } | 97 } |
| 100 | 98 |
| 101 String Response::type() const | 99 String Response::type() const |
| 102 { | 100 { |
| 103 // "The type attribute's getter must return response's type." | 101 // "The type attribute's getter must return response's type." |
| 104 switch (m_response->type()) { | 102 switch (m_response->type()) { |
| 105 case FetchResponseData::BasicType: | 103 case FetchResponseData::BasicType: |
| 106 return "basic"; | 104 return "basic"; |
| 107 case FetchResponseData::CORSType: | 105 case FetchResponseData::CORSType: |
| 108 return "cors"; | 106 return "cors"; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 134 // "The status attribute's getter must return response's status." | 132 // "The status attribute's getter must return response's status." |
| 135 return m_response->status(); | 133 return m_response->status(); |
| 136 } | 134 } |
| 137 | 135 |
| 138 String Response::statusText() const | 136 String Response::statusText() const |
| 139 { | 137 { |
| 140 // "The statusText attribute's getter must return response's status message.
" | 138 // "The statusText attribute's getter must return response's status message.
" |
| 141 return m_response->statusMessage(); | 139 return m_response->statusMessage(); |
| 142 } | 140 } |
| 143 | 141 |
| 144 PassRefPtrWillBeRawPtr<Headers> Response::headers() const | 142 Headers* Response::headers() const |
| 145 { | 143 { |
| 146 // "The headers attribute's getter must return the associated Headers object
." | 144 // "The headers attribute's getter must return the associated Headers object
." |
| 147 return m_headers; | 145 return m_headers; |
| 148 } | 146 } |
| 149 | 147 |
| 150 PassRefPtrWillBeRawPtr<FetchBodyStream> Response::body(ExecutionContext* context
) | 148 FetchBodyStream* Response::body(ExecutionContext* context) |
| 151 { | 149 { |
| 152 if (!m_response->blobDataHandle()) | 150 if (!m_response->blobDataHandle()) |
| 153 return nullptr; | 151 return 0; |
| 154 if (!m_fetchBodyStream) | 152 if (!m_fetchBodyStream) |
| 155 m_fetchBodyStream = FetchBodyStream::create(context, m_response->blobDat
aHandle()); | 153 m_fetchBodyStream = FetchBodyStream::create(context, m_response->blobDat
aHandle()); |
| 156 return m_fetchBodyStream; | 154 return m_fetchBodyStream; |
| 157 } | 155 } |
| 158 | 156 |
| 159 void Response::populateWebServiceWorkerResponse(WebServiceWorkerResponse& respon
se) | 157 void Response::populateWebServiceWorkerResponse(WebServiceWorkerResponse& respon
se) |
| 160 { | 158 { |
| 161 m_response->populateWebServiceWorkerResponse(response); | 159 m_response->populateWebServiceWorkerResponse(response); |
| 162 } | 160 } |
| 163 | 161 |
| 164 Response::Response() | 162 Response::Response() |
| 165 : m_response(FetchResponseData::create()) | 163 : m_response(FetchResponseData::create()) |
| 166 , m_headers(Headers::create(m_response->headerList())) | 164 , m_headers(Headers::create(m_response->headerList())) |
| 167 { | 165 { |
| 168 m_headers->setGuard(Headers::ResponseGuard); | 166 m_headers->setGuard(Headers::ResponseGuard); |
| 169 ScriptWrappable::init(this); | 167 ScriptWrappable::init(this); |
| 170 } | 168 } |
| 171 | 169 |
| 172 Response::Response(PassRefPtrWillBeRawPtr<FetchResponseData> response) | 170 Response::Response(FetchResponseData* response) |
| 173 : m_response(response) | 171 : m_response(response) |
| 174 , m_headers(Headers::create(m_response->headerList())) | 172 , m_headers(Headers::create(m_response->headerList())) |
| 175 { | 173 { |
| 176 m_headers->setGuard(Headers::ResponseGuard); | 174 m_headers->setGuard(Headers::ResponseGuard); |
| 177 ScriptWrappable::init(this); | 175 ScriptWrappable::init(this); |
| 178 } | 176 } |
| 179 | 177 |
| 180 void Response::trace(Visitor* visitor) | 178 void Response::trace(Visitor* visitor) |
| 181 { | 179 { |
| 182 visitor->trace(m_response); | 180 visitor->trace(m_response); |
| 183 visitor->trace(m_headers); | 181 visitor->trace(m_headers); |
| 184 visitor->trace(m_fetchBodyStream); | 182 visitor->trace(m_fetchBodyStream); |
| 185 } | 183 } |
| 186 | 184 |
| 187 } // namespace blink | 185 } // namespace blink |
| OLD | NEW |