Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: Source/modules/serviceworkers/Response.cpp

Issue 555443002: [Fetch API] Put body members directly on Response/Request (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "bindings/core/v8/ExceptionState.h" 9 #include "bindings/core/v8/ExceptionState.h"
10 #include "core/fileapi/Blob.h" 10 #include "core/fileapi/Blob.h"
11 #include "modules/serviceworkers/FetchBodyStream.h"
12 #include "modules/serviceworkers/ResponseInit.h" 11 #include "modules/serviceworkers/ResponseInit.h"
13 #include "public/platform/WebServiceWorkerResponse.h" 12 #include "public/platform/WebServiceWorkerResponse.h"
14 13
15 namespace blink { 14 namespace blink {
16 15
17 namespace { 16 namespace {
18 17
19 FetchResponseData* createFetchResponseDataFromWebResponse(const WebServiceWorker Response& webResponse) 18 FetchResponseData* createFetchResponseDataFromWebResponse(const WebServiceWorker Response& webResponse)
20 { 19 {
21 FetchResponseData* response = 0; 20 FetchResponseData* response = 0;
(...skipping 15 matching lines...) Expand all
37 for (HashMap<String, String>::const_iterator i = webResponse.headers().begin (), end = webResponse.headers().end(); i != end; ++i) { 36 for (HashMap<String, String>::const_iterator i = webResponse.headers().begin (), end = webResponse.headers().end(); i != end; ++i) {
38 headers->append(i->key, i->value, exceptionState); 37 headers->append(i->key, i->value, exceptionState);
39 if (exceptionState.hadException()) 38 if (exceptionState.hadException())
40 return 0; 39 return 0;
41 } 40 }
42 return headers; 41 return headers;
43 } 42 }
44 43
45 } 44 }
46 45
47 Response* Response::create(Blob* body, const Dictionary& responseInit, Exception State& exceptionState) 46 Response* Response::create(ExecutionContext* context, Blob* body, const Dictiona ry& responseInit, ExceptionState& exceptionState)
48 { 47 {
49 return create(body, ResponseInit(responseInit), exceptionState); 48 return create(context, body, ResponseInit(responseInit), exceptionState);
50 } 49 }
51 50
52 Response* Response::create(const String& body, const Dictionary& responseInit, E xceptionState& exceptionState) 51 Response* Response::create(ExecutionContext* context, const String& body, const Dictionary& responseInit, ExceptionState& exceptionState)
53 { 52 {
54 OwnPtr<BlobData> blobData = BlobData::create(); 53 OwnPtr<BlobData> blobData = BlobData::create();
55 blobData->appendText(body, false); 54 blobData->appendText(body, false);
56 // "Set |Content-Type| to `text/plain;charset=UTF-8`." 55 // "Set |Content-Type| to `text/plain;charset=UTF-8`."
57 blobData->setContentType("text/plain;charset=UTF-8"); 56 blobData->setContentType("text/plain;charset=UTF-8");
58 const long long length = blobData->length(); 57 const long long length = blobData->length();
59 RefPtrWillBeRawPtr<Blob> blob = Blob::create(BlobDataHandle::create(blobData .release(), length)); 58 RefPtrWillBeRawPtr<Blob> blob = Blob::create(BlobDataHandle::create(blobData .release(), length));
60 return create(blob.get(), ResponseInit(responseInit), exceptionState); 59 return create(context, blob.get(), ResponseInit(responseInit), exceptionStat e);
61 } 60 }
62 61
63 Response* Response::create(Blob* body, const ResponseInit& responseInit, Excepti onState& exceptionState) 62 Response* Response::create(ExecutionContext* context, Blob* body, const Response Init& responseInit, ExceptionState& exceptionState)
64 { 63 {
65 // "1. If |init|'s status member is not in the range 200 to 599, throw a 64 // "1. If |init|'s status member is not in the range 200 to 599, throw a
66 // RangeError." 65 // RangeError."
67 if (responseInit.status < 200 || 599 < responseInit.status) { 66 if (responseInit.status < 200 || 599 < responseInit.status) {
68 exceptionState.throwRangeError("Invalid status"); 67 exceptionState.throwRangeError("Invalid status");
69 return 0; 68 return 0;
70 } 69 }
71 70
72 // FIXME: "2. If |init|'s statusText member does not match the Reason-Phrase 71 // FIXME: "2. If |init|'s statusText member does not match the Reason-Phrase
73 // token production, throw a TypeError." 72 // token production, throw a TypeError."
74 73
75 // "3. Let |r| be a new Response object, associated with a new response, 74 // "3. Let |r| be a new Response object, associated with a new response,
76 // Headers object, and FetchBodyStream object." 75 // Headers object, and Body object."
77 Response* r = new Response(); 76 Response* r = new Response(context);
77 r->suspendIfNeeded();
78 78
79 // "4. Set |r|'s response's status to |init|'s status member." 79 // "4. Set |r|'s response's status to |init|'s status member."
80 r->m_response->setStatus(responseInit.status); 80 r->m_response->setStatus(responseInit.status);
81 81
82 // "5. Set |r|'s response's status message to |init|'s statusText member." 82 // "5. Set |r|'s response's status message to |init|'s statusText member."
83 r->m_response->setStatusMessage(AtomicString(responseInit.statusText)); 83 r->m_response->setStatusMessage(AtomicString(responseInit.statusText));
84 84
85 // "6. If |init|'s headers member is present, run these substeps:" 85 // "6. If |init|'s headers member is present, run these substeps:"
86 if (responseInit.headers) { 86 if (responseInit.headers) {
87 // "1. Empty |r|'s response's header list." 87 // "1. Empty |r|'s response's header list."
(...skipping 17 matching lines...) Expand all
105 // "1. Let |stream| and |Content-Type| be the result of extracting body. " 105 // "1. Let |stream| and |Content-Type| be the result of extracting body. "
106 // "2. Set |r|'s response's body to |stream|." 106 // "2. Set |r|'s response's body to |stream|."
107 // "3. If |Content-Type| is non-null and |r|'s response's header list 107 // "3. If |Content-Type| is non-null and |r|'s response's header list
108 // contains no header named `Content-Type`, append `Content-Type`/ 108 // contains no header named `Content-Type`, append `Content-Type`/
109 // |Content-Type| to |r|'s response's header list." 109 // |Content-Type| to |r|'s response's header list."
110 r->m_response->setBlobDataHandle(body->blobDataHandle()); 110 r->m_response->setBlobDataHandle(body->blobDataHandle());
111 if (!body->type().isNull() && !r->m_response->headerList()->has("Content -Type")) 111 if (!body->type().isNull() && !r->m_response->headerList()->has("Content -Type"))
112 r->m_response->headerList()->append("Content-Type", body->type()); 112 r->m_response->headerList()->append("Content-Type", body->type());
113 } 113 }
114 114
115 // FIXME: "8. Set |r|'s FetchBodyStream object's MIME type to the result of 115 // FIXME: "8. Set |r|'s MIME type to the result of extracting a MIME type
116 // extracting a MIME type from |r|'s response's header list." 116 // from |r|'s response's header list."
117 117
118 // "9. Return |r|." 118 // "9. Return |r|."
119 return r; 119 return r;
120 } 120 }
121 121
122 Response* Response::create(FetchResponseData* response) 122 Response* Response::create(ExecutionContext* context, FetchResponseData* respons e)
123 { 123 {
124 return new Response(response); 124 Response* r = new Response(context, response);
125 r->suspendIfNeeded();
126 return r;
125 } 127 }
126 128
127 Response* Response::create(const WebServiceWorkerResponse& webResponse) 129 Response* Response::create(ExecutionContext* context, const WebServiceWorkerResp onse& webResponse)
128 { 130 {
129 return new Response(webResponse); 131 Response* r = new Response(context, webResponse);
132 r->suspendIfNeeded();
133 return r;
130 } 134 }
131 135
132 String Response::type() const 136 String Response::type() const
133 { 137 {
134 // "The type attribute's getter must return response's type." 138 // "The type attribute's getter must return response's type."
135 switch (m_response->type()) { 139 switch (m_response->type()) {
136 case FetchResponseData::BasicType: 140 case FetchResponseData::BasicType:
137 return "basic"; 141 return "basic";
138 case FetchResponseData::CORSType: 142 case FetchResponseData::CORSType:
139 return "cors"; 143 return "cors";
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 // "The statusText attribute's getter must return response's status message. " 175 // "The statusText attribute's getter must return response's status message. "
172 return m_response->statusMessage(); 176 return m_response->statusMessage();
173 } 177 }
174 178
175 Headers* Response::headers() const 179 Headers* Response::headers() const
176 { 180 {
177 // "The headers attribute's getter must return the associated Headers object ." 181 // "The headers attribute's getter must return the associated Headers object ."
178 return m_headers; 182 return m_headers;
179 } 183 }
180 184
181 FetchBodyStream* Response::body(ExecutionContext* context)
182 {
183 if (!m_response->blobDataHandle())
184 return 0;
185 if (!m_fetchBodyStream)
186 m_fetchBodyStream = FetchBodyStream::create(context, m_response->blobDat aHandle());
187 return m_fetchBodyStream;
188 }
189
190 void Response::populateWebServiceWorkerResponse(WebServiceWorkerResponse& respon se) 185 void Response::populateWebServiceWorkerResponse(WebServiceWorkerResponse& respon se)
191 { 186 {
192 m_response->populateWebServiceWorkerResponse(response); 187 m_response->populateWebServiceWorkerResponse(response);
193 } 188 }
194 189
195 Response::Response() 190 Response::Response(ExecutionContext* context)
196 : m_response(FetchResponseData::create()) 191 : Body(context)
192 , m_response(FetchResponseData::create())
197 , m_headers(Headers::create(m_response->headerList())) 193 , m_headers(Headers::create(m_response->headerList()))
198 { 194 {
199 m_headers->setGuard(Headers::ResponseGuard); 195 m_headers->setGuard(Headers::ResponseGuard);
200 } 196 }
201 197
202 Response::Response(FetchResponseData* response) 198 Response::Response(ExecutionContext* context, FetchResponseData* response)
203 : m_response(response) 199 : Body(context)
200 , m_response(response)
204 , m_headers(Headers::create(m_response->headerList())) 201 , m_headers(Headers::create(m_response->headerList()))
205 { 202 {
206 m_headers->setGuard(Headers::ResponseGuard); 203 m_headers->setGuard(Headers::ResponseGuard);
207 } 204 }
208 205
209 // FIXME: Handle response body data. 206 // FIXME: Handle response body data.
210 Response::Response(const WebServiceWorkerResponse& webResponse) 207 Response::Response(ExecutionContext* context, const WebServiceWorkerResponse& we bResponse)
211 : m_response(createFetchResponseDataFromWebResponse(webResponse)) 208 : Body(context)
209 , m_response(createFetchResponseDataFromWebResponse(webResponse))
212 , m_headers(createHeadersFromWebResponse(webResponse)) 210 , m_headers(createHeadersFromWebResponse(webResponse))
213 { 211 {
214 m_headers->setGuard(Headers::ResponseGuard); 212 m_headers->setGuard(Headers::ResponseGuard);
215 } 213 }
216 214
215 PassRefPtr<BlobDataHandle> Response::blobDataHandle()
216 {
217 return m_response->blobDataHandle();
218 }
219
217 void Response::trace(Visitor* visitor) 220 void Response::trace(Visitor* visitor)
218 { 221 {
222 Body::trace(visitor);
219 visitor->trace(m_response); 223 visitor->trace(m_response);
220 visitor->trace(m_headers); 224 visitor->trace(m_headers);
221 visitor->trace(m_fetchBodyStream);
222 } 225 }
223 226
224 } // namespace blink 227 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698