Skip to content

Commit

Permalink
webextension: handler for runtime messages now returns a promise (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
remusao committed Jan 16, 2020
1 parent 74de503 commit 8bb8dc4
Showing 1 changed file with 80 additions and 51 deletions.
131 changes: 80 additions & 51 deletions packages/adblocker-webextension/adblocker.ts
Expand Up @@ -227,48 +227,20 @@ export class WebExtensionBlocker extends FiltersEngine {
}
}

/**
* Deal with request cancellation (`{ cancel: true }`) and redirection (`{ redirectUrl: '...' }`).
*/
private onBeforeRequest = (
details: WebRequestBeforeRequestDetails,
): chrome.webRequest.BlockingResponse => {
const request = fromWebRequestDetails(details);
if (request.isMainFrame()) {
this.performHTMLFiltering(request);
return {};
public handleRuntimeMessage = async (
msg: IBackgroundCallback & { action?: string },
sender: chrome.runtime.MessageSender,
): Promise<any> => {
if (sender.tab === undefined) {
throw new Error('required "sender.tab" information is not available');
}

const { redirect, match } = this.match(request);

if (redirect !== undefined) {
return { redirectUrl: redirect.dataUrl };
} else if (match === true) {
return { cancel: true };
if (sender.tab.id === undefined) {
throw new Error('required "sender.tab.id" information is not available');
}

return {};
};

/**
*
*/
private onHeadersReceived = (
details: WebRequestHeadersReceivedDetails,
): chrome.webRequest.BlockingResponse => {
return updateResponseHeadersWithCSP(
details,
this.getCSPDirectives(fromWebRequestDetails(details)),
);
};

private onRuntimeMessage = (
msg: IBackgroundCallback & { action?: string },
sender: chrome.runtime.MessageSender,
sendResponse: (response?: any) => void,
): void => {
if (sender.tab === undefined || sender.tab.id === undefined || sender.frameId === undefined) {
return;
if (sender.frameId === undefined) {
throw new Error('required "sender.frameId" information is not available');
}

// Make sure we only listen to messages coming from our content-script
Expand Down Expand Up @@ -306,7 +278,7 @@ export class WebExtensionBlocker extends FiltersEngine {
return;
}

this.injectStylesWebExtension(styles, { tabId: sender.tab.id, allFrames: true });
await this.injectStylesWebExtension(styles, { tabId: sender.tab.id, allFrames: true });
}

// Separately, requests cosmetics which depend on the page it self
Expand Down Expand Up @@ -337,7 +309,7 @@ export class WebExtensionBlocker extends FiltersEngine {
return;
}

this.injectStylesWebExtension(styles, { tabId: sender.tab.id, frameId });
await this.injectStylesWebExtension(styles, { tabId: sender.tab.id, frameId });

// Inject scripts from content script
const responseFromBackground: IMessageFromBackground = {
Expand All @@ -346,12 +318,56 @@ export class WebExtensionBlocker extends FiltersEngine {
scripts,
styles: '',
};
sendResponse(responseFromBackground);
return responseFromBackground;
}
}
};

private injectStylesWebExtension(
/**
* Deal with request cancellation (`{ cancel: true }`) and redirection (`{ redirectUrl: '...' }`).
*/
private onBeforeRequest = (
details: WebRequestBeforeRequestDetails,
): chrome.webRequest.BlockingResponse => {
const request = fromWebRequestDetails(details);
if (request.isMainFrame()) {
this.performHTMLFiltering(request);
return {};
}

const { redirect, match } = this.match(request);

if (redirect !== undefined) {
return { redirectUrl: redirect.dataUrl };
} else if (match === true) {
return { cancel: true };
}

return {};
};

private onHeadersReceived = (
details: WebRequestHeadersReceivedDetails,
): chrome.webRequest.BlockingResponse => {
return updateResponseHeadersWithCSP(
details,
this.getCSPDirectives(fromWebRequestDetails(details)),
);
};

private onRuntimeMessage = (
msg: IBackgroundCallback & { action?: string },
sender: chrome.runtime.MessageSender,
sendResponse: (response?: any) => void,
): void => {
this.handleRuntimeMessage(msg, sender)
.then(sendResponse)
.catch((ex) => {
console.error('Error while handling runtime message:', ex);
});
};

private async injectStylesWebExtension(
styles: string,
{
tabId,
Expand All @@ -362,13 +378,24 @@ export class WebExtensionBlocker extends FiltersEngine {
frameId?: number;
allFrames?: boolean;
},
): void {
if (
styles.length > 0 &&
typeof chrome !== 'undefined' &&
chrome.tabs &&
chrome.tabs.insertCSS
) {
): Promise<void> {
// Abort if stylesheet is empty.
if (styles.length === 0) {
return;
}

// Abort if `chrome` global is not accessible.
if (typeof chrome === 'undefined') {
throw new Error('required "chrome" global object is not accessible');
}

// Abort if `chrome.tabs.insertCSS` is not available.
if (chrome.tabs === undefined || chrome.tabs.insertCSS === undefined) {
throw new Error('required "chrome.tabs.insertCSS" is not available');
}

// Proceed with stylesheet injection.
return new Promise((resolve, reject) => {
chrome.tabs.insertCSS(
tabId,
{
Expand All @@ -381,11 +408,13 @@ export class WebExtensionBlocker extends FiltersEngine {
},
() => {
if (chrome.runtime.lastError) {
console.error('Error while injecting CSS', chrome.runtime.lastError.message);
reject(chrome.runtime.lastError.message);
} else {
resolve();
}
},
);
}
});
}
}

Expand Down

0 comments on commit 8bb8dc4

Please sign in to comment.