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

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

Powered by Google App Engine
This is Rietveld 408576698