Repository: electron/electron (language: cpp)

Issue:
BrowserWindow.fromWebContents returns undefined for windows created using window.open
### Preflight Checklist
* [x] I have read the [Contributing Guidelines](https://github.com/electron/electron/blob/master/CONTRIBUTING.md) for this project.
* [x] I agree to follow the [Code of Conduct](https://github.com/electron/electron/blob/master/CODE_OF_CONDUCT.md) that this project adheres to.
* [x] I have searched the issue tracker for an issue that matches the one I want to file, without success.

### Issue Details

* **Electron Version:** 5.0.7
* **Operating System:** Windows 10

### Expected Behavior
I expect that calling `BrowserWindow.fromWebContents(win.webContents)` returns `win`.

### Actual Behavior
The API returns `undefined`.

### To Reproduce
```
const { app, BrowserWindow } = require('electron')

async function createWindow() {
  var mainWindow = new BrowserWindow({
    webPreferences: {
      nativeWindowOpen: true
    }
  });
  await mainWindow.loadURL("https://google.com");
  mainWindow.webContents.openDevTools();
  console.log(BrowserWindow.fromWebContents(mainWindow.webContents)); // correctly returns mainWindow

  app.on("browser-window-created", (e, window) => {
    console.log(BrowserWindow.fromWebContents(window.webContents)); // returns undefined
    window.once("will-move", (e) => {
      console.log(BrowserWindow.fromWebContents(e.sender)); // extra test; also returns undefined
    });
  });
}

app.on('ready', createWindow)
```

Run `npm start` and open a window from devtools (e.g. `window.open("https://google.com")`). Notice that `fromWebContents` can't find the browser window for the new window.


Discussion / hints:
I wonder if this will be fixed by [this PR](https://github.com/electron/electron/pull/21236)
Thank you for taking the time to report this issue and helping to make Electron better.

The version of Electron you reported this on has been superseded by newer releases. 

If you're still experiencing this issue in Electron 6.x.y or later, please add a comment specifying the version you're testing with and any other new information that a maintainer trying to reproduce the issue should know.

I'm setting the `blocked/need-info` label for the above reasons. This issue will be closed 7 days from now if there is no response.

Thanks in advance! Your help is appreciated.
This is still an issue in Electron 8.
Still a problem in Electron 10.1.2
Still repros with Electron 12.0.1.

Though the call to `BrowserWindow.fromWebContents(e.sender)` now fails with:

![image](https://user-images.githubusercontent.com/6374473/111076404-2eddbf80-84ba-11eb-8386-287acc58b0d3.png)

Here's a [bugbot](/electron/bugbot)-compatible gist: https://gist.github.com/decc0f7190ce719279b5f792711c7e6c
/bugbot test https://gist.github.com/decc0f7190ce719279b5f792711c7e6c

Fix patch:
diff --git a/shell/common/gin_helper/trackable_object.h b/shell/common/gin_helper/trackable_object.h
index 309e412e8a6f6..7f5b45c75fbcb 100644
--- a/shell/common/gin_helper/trackable_object.h
+++ b/shell/common/gin_helper/trackable_object.h
@@ -117,11 +117,11 @@ class TrackableObject : public TrackableObjectBase, public EventEmitter<T> {
   ~TrackableObject() override { RemoveFromWeakMap(); }
 
   void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) override {
-    gin_helper::WrappableBase::InitWith(isolate, wrapper);
     if (!weak_map_) {
       weak_map_ = new electron::KeyWeakMap<int32_t>;
     }
     weak_map_->Set(isolate, weak_map_id_, wrapper);
+    gin_helper::WrappableBase::InitWith(isolate, wrapper);
   }
 
  private:
diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts
index 1ed8734711f33..89ff468ec5202 100644
--- a/spec-main/api-browser-window-spec.ts
+++ b/spec-main/api-browser-window-spec.ts
@@ -1816,6 +1816,18 @@ describe('BrowserWindow module', () => {
       expect(BrowserWindow.fromWebContents(webviewContents)!.id).to.equal(w.id);
       await p;
     });
+
+    it('is usable immediately on browser-window-created', async () => {
+      const w = new BrowserWindow({ show: false });
+      w.loadURL('about:blank');
+      w.webContents.executeJavaScript('window.open(""); null');
+      const [win, winFromWebContents] = await new Promise((resolve) => {
+        app.once('browser-window-created', (e, win) => {
+          resolve([win, BrowserWindow.fromWebContents(win.webContents)]);
+        });
+      });
+      expect(winFromWebContents).to.equal(win);
+    });
   });
 
   describe('BrowserWindow.openDevTools()', () => {


Test patch: