Merge pull request #11225 from electron/get-current-browser-view

feature: BrowserView.getAllViews() & BrowserView.fromWebContents()
This commit is contained in:
Charles Kerr 2017-11-23 09:49:11 +01:00 committed by GitHub
commit 3c607394d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 0 deletions

View file

@ -154,6 +154,8 @@ void Initialize(v8::Local<v8::Object> exports,
isolate, BrowserView::GetConstructor(isolate)->GetFunction());
browser_view.SetMethod("fromId",
&mate::TrackableObject<BrowserView>::FromWeakMapID);
browser_view.SetMethod("getAllViews",
&mate::TrackableObject<BrowserView>::GetAll);
mate::Dictionary dict(isolate, exports);
dict.Set("BrowserView", browser_view);
}

View file

@ -40,6 +40,17 @@ view.webContents.loadURL('https://electronjs.org')
### Static Methods
#### `BrowserView.getAllViews()`
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

View file

@ -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 view
}
return null
}
module.exports = BrowserView

View file

@ -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)
})
})
})