Expose more integration pipe handling code

This should eventually reduce the amount of duplicated code needed to
handle Word 2016 for Mac
This commit is contained in:
Simon Kornblith 2015-07-12 13:45:39 -04:00
parent e355cab886
commit 7d87d70e92

View file

@ -88,7 +88,7 @@ Zotero.Integration = new function() {
sharedPipe.append(".zoteroIntegrationPipe_"+logname);
if(sharedPipe.exists()) {
if(_deletePipe(sharedPipe) && sharedDir.isWritable()) {
if(this.deletePipe(sharedPipe) && sharedDir.isWritable()) {
pipe = sharedPipe;
}
} else if(sharedDir.isWritable()) {
@ -104,25 +104,12 @@ Zotero.Integration = new function() {
pipe.append(".zoteroIntegrationPipe");
// destroy old pipe, if one exists
if(!_deletePipe(pipe)) return;
if(!this.deletePipe(pipe)) return;
}
// try to initialize pipe
try {
Zotero.IPC.Pipe.initPipeListener(pipe, function(string) {
if(string != "") {
// exec command if possible
var parts = string.match(/^([^ \n]*) ([^ \n]*)(?: ([^\n]*))?\n?$/);
if(parts) {
var agent = parts[1].toString();
var cmd = parts[2].toString();
var document = parts[3] ? parts[3].toString() : null;
Zotero.Integration.execCommand(agent, cmd, document);
} else {
Components.utils.reportError("Zotero: Invalid integration input received: "+string);
}
}
});
this.initPipe(pipe);
} catch(e) {
Zotero.logError(e);
}
@ -130,10 +117,31 @@ Zotero.Integration = new function() {
Q.delay(1000).then(_checkPluginVersions);
}
/**
* Begin listening for integration commands on the given pipe
* @param {String} pipe The path to the pipe
*/
this.initPipe = function(pipe) {
Zotero.IPC.Pipe.initPipeListener(pipe, function(string) {
if(string != "") {
// exec command if possible
var parts = string.match(/^([^ \n]*) ([^ \n]*)(?: ([^\n]*))?\n?$/);
if(parts) {
var agent = parts[1].toString();
var cmd = parts[2].toString();
var document = parts[3] ? parts[3].toString() : null;
Zotero.Integration.execCommand(agent, cmd, document);
} else {
Components.utils.reportError("Zotero: Invalid integration input received: "+string);
}
}
});
}
/**
* Deletes a defunct pipe on OS X
*/
function _deletePipe(pipe) {
this.deletePipe = function(pipe) {
try {
if(pipe.exists()) {
Zotero.IPC.safePipeWrite(pipe, "Zotero shutdown\n");