ugh. poll integration pipe on Firefox 3.6.

This commit is contained in:
Simon Kornblith 2011-03-31 17:37:03 +00:00
parent f12e5e6a74
commit 390bcc37fb

View File

@ -36,13 +36,12 @@ const INTEGRATION_MIN_VERSION = "3.1a0";
Zotero.Integration = new function() {
var _fifoFile = null;
var _tmpFile = null;
var _shCmd = null;
var _shProc = null;
var _osascriptFile;
var _inProgress = false;
var _integrationVersionsOK = null;
var _pipeMode = false;
var _winUser32;
var _timer;
this.sessions = {};
@ -233,22 +232,21 @@ Zotero.Integration = new function() {
}};
/**
* Reads from the temp file set up to handle integration pipe and executes the appropriate
* integration command
*
* Used to read from the integration pipe on Fx 3.6
* Polling mechanism for file
*/
var _integrationPipeObserverFx36 = {"observe":function(subject) {
// if we had an error reading from the pipe, return immediately, because trying to read
// again will probably just cause us to loop
if(subject.exitValue !== 0) {
Components.utils.reportError("Zotero: An error occurred reading from integration pipe");
return;
}
var _integrationPipeObserverFx36 = {"notify":function() {
if(_fifoFile.fileSize === 0) return;
// read from pipe
var string = Zotero.File.getContents(_tmpFile);
// read from pipe (file, actually)
var string = Zotero.File.getContents(_fifoFile);
// clear file
var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
createInstance(Components.interfaces.nsIFileOutputStream);
foStream.init(_fifoFile, 0x02 | 0x08 | 0x20, 0666, 0);
foStream.close();
// run command
_parseIntegrationPipeCommand(string);
}};
@ -256,7 +254,6 @@ Zotero.Integration = new function() {
* Initializes the nsIInputStream and nsIInputStreamPump to read from _fifoFile
*/
function _initializePipeStreamPump() {
if(_pipeMode === "deferredOpen") {
// Fx >4 supports deferred open; no need to use sh
var fifoStream = Components.classes["@mozilla.org/network/file-input-stream;1"].
createInstance(Components.interfaces.nsIFileInputStream);
@ -268,10 +265,6 @@ Zotero.Integration = new function() {
createInstance(Components.interfaces.nsIInputStreamPump);
pump.init(fifoStream, -1, -1, 4096, 1, true);
pump.asyncRead(_integrationPipeListenerFx42, null);
} else {
// Fx 3.6 doesn't support deferred open
_shProc.runAsync(_shCmd, _shCmd.length, _integrationPipeObserverFx36);
}
}
/**
@ -294,7 +287,7 @@ Zotero.Integration = new function() {
}
} else {
if(Zotero.isMac) {
_pipeMode = "subprocess";
_pipeMode = "poll";
} else {
_pipeMode = "fx36thread";
}
@ -302,6 +295,20 @@ Zotero.Integration = new function() {
Zotero.debug("Using integration pipe mode "+_pipeMode);
if(_pipeMode === "poll") {
// create empty file
var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
createInstance(Components.interfaces.nsIFileOutputStream);
foStream.init(_fifoFile, 0x02 | 0x08 | 0x20, 0666, 0);
foStream.close();
// no deferred open capability, so we need to poll
// has to be global so that we don't get garbage collected
_timer = Components.classes["@mozilla.org/timer;1"].
createInstance(Components.interfaces.nsITimer);
_timer.initWithCallback(_integrationPipeObserverFx36, 1000,
Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
} else {
// make a new pipe
var mkfifo = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
@ -309,12 +316,7 @@ Zotero.Integration = new function() {
if(!mkfifo.exists()) mkfifo.initWithPath("/bin/mkfifo");
if(!mkfifo.exists()) mkfifo.initWithPath("/usr/local/bin/mkfifo");
// get sh
var sh = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
sh.initWithPath("/bin/sh");
if(mkfifo.exists() && sh.exists()) {
if(mkfifo.exists()) {
// create named pipe
var proc = Components.classes["@mozilla.org/process/util;1"].
createInstance(Components.interfaces.nsIProcess);
@ -322,27 +324,7 @@ Zotero.Integration = new function() {
proc.run(true, [_fifoFile.path], 1);
if(_fifoFile.exists()) {
if(_pipeMode === "subprocess") {
// no deferred open capability, so we need to use the sh/tmpfile hack
// make a tmp file
_tmpFile = Components.classes["@mozilla.org/file/directory_service;1"].
getService(Components.interfaces.nsIProperties).
get("TmpD", Components.interfaces.nsIFile);
_tmpFile.append("zoteroIntegrationTmp");
_tmpFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);
// begin reading from named pipe
_shCmd = ["-c", "cat '"+_fifoFile.path.replace("'", "'\\''")+"' > '"+
_tmpFile.path.replace("'", "'\\''")+"'"];
Zotero.debug("Calling sh "+_shCmd.join(" "));
_shProc = Components.classes["@mozilla.org/process/util;1"].
createInstance(Components.interfaces.nsIProcess);
_shProc.init(sh);
_initializePipeStreamPump();
} else if(_pipeMode === "deferredOpen") {
if(_pipeMode === "deferredOpen") {
_initializePipeStreamPump();
} else if(_pipeMode === "fx36thread") {
var main = Components.classes["@mozilla.org/thread-manager;1"].getService().mainThread;
@ -437,18 +419,19 @@ Zotero.Integration = new function() {
}
worker.postMessage({"path":_fifoFile.path, "libc":libc});
}
return true;
}
} else {
Components.utils.reportError("Zotero: mkfifo failed -- not initializing integration pipe");
return false;
}
} else {
Components.utils.reportError("Zotero: mkfifo or sh not found -- not initializing integration pipe");
return false;
}
}
return true;
}
/**
* Calls the Integration applicatoon
*/
@ -549,6 +532,7 @@ Zotero.Integration = new function() {
* Destroys the integration pipe.
*/
this.destroy = function() {
if(_pipeMode !== "poll") {
// send shutdown message to fifo thread
var oStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
getService(Components.interfaces.nsIFileOutputStream);
@ -556,8 +540,8 @@ Zotero.Integration = new function() {
var cmd = "Zotero shutdown\n";
oStream.write(cmd, cmd.length);
oStream.close();
}
_fifoFile.remove(false);
if(_pipeMode === "subprocess") _tmpFile.remove(false);
}
/**