From 3cb062b3d60b7dff9ae3e78eefd2f0b743286577 Mon Sep 17 00:00:00 2001 From: Felix Rieseberg Date: Wed, 22 Nov 2017 16:58:32 -0800 Subject: [PATCH 1/6] :wrench: BrowserView.getAllViews() --- atom/browser/api/atom_api_browser_view.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/atom/browser/api/atom_api_browser_view.cc b/atom/browser/api/atom_api_browser_view.cc index 57fb06fcd2a3..454ec3d5371d 100644 --- a/atom/browser/api/atom_api_browser_view.cc +++ b/atom/browser/api/atom_api_browser_view.cc @@ -154,6 +154,8 @@ void Initialize(v8::Local exports, isolate, BrowserView::GetConstructor(isolate)->GetFunction()); browser_view.SetMethod("fromId", &mate::TrackableObject::FromWeakMapID); + browser_view.SetMethod("getAllViews", + &mate::TrackableObject::GetAll); mate::Dictionary dict(isolate, exports); dict.Set("BrowserView", browser_view); } From 4d18cbe728b8973388075cb7f982eb6a476ed4d2 Mon Sep 17 00:00:00 2001 From: Felix Rieseberg Date: Wed, 22 Nov 2017 16:59:00 -0800 Subject: [PATCH 2/6] :wrench: BrowserView.fromWebContents() --- lib/browser/api/browser-view.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/browser/api/browser-view.js b/lib/browser/api/browser-view.js index 60023fef92bb..fe693d27c0d0 100644 --- a/lib/browser/api/browser-view.js +++ b/lib/browser/api/browser-view.js @@ -5,4 +5,12 @@ const {BrowserView} = process.atomBinding('browser_view') Object.setPrototypeOf(BrowserView.prototype, EventEmitter.prototype) +BrowserView.fromWebContents = (webContents) => { + for (const view of BrowserView.getAllViews()) { + if (view.webContents.equal(webContents)) return window + } + + return null +} + module.exports = BrowserView From 69e461083a10c0a08b7d258ca890ea55381d69cf Mon Sep 17 00:00:00 2001 From: Felix Rieseberg Date: Wed, 22 Nov 2017 17:00:09 -0800 Subject: [PATCH 3/6] :memo: Document `BrowserView.getAllViews()` --- docs/api/browser-view.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/api/browser-view.md b/docs/api/browser-view.md index 98d5c746add5..125789edc8a9 100644 --- a/docs/api/browser-view.md +++ b/docs/api/browser-view.md @@ -40,6 +40,10 @@ view.webContents.loadURL('https://electronjs.org') ### Static Methods +#### `BrowserView.getAllViews()` + +Returns `BrowserView[]` - An array of all opened BrowserViews. + #### `BrowserView.fromId(id)` * `id` Integer From 6298748d0b89dddc8800f54041792ebfc50aa01c Mon Sep 17 00:00:00 2001 From: Felix Rieseberg Date: Wed, 22 Nov 2017 17:02:01 -0800 Subject: [PATCH 4/6] :memo: Document `BrowserView.fromWebContents()` --- docs/api/browser-view.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/api/browser-view.md b/docs/api/browser-view.md index 125789edc8a9..6ed06d4be4a8 100644 --- a/docs/api/browser-view.md +++ b/docs/api/browser-view.md @@ -44,6 +44,13 @@ view.webContents.loadURL('https://electronjs.org') Returns `BrowserView[]` - An array of all opened BrowserViews. +#### `BrowserView.fromWebContents(webContents)` + +* `webContents` [WebContents](web-contents.md) + +Returns `BrowserView | null` - The BrowserView that owns the given `webContents` +or `null` if the contents are not owned by a BrowserView. + #### `BrowserView.fromId(id)` * `id` Integer From 690e8cf75b441b1ea3cbea598484628d6a973557 Mon Sep 17 00:00:00 2001 From: Felix Rieseberg Date: Wed, 22 Nov 2017 17:06:14 -0800 Subject: [PATCH 5/6] :construction_worker: Add specs --- spec/api-browser-view-spec.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spec/api-browser-view-spec.js b/spec/api-browser-view-spec.js index 6debec2a7682..ab96021ebf4c 100644 --- a/spec/api-browser-view-spec.js +++ b/spec/api-browser-view-spec.js @@ -125,4 +125,26 @@ describe('BrowserView module', () => { assert.equal(view2.webContents.id, view.webContents.id) }) }) + + describe('BrowserView.fromWebContents()', () => { + it('returns the view with given id', () => { + view = new BrowserView() + w.setBrowserView(view) + assert.notEqual(view.id, null) + let view2 = BrowserView.fromWebContents(view.webContents) + assert.equal(view2.webContents.id, view.webContents.id) + }) + }) + + describe('BrowserView.getAllViews()', () => { + it('returns all views', () => { + view = new BrowserView() + w.setBrowserView(view) + assert.notEqual(view.id, null) + + const views = BrowserView.getAllViews() + assert.equal(views.length, 1) + assert.equal(views[0].webContents.id, view.webContents.id) + }) + }) }) From 037f76c47b118fc638ed1def9aaed8090cdbd433 Mon Sep 17 00:00:00 2001 From: Felix Rieseberg Date: Wed, 22 Nov 2017 17:07:57 -0800 Subject: [PATCH 6/6] :bug: Ooops, fix that --- lib/browser/api/browser-view.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/browser/api/browser-view.js b/lib/browser/api/browser-view.js index fe693d27c0d0..9e7c81de5cbc 100644 --- a/lib/browser/api/browser-view.js +++ b/lib/browser/api/browser-view.js @@ -7,7 +7,7 @@ Object.setPrototypeOf(BrowserView.prototype, EventEmitter.prototype) BrowserView.fromWebContents = (webContents) => { for (const view of BrowserView.getAllViews()) { - if (view.webContents.equal(webContents)) return window + if (view.webContents.equal(webContents)) return view } return null