browser/extensions/pdfjs/content/network.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
michael@0 3 /* Copyright 2012 Mozilla Foundation
michael@0 4 *
michael@0 5 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 6 * you may not use this file except in compliance with the License.
michael@0 7 * You may obtain a copy of the License at
michael@0 8 *
michael@0 9 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 10 *
michael@0 11 * Unless required by applicable law or agreed to in writing, software
michael@0 12 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 14 * See the License for the specific language governing permissions and
michael@0 15 * limitations under the License.
michael@0 16 */
michael@0 17
michael@0 18 // NOTE: Be careful what goes in this file, as it is also used from the context
michael@0 19 // of the addon. So using warn/error in here will break the addon.
michael@0 20
michael@0 21 'use strict';
michael@0 22
michael@0 23
michael@0 24
michael@0 25 Components.utils.import('resource://gre/modules/Services.jsm');
michael@0 26
michael@0 27 var EXPORTED_SYMBOLS = ['NetworkManager'];
michael@0 28
michael@0 29 var console = {
michael@0 30 log: function console_log(aMsg) {
michael@0 31 var msg = 'network.js: ' + (aMsg.join ? aMsg.join('') : aMsg);
michael@0 32 Services.console.logStringMessage(msg);
michael@0 33 // TODO(mack): dump() doesn't seem to work here...
michael@0 34 dump(msg + '\n');
michael@0 35 }
michael@0 36 }
michael@0 37
michael@0 38 var NetworkManager = (function NetworkManagerClosure() {
michael@0 39
michael@0 40 var OK_RESPONSE = 200;
michael@0 41 var PARTIAL_CONTENT_RESPONSE = 206;
michael@0 42
michael@0 43 function NetworkManager(url, args) {
michael@0 44 this.url = url;
michael@0 45 args = args || {};
michael@0 46 this.isHttp = /^https?:/i.test(url);
michael@0 47 this.httpHeaders = (this.isHttp && args.httpHeaders) || {};
michael@0 48 this.withCredentials = args.withCredentials || false;
michael@0 49 this.getXhr = args.getXhr ||
michael@0 50 function NetworkManager_getXhr() {
michael@0 51 return new XMLHttpRequest();
michael@0 52 };
michael@0 53
michael@0 54 this.currXhrId = 0;
michael@0 55 this.pendingRequests = {};
michael@0 56 this.loadedRequests = {};
michael@0 57 }
michael@0 58
michael@0 59 function getArrayBuffer(xhr) {
michael@0 60 var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
michael@0 61 xhr.responseArrayBuffer || xhr.response);
michael@0 62 if (typeof data !== 'string') {
michael@0 63 return data;
michael@0 64 }
michael@0 65 var length = data.length;
michael@0 66 var buffer = new Uint8Array(length);
michael@0 67 for (var i = 0; i < length; i++) {
michael@0 68 buffer[i] = data.charCodeAt(i) & 0xFF;
michael@0 69 }
michael@0 70 return buffer;
michael@0 71 }
michael@0 72
michael@0 73 NetworkManager.prototype = {
michael@0 74 requestRange: function NetworkManager_requestRange(begin, end, listeners) {
michael@0 75 var args = {
michael@0 76 begin: begin,
michael@0 77 end: end
michael@0 78 };
michael@0 79 for (var prop in listeners) {
michael@0 80 args[prop] = listeners[prop];
michael@0 81 }
michael@0 82 return this.request(args);
michael@0 83 },
michael@0 84
michael@0 85 requestFull: function NetworkManager_requestRange(listeners) {
michael@0 86 return this.request(listeners);
michael@0 87 },
michael@0 88
michael@0 89 request: function NetworkManager_requestRange(args) {
michael@0 90 var xhr = this.getXhr();
michael@0 91 var xhrId = this.currXhrId++;
michael@0 92 var pendingRequest = this.pendingRequests[xhrId] = {
michael@0 93 xhr: xhr
michael@0 94 };
michael@0 95
michael@0 96 xhr.open('GET', this.url);
michael@0 97 xhr.withCredentials = this.withCredentials;
michael@0 98 for (var property in this.httpHeaders) {
michael@0 99 var value = this.httpHeaders[property];
michael@0 100 if (typeof value === 'undefined') {
michael@0 101 continue;
michael@0 102 }
michael@0 103 xhr.setRequestHeader(property, value);
michael@0 104 }
michael@0 105 if (this.isHttp && 'begin' in args && 'end' in args) {
michael@0 106 var rangeStr = args.begin + '-' + (args.end - 1);
michael@0 107 xhr.setRequestHeader('Range', 'bytes=' + rangeStr);
michael@0 108 pendingRequest.expectedStatus = 206;
michael@0 109 } else {
michael@0 110 pendingRequest.expectedStatus = 200;
michael@0 111 }
michael@0 112
michael@0 113 xhr.mozResponseType = xhr.responseType = 'arraybuffer';
michael@0 114
michael@0 115 if (args.onProgress) {
michael@0 116 xhr.onprogress = args.onProgress;
michael@0 117 }
michael@0 118 if (args.onError) {
michael@0 119 xhr.onerror = function(evt) {
michael@0 120 args.onError(xhr.status);
michael@0 121 };
michael@0 122 }
michael@0 123 xhr.onreadystatechange = this.onStateChange.bind(this, xhrId);
michael@0 124
michael@0 125 pendingRequest.onHeadersReceived = args.onHeadersReceived;
michael@0 126 pendingRequest.onDone = args.onDone;
michael@0 127 pendingRequest.onError = args.onError;
michael@0 128
michael@0 129 xhr.send(null);
michael@0 130
michael@0 131 return xhrId;
michael@0 132 },
michael@0 133
michael@0 134 onStateChange: function NetworkManager_onStateChange(xhrId, evt) {
michael@0 135 var pendingRequest = this.pendingRequests[xhrId];
michael@0 136 if (!pendingRequest) {
michael@0 137 // Maybe abortRequest was called...
michael@0 138 return;
michael@0 139 }
michael@0 140
michael@0 141 var xhr = pendingRequest.xhr;
michael@0 142 if (xhr.readyState >= 2 && pendingRequest.onHeadersReceived) {
michael@0 143 pendingRequest.onHeadersReceived();
michael@0 144 delete pendingRequest.onHeadersReceived;
michael@0 145 }
michael@0 146
michael@0 147 if (xhr.readyState !== 4) {
michael@0 148 return;
michael@0 149 }
michael@0 150
michael@0 151 if (!(xhrId in this.pendingRequests)) {
michael@0 152 // The XHR request might have been aborted in onHeadersReceived()
michael@0 153 // callback, in which case we should abort request
michael@0 154 return;
michael@0 155 }
michael@0 156
michael@0 157 delete this.pendingRequests[xhrId];
michael@0 158
michael@0 159 // success status == 0 can be on ftp, file and other protocols
michael@0 160 if (xhr.status === 0 && this.isHttp) {
michael@0 161 if (pendingRequest.onError) {
michael@0 162 pendingRequest.onError(xhr.status);
michael@0 163 }
michael@0 164 return;
michael@0 165 }
michael@0 166 var xhrStatus = xhr.status || OK_RESPONSE;
michael@0 167
michael@0 168 // From http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.2:
michael@0 169 // "A server MAY ignore the Range header". This means it's possible to
michael@0 170 // get a 200 rather than a 206 response from a range request.
michael@0 171 var ok_response_on_range_request =
michael@0 172 xhrStatus === OK_RESPONSE &&
michael@0 173 pendingRequest.expectedStatus === PARTIAL_CONTENT_RESPONSE;
michael@0 174
michael@0 175 if (!ok_response_on_range_request &&
michael@0 176 xhrStatus !== pendingRequest.expectedStatus) {
michael@0 177 if (pendingRequest.onError) {
michael@0 178 pendingRequest.onError(xhr.status);
michael@0 179 }
michael@0 180 return;
michael@0 181 }
michael@0 182
michael@0 183 this.loadedRequests[xhrId] = true;
michael@0 184
michael@0 185 var chunk = getArrayBuffer(xhr);
michael@0 186 if (xhrStatus === PARTIAL_CONTENT_RESPONSE) {
michael@0 187 var rangeHeader = xhr.getResponseHeader('Content-Range');
michael@0 188 var matches = /bytes (\d+)-(\d+)\/(\d+)/.exec(rangeHeader);
michael@0 189 var begin = parseInt(matches[1], 10);
michael@0 190 pendingRequest.onDone({
michael@0 191 begin: begin,
michael@0 192 chunk: chunk
michael@0 193 });
michael@0 194 } else {
michael@0 195 pendingRequest.onDone({
michael@0 196 begin: 0,
michael@0 197 chunk: chunk
michael@0 198 });
michael@0 199 }
michael@0 200 },
michael@0 201
michael@0 202 hasPendingRequests: function NetworkManager_hasPendingRequests() {
michael@0 203 for (var xhrId in this.pendingRequests) {
michael@0 204 return true;
michael@0 205 }
michael@0 206 return false;
michael@0 207 },
michael@0 208
michael@0 209 getRequestXhr: function NetworkManager_getXhr(xhrId) {
michael@0 210 return this.pendingRequests[xhrId].xhr;
michael@0 211 },
michael@0 212
michael@0 213 isPendingRequest: function NetworkManager_isPendingRequest(xhrId) {
michael@0 214 return xhrId in this.pendingRequests;
michael@0 215 },
michael@0 216
michael@0 217 isLoadedRequest: function NetworkManager_isLoadedRequest(xhrId) {
michael@0 218 return xhrId in this.loadedRequests;
michael@0 219 },
michael@0 220
michael@0 221 abortAllRequests: function NetworkManager_abortAllRequests() {
michael@0 222 for (var xhrId in this.pendingRequests) {
michael@0 223 this.abortRequest(xhrId | 0);
michael@0 224 }
michael@0 225 },
michael@0 226
michael@0 227 abortRequest: function NetworkManager_abortRequest(xhrId) {
michael@0 228 var xhr = this.pendingRequests[xhrId].xhr;
michael@0 229 delete this.pendingRequests[xhrId];
michael@0 230 xhr.abort();
michael@0 231 }
michael@0 232 };
michael@0 233
michael@0 234 return NetworkManager;
michael@0 235 })();
michael@0 236
michael@0 237

mercurial