www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

standalone.js (3449B)


      1 /*
      2     ***** BEGIN LICENSE BLOCK *****
      3     
      4     Copyright © 2012 Center for History and New Media
      5                      George Mason University, Fairfax, Virginia, USA
      6                      http://zotero.org
      7     
      8     This file is part of Zotero.
      9     
     10     Zotero is free software: you can redistribute it and/or modify
     11     it under the terms of the GNU Affero General Public License as published by
     12     the Free Software Foundation, either version 3 of the License, or
     13     (at your option) any later version.
     14     
     15     Zotero is distributed in the hope that it will be useful,
     16     but WITHOUT ANY WARRANTY; without even the implied warranty of
     17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18     GNU Affero General Public License for more details.
     19     
     20     You should have received a copy of the GNU Affero General Public License
     21     along with Zotero.  If not, see <http://www.gnu.org/licenses/>.
     22     
     23     ***** END LICENSE BLOCK *****
     24 */
     25 Components.utils.import("resource://gre/modules/Services.jsm");
     26 
     27 Zotero.Standalone = new function() {
     28 	/**
     29 	 * Stream listener proxy for AMO requests to replace Firefox's app ID with toolkit@mozilla.org.
     30 	 * This means add-ons hosted at AMO will update properly for us.
     31 	 */
     32 	var AMOStreamListener = function() {};
     33 	AMOStreamListener.prototype = {	
     34 		"QueryInterface": function(arg) {
     35 			if (!iid.equals(Components.interfaces.nsIStreamListener)
     36 					&& !iid.equals(Components.interfaces.nsIRequestObserver)
     37 					&& !iid.equals(Components.interfaces.nsISupports)) {
     38 				throw Components.results.NS_ERROR_NO_INTERFACE;
     39 			}
     40 			return this;
     41 		},
     42 		
     43 		"onStartRequest": function(aRequest, aContext) {
     44 			this._stream = Cc["@mozilla.org/binaryinputstream;1"].
     45 						   createInstance(Ci.nsIBinaryInputStream);
     46 			this._bytes = "";
     47 			this.oldListener.onStartRequest(aRequest, aContext);
     48 		},
     49 		
     50 		"onStopRequest": function(aRequest, aContext, aStatusCode) {
     51 			var requestFailed = !Components.isSuccessCode(aStatusCode);
     52 			if(!requestFailed && (aRequest instanceof Ci.nsIHttpChannel))
     53 				requestFailed = !aRequest.requestSucceeded;
     54 			
     55 			if(!requestFailed) {
     56 				var data = this._bytes.replace("{ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
     57 					"toolkit@mozilla.org", "g")
     58 				var nBytes = data.length;
     59 				var inputStream = Cc["@mozilla.org/io/string-input-stream;1"].
     60 					createInstance(Ci.nsIStringInputStream);
     61 				inputStream.setData(data, nBytes);
     62 				this.oldListener.onDataAvailable(aRequest, aContext, inputStream, 0, nBytes);
     63 			}
     64 			this.oldListener.onStopRequest(aRequest, aContext, aStatusCode);
     65 		},
     66 		
     67 		"onDataAvailable": function(aRequest, aContext, aInputStream, aOffset, aCount) {
     68 			this._stream.setInputStream(aInputStream);
     69 			this._bytes += this._stream.readBytes(aCount);
     70 		}
     71 	};
     72 	
     73 	this.init = function() {
     74 		// Set not offline
     75 		Services.io.offline = false;
     76 		
     77 		// Add an observer to handle AMO requests
     78 		Components.classes["@mozilla.org/observer-service;1"].
     79 			getService(Components.interfaces.nsIObserverService).
     80 			addObserver({
     81 				"observe":function(ch) {
     82 					try {
     83 						if(ch.QueryInterface(Components.interfaces.nsIRequest).URI.host
     84 							!== "versioncheck.addons.mozilla.org") return;
     85 					} catch(e) {
     86 						return;
     87 					}
     88 					var newListener = new AMOStreamListener;
     89 					newListener.oldListener = ch.
     90 						QueryInterface(Components.interfaces.nsITraceableChannel).
     91 						setNewListener(newListener);
     92 				}
     93 			}, "http-on-examine-response", false);
     94 	}
     95 }