diff --git a/atom/browser/api/atom_api_view.h b/atom/browser/api/atom_api_view.h index b1fb38e4c439..4e851ef28983 100644 --- a/atom/browser/api/atom_api_view.h +++ b/atom/browser/api/atom_api_view.h @@ -7,14 +7,14 @@ #include -#include "atom/browser/api/event_emitter.h" +#include "atom/browser/api/trackable_object.h" #include "ui/views/view.h" namespace atom { namespace api { -class View : public mate::EventEmitter { +class View : public mate::TrackableObject { public: static mate::WrappableBase* New(mate::Arguments* args); diff --git a/atom/browser/api/atom_api_web_contents_view.cc b/atom/browser/api/atom_api_web_contents_view.cc index 5bfd94dfee94..2b9443ba2a41 100644 --- a/atom/browser/api/atom_api_web_contents_view.cc +++ b/atom/browser/api/atom_api_web_contents_view.cc @@ -18,16 +18,16 @@ namespace atom { namespace api { -WebContentsView::WebContentsView( - v8::Isolate* isolate, - v8::Local web_contents_wrapper, - brightray::InspectableWebContents* web_contents) +WebContentsView::WebContentsView(v8::Isolate* isolate, + mate::Handle web_contents, + brightray::InspectableWebContents* iwc) #if defined(OS_MACOSX) - : View(new DelayedNativeViewHost(web_contents->GetView()->GetNativeView())), + : View(new DelayedNativeViewHost(iwc->GetView()->GetNativeView())), #else - : View(web_contents->GetView()->GetView()), + : View(iwc->GetView()->GetView()), #endif - web_contents_wrapper_(isolate, web_contents_wrapper) { + web_contents_(isolate, web_contents->GetWrapper()), + api_web_contents_(web_contents.get()) { #if defined(OS_MACOSX) // On macOS a View is created to wrap the NSView, and its lifetime is managed // by us. @@ -36,9 +36,17 @@ WebContentsView::WebContentsView( // On other platforms the View is managed by InspectableWebContents. set_delete_view(false); #endif + api_web_contents_->AddObserver(this); } -WebContentsView::~WebContentsView() {} +WebContentsView::~WebContentsView() { + api_web_contents_->RemoveObserver(this); + api_web_contents_->DestroyWebContents(false /* async */); +} + +void WebContentsView::OnCloseContents() { + // TODO(zcbenz): WebContents is closed, report the event. +} // static mate::WrappableBase* WebContentsView::New( @@ -50,7 +58,7 @@ mate::WrappableBase* WebContentsView::New( v8::Exception::Error(mate::StringToV8(args->isolate(), error))); return nullptr; } - auto* view = new WebContentsView(args->isolate(), web_contents->GetWrapper(), + auto* view = new WebContentsView(args->isolate(), web_contents, web_contents->managed_web_contents()); view->InitWith(args->isolate(), args->GetThis()); return view; diff --git a/atom/browser/api/atom_api_web_contents_view.h b/atom/browser/api/atom_api_web_contents_view.h index e3cf96f99085..c29657d870f9 100644 --- a/atom/browser/api/atom_api_web_contents_view.h +++ b/atom/browser/api/atom_api_web_contents_view.h @@ -6,11 +6,7 @@ #define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_VIEW_H_ #include "atom/browser/api/atom_api_view.h" -#include "native_mate/handle.h" - -namespace brightray { -class InspectableWebContents; -} +#include "atom/browser/api/atom_api_web_contents.h" namespace atom { @@ -18,7 +14,7 @@ namespace api { class WebContents; -class WebContentsView : public View { +class WebContentsView : public View, public ExtendedWebContentsObserver { public: static mate::WrappableBase* New(mate::Arguments* args, mate::Handle web_contents); @@ -28,13 +24,17 @@ class WebContentsView : public View { protected: WebContentsView(v8::Isolate* isolate, - v8::Local web_contents_wrapper, - brightray::InspectableWebContents* web_contents); + mate::Handle web_contents, + brightray::InspectableWebContents* iwc); ~WebContentsView() override; + // ExtendedWebContentsObserver: + void OnCloseContents() override; + private: // Keep a reference to v8 wrapper. - v8::Global web_contents_wrapper_; + v8::Global web_contents_; + api::WebContents* api_web_contents_; DISALLOW_COPY_AND_ASSIGN(WebContentsView); };