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

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

Issue 14246006: Implementing timeout support for XHR (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@timeoutResourceHandle
Patch Set: Created 7 years, 8 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 /* 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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 RefPtr<XMLHttpRequest> xmlHttpRequest(adoptRef(new XMLHttpRequest(context, s ecurityOrigin))); 164 RefPtr<XMLHttpRequest> xmlHttpRequest(adoptRef(new XMLHttpRequest(context, s ecurityOrigin)));
165 xmlHttpRequest->suspendIfNeeded(); 165 xmlHttpRequest->suspendIfNeeded();
166 166
167 return xmlHttpRequest.release(); 167 return xmlHttpRequest.release();
168 } 168 }
169 169
170 XMLHttpRequest::XMLHttpRequest(ScriptExecutionContext* context, PassRefPtr<Secur ityOrigin> securityOrigin) 170 XMLHttpRequest::XMLHttpRequest(ScriptExecutionContext* context, PassRefPtr<Secur ityOrigin> securityOrigin)
171 : ActiveDOMObject(context) 171 : ActiveDOMObject(context)
172 , m_async(true) 172 , m_async(true)
173 , m_includeCredentials(false) 173 , m_includeCredentials(false)
174 #if ENABLE(XHR_TIMEOUT)
175 , m_timeoutMilliseconds(0) 174 , m_timeoutMilliseconds(0)
176 #endif
177 , m_state(UNSENT) 175 , m_state(UNSENT)
178 , m_createdDocument(false) 176 , m_createdDocument(false)
179 , m_error(false) 177 , m_error(false)
180 , m_uploadEventsAllowed(true) 178 , m_uploadEventsAllowed(true)
181 , m_uploadComplete(false) 179 , m_uploadComplete(false)
182 , m_sameOriginRequest(true) 180 , m_sameOriginRequest(true)
183 , m_receivedLength(0) 181 , m_receivedLength(0)
184 , m_lastSendLineNumber(0) 182 , m_lastSendLineNumber(0)
185 , m_exceptionCode(0) 183 , m_exceptionCode(0)
186 , m_progressEventThrottle(this) 184 , m_progressEventThrottle(this)
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 return 0; 307 return 0;
310 308
311 if (!m_responseArrayBuffer.get() && m_binaryResponseBuilder.get() && m_binar yResponseBuilder->size() > 0) { 309 if (!m_responseArrayBuffer.get() && m_binaryResponseBuilder.get() && m_binar yResponseBuilder->size() > 0) {
312 m_responseArrayBuffer = ArrayBuffer::create(const_cast<char*>(m_binaryRe sponseBuilder->data()), static_cast<unsigned>(m_binaryResponseBuilder->size())); 310 m_responseArrayBuffer = ArrayBuffer::create(const_cast<char*>(m_binaryRe sponseBuilder->data()), static_cast<unsigned>(m_binaryResponseBuilder->size()));
313 m_binaryResponseBuilder.clear(); 311 m_binaryResponseBuilder.clear();
314 } 312 }
315 313
316 return m_responseArrayBuffer.get(); 314 return m_responseArrayBuffer.get();
317 } 315 }
318 316
319 #if ENABLE(XHR_TIMEOUT)
320 void XMLHttpRequest::setTimeout(unsigned long timeout, ExceptionCode& ec) 317 void XMLHttpRequest::setTimeout(unsigned long timeout, ExceptionCode& ec)
321 { 318 {
322 // FIXME: Need to trigger or update the timeout Timer here, if needed. http: //webkit.org/b/98156 319 // FIXME: Need to trigger or update the timeout Timer here, if needed. http: //webkit.org/b/98156
323 // XHR2 spec, 4.7.3. "This implies that the timeout attribute can be set whi le fetching is in progress. If that occurs it will still be measured relative to the start of fetching." 320 // XHR2 spec, 4.7.3. "This implies that the timeout attribute can be set whi le fetching is in progress. If that occurs it will still be measured relative to the start of fetching."
324 if (scriptExecutionContext()->isDocument() && !m_async) { 321 if (scriptExecutionContext()->isDocument() && !m_async) {
325 logConsoleError(scriptExecutionContext(), "XMLHttpRequest.timeout cannot be set for synchronous HTTP(S) requests made from the window context."); 322 logConsoleError(scriptExecutionContext(), "XMLHttpRequest.timeout cannot be set for synchronous HTTP(S) requests made from the window context.");
326 ec = INVALID_ACCESS_ERR; 323 ec = INVALID_ACCESS_ERR;
327 return; 324 return;
328 } 325 }
329 m_timeoutMilliseconds = timeout; 326 m_timeoutMilliseconds = timeout;
330 } 327 }
331 #endif
332 328
333 void XMLHttpRequest::setResponseType(const String& responseType, ExceptionCode& ec) 329 void XMLHttpRequest::setResponseType(const String& responseType, ExceptionCode& ec)
334 { 330 {
335 if (m_state >= LOADING) { 331 if (m_state >= LOADING) {
336 ec = INVALID_STATE_ERR; 332 ec = INVALID_STATE_ERR;
337 return; 333 return;
338 } 334 }
339 335
340 // Newer functionality is not available to synchronous requests in window co ntexts, as a spec-mandated 336 // Newer functionality is not available to synchronous requests in window co ntexts, as a spec-mandated
341 // attempt to discourage synchronous XHR use. responseType is one such piece of functionality. 337 // attempt to discourage synchronous XHR use. responseType is one such piece of functionality.
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 // Newer functionality is not available to synchronous requests in windo w contexts, as a spec-mandated 495 // Newer functionality is not available to synchronous requests in windo w contexts, as a spec-mandated
500 // attempt to discourage synchronous XHR use. responseType is one such p iece of functionality. 496 // attempt to discourage synchronous XHR use. responseType is one such p iece of functionality.
501 // We'll only disable this functionality for HTTP(S) requests since sync requests for local protocols 497 // We'll only disable this functionality for HTTP(S) requests since sync requests for local protocols
502 // such as file: and data: still make sense to allow. 498 // such as file: and data: still make sense to allow.
503 if (url.protocolIsInHTTPFamily() && m_responseTypeCode != ResponseTypeDe fault) { 499 if (url.protocolIsInHTTPFamily() && m_responseTypeCode != ResponseTypeDe fault) {
504 logConsoleError(scriptExecutionContext(), "Synchronous HTTP(S) reque sts made from the window context cannot have XMLHttpRequest.responseType set."); 500 logConsoleError(scriptExecutionContext(), "Synchronous HTTP(S) reque sts made from the window context cannot have XMLHttpRequest.responseType set.");
505 ec = INVALID_ACCESS_ERR; 501 ec = INVALID_ACCESS_ERR;
506 return; 502 return;
507 } 503 }
508 504
509 #if ENABLE(XHR_TIMEOUT)
510 // Similarly, timeouts are disabled for synchronous requests as well. 505 // Similarly, timeouts are disabled for synchronous requests as well.
511 if (m_timeoutMilliseconds > 0) { 506 if (m_timeoutMilliseconds > 0) {
512 logConsoleError(scriptExecutionContext(), "Synchronous XMLHttpReques ts must not have a timeout value set."); 507 logConsoleError(scriptExecutionContext(), "Synchronous XMLHttpReques ts must not have a timeout value set.");
513 ec = INVALID_ACCESS_ERR; 508 ec = INVALID_ACCESS_ERR;
514 return; 509 return;
515 } 510 }
516 #endif
517 } 511 }
518 512
519 m_method = uppercaseKnownHTTPMethod(method); 513 m_method = uppercaseKnownHTTPMethod(method);
520 514
521 m_url = url; 515 m_url = url;
522 516
523 m_async = async; 517 m_async = async;
524 518
525 ASSERT(!m_loader); 519 ASSERT(!m_loader);
526 520
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
749 743
750 ThreadableLoaderOptions options; 744 ThreadableLoaderOptions options;
751 options.sendLoadCallbacks = SendCallbacks; 745 options.sendLoadCallbacks = SendCallbacks;
752 options.sniffContent = DoNotSniffContent; 746 options.sniffContent = DoNotSniffContent;
753 options.preflightPolicy = uploadEvents ? ForcePreflight : ConsiderPreflight; 747 options.preflightPolicy = uploadEvents ? ForcePreflight : ConsiderPreflight;
754 options.allowCredentials = (m_sameOriginRequest || m_includeCredentials) ? A llowStoredCredentials : DoNotAllowStoredCredentials; 748 options.allowCredentials = (m_sameOriginRequest || m_includeCredentials) ? A llowStoredCredentials : DoNotAllowStoredCredentials;
755 options.crossOriginRequestPolicy = UseAccessControl; 749 options.crossOriginRequestPolicy = UseAccessControl;
756 options.securityOrigin = securityOrigin(); 750 options.securityOrigin = securityOrigin();
757 options.initiator = cachedResourceRequestInitiators().xmlhttprequest; 751 options.initiator = cachedResourceRequestInitiators().xmlhttprequest;
758 752
759 #if ENABLE(XHR_TIMEOUT)
760 if (m_timeoutMilliseconds) 753 if (m_timeoutMilliseconds)
761 request.setTimeoutInterval(m_timeoutMilliseconds / 1000.0); 754 request.setTimeoutInterval(m_timeoutMilliseconds / 1000.0);
762 #endif
763 755
764 m_exceptionCode = 0; 756 m_exceptionCode = 0;
765 m_error = false; 757 m_error = false;
766 758
767 if (m_async) { 759 if (m_async) {
768 if (m_upload) 760 if (m_upload)
769 request.setReportUploadProgress(true); 761 request.setReportUploadProgress(true);
770 762
771 // ThreadableLoader::create can return null here, for example if we're n o longer attached to a page. 763 // ThreadableLoader::create can return null here, for example if we're n o longer attached to a page.
772 // This is true while running onunload handlers. 764 // This is true while running onunload handlers.
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
1054 // If we are already in an error state, for instance we called abort(), bail out early. 1046 // If we are already in an error state, for instance we called abort(), bail out early.
1055 if (m_error) 1047 if (m_error)
1056 return; 1048 return;
1057 1049
1058 if (error.isCancellation()) { 1050 if (error.isCancellation()) {
1059 m_exceptionCode = XMLHttpRequestException::ABORT_ERR; 1051 m_exceptionCode = XMLHttpRequestException::ABORT_ERR;
1060 abortError(); 1052 abortError();
1061 return; 1053 return;
1062 } 1054 }
1063 1055
1064 #if ENABLE(XHR_TIMEOUT)
1065 if (error.isTimeout()) { 1056 if (error.isTimeout()) {
1066 didTimeout(); 1057 didTimeout();
1067 return; 1058 return;
1068 } 1059 }
1069 #endif
1070 1060
1071 // Network failures are already reported to Web Inspector by ResourceLoader. 1061 // Network failures are already reported to Web Inspector by ResourceLoader.
1072 if (error.domain() == errorDomainWebKitInternal) 1062 if (error.domain() == errorDomainWebKitInternal)
1073 logConsoleError(scriptExecutionContext(), "XMLHttpRequest cannot load " + error.failingURL() + ". " + error.localizedDescription()); 1063 logConsoleError(scriptExecutionContext(), "XMLHttpRequest cannot load " + error.failingURL() + ". " + error.localizedDescription());
1074 1064
1075 m_exceptionCode = XMLHttpRequestException::NETWORK_ERR; 1065 m_exceptionCode = XMLHttpRequestException::NETWORK_ERR;
1076 networkError(); 1066 networkError();
1077 } 1067 }
1078 1068
1079 void XMLHttpRequest::didFailRedirectCheck() 1069 void XMLHttpRequest::didFailRedirectCheck()
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1185 } 1175 }
1186 1176
1187 if (m_state != LOADING) 1177 if (m_state != LOADING)
1188 changeState(LOADING); 1178 changeState(LOADING);
1189 else 1179 else
1190 // Firefox calls readyStateChanged every time it receives data, 4449 442 1180 // Firefox calls readyStateChanged every time it receives data, 4449 442
1191 callReadyStateChangeListener(); 1181 callReadyStateChangeListener();
1192 } 1182 }
1193 } 1183 }
1194 1184
1195 #if ENABLE(XHR_TIMEOUT) 1185
1196 void XMLHttpRequest::didTimeout() 1186 void XMLHttpRequest::didTimeout()
1197 { 1187 {
1198 // internalAbort() calls dropProtection(), which may release the last refere nce. 1188 // internalAbort() calls dropProtection(), which may release the last refere nce.
1199 RefPtr<XMLHttpRequest> protect(this); 1189 RefPtr<XMLHttpRequest> protect(this);
1200 internalAbort(); 1190 internalAbort();
1201 1191
1202 clearResponse(); 1192 clearResponse();
1203 clearRequest(); 1193 clearRequest();
1204 1194
1205 m_error = true; 1195 m_error = true;
1206 m_exceptionCode = XMLHttpRequestException::TIMEOUT_ERR; 1196 m_exceptionCode = XMLHttpRequestException::TIMEOUT_ERR;
1207 1197
1208 if (!m_async) { 1198 if (!m_async) {
1209 m_state = DONE; 1199 m_state = DONE;
1210 m_exceptionCode = TIMEOUT_ERR; 1200 m_exceptionCode = TIMEOUT_ERR;
1211 return; 1201 return;
1212 } 1202 }
1213 1203
1214 changeState(DONE); 1204 changeState(DONE);
1215 1205
1216 if (!m_uploadComplete) { 1206 if (!m_uploadComplete) {
1217 m_uploadComplete = true; 1207 m_uploadComplete = true;
1218 if (m_upload && m_uploadEventsAllowed) 1208 if (m_upload && m_uploadEventsAllowed)
1219 m_upload->dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent::creat e(eventNames().timeoutEvent)); 1209 m_upload->dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent::creat e(eventNames().timeoutEvent));
1220 } 1210 }
1221 m_progressEventThrottle.dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent: :create(eventNames().timeoutEvent)); 1211 m_progressEventThrottle.dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent: :create(eventNames().timeoutEvent));
1222 } 1212 }
1223 #endif
1224 1213
1225 bool XMLHttpRequest::canSuspend() const 1214 bool XMLHttpRequest::canSuspend() const
1226 { 1215 {
1227 return !m_loader; 1216 return !m_loader;
1228 } 1217 }
1229 1218
1230 void XMLHttpRequest::suspend(ReasonForSuspension) 1219 void XMLHttpRequest::suspend(ReasonForSuspension)
1231 { 1220 {
1232 m_progressEventThrottle.suspend(); 1221 m_progressEventThrottle.suspend();
1233 } 1222 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1288 info.addMember(m_responseDocument, "responseDocument"); 1277 info.addMember(m_responseDocument, "responseDocument");
1289 info.addMember(m_binaryResponseBuilder, "binaryResponseBuilder"); 1278 info.addMember(m_binaryResponseBuilder, "binaryResponseBuilder");
1290 info.addMember(m_responseArrayBuffer, "responseArrayBuffer"); 1279 info.addMember(m_responseArrayBuffer, "responseArrayBuffer");
1291 info.addMember(m_lastSendURL, "lastSendURL"); 1280 info.addMember(m_lastSendURL, "lastSendURL");
1292 info.addMember(m_eventTargetData, "eventTargetData"); 1281 info.addMember(m_eventTargetData, "eventTargetData");
1293 info.addMember(m_progressEventThrottle, "progressEventThrottle"); 1282 info.addMember(m_progressEventThrottle, "progressEventThrottle");
1294 info.addMember(m_securityOrigin, "securityOrigin"); 1283 info.addMember(m_securityOrigin, "securityOrigin");
1295 } 1284 }
1296 1285
1297 } // namespace WebCore 1286 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698