Feature-test moz-chunked-arraybuffer before use

This commit is contained in:
Rob Wu 2014-10-29 16:31:15 +01:00
parent 67816bd085
commit 2e63dcdcf5

View File

@ -75,6 +75,28 @@ var NetworkManager = (function NetworkManagerClosure() {
return array.buffer; return array.buffer;
} }
//#if !(CHROME || FIREFOX || MOZCENTRAL)
var supportsMozChunked = (function supportsMozChunkedClosure() {
var x = new XMLHttpRequest();
try {
// Firefox 37- required .open() to be called before setting responseType.
// https://bugzilla.mozilla.org/show_bug.cgi?id=707484
x.open('GET', 'https://example.com');
} catch (e) {
// Even though the URL is not visited, .open() could fail if the URL is
// blocked, e.g. via the connect-src CSP directive or the NoScript addon.
// When this error occurs, this feature detection method will mistakenly
// report that moz-chunked-arraybuffer is not supported in Firefox 37-.
}
try {
x.responseType = 'moz-chunked-arraybuffer';
return x.responseType === 'moz-chunked-arraybuffer';
} catch (e) {
return false;
}
})();
//#endif
NetworkManager.prototype = { NetworkManager.prototype = {
requestRange: function NetworkManager_requestRange(begin, end, listeners) { requestRange: function NetworkManager_requestRange(begin, end, listeners) {
var args = { var args = {
@ -115,17 +137,19 @@ var NetworkManager = (function NetworkManagerClosure() {
pendingRequest.expectedStatus = 200; pendingRequest.expectedStatus = 200;
} }
if (args.onProgressiveData) { //#if CHROME
// Some legacy browsers might throw an exception. // var useMozChunkedLoading = false;
try { //#endif
xhr.responseType = 'moz-chunked-arraybuffer'; //#if (FIREFOX || MOZCENTRAL)
} catch(e) {} // var useMozChunkedLoading = !!args.onProgressiveData;
if (xhr.responseType === 'moz-chunked-arraybuffer') { //#endif
pendingRequest.onProgressiveData = args.onProgressiveData; //#if !(CHROME || FIREFOX || MOZCENTRAL)
pendingRequest.mozChunked = true; var useMozChunkedLoading = supportsMozChunked && !!args.onProgressiveData;
} else { //#endif
xhr.responseType = 'arraybuffer'; if (useMozChunkedLoading) {
} xhr.responseType = 'moz-chunked-arraybuffer';
pendingRequest.onProgressiveData = args.onProgressiveData;
pendingRequest.mozChunked = true;
} else { } else {
xhr.responseType = 'arraybuffer'; xhr.responseType = 'arraybuffer';
} }