From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samuel Attard <samuel.r.attard@gmail.com>
Date: Fri, 6 Aug 2021 03:36:57 -0700
Subject: chore: do not use chrome.windows in cryptotoken webrequestsender

Electron does not support chrome.windows which this method depends on.  It also does not need to, the method's goal is to determine if the webContents that triggered the cryptotoken request is still "active".  In Chrome this means active tab in the tab strip === this tab + the window owning that tab strip is focused.

In Electron that can be simplified to webContents.isFocused() which maps to "is the webContents view focused and is the owning window the key window".  We map tab.active to that IsFocused() value and we can remove the chrome.windows logic here.

This can't be upstreamed but the patch is minimal.

diff --git a/chrome/browser/resources/cryptotoken/webrequestsender.js b/chrome/browser/resources/cryptotoken/webrequestsender.js
index 8ea2d1c6d7269d25cd34a61d971d35c81b561670..8609defbb5de5c11614858586a9100974aed067c 100644
--- a/chrome/browser/resources/cryptotoken/webrequestsender.js
+++ b/chrome/browser/resources/cryptotoken/webrequestsender.js
@@ -142,10 +142,11 @@ function tabInForeground(tabId) {
       reject();
       return;
     }
-    if (!chrome.windows || !chrome.windows.get) {
-      reject();
-      return;
-    }
+    // Electron does not support chrome.windows
+    // if (!chrome.windows || !chrome.windows.get) {
+    //   reject();
+    //   return;
+    // }
     chrome.tabs.get(tabId, function(tab) {
       if (chrome.runtime.lastError) {
         resolve(false);
@@ -155,9 +156,13 @@ function tabInForeground(tabId) {
         resolve(false);
         return;
       }
-      chrome.windows.get(tab.windowId, function(aWindow) {
-        resolve(aWindow && aWindow.focused);
-      });
+      // tab.active in Electron maps to the "focused" state of the view
+      // which is only true when both the webContents and the window are
+      // focused.
+      resolve(true);
+      // chrome.windows.get(tab.windowId, function(aWindow) {
+      //   resolve(aWindow && aWindow.focused);
+      // });
     });
   });
 }
