webview: api to get webcontents and associated host webcontents
This commit is contained in:
parent
537ead8917
commit
6fcc197db8
7 changed files with 46 additions and 5 deletions
|
@ -218,7 +218,8 @@ WebContents::WebContents(content::WebContents* web_contents)
|
||||||
|
|
||||||
WebContents::WebContents(v8::Isolate* isolate,
|
WebContents::WebContents(v8::Isolate* isolate,
|
||||||
const mate::Dictionary& options)
|
const mate::Dictionary& options)
|
||||||
: request_id_(0) {
|
: embedder_(nullptr),
|
||||||
|
request_id_(0) {
|
||||||
// Whether it is a guest WebContents.
|
// Whether it is a guest WebContents.
|
||||||
bool is_guest = false;
|
bool is_guest = false;
|
||||||
options.Get("isGuest", &is_guest);
|
options.Get("isGuest", &is_guest);
|
||||||
|
@ -273,10 +274,10 @@ WebContents::WebContents(v8::Isolate* isolate,
|
||||||
guest_delegate_->Initialize(this);
|
guest_delegate_->Initialize(this);
|
||||||
|
|
||||||
NativeWindow* owner_window = nullptr;
|
NativeWindow* owner_window = nullptr;
|
||||||
WebContents* embedder = nullptr;
|
if (options.Get("embedder", &embedder_) && embedder_) {
|
||||||
if (options.Get("embedder", &embedder) && embedder) {
|
|
||||||
// New WebContents's owner_window is the embedder's owner_window.
|
// New WebContents's owner_window is the embedder's owner_window.
|
||||||
auto relay = NativeWindowRelay::FromWebContents(embedder->web_contents());
|
auto relay =
|
||||||
|
NativeWindowRelay::FromWebContents(embedder_->web_contents());
|
||||||
if (relay)
|
if (relay)
|
||||||
owner_window = relay->window.get();
|
owner_window = relay->window.get();
|
||||||
}
|
}
|
||||||
|
@ -1116,6 +1117,12 @@ v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
|
||||||
return v8::Local<v8::Value>::New(isolate, session_);
|
return v8::Local<v8::Value>::New(isolate, session_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
content::WebContents* WebContents::HostWebContents() {
|
||||||
|
if (!embedder_)
|
||||||
|
return nullptr;
|
||||||
|
return embedder_->web_contents();
|
||||||
|
}
|
||||||
|
|
||||||
v8::Local<v8::Value> WebContents::DevToolsWebContents(v8::Isolate* isolate) {
|
v8::Local<v8::Value> WebContents::DevToolsWebContents(v8::Isolate* isolate) {
|
||||||
if (devtools_web_contents_.IsEmpty())
|
if (devtools_web_contents_.IsEmpty())
|
||||||
return v8::Null(isolate);
|
return v8::Null(isolate);
|
||||||
|
@ -1199,6 +1206,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
|
||||||
.SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
|
.SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
|
||||||
.SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
|
.SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
|
||||||
.SetProperty("session", &WebContents::Session)
|
.SetProperty("session", &WebContents::Session)
|
||||||
|
.SetProperty("hostWebContents", &WebContents::HostWebContents)
|
||||||
.SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents)
|
.SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents)
|
||||||
.SetProperty("debugger", &WebContents::Debugger);
|
.SetProperty("debugger", &WebContents::Debugger);
|
||||||
}
|
}
|
||||||
|
|
|
@ -147,6 +147,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
|
|
||||||
// Properties.
|
// Properties.
|
||||||
v8::Local<v8::Value> Session(v8::Isolate* isolate);
|
v8::Local<v8::Value> Session(v8::Isolate* isolate);
|
||||||
|
content::WebContents* HostWebContents();
|
||||||
v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
|
v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
|
||||||
v8::Local<v8::Value> Debugger(v8::Isolate* isolate);
|
v8::Local<v8::Value> Debugger(v8::Isolate* isolate);
|
||||||
|
|
||||||
|
@ -287,6 +288,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
|
|
||||||
scoped_ptr<WebViewGuestDelegate> guest_delegate_;
|
scoped_ptr<WebViewGuestDelegate> guest_delegate_;
|
||||||
|
|
||||||
|
// The host webcontents that may contain this webcontents.
|
||||||
|
WebContents* embedder_;
|
||||||
|
|
||||||
// The type of current WebContents.
|
// The type of current WebContents.
|
||||||
Type type_;
|
Type type_;
|
||||||
|
|
||||||
|
|
|
@ -173,6 +173,8 @@ bool Converter<content::StopFindAction>::FromV8(
|
||||||
// static
|
// static
|
||||||
v8::Local<v8::Value> Converter<content::WebContents*>::ToV8(
|
v8::Local<v8::Value> Converter<content::WebContents*>::ToV8(
|
||||||
v8::Isolate* isolate, content::WebContents* val) {
|
v8::Isolate* isolate, content::WebContents* val) {
|
||||||
|
if (!val)
|
||||||
|
return v8::Null(isolate);
|
||||||
return atom::api::WebContents::CreateFrom(isolate, val).ToV8();
|
return atom::api::WebContents::CreateFrom(isolate, val).ToV8();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -388,7 +388,7 @@ var registerWebViewElement = function() {
|
||||||
'downloadURL',
|
'downloadURL',
|
||||||
'inspectServiceWorker',
|
'inspectServiceWorker',
|
||||||
'print',
|
'print',
|
||||||
'printToPDF'
|
'printToPDF',
|
||||||
];
|
];
|
||||||
nonblockMethods = [
|
nonblockMethods = [
|
||||||
'executeJavaScript',
|
'executeJavaScript',
|
||||||
|
@ -430,6 +430,12 @@ var registerWebViewElement = function() {
|
||||||
proto[m] = createNonBlockHandler(m);
|
proto[m] = createNonBlockHandler(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WebContents associated with this webview.
|
||||||
|
proto.getWebContents = function() {
|
||||||
|
var internal = v8Util.getHiddenValue(this, 'internal');
|
||||||
|
return internal.webContents;
|
||||||
|
};
|
||||||
|
|
||||||
// Deprecated.
|
// Deprecated.
|
||||||
deprecate.rename(proto, 'getUrl', 'getURL');
|
deprecate.rename(proto, 'getUrl', 'getURL');
|
||||||
window.WebView = webFrame.registerEmbedderCustomElement('webview', {
|
window.WebView = webFrame.registerEmbedderCustomElement('webview', {
|
||||||
|
|
|
@ -842,6 +842,10 @@ win.webContents.on('did-finish-load', function() {
|
||||||
|
|
||||||
Returns the [session](session.md) object used by this webContents.
|
Returns the [session](session.md) object used by this webContents.
|
||||||
|
|
||||||
|
### `webContents.hostWebContents`
|
||||||
|
|
||||||
|
Returns the `WebContents` that might own this `WebContents`.
|
||||||
|
|
||||||
### `webContents.devToolsWebContents`
|
### `webContents.devToolsWebContents`
|
||||||
|
|
||||||
Get the `WebContents` of DevTools for this `WebContents`.
|
Get the `WebContents` of DevTools for this `WebContents`.
|
||||||
|
|
|
@ -438,6 +438,10 @@ Sends an input `event` to the page.
|
||||||
See [webContents.sendInputEvent](web-contents.md##webcontentssendinputeventevent)
|
See [webContents.sendInputEvent](web-contents.md##webcontentssendinputeventevent)
|
||||||
for detailed description of `event` object.
|
for detailed description of `event` object.
|
||||||
|
|
||||||
|
### `<webview>.getWebContents()`
|
||||||
|
|
||||||
|
Returns the [WebContents](web-contents.md) associated with this `webview`.
|
||||||
|
|
||||||
## DOM events
|
## DOM events
|
||||||
|
|
||||||
The following DOM events are available to the `webview` tag:
|
The following DOM events are available to the `webview` tag:
|
||||||
|
|
|
@ -703,4 +703,17 @@ describe('<webview> tag', function() {
|
||||||
document.body.appendChild(webview);
|
document.body.appendChild(webview);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('<webview>.getWebContents', function() {
|
||||||
|
it('can return the webcontents associated', function(done) {
|
||||||
|
webview.addEventListener('did-finish-load', function() {
|
||||||
|
const webviewContents = webview.getWebContents();
|
||||||
|
assert(webviewContents);
|
||||||
|
assert.equal(webviewContents.getURL(), 'about:blank');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
webview.src = "about:blank";
|
||||||
|
document.body.appendChild(webview);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue