diff --git a/atom/browser/api/atom_api_browser_window.cc b/atom/browser/api/atom_api_browser_window.cc index db98e4bcf401..ce85e52d4370 100644 --- a/atom/browser/api/atom_api_browser_window.cc +++ b/atom/browser/api/atom_api_browser_window.cc @@ -4,7 +4,6 @@ #include "atom/browser/api/atom_api_browser_window.h" -#include "atom/browser/api/atom_api_web_contents_view.h" #include "atom/browser/browser.h" #include "atom/browser/unresponsive_suppressor.h" #include "atom/browser/web_contents_preferences.h" @@ -68,11 +67,6 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate, api_web_contents_->AddObserver(this); Observe(api_web_contents_->web_contents()); - // Create a WebContentsView for the WebContents. - auto* web_contents_view = static_cast( - WebContentsView::New(isolate, web_contents)); - web_contents_view_.Reset(isolate, web_contents_view->GetWrapper()); - // Keep a copy of the options for later use. mate::Dictionary(isolate, web_contents->GetWrapper()) .Set("browserWindowOptions", options); @@ -83,7 +77,6 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate, // Associate with BrowserWindow. web_contents->SetOwnerWindow(window()); - window()->SetContentView(web_contents_view->view()); auto* host = web_contents->web_contents()->GetRenderViewHost(); if (host) diff --git a/atom/browser/api/atom_api_browser_window.h b/atom/browser/api/atom_api_browser_window.h index 3af4402d19ae..5f9610f3a3c2 100644 --- a/atom/browser/api/atom_api_browser_window.h +++ b/atom/browser/api/atom_api_browser_window.h @@ -109,8 +109,6 @@ class BrowserWindow : public TopLevelWindow, v8::Global web_contents_; api::WebContents* api_web_contents_; - v8::Global web_contents_view_; - base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(BrowserWindow); diff --git a/atom/browser/api/atom_api_top_level_window.cc b/atom/browser/api/atom_api_top_level_window.cc index 99d24e0e4026..9a168504ff27 100644 --- a/atom/browser/api/atom_api_top_level_window.cc +++ b/atom/browser/api/atom_api_top_level_window.cc @@ -9,6 +9,7 @@ #include "atom/browser/api/atom_api_browser_view.h" #include "atom/browser/api/atom_api_menu.h" +#include "atom/browser/api/atom_api_view.h" #include "atom/browser/api/atom_api_web_contents.h" #include "atom/common/color_util.h" #include "atom/common/native_mate_converters/callback.h" @@ -267,6 +268,11 @@ void TopLevelWindow::OnWindowMessage(UINT message, } #endif +void TopLevelWindow::SetContentView(mate::Handle view) { + content_view_.Reset(isolate(), view.ToV8()); + window_->SetContentView(view->view()); +} + void TopLevelWindow::Close() { window_->Close(); } @@ -767,6 +773,13 @@ void TopLevelWindow::CloseFilePreview() { window_->CloseFilePreview(); } +v8::Local TopLevelWindow::GetContentView() const { + if (content_view_.IsEmpty()) + return v8::Null(isolate()); + else + return v8::Local::New(isolate(), content_view_); +} + v8::Local TopLevelWindow::GetParentWindow() const { if (parent_window_.IsEmpty()) return v8::Null(isolate()); @@ -915,6 +928,7 @@ void TopLevelWindow::BuildPrototype(v8::Isolate* isolate, prototype->SetClassName(mate::StringToV8(isolate, "TopLevelWindow")); mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) .MakeDestroyable() + .SetMethod("setContentView", &TopLevelWindow::SetContentView) .SetMethod("close", &TopLevelWindow::Close) .SetMethod("focus", &TopLevelWindow::Focus) .SetMethod("blur", &TopLevelWindow::Blur) @@ -1024,6 +1038,7 @@ void TopLevelWindow::BuildPrototype(v8::Isolate* isolate, .SetMethod("setAspectRatio", &TopLevelWindow::SetAspectRatio) .SetMethod("previewFile", &TopLevelWindow::PreviewFile) .SetMethod("closeFilePreview", &TopLevelWindow::CloseFilePreview) + .SetMethod("getContentView", &TopLevelWindow::GetContentView) .SetMethod("getParentWindow", &TopLevelWindow::GetParentWindow) .SetMethod("getChildWindows", &TopLevelWindow::GetChildWindows) .SetMethod("getBrowserView", &TopLevelWindow::GetBrowserView) diff --git a/atom/browser/api/atom_api_top_level_window.h b/atom/browser/api/atom_api_top_level_window.h index caba53c0e0f0..bf64ac7c616a 100644 --- a/atom/browser/api/atom_api_top_level_window.h +++ b/atom/browser/api/atom_api_top_level_window.h @@ -20,6 +20,8 @@ namespace atom { namespace api { +class View; + class TopLevelWindow : public mate::TrackableObject, public NativeWindowObserver { public: @@ -79,6 +81,7 @@ class TopLevelWindow : public mate::TrackableObject, #endif // Public APIs of NativeWindow. + void SetContentView(mate::Handle view); void Close(); virtual void Focus(); virtual void Blur(); @@ -179,6 +182,7 @@ class TopLevelWindow : public mate::TrackableObject, void CloseFilePreview(); // Public getters of NativeWindow. + v8::Local GetContentView() const; v8::Local GetParentWindow() const; std::vector> GetChildWindows() const; v8::Local GetBrowserView() const; @@ -215,6 +219,7 @@ class TopLevelWindow : public mate::TrackableObject, MessageCallbackMap messages_callback_map_; #endif + v8::Global content_view_; v8::Global browser_view_; v8::Global menu_; v8::Global parent_window_; @@ -231,7 +236,7 @@ class TopLevelWindow : public mate::TrackableObject, namespace mate { -template<> +template <> struct Converter { static bool FromV8(v8::Isolate* isolate, v8::Local val, diff --git a/atom/browser/api/atom_api_view.cc b/atom/browser/api/atom_api_view.cc index f986835ac30a..07db4bfca17a 100644 --- a/atom/browser/api/atom_api_view.cc +++ b/atom/browser/api/atom_api_view.cc @@ -25,7 +25,9 @@ View::~View() { // static mate::WrappableBase* View::New(mate::Arguments* args) { - return new View(); + auto* view = new View(); + view->InitWith(args->isolate(), args->GetThis()); + return view; } // static diff --git a/atom/browser/api/atom_api_view.h b/atom/browser/api/atom_api_view.h index 6be2423afc19..b1fb38e4c439 100644 --- a/atom/browser/api/atom_api_view.h +++ b/atom/browser/api/atom_api_view.h @@ -42,4 +42,21 @@ class View : public mate::EventEmitter { } // namespace atom +namespace mate { + +template <> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + views::View** out) { + atom::api::View* view; + if (!Converter::FromV8(isolate, val, &view)) + return false; + *out = view->view(); + return true; + } +}; + +} // namespace mate + #endif // ATOM_BROWSER_API_ATOM_API_VIEW_H_ diff --git a/atom/browser/api/atom_api_web_contents_view.cc b/atom/browser/api/atom_api_web_contents_view.cc index e8ed06b8aeca..5bfd94dfee94 100644 --- a/atom/browser/api/atom_api_web_contents_view.cc +++ b/atom/browser/api/atom_api_web_contents_view.cc @@ -42,16 +42,18 @@ WebContentsView::~WebContentsView() {} // static mate::WrappableBase* WebContentsView::New( - v8::Isolate* isolate, + mate::Arguments* args, mate::Handle web_contents) { if (!web_contents->managed_web_contents()) { const char* error = "The WebContents must be created by user"; - isolate->ThrowException( - v8::Exception::Error(mate::StringToV8(isolate, error))); + args->isolate()->ThrowException( + v8::Exception::Error(mate::StringToV8(args->isolate(), error))); return nullptr; } - return new WebContentsView(isolate, web_contents->GetWrapper(), - web_contents->managed_web_contents()); + auto* view = new WebContentsView(args->isolate(), web_contents->GetWrapper(), + web_contents->managed_web_contents()); + view->InitWith(args->isolate(), args->GetThis()); + return view; } // static diff --git a/atom/browser/api/atom_api_web_contents_view.h b/atom/browser/api/atom_api_web_contents_view.h index 0dae03db0b94..e3cf96f99085 100644 --- a/atom/browser/api/atom_api_web_contents_view.h +++ b/atom/browser/api/atom_api_web_contents_view.h @@ -20,7 +20,7 @@ class WebContents; class WebContentsView : public View { public: - static mate::WrappableBase* New(v8::Isolate* isolate, + static mate::WrappableBase* New(mate::Arguments* args, mate::Handle web_contents); static void BuildPrototype(v8::Isolate* isolate, diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index 924a99c4bebb..cb523496d65c 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -1,7 +1,7 @@ 'use strict' const electron = require('electron') -const {ipcMain, TopLevelWindow} = electron +const {ipcMain, WebContentsView, TopLevelWindow} = electron const {BrowserWindow} = process.atomBinding('window') const v8Util = process.atomBinding('v8_util') @@ -14,6 +14,9 @@ BrowserWindow.prototype._init = function () { // Avoid recursive require. const {app} = electron + // Create WebContentsView. + this.setContentView(new WebContentsView(this.webContents)) + // Make new windows requested by links behave like "window.open" this.webContents.on('-new-window', (event, url, frameName, disposition, additionalFeatures, postData, diff --git a/lib/browser/api/view.js b/lib/browser/api/view.js index ba215aeadaa2..53fb96e3fafb 100644 --- a/lib/browser/api/view.js +++ b/lib/browser/api/view.js @@ -5,4 +5,7 @@ const {View} = process.atomBinding('view') Object.setPrototypeOf(View.prototype, EventEmitter.prototype) +View.prototype._init = function () { +} + module.exports = View