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

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

Issue 14246006: Implementing timeout support for XHR (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@timeoutResourceHandle
Patch Set: Timeout support in DocumentThreadableLoader (rebased after eb2eb02c34d16c) Created 7 years, 7 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 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 // Newer functionality is not available to synchronous requests in windo w contexts, as a spec-mandated 489 // Newer functionality is not available to synchronous requests in windo w contexts, as a spec-mandated
494 // attempt to discourage synchronous XHR use. responseType is one such p iece of functionality. 490 // attempt to discourage synchronous XHR use. responseType is one such p iece of functionality.
495 // We'll only disable this functionality for HTTP(S) requests since sync requests for local protocols 491 // We'll only disable this functionality for HTTP(S) requests since sync requests for local protocols
496 // such as file: and data: still make sense to allow. 492 // such as file: and data: still make sense to allow.
497 if (url.protocolIsInHTTPFamily() && m_responseTypeCode != ResponseTypeDe fault) { 493 if (url.protocolIsInHTTPFamily() && m_responseTypeCode != ResponseTypeDe fault) {
498 logConsoleError(scriptExecutionContext(), "Synchronous HTTP(S) reque sts made from the window context cannot have XMLHttpRequest.responseType set."); 494 logConsoleError(scriptExecutionContext(), "Synchronous HTTP(S) reque sts made from the window context cannot have XMLHttpRequest.responseType set.");
499 ec = INVALID_ACCESS_ERR; 495 ec = INVALID_ACCESS_ERR;
500 return; 496 return;
501 } 497 }
502 498
503 #if ENABLE(XHR_TIMEOUT)
504 // Similarly, timeouts are disabled for synchronous requests as well. 499 // Similarly, timeouts are disabled for synchronous requests as well.
505 if (m_timeoutMilliseconds > 0) { 500 if (m_timeoutMilliseconds > 0) {
506 logConsoleError(scriptExecutionContext(), "Synchronous XMLHttpReques ts must not have a timeout value set."); 501 logConsoleError(scriptExecutionContext(), "Synchronous XMLHttpReques ts must not have a timeout value set.");
507 ec = INVALID_ACCESS_ERR; 502 ec = INVALID_ACCESS_ERR;
508 return; 503 return;
509 } 504 }
510 #endif
511 } 505 }
512 506
513 m_method = uppercaseKnownHTTPMethod(method); 507 m_method = uppercaseKnownHTTPMethod(method);
514 508
515 m_url = url; 509 m_url = url;
516 510
517 m_async = async; 511 m_async = async;
518 512
519 ASSERT(!m_loader); 513 ASSERT(!m_loader);
520 514
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 ThreadableLoaderOptions options; 734 ThreadableLoaderOptions options;
741 options.sendLoadCallbacks = SendCallbacks; 735 options.sendLoadCallbacks = SendCallbacks;
742 options.sniffContent = DoNotSniffContent; 736 options.sniffContent = DoNotSniffContent;
743 options.preflightPolicy = uploadEvents ? ForcePreflight : ConsiderPreflight; 737 options.preflightPolicy = uploadEvents ? ForcePreflight : ConsiderPreflight;
744 options.allowCredentials = (m_sameOriginRequest || m_includeCredentials) ? A llowStoredCredentials : DoNotAllowStoredCredentials; 738 options.allowCredentials = (m_sameOriginRequest || m_includeCredentials) ? A llowStoredCredentials : DoNotAllowStoredCredentials;
745 options.credentialsRequested = m_includeCredentials ? ClientRequestedCredent ials : ClientDidNotRequestCredentials; 739 options.credentialsRequested = m_includeCredentials ? ClientRequestedCredent ials : ClientDidNotRequestCredentials;
746 options.crossOriginRequestPolicy = UseAccessControl; 740 options.crossOriginRequestPolicy = UseAccessControl;
747 options.securityOrigin = securityOrigin(); 741 options.securityOrigin = securityOrigin();
748 options.initiator = cachedResourceRequestInitiators().xmlhttprequest; 742 options.initiator = cachedResourceRequestInitiators().xmlhttprequest;
749 options.contentSecurityPolicyEnforcement = ContentSecurityPolicy::shouldBypa ssMainWorld(scriptExecutionContext()) ? DoNotEnforceContentSecurityPolicy : Enfo rceConnectSrcDirective; 743 options.contentSecurityPolicyEnforcement = ContentSecurityPolicy::shouldBypa ssMainWorld(scriptExecutionContext()) ? DoNotEnforceContentSecurityPolicy : Enfo rceConnectSrcDirective;
750 744 options.timeoutMilliseconds = m_timeoutMilliseconds;
751 #if ENABLE(XHR_TIMEOUT)
752 if (m_timeoutMilliseconds)
753 request.setTimeoutInterval(m_timeoutMilliseconds / 1000.0);
754 #endif
755 745
756 m_exceptionCode = 0; 746 m_exceptionCode = 0;
757 m_error = false; 747 m_error = false;
758 748
759 if (m_async) { 749 if (m_async) {
760 if (m_upload) 750 if (m_upload)
761 request.setReportUploadProgress(true); 751 request.setReportUploadProgress(true);
762 752
763 // ThreadableLoader::create can return null here, for example if we're n o longer attached to a page. 753 // ThreadableLoader::create can return null here, for example if we're n o longer attached to a page.
764 // This is true while running onunload handlers. 754 // This is true while running onunload handlers.
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
1046 // If we are already in an error state, for instance we called abort(), bail out early. 1036 // If we are already in an error state, for instance we called abort(), bail out early.
1047 if (m_error) 1037 if (m_error)
1048 return; 1038 return;
1049 1039
1050 if (error.isCancellation()) { 1040 if (error.isCancellation()) {
1051 m_exceptionCode = ABORT_ERR; 1041 m_exceptionCode = ABORT_ERR;
1052 abortError(); 1042 abortError();
1053 return; 1043 return;
1054 } 1044 }
1055 1045
1056 #if ENABLE(XHR_TIMEOUT)
1057 if (error.isTimeout()) { 1046 if (error.isTimeout()) {
1058 didTimeout(); 1047 didTimeout();
1059 return; 1048 return;
1060 } 1049 }
1061 #endif
1062 1050
1063 // Network failures are already reported to Web Inspector by ResourceLoader. 1051 // Network failures are already reported to Web Inspector by ResourceLoader.
1064 if (error.domain() == errorDomainWebKitInternal) 1052 if (error.domain() == errorDomainWebKitInternal)
1065 logConsoleError(scriptExecutionContext(), "XMLHttpRequest cannot load " + error.failingURL() + ". " + error.localizedDescription()); 1053 logConsoleError(scriptExecutionContext(), "XMLHttpRequest cannot load " + error.failingURL() + ". " + error.localizedDescription());
1066 1054
1067 m_exceptionCode = NETWORK_ERR; 1055 m_exceptionCode = NETWORK_ERR;
1068 networkError(); 1056 networkError();
1069 } 1057 }
1070 1058
1071 void XMLHttpRequest::didFailRedirectCheck() 1059 void XMLHttpRequest::didFailRedirectCheck()
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1177 } 1165 }
1178 1166
1179 if (m_state != LOADING) 1167 if (m_state != LOADING)
1180 changeState(LOADING); 1168 changeState(LOADING);
1181 else 1169 else
1182 // Firefox calls readyStateChanged every time it receives data, 4449 442 1170 // Firefox calls readyStateChanged every time it receives data, 4449 442
1183 callReadyStateChangeListener(); 1171 callReadyStateChangeListener();
1184 } 1172 }
1185 } 1173 }
1186 1174
1187 #if ENABLE(XHR_TIMEOUT)
1188 void XMLHttpRequest::didTimeout() 1175 void XMLHttpRequest::didTimeout()
1189 { 1176 {
1190 // internalAbort() calls dropProtection(), which may release the last refere nce. 1177 // internalAbort() calls dropProtection(), which may release the last refere nce.
1191 RefPtr<XMLHttpRequest> protect(this); 1178 RefPtr<XMLHttpRequest> protect(this);
1192 internalAbort(); 1179 internalAbort();
1193 1180
1194 clearResponse(); 1181 clearResponse();
1195 clearRequest(); 1182 clearRequest();
1196 1183
1197 m_error = true; 1184 m_error = true;
1198 m_exceptionCode = TIMEOUT_ERR; 1185 m_exceptionCode = TIMEOUT_ERR;
1199 1186
1200 if (!m_async) { 1187 if (!m_async) {
1201 m_state = DONE; 1188 m_state = DONE;
1202 m_exceptionCode = TIMEOUT_ERR; 1189 m_exceptionCode = TIMEOUT_ERR;
1203 return; 1190 return;
1204 } 1191 }
1205 1192
1206 changeState(DONE); 1193 changeState(DONE);
1207 1194
1208 if (!m_uploadComplete) { 1195 if (!m_uploadComplete) {
1209 m_uploadComplete = true; 1196 m_uploadComplete = true;
1210 if (m_upload && m_uploadEventsAllowed) 1197 if (m_upload && m_uploadEventsAllowed)
1211 m_upload->dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent::creat e(eventNames().timeoutEvent)); 1198 m_upload->dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent::creat e(eventNames().timeoutEvent));
1212 } 1199 }
1213 m_progressEventThrottle.dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent: :create(eventNames().timeoutEvent)); 1200 m_progressEventThrottle.dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent: :create(eventNames().timeoutEvent));
1214 } 1201 }
1215 #endif
1216 1202
1217 bool XMLHttpRequest::canSuspend() const 1203 bool XMLHttpRequest::canSuspend() const
1218 { 1204 {
1219 return !m_loader; 1205 return !m_loader;
1220 } 1206 }
1221 1207
1222 void XMLHttpRequest::suspend(ReasonForSuspension) 1208 void XMLHttpRequest::suspend(ReasonForSuspension)
1223 { 1209 {
1224 m_progressEventThrottle.suspend(); 1210 m_progressEventThrottle.suspend();
1225 } 1211 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1280 info.addMember(m_responseDocument, "responseDocument"); 1266 info.addMember(m_responseDocument, "responseDocument");
1281 info.addMember(m_binaryResponseBuilder, "binaryResponseBuilder"); 1267 info.addMember(m_binaryResponseBuilder, "binaryResponseBuilder");
1282 info.addMember(m_responseArrayBuffer, "responseArrayBuffer"); 1268 info.addMember(m_responseArrayBuffer, "responseArrayBuffer");
1283 info.addMember(m_lastSendURL, "lastSendURL"); 1269 info.addMember(m_lastSendURL, "lastSendURL");
1284 info.addMember(m_eventTargetData, "eventTargetData"); 1270 info.addMember(m_eventTargetData, "eventTargetData");
1285 info.addMember(m_progressEventThrottle, "progressEventThrottle"); 1271 info.addMember(m_progressEventThrottle, "progressEventThrottle");
1286 info.addMember(m_securityOrigin, "securityOrigin"); 1272 info.addMember(m_securityOrigin, "securityOrigin");
1287 } 1273 }
1288 1274
1289 } // namespace WebCore 1275 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698