Generalize RemoteResourceBlockingObserver -> BlockingObserver
This commit is contained in:
parent
965149fe0f
commit
6095ee91ff
2 changed files with 83 additions and 35 deletions
75
chrome/content/zotero/BlockingObserver.jsm
Normal file
75
chrome/content/zotero/BlockingObserver.jsm
Normal file
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
***** BEGIN LICENSE BLOCK *****
|
||||
|
||||
Copyright © 2024 Corporation for Digital Scholarship
|
||||
Vienna, Virginia, USA
|
||||
https://www.zotero.org
|
||||
|
||||
This file is part of Zotero.
|
||||
|
||||
Zotero is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Zotero is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
***** END LICENSE BLOCK *****
|
||||
*/
|
||||
|
||||
const { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
|
||||
XPCOMUtils.defineLazyModuleGetters(this, {
|
||||
Services: "resource://gre/modules/Services.jsm",
|
||||
Zotero: "chrome://zotero/content/include.jsm"
|
||||
});
|
||||
|
||||
var EXPORTED_SYMBOLS = ["BlockingObserver"];
|
||||
|
||||
class BlockingObserver {
|
||||
shouldBlock;
|
||||
|
||||
_observerAdded = false;
|
||||
_ids = new Set();
|
||||
|
||||
/**
|
||||
* @param {(uri: nsIURI) => boolean} shouldBlock
|
||||
*/
|
||||
constructor({ shouldBlock }) {
|
||||
this.shouldBlock = shouldBlock;
|
||||
}
|
||||
|
||||
register(browser) {
|
||||
let id = Zotero.platformMajorVersion > 102 ? browser.browserId : browser.browsingContext.top.id;
|
||||
this._ids.add(id);
|
||||
if (!this._observerAdded) {
|
||||
Services.obs.addObserver(this, 'http-on-modify-request');
|
||||
Zotero.debug('BlockingObserver: Added observer');
|
||||
this._observerAdded = true;
|
||||
}
|
||||
}
|
||||
|
||||
unregister(browser) {
|
||||
let id = Zotero.platformMajorVersion > 102 ? browser.browserId : browser.browsingContext.top.id;
|
||||
this._ids.delete(id);
|
||||
if (this._observerAdded && !this._ids.size) {
|
||||
Services.obs.removeObserver(this, 'http-on-modify-request');
|
||||
Zotero.debug('BlockingObserver: Removed observer');
|
||||
this._observerAdded = false;
|
||||
}
|
||||
}
|
||||
|
||||
observe(subject) {
|
||||
let channel = subject.QueryInterface(Ci.nsIHttpChannel);
|
||||
let id = Zotero.platformMajorVersion > 102 ? channel.browserId : channel.topBrowsingContextId;
|
||||
if (this._ids.has(id) && this.shouldBlock(channel.URI)) {
|
||||
channel.cancel(Cr.NS_BINDING_ABORTED);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -27,6 +27,7 @@
|
|||
var EXPORTED_SYMBOLS = ["HiddenBrowser"];
|
||||
|
||||
const { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
const { BlockingObserver } = ChromeUtils.import("chrome://zotero/content/BlockingObserver.jsm");
|
||||
|
||||
/* global HiddenFrame, E10SUtils, this */
|
||||
XPCOMUtils.defineLazyModuleGetters(this, {
|
||||
|
@ -95,7 +96,12 @@ class HiddenBrowser {
|
|||
}
|
||||
|
||||
if (options.blockRemoteResources) {
|
||||
RemoteResourceBlockingObserver.watch(browser);
|
||||
this._blockingObserver = new BlockingObserver({
|
||||
shouldBlock(uri) {
|
||||
return uri.scheme !== 'file';
|
||||
}
|
||||
});
|
||||
this._blockingObserver.register(browser);
|
||||
}
|
||||
|
||||
this._browser = browser;
|
||||
|
@ -258,7 +264,7 @@ class HiddenBrowser {
|
|||
if (this._frame) {
|
||||
(async () => {
|
||||
await this._createdPromise;
|
||||
RemoteResourceBlockingObserver.unwatch(this);
|
||||
this._blockingObserver?.unregister(this._browser);
|
||||
this._frame.destroy();
|
||||
this._frame = null;
|
||||
Zotero.debug("Deleted hidden browser");
|
||||
|
@ -266,36 +272,3 @@ class HiddenBrowser {
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
const RemoteResourceBlockingObserver = {
|
||||
_observerAdded: false,
|
||||
_ids: new Set(),
|
||||
|
||||
observe(subject) {
|
||||
let channel = subject.QueryInterface(Ci.nsIHttpChannel);
|
||||
let id = Zotero.platformMajorVersion > 102 ? channel.browserId : channel.topBrowsingContextId;
|
||||
if (this._ids.has(id) && channel.URI.scheme !== 'file') {
|
||||
channel.cancel(Cr.NS_BINDING_ABORTED);
|
||||
}
|
||||
},
|
||||
|
||||
watch(browser) {
|
||||
let id = Zotero.platformMajorVersion > 102 ? browser.browserId : browser.browsingContext.id;
|
||||
this._ids.add(id);
|
||||
if (!this._observerAdded) {
|
||||
Services.obs.addObserver(this, 'http-on-modify-request');
|
||||
Zotero.debug('RemoteResourceBlockingObserver: Added observer');
|
||||
this._observerAdded = true;
|
||||
}
|
||||
},
|
||||
|
||||
unwatch(browser) {
|
||||
let id = Zotero.platformMajorVersion > 102 ? browser.browserId : browser.browsingContext.id;
|
||||
this._ids.delete(id);
|
||||
if (this._observerAdded && !this._ids.size) {
|
||||
Services.obs.removeObserver(this, 'http-on-modify-request');
|
||||
Zotero.debug('RemoteResourceBlockingObserver: Removed observer');
|
||||
this._observerAdded = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue