Closed Bug 1817872 Opened 2 years ago Closed 2 years ago

browser.tabs.onCreated should wait for tab to fully load before notifying extensions (supernova)

Categories

(Thunderbird :: Add-Ons: Extensions API, enhancement)

enhancement

Tracking

(thunderbird_esr102 wontfix)

RESOLVED FIXED
114 Branch
Tracking Status
thunderbird_esr102 --- wontfix

People

(Reporter: standard8, Assigned: TbSync)

References

Details

(Whiteboard: [Supernova3p])

Attachments

(1 file)

In a WebExtension, try and doing something like:

browser.tabs.onCreated.addListener(async (tab) => {
 await browser.messageDisplay.getDisplayedMessage(tab.id)
});

This fails with:

can't access property "getSelectedMsgHdrs", nativeTab.chromeBrowser.contentWindow.gDBView is undefined in ext-messageDisplay.js

This is because the new code for tabs is being loaded in a browser that may not have loaded yet. The onCreated listener is getting fired too early. Something like this seems to be enough when I tried in a WebExtension experiment API:

  return new Promise((resolve) => {
    if (win.document.readyState == "complete") {
      resolve();
    } else {
      win.addEventListener(
        "load",
        () => {
          resolve();
        },
        { once: true }
      );
    }
  });
Assignee: nobody → john

I personally have never understood, why the specs for onCreated did not wait for load. Nobody wants to do something with a tab before it has fully loaded. If this worked for you for message tabs prior to Supernova, I assume this was luck :-). Also content tabs are returned before the content has loaded.

I think I should not change the default behaviour, but I can add a parameter to tabs.onCreated.addListerner() which will ensure you get the tab only after it has fully loaded.

Edit: No, I will guard the messageDisplay API to wait until the tab is fully loaded, like we do it for the compose API already (partially)

I removed the regression keyword and marked this as an enhancement.

Type: defect → enhancement
Keywords: regression

I tested this code:

browser.tabs.onCreated.addListener(async (tab) => {
  console.log(tab.status, tab.type);
  if (tab.type == "messageDisplay") {
    console.log("MSG");
    let msg = await browser.messageDisplay.getDisplayedMessage(tab.id)
    console.log(msg);
  }
});

I get a "completed" status, but no msg. So that is inconsistent and wrong.

Adding [Supernova] to Whiteboard. 20230406_1102

Whiteboard: [Supernova]

What I did in the patch is fixing the wrong reporting of the tab being completely loaded.

I did not change the behaviour of tabs.onCreated to report tabs before they are loaded. I added a test to verify the fixed behaviour, which one could use in add-ons as well (still have to wait for the try-run):

      let messageTab = await new Promise(resolve => {
        let createListener = tab => {
          browser.tabs.onCreated.removeListener(createListener);
          if (tab.status == "complete") {
            resolve(tab);
          } else {
            browser.tabs.onUpdated.addListener(updateListener, {
              tabId: tab.id,
            });
          }
        };
        let updateListener = (tabId, changeInfo, tab) => {
          if (changeInfo?.status == "complete") {
            browser.tabs.onUpdated.removeListener(updateListener);
            resolve(tab);
          }
        };
        browser.tabs.onCreated.addListener(createListener);
        browser.messageDisplay.open({
          location: "tab",
          messageId: <some message id>,
        });
      });
      // We should now be able to get the message.
      let message = await browser.messageDisplay.getDisplayedMessage(
        messageTab.id
      );

Note: If you use the messageDisplay.onMessageDisplayed event, you get the tab and message directly.
https://webextension-api.thunderbird.net/en/stable/messageDisplay.html#onmessagedisplayed

Attachment #9328213 - Attachment description: Bug 1817872 - Make sure messageDisplay tabs have finish loading in onCreated. r=standard8,darktrojan → Bug 1817872 - Return the correct load status for message tabs. r=standard8,darktrojan

In a second patch, I will add a guard for messageDisplay.getDisplayedMessage() to wait until the tab is fully loaded, before returning the message. We do something similar for compose.getComposeDetails().

[Here was listed a no longer valid try run, which has now been removed.]

Depends on: 1828054
Blocks: 1828056
No longer blocks: 1828056
No longer depends on: 1828054
See Also: → 1828054, 1828056
Whiteboard: [Supernova] → [Supernova3p]
Status: NEW → ASSIGNED
Target Milestone: --- → 114 Branch

Pushed by mkmelin@iki.fi:
https://hg.mozilla.org/comm-central/rev/2886757a85a9
Return the correct load status for message tabs. r=darktrojan

Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
Pushed by mkmelin@iki.fi: https://hg.mozilla.org/comm-central/rev/e19f3cea28fb follow-up - add files accidentially left out when unbitrotting. rs=bustage-fix

Something went wrong here. The patch does not include

mail/components/extensions/test/browser/browser_ext_tabs_onCreated_bug1817872.js

but its in the try:
https://hg.mozilla.org/try-comm-central/diff/559f0851347f07d185f8b1fbc0137e42f29e5335/mail/components/extensions/test/browser/browser_ext_tabs_onCreated_bug1817872.js

But file

mail/components/extensions/test/browser/browser_ext_tabs_move.js

is probably wrong. Will check. Sorry. Sorry. Sorry.

Everything seems to look good. But I have no idea what happened. The history shows, that the file was removed from the patch, but I do not know how and why:
https://phabricator.services.mozilla.com/D175276?vs=710014&id=710489#toc

I probably should have rebased before requesting checkin, is that it?

Two of the patches landing needed fixing due to a conflict in browser.ini. Since such a conflict happened I had to fix+add back commit message. I had forgot to hg add the added files. Other than that, yes something must have been included in the wrong patch or patches, but I didn't go figuring out exactly what since the end result seemed to work.

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: