Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | |
michael@0 | 5 | // A simple class that encapsulates a request. You'll notice the |
michael@0 | 6 | // style here is different from the rest of the extension; that's |
michael@0 | 7 | // because this was re-used from really old code we had. At some |
michael@0 | 8 | // point it might be nice to replace this with something better |
michael@0 | 9 | // (e.g., something that has explicit onerror handler, ability |
michael@0 | 10 | // to set headers, and so on). |
michael@0 | 11 | |
michael@0 | 12 | /** |
michael@0 | 13 | * Because we might be in a component, we can't just assume that |
michael@0 | 14 | * XMLHttpRequest exists. So we use this tiny factory function to wrap the |
michael@0 | 15 | * XPCOM version. |
michael@0 | 16 | * |
michael@0 | 17 | * @return XMLHttpRequest object |
michael@0 | 18 | */ |
michael@0 | 19 | function PROT_NewXMLHttpRequest() { |
michael@0 | 20 | var Cc = Components.classes; |
michael@0 | 21 | var Ci = Components.interfaces; |
michael@0 | 22 | var request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] |
michael@0 | 23 | .createInstance(Ci.nsIXMLHttpRequest); |
michael@0 | 24 | // Need the following so we get onerror/load/progresschange |
michael@0 | 25 | request.QueryInterface(Ci.nsIJSXMLHttpRequest); |
michael@0 | 26 | return request; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | /** |
michael@0 | 30 | * A helper class that does HTTP GETs and calls back a function with |
michael@0 | 31 | * the content it receives. Asynchronous, so uses a closure for the |
michael@0 | 32 | * callback. |
michael@0 | 33 | * |
michael@0 | 34 | * Note, that XMLFetcher is only used for SafeBrowsing, therefore |
michael@0 | 35 | * we inherit from nsILoadContext, so we can use the callbacks on the |
michael@0 | 36 | * channel to separate the safebrowsing cookie based on a reserved |
michael@0 | 37 | * appId. |
michael@0 | 38 | * @constructor |
michael@0 | 39 | */ |
michael@0 | 40 | function PROT_XMLFetcher() { |
michael@0 | 41 | this.debugZone = "xmlfetcher"; |
michael@0 | 42 | this._request = PROT_NewXMLHttpRequest(); |
michael@0 | 43 | // implements nsILoadContext |
michael@0 | 44 | this.appId = Ci.nsIScriptSecurityManager.SAFEBROWSING_APP_ID; |
michael@0 | 45 | this.isInBrowserElement = false; |
michael@0 | 46 | this.usePrivateBrowsing = false; |
michael@0 | 47 | this.isContent = false; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | PROT_XMLFetcher.prototype = { |
michael@0 | 51 | /** |
michael@0 | 52 | * Function that will be called back upon fetch completion. |
michael@0 | 53 | */ |
michael@0 | 54 | _callback: null, |
michael@0 | 55 | |
michael@0 | 56 | |
michael@0 | 57 | /** |
michael@0 | 58 | * Fetches some content. |
michael@0 | 59 | * |
michael@0 | 60 | * @param page URL to fetch |
michael@0 | 61 | * @param callback Function to call back when complete. |
michael@0 | 62 | */ |
michael@0 | 63 | get: function(page, callback) { |
michael@0 | 64 | this._request.abort(); // abort() is asynchronous, so |
michael@0 | 65 | this._request = PROT_NewXMLHttpRequest(); |
michael@0 | 66 | this._callback = callback; |
michael@0 | 67 | var asynchronous = true; |
michael@0 | 68 | this._request.open("GET", page, asynchronous); |
michael@0 | 69 | this._request.channel.notificationCallbacks = this; |
michael@0 | 70 | |
michael@0 | 71 | // Create a closure |
michael@0 | 72 | var self = this; |
michael@0 | 73 | this._request.addEventListener("readystatechange", function() { |
michael@0 | 74 | self.readyStateChange(self); |
michael@0 | 75 | }, false); |
michael@0 | 76 | |
michael@0 | 77 | this._request.send(null); |
michael@0 | 78 | }, |
michael@0 | 79 | |
michael@0 | 80 | cancel: function() { |
michael@0 | 81 | this._request.abort(); |
michael@0 | 82 | this._request = null; |
michael@0 | 83 | }, |
michael@0 | 84 | |
michael@0 | 85 | /** |
michael@0 | 86 | * Called periodically by the request to indicate some state change. 4 |
michael@0 | 87 | * means content has been received. |
michael@0 | 88 | */ |
michael@0 | 89 | readyStateChange: function(fetcher) { |
michael@0 | 90 | if (fetcher._request.readyState != 4) |
michael@0 | 91 | return; |
michael@0 | 92 | |
michael@0 | 93 | // If the request fails, on trunk we get status set to |
michael@0 | 94 | // NS_ERROR_NOT_AVAILABLE. On 1.8.1 branch we get an exception |
michael@0 | 95 | // forwarded from nsIHttpChannel::GetResponseStatus. To be consistent |
michael@0 | 96 | // between branch and trunk, we send back NS_ERROR_NOT_AVAILABLE for |
michael@0 | 97 | // http failures. |
michael@0 | 98 | var responseText = null; |
michael@0 | 99 | var status = Components.results.NS_ERROR_NOT_AVAILABLE; |
michael@0 | 100 | try { |
michael@0 | 101 | G_Debug(this, "xml fetch status code: \"" + |
michael@0 | 102 | fetcher._request.status + "\""); |
michael@0 | 103 | status = fetcher._request.status; |
michael@0 | 104 | responseText = fetcher._request.responseText; |
michael@0 | 105 | } catch(e) { |
michael@0 | 106 | G_Debug(this, "Caught exception trying to read xmlhttprequest " + |
michael@0 | 107 | "status/response."); |
michael@0 | 108 | G_Debug(this, e); |
michael@0 | 109 | } |
michael@0 | 110 | if (fetcher._callback) |
michael@0 | 111 | fetcher._callback(responseText, status); |
michael@0 | 112 | }, |
michael@0 | 113 | |
michael@0 | 114 | // nsIInterfaceRequestor |
michael@0 | 115 | getInterface: function(iid) { |
michael@0 | 116 | return this.QueryInterface(iid); |
michael@0 | 117 | }, |
michael@0 | 118 | |
michael@0 | 119 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIInterfaceRequestor, |
michael@0 | 120 | Ci.nsISupports, |
michael@0 | 121 | Ci.nsILoadContext]) |
michael@0 | 122 | }; |