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

Side by Side Diff: Source/core/xml/XMLHttpRequest.cpp

Issue 21600002: Add json responseType support to XMLHttpRequest (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebase Created 7 years, 4 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
« no previous file with comments | « Source/core/xml/XMLHttpRequest.h ('k') | Source/core/xml/XMLHttpRequest.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2005-2007 Alexey Proskuryakov <ap@webkit.org> 3 * Copyright (C) 2005-2007 Alexey Proskuryakov <ap@webkit.org>
4 * Copyright (C) 2007, 2008 Julien Chaffraix <jchaffraix@webkit.org> 4 * Copyright (C) 2007, 2008 Julien Chaffraix <jchaffraix@webkit.org>
5 * Copyright (C) 2008, 2011 Google Inc. All rights reserved. 5 * Copyright (C) 2008, 2011 Google Inc. All rights reserved.
6 * Copyright (C) 2012 Intel Corporation 6 * Copyright (C) 2012 Intel Corporation
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public 9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 { 217 {
218 if (m_responseTypeCode != ResponseTypeDefault && m_responseTypeCode != Respo nseTypeText) { 218 if (m_responseTypeCode != ResponseTypeDefault && m_responseTypeCode != Respo nseTypeText) {
219 es.throwDOMException(InvalidStateError, ExceptionMessages::failedToGet(" responseText", "XMLHttpRequest", "the value is only accessible if the object's ' responseType' is '' or 'text' (was '" + responseType() + "').")); 219 es.throwDOMException(InvalidStateError, ExceptionMessages::failedToGet(" responseText", "XMLHttpRequest", "the value is only accessible if the object's ' responseType' is '' or 'text' (was '" + responseType() + "')."));
220 return ScriptString(); 220 return ScriptString();
221 } 221 }
222 if (m_error || (m_state != LOADING && m_state != DONE)) 222 if (m_error || (m_state != LOADING && m_state != DONE))
223 return ScriptString(); 223 return ScriptString();
224 return m_responseText; 224 return m_responseText;
225 } 225 }
226 226
227 ScriptString XMLHttpRequest::responseJSONSource(ExceptionState& es)
228 {
229 if (m_responseTypeCode != ResponseTypeJSON) {
230 es.throwDOMException(InvalidStateError);
231 return ScriptString();
232 }
233 if (m_error || m_state != DONE)
234 return ScriptString();
235 return m_responseText;
236 }
237
227 Document* XMLHttpRequest::responseXML(ExceptionState& es) 238 Document* XMLHttpRequest::responseXML(ExceptionState& es)
228 { 239 {
229 if (m_responseTypeCode != ResponseTypeDefault && m_responseTypeCode != Respo nseTypeDocument) { 240 if (m_responseTypeCode != ResponseTypeDefault && m_responseTypeCode != Respo nseTypeDocument) {
230 es.throwDOMException(InvalidStateError, ExceptionMessages::failedToGet(" responseXML", "XMLHttpRequest", "the value is only accessible if the object's 'r esponseType' is '' or 'document' (was '" + responseType() + "').")); 241 es.throwDOMException(InvalidStateError, ExceptionMessages::failedToGet(" responseXML", "XMLHttpRequest", "the value is only accessible if the object's 'r esponseType' is '' or 'document' (was '" + responseType() + "')."));
231 return 0; 242 return 0;
232 } 243 }
233 244
234 if (m_error || m_state != DONE) 245 if (m_error || m_state != DONE)
235 return 0; 246 return 0;
236 247
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 // such as file: and data: still make sense to allow. 344 // such as file: and data: still make sense to allow.
334 if (!m_async && scriptExecutionContext()->isDocument() && m_url.protocolIsIn HTTPFamily()) { 345 if (!m_async && scriptExecutionContext()->isDocument() && m_url.protocolIsIn HTTPFamily()) {
335 es.throwDOMException(InvalidAccessError, ExceptionMessages::failedToSet( "responseType", "XMLHttpRequest", "the response type can only be changed for asy nchronous HTTP requests made from a document.")); 346 es.throwDOMException(InvalidAccessError, ExceptionMessages::failedToSet( "responseType", "XMLHttpRequest", "the response type can only be changed for asy nchronous HTTP requests made from a document."));
336 return; 347 return;
337 } 348 }
338 349
339 if (responseType == "") 350 if (responseType == "")
340 m_responseTypeCode = ResponseTypeDefault; 351 m_responseTypeCode = ResponseTypeDefault;
341 else if (responseType == "text") 352 else if (responseType == "text")
342 m_responseTypeCode = ResponseTypeText; 353 m_responseTypeCode = ResponseTypeText;
354 else if (responseType == "json")
355 m_responseTypeCode = ResponseTypeJSON;
343 else if (responseType == "document") 356 else if (responseType == "document")
344 m_responseTypeCode = ResponseTypeDocument; 357 m_responseTypeCode = ResponseTypeDocument;
345 else if (responseType == "blob") 358 else if (responseType == "blob")
346 m_responseTypeCode = ResponseTypeBlob; 359 m_responseTypeCode = ResponseTypeBlob;
347 else if (responseType == "arraybuffer") 360 else if (responseType == "arraybuffer")
348 m_responseTypeCode = ResponseTypeArrayBuffer; 361 m_responseTypeCode = ResponseTypeArrayBuffer;
349 else 362 else
350 ASSERT_NOT_REACHED(); 363 ASSERT_NOT_REACHED();
351 } 364 }
352 365
353 String XMLHttpRequest::responseType() 366 String XMLHttpRequest::responseType()
354 { 367 {
355 switch (m_responseTypeCode) { 368 switch (m_responseTypeCode) {
356 case ResponseTypeDefault: 369 case ResponseTypeDefault:
357 return ""; 370 return "";
358 case ResponseTypeText: 371 case ResponseTypeText:
359 return "text"; 372 return "text";
373 case ResponseTypeJSON:
374 return "json";
360 case ResponseTypeDocument: 375 case ResponseTypeDocument:
361 return "document"; 376 return "document";
362 case ResponseTypeBlob: 377 case ResponseTypeBlob:
363 return "blob"; 378 return "blob";
364 case ResponseTypeArrayBuffer: 379 case ResponseTypeArrayBuffer:
365 return "arraybuffer"; 380 return "arraybuffer";
366 } 381 }
367 return ""; 382 return "";
368 } 383 }
369 384
(...skipping 752 matching lines...) Expand 10 before | Expand all | Expand 10 after
1122 } 1137 }
1123 1138
1124 void XMLHttpRequest::didReceiveData(const char* data, int len) 1139 void XMLHttpRequest::didReceiveData(const char* data, int len)
1125 { 1140 {
1126 if (m_error) 1141 if (m_error)
1127 return; 1142 return;
1128 1143
1129 if (m_state < HEADERS_RECEIVED) 1144 if (m_state < HEADERS_RECEIVED)
1130 changeState(HEADERS_RECEIVED); 1145 changeState(HEADERS_RECEIVED);
1131 1146
1132 bool useDecoder = m_responseTypeCode == ResponseTypeDefault || m_responseTyp eCode == ResponseTypeText || m_responseTypeCode == ResponseTypeDocument; 1147 bool useDecoder = m_responseTypeCode == ResponseTypeDefault || m_responseTyp eCode == ResponseTypeText || m_responseTypeCode == ResponseTypeJSON || m_respons eTypeCode == ResponseTypeDocument;
1133 1148
1134 if (useDecoder && !m_decoder) { 1149 if (useDecoder && !m_decoder) {
1135 if (!m_responseEncoding.isEmpty()) 1150 if (m_responseTypeCode == ResponseTypeJSON)
1151 m_decoder = TextResourceDecoder::create("application/json", "UTF-8") ;
1152 else if (!m_responseEncoding.isEmpty())
1136 m_decoder = TextResourceDecoder::create("text/plain", m_responseEnco ding); 1153 m_decoder = TextResourceDecoder::create("text/plain", m_responseEnco ding);
1137 // allow TextResourceDecoder to look inside the m_response if it's XML o r HTML 1154 // allow TextResourceDecoder to look inside the m_response if it's XML o r HTML
1138 else if (responseIsXML()) { 1155 else if (responseIsXML()) {
1139 m_decoder = TextResourceDecoder::create("application/xml"); 1156 m_decoder = TextResourceDecoder::create("application/xml");
1140 // Don't stop on encoding errors, unlike it is done for other kinds of XML resources. This matches the behavior of previous WebKit versions, Firefox and Opera. 1157 // Don't stop on encoding errors, unlike it is done for other kinds of XML resources. This matches the behavior of previous WebKit versions, Firefox and Opera.
1141 m_decoder->useLenientXMLDecoding(); 1158 m_decoder->useLenientXMLDecoding();
1142 } else if (equalIgnoringCase(responseMIMEType(), "text/html")) 1159 } else if (equalIgnoringCase(responseMIMEType(), "text/html"))
1143 m_decoder = TextResourceDecoder::create("text/html", "UTF-8"); 1160 m_decoder = TextResourceDecoder::create("text/html", "UTF-8");
1144 else 1161 else
1145 m_decoder = TextResourceDecoder::create("text/plain", "UTF-8"); 1162 m_decoder = TextResourceDecoder::create("text/plain", "UTF-8");
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1236 { 1253 {
1237 return eventNames().interfaceForXMLHttpRequest; 1254 return eventNames().interfaceForXMLHttpRequest;
1238 } 1255 }
1239 1256
1240 ScriptExecutionContext* XMLHttpRequest::scriptExecutionContext() const 1257 ScriptExecutionContext* XMLHttpRequest::scriptExecutionContext() const
1241 { 1258 {
1242 return ActiveDOMObject::scriptExecutionContext(); 1259 return ActiveDOMObject::scriptExecutionContext();
1243 } 1260 }
1244 1261
1245 } // namespace WebCore 1262 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/xml/XMLHttpRequest.h ('k') | Source/core/xml/XMLHttpRequest.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698