Fix proxy saving/deleting for async DB

This commit is contained in:
Dan Stillman 2016-03-06 16:48:00 -05:00
parent 244a52ad22
commit 285dac425c
2 changed files with 43 additions and 41 deletions

View File

@ -70,8 +70,9 @@ Zotero.ID_Tracker = function () {
// Non-autoincrement tables // Non-autoincrement tables
// //
// TODO: use autoincrement instead where available in 1.5 // TODO: Use for everything
case 'itemDataValues': case 'itemDataValues':
case 'proxies':
var id = yield _getNextAvailable(table); var id = yield _getNextAvailable(table);
if (!id) { if (!id) {
// If we can't find an empty id quickly, just use MAX() + 1 // If we can't find an empty id quickly, just use MAX() + 1
@ -144,6 +145,7 @@ Zotero.ID_Tracker = function () {
case 'tags': case 'tags':
case 'customItemTypes': case 'customItemTypes':
case 'customFields': case 'customFields':
case 'proxies':
return table; return table;
default: default:
@ -248,6 +250,7 @@ Zotero.ID_Tracker = function () {
case 'savedSearches': case 'savedSearches':
case 'customItemTypes': case 'customItemTypes':
case 'customFields': case 'customFields':
case 'proxies':
var maxToFind = 100; var maxToFind = 100;
break; break;
@ -341,6 +344,9 @@ Zotero.ID_Tracker = function () {
case 'creatorData': case 'creatorData':
return 'creatorDataID'; return 'creatorDataID';
case 'proxies':
return 'proxyID';
default: default:
return table.substr(0, table.length - 1) + 'ID'; return table.substr(0, table.length - 1) + 'ID';
} }

View File

@ -92,7 +92,7 @@ Zotero.Proxies = new function() {
* *
* @param {nsIChannel} channel * @param {nsIChannel} channel
*/ */
this.observe = function(channel) { this.observe = Zotero.Promise.coroutine(function* (channel) {
// try to detect a proxy // try to detect a proxy
channel.QueryInterface(Components.interfaces.nsIHttpChannel); channel.QueryInterface(Components.interfaces.nsIHttpChannel);
var url = channel.URI.spec; var url = channel.URI.spec;
@ -117,7 +117,7 @@ Zotero.Proxies = new function() {
&& !_isBlacklisted(host) // and host is not blacklisted && !_isBlacklisted(host) // and host is not blacklisted
) { ) {
proxy.hosts.push(host); proxy.hosts.push(host);
proxy.save(true); yield proxy.save(true);
var bw = _getBrowserAndWindow(channel.notificationCallbacks); var bw = _getBrowserAndWindow(channel.notificationCallbacks);
if(!bw) return; if(!bw) return;
@ -151,7 +151,7 @@ Zotero.Proxies = new function() {
[{ label: "proxies.notification.enable.button", callback: function() { _showDialog(proxy.hosts[0], channel.URI.hostPort, proxy); } }]); [{ label: "proxies.notification.enable.button", callback: function() { _showDialog(proxy.hosts[0], channel.URI.hostPort, proxy); } }]);
} }
proxy.save(); yield proxy.save();
break; break;
} }
@ -200,7 +200,7 @@ Zotero.Proxies = new function() {
} }
_maybeRedirect(channel, channel.notificationCallbacks, proxied); _maybeRedirect(channel, channel.notificationCallbacks, proxied);
} });
function _maybeRedirect(channel, notificationCallbacks, proxied) { function _maybeRedirect(channel, notificationCallbacks, proxied) {
// try to find a corresponding browser object // try to find a corresponding browser object
@ -409,7 +409,7 @@ Zotero.Proxies = new function() {
* @param {String} proxyHost The host through which the given site would be redirected. * @param {String} proxyHost The host through which the given site would be redirected.
* @returns {Boolean} True if proxy should be added; false if it should not be. * @returns {Boolean} True if proxy should be added; false if it should not be.
*/ */
function _showDialog(proxiedHost, proxyHost, proxy) { var _showDialog = Zotero.Promise.coroutine(function* (proxiedHost, proxyHost, proxy) {
// ask user whether to add this proxy // ask user whether to add this proxy
var io = {site:proxiedHost, proxy:proxyHost}; var io = {site:proxiedHost, proxy:proxyHost};
var window = Components.classes["@mozilla.org/appshell/window-mediator;1"] var window = Components.classes["@mozilla.org/appshell/window-mediator;1"]
@ -424,10 +424,10 @@ Zotero.Proxies = new function() {
} }
if(io.add) { if(io.add) {
proxy.erase(); yield proxy.erase();
proxy.save(true); yield proxy.save(true);
} }
} });
/** /**
* Get browser and window from notificationCallbacks * Get browser and window from notificationCallbacks
@ -606,7 +606,7 @@ Zotero.Proxy.prototype.validate = function() {
* *
* @param {Boolean} transparent True if proxy should be saved as a persisting, transparent proxy * @param {Boolean} transparent True if proxy should be saved as a persisting, transparent proxy
*/ */
Zotero.Proxy.prototype.save = function(transparent) { Zotero.Proxy.prototype.save = Zotero.Promise.coroutine(function* (transparent) {
// ensure this proxy is valid // ensure this proxy is valid
var hasErrors = this.validate(); var hasErrors = this.validate();
if(hasErrors) throw "Proxy: could not be saved because it is invalid: error "+hasErrors[0]; if(hasErrors) throw "Proxy: could not be saved because it is invalid: error "+hasErrors[0];
@ -618,31 +618,32 @@ Zotero.Proxy.prototype.save = function(transparent) {
this.compileRegexp(); this.compileRegexp();
if(transparent) { if(transparent) {
try { yield Zotero.DB.executeTransaction(function* () {
Zotero.DB.beginTransaction();
if(this.proxyID) { if(this.proxyID) {
Zotero.DB.query("UPDATE proxies SET multiHost = ?, autoAssociate = ?, scheme = ? WHERE proxyID = ?", yield Zotero.DB.queryAsync(
[this.multiHost ? 1 : 0, this.autoAssociate ? 1 : 0, this.scheme, this.proxyID]); "UPDATE proxies SET multiHost = ?, autoAssociate = ?, scheme = ? WHERE proxyID = ?",
Zotero.DB.query("DELETE FROM proxyHosts WHERE proxyID = ?", [this.proxyID]); [this.multiHost ? 1 : 0, this.autoAssociate ? 1 : 0, this.scheme, this.proxyID]
);
yield Zotero.DB.queryAsync("DELETE FROM proxyHosts WHERE proxyID = ?", [this.proxyID]);
} else { } else {
this.proxyID = Zotero.DB.query("INSERT INTO proxies (multiHost, autoAssociate, scheme) VALUES (?, ?, ?)", let id = yield Zotero.ID.get('proxies');
[this.multiHost ? 1 : 0, this.autoAssociate ? 1 : 0, this.scheme]); yield Zotero.DB.queryAsync(
"INSERT INTO proxies (proxyID, multiHost, autoAssociate, scheme) VALUES (?, ?, ?, ?)",
[id, this.multiHost ? 1 : 0, this.autoAssociate ? 1 : 0, this.scheme]
);
this.proxyID = id;
} }
this.hosts = this.hosts.sort(); this.hosts = this.hosts.sort();
var host; var host;
for(var i in this.hosts) { for(var i in this.hosts) {
host = this.hosts[i] = this.hosts[i].toLowerCase(); host = this.hosts[i] = this.hosts[i].toLowerCase();
Zotero.DB.query("INSERT INTO proxyHosts (proxyID, hostname) VALUES (?, ?)", yield Zotero.DB.queryAsync(
[this.proxyID, host]); "INSERT INTO proxyHosts (proxyID, hostname) VALUES (?, ?)",
[this.proxyID, host]
);
} }
}.bind(this));
Zotero.DB.commitTransaction();
} catch(e) {
Zotero.DB.rollbackTransaction();
throw(e);
}
} }
if(newProxy) { if(newProxy) {
@ -651,7 +652,7 @@ Zotero.Proxy.prototype.save = function(transparent) {
Zotero.Proxies.refreshHostMap(this); Zotero.Proxies.refreshHostMap(this);
if(!transparent) throw "Proxy: cannot save transparent proxy without transparent param"; if(!transparent) throw "Proxy: cannot save transparent proxy without transparent param";
} }
} });
/** /**
* Reverts to the previously saved version of this proxy * Reverts to the previously saved version of this proxy
@ -665,21 +666,16 @@ Zotero.Proxy.prototype.revert = Zotero.Promise.coroutine(function* () {
/** /**
* Deletes this proxy * Deletes this proxy
*/ */
Zotero.Proxy.prototype.erase = function() { Zotero.Proxy.prototype.erase = Zotero.Promise.coroutine(function* () {
Zotero.Proxies.remove(this); Zotero.Proxies.remove(this);
if(this.proxyID) { if(this.proxyID) {
try { yield Zotero.DB.executeTransaction(function* () {
Zotero.DB.beginTransaction(); yield Zotero.DB.queryAsync("DELETE FROM proxyHosts WHERE proxyID = ?", [this.proxyID]);
Zotero.DB.query("DELETE FROM proxyHosts WHERE proxyID = ?", [this.proxyID]); yield Zotero.DB.queryAsync("DELETE FROM proxies WHERE proxyID = ?", [this.proxyID]);
Zotero.DB.query("DELETE FROM proxies WHERE proxyID = ?", [this.proxyID]); }.bind(this));
Zotero.DB.commitTransaction();
} catch(e) {
Zotero.DB.rollbackTransaction();
throw(e);
}
} }
} });
/** /**
* Converts a proxied URL to an unproxied URL using this proxy * Converts a proxied URL to an unproxied URL using this proxy
@ -869,7 +865,7 @@ Zotero.Proxies.Detectors.EZProxy.Observer = function(newChannel) {
Services.obs.addObserver(this, "http-on-modify-request", false); Services.obs.addObserver(this, "http-on-modify-request", false);
Services.obs.addObserver(this, "http-on-examine-response", false); Services.obs.addObserver(this, "http-on-examine-response", false);
} }
Zotero.Proxies.Detectors.EZProxy.Observer.prototype.observe = function(aSubject, aTopic, aData) { Zotero.Proxies.Detectors.EZProxy.Observer.prototype.observe = Zotero.Promise.coroutine(function* (aSubject, aTopic, aData) {
if (aSubject == this.channel) { if (aSubject == this.channel) {
if(aTopic === "http-on-modify-request") { if(aTopic === "http-on-modify-request") {
try { try {
@ -893,7 +889,7 @@ Zotero.Proxies.Detectors.EZProxy.Observer.prototype.observe = function(aSubject,
var proxy = Zotero.Proxies.Detectors.EZProxy.learn(Services.io.newURI(loginURL, null, null), aSubject.URI); var proxy = Zotero.Proxies.Detectors.EZProxy.learn(Services.io.newURI(loginURL, null, null), aSubject.URI);
if(proxy) { if(proxy) {
Zotero.debug("Proxies: Proxy-by-port EZProxy "+aSubject.URI.hostPort+" corresponds to "+proxy.hosts[0]); Zotero.debug("Proxies: Proxy-by-port EZProxy "+aSubject.URI.hostPort+" corresponds to "+proxy.hosts[0]);
proxy.save(); yield proxy.save();
} }
} catch(e) { } catch(e) {
Zotero.logError(e); Zotero.logError(e);
@ -903,7 +899,7 @@ Zotero.Proxies.Detectors.EZProxy.Observer.prototype.observe = function(aSubject,
} }
} }
} }
} });
Zotero.Proxies.Detectors.EZProxy.Observer.prototype.QueryInterface = function(aIID) { Zotero.Proxies.Detectors.EZProxy.Observer.prototype.QueryInterface = function(aIID) {
if (aIID.equals(Components.interfaces.nsISupports) || if (aIID.equals(Components.interfaces.nsISupports) ||
aIID.equals(Components.interfaces.nsIObserver)) return this; aIID.equals(Components.interfaces.nsIObserver)) return this;