*Add requestData map to store arbitrary state information for requests

*Add getRequestData and setRequestData methods
This commit is contained in:
Derrick Hammer 2022-07-31 00:52:47 -04:00
parent 1142576ea5
commit 4b1717c021
1 changed files with 32 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import {
export default class WebEngine { export default class WebEngine {
private contentProviders: BaseProvider[] = []; private contentProviders: BaseProvider[] = [];
private requests: Map<string, OnBeforeRequestDetailsType> = new Map(); private requests: Map<string, OnBeforeRequestDetailsType> = new Map();
private requestData: Map<string, {}> = new Map();
constructor() { constructor() {
browser.webRequest.onHeadersReceived.addListener( browser.webRequest.onHeadersReceived.addListener(
@ -28,6 +29,7 @@ export default class WebEngine {
{ urls: ["<all_urls>"] }, { urls: ["<all_urls>"] },
["blocking"] ["blocking"]
); );
browser.webRequest.onCompleted.addListener( browser.webRequest.onCompleted.addListener(
this.onCompletedHandler.bind(this), this.onCompletedHandler.bind(this),
{ {
@ -108,6 +110,10 @@ export default class WebEngine {
if (this.requests.has(details.requestId)) { if (this.requests.has(details.requestId)) {
this.requests.delete(details.requestId); this.requests.delete(details.requestId);
} }
if (this.requestData.has(details.requestId)) {
this.requests.delete(details.requestId);
}
} }
private async onErrorHandler( private async onErrorHandler(
@ -116,6 +122,10 @@ export default class WebEngine {
if (this.requests.has(details.requestId)) { if (this.requests.has(details.requestId)) {
this.requests.delete(details.requestId); this.requests.delete(details.requestId);
} }
if (this.requestData.has(details.requestId)) {
this.requests.delete(details.requestId);
}
} }
public registerContentProvider(provider: BaseProvider) { public registerContentProvider(provider: BaseProvider) {
@ -125,4 +135,26 @@ export default class WebEngine {
this.contentProviders.push(provider); this.contentProviders.push(provider);
} }
public getRequestData(requestId: string, key: string) {
if (!this.requestData.has(requestId)) {
return undefined;
}
const store: any = this.requestData.get(requestId);
return store[key];
}
public setRequestData(requestId: string, key: string, value: any) {
let store: any = {};
if (this.requestData.has(requestId)) {
store = this.requestData.get(requestId);
}
store[key] = value;
this.requestData.set(requestId, store);
}
} }