Merge pull request #4518 from deepak1556/webview_host_window_patch

webview: api to get host browser window
This commit is contained in:
Cheng Zhao 2016-02-25 12:26:53 +08:00
commit d00490271b
7 changed files with 46 additions and 5 deletions

View file

@ -218,7 +218,8 @@ WebContents::WebContents(content::WebContents* web_contents)
WebContents::WebContents(v8::Isolate* isolate,
const mate::Dictionary& options)
: request_id_(0) {
: embedder_(nullptr),
request_id_(0) {
// Whether it is a guest WebContents.
bool is_guest = false;
options.Get("isGuest", &is_guest);
@ -273,10 +274,10 @@ WebContents::WebContents(v8::Isolate* isolate,
guest_delegate_->Initialize(this);
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.
auto relay = NativeWindowRelay::FromWebContents(embedder->web_contents());
auto relay =
NativeWindowRelay::FromWebContents(embedder_->web_contents());
if (relay)
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_);
}
content::WebContents* WebContents::HostWebContents() {
if (!embedder_)
return nullptr;
return embedder_->web_contents();
}
v8::Local<v8::Value> WebContents::DevToolsWebContents(v8::Isolate* isolate) {
if (devtools_web_contents_.IsEmpty())
return v8::Null(isolate);
@ -1199,6 +1206,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("addWorkSpace", &WebContents::AddWorkSpace)
.SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace)
.SetProperty("session", &WebContents::Session)
.SetProperty("hostWebContents", &WebContents::HostWebContents)
.SetProperty("devToolsWebContents", &WebContents::DevToolsWebContents)
.SetProperty("debugger", &WebContents::Debugger);
}

View file

@ -147,6 +147,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
// Properties.
v8::Local<v8::Value> Session(v8::Isolate* isolate);
content::WebContents* HostWebContents();
v8::Local<v8::Value> DevToolsWebContents(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_;
// The host webcontents that may contain this webcontents.
WebContents* embedder_;
// The type of current WebContents.
Type type_;

View file

@ -173,6 +173,8 @@ bool Converter<content::StopFindAction>::FromV8(
// static
v8::Local<v8::Value> Converter<content::WebContents*>::ToV8(
v8::Isolate* isolate, content::WebContents* val) {
if (!val)
return v8::Null(isolate);
return atom::api::WebContents::CreateFrom(isolate, val).ToV8();
}

View file

@ -388,7 +388,7 @@ var registerWebViewElement = function() {
'downloadURL',
'inspectServiceWorker',
'print',
'printToPDF'
'printToPDF',
];
nonblockMethods = [
'executeJavaScript',
@ -430,6 +430,12 @@ var registerWebViewElement = function() {
proto[m] = createNonBlockHandler(m);
}
// WebContents associated with this webview.
proto.getWebContents = function() {
var internal = v8Util.getHiddenValue(this, 'internal');
return internal.webContents;
};
// Deprecated.
deprecate.rename(proto, 'getUrl', 'getURL');
window.WebView = webFrame.registerEmbedderCustomElement('webview', {

View file

@ -842,6 +842,10 @@ win.webContents.on('did-finish-load', function() {
Returns the [session](session.md) object used by this webContents.
### `webContents.hostWebContents`
Returns the `WebContents` that might own this `WebContents`.
### `webContents.devToolsWebContents`
Get the `WebContents` of DevTools for this `WebContents`.

View file

@ -438,6 +438,10 @@ Sends an input `event` to the page.
See [webContents.sendInputEvent](web-contents.md##webcontentssendinputeventevent)
for detailed description of `event` object.
### `<webview>.getWebContents()`
Returns the [WebContents](web-contents.md) associated with this `webview`.
## DOM events
The following DOM events are available to the `webview` tag:

View file

@ -703,4 +703,17 @@ describe('<webview> tag', function() {
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);
});
});
});