New issue
Advanced search Search tips

Issue 806283 link

Starred by 2 users

Issue metadata

Status: Duplicate
Merged: issue 328932
Owner: ----
Closed: Feb 2018
Components:
EstimatedDays: ----
NextAction: ----
OS: Linux , Windows , Chrome , Mac
Pri: 2
Type: Feature



Sign in to add a comment

Add support for returning a Promise from webRequest.onBeforeSendHeaders listener

Reported by imp...@gmail.com, Jan 26 2018

Issue description

UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

Steps to reproduce the problem:
1. Create an extension with this code in the background script

function promiseReturningFunction() {
    return new Promise((resolved) => {
        setTimeout(() => {
            resolved('Boum');
        }, 300);
    });
}

function createListener() {
    return async function (request) {
        const result = await promiseReturningFunction();
        const headers = [{ name: 'X-SPECIAL-HEADER', value: result }];

        return { requestHeaders: request.requestHeaders.concat(headers) };
    };
}

chrome.webRequest.onBeforeSendHeaders.addListener(createListener(), { urls: ['<all_urls>'] }, ['requestHeaders', 'blocking']);

What is the expected behavior?
All requests should now have a HTTP header

X-SPECIAL-HEADER: Boum

What went wrong?
The header is not present.
If I change the code to 

function createListener() {
    return function (request) {
        const result = 'Boum';

it works as expected.

Did this work before? N/A 

Does this work in other browsers? Yes

Chrome version: 63.0.3239.132  Channel: stable
OS Version: OS X 10.12.6
Flash Version: 

It seems related to async functions.
FYI it works on latest  Firefox
 
Labels: -Hotlist-Interop OS-Chrome OS-Linux OS-Windows
(I assume this happens across platforms)
Sounds like a WontFix to me, as asynchronous functions return a promise, which is not a valid value to return for your expected outcome.
Unless this is a feature request.
Components: Platform>Extensions>API

Comment 3 by imp...@gmail.com, Jan 29 2018

Hello,

Thanks for your quick response and triage !

Actually, since the part of the spec allowing to return a Promise resolving to a BlockingResponse does not seem to be implemented as well (see 4th paragraph here https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/webRequest/onBeforeSendHeaders), I don't see how to implement my use case in a Chromium extension

Indeed, this piece of code does not work as expected whereas it works as expected in Firefox :

function promiseReturningFunction() {
    return new Promise((resolved) => {
        setTimeout(() => {
            resolved('Boum Paf Paf');
        }, 300);
    });
}

function createListener() {
    return function (request) {
        return promiseReturningFunction().then((result) => {
            const headers = [{ name: 'X-SPECIAL-HEADER', value: result }];

            return { requestHeaders: request.requestHeaders.concat(headers) };
        });
    };
}

chrome.webRequest.onBeforeSendHeaders.addListener(createListener(), { urls: ['<all_urls>'] }, ['requestHeaders', 'blocking']);


If you think I forgot something, misuse or did something wrong, let me know. If you have any pointer, comments or advice, I'd love to hear them.

Thanks again,

Comment 4 by imp...@gmail.com, Jan 29 2018

(FYI I've updated the async/await implementation of my opening ticket description in favor of a Promise resolving to a BlockingResponse implementation in my latest comment)

Comment 5 by phistuck@gmail.com, Jan 29 2018

Looking at MDN for Chrome extensions can be misleading, because Firefox implements the Chrome extension API with some deviations and missing features. I think you caught one of the deviations here.

https://developer.chrome.com/extensions/webRequest#event-onBeforeSendHeaders does not mention any support for promises. Sorry.

Comment 6 by imp...@gmail.com, Jan 29 2018

Then it is a feature request 
Labels: -Type-Bug Type-Feature
Status: Untriaged (was: Unconfirmed)
Summary: Add support for returning a Promise from webRequest.onBeforeSendHeaders listener (was: Return value of async function as webRequest.onBeforeSendHeaders listener is not correctly handled)
I doubt no one has filed such a request already, but here you go.
Mergedinto: 328932
Status: Duplicate (was: Untriaged)
Currently AFAIK all extension APIs use callbacks. Merging with crbug.com/328932.

Sign in to add a comment