destroy WebContents when view is destroyed

This commit is contained in:
Cheng Zhao 2018-05-16 19:29:44 +09:00
parent 7c2303c758
commit fd4a0626c5
3 changed files with 28 additions and 20 deletions

View file

@ -7,14 +7,14 @@
#include <memory> #include <memory>
#include "atom/browser/api/event_emitter.h" #include "atom/browser/api/trackable_object.h"
#include "ui/views/view.h" #include "ui/views/view.h"
namespace atom { namespace atom {
namespace api { namespace api {
class View : public mate::EventEmitter<View> { class View : public mate::TrackableObject<View> {
public: public:
static mate::WrappableBase* New(mate::Arguments* args); static mate::WrappableBase* New(mate::Arguments* args);

View file

@ -18,16 +18,16 @@ namespace atom {
namespace api { namespace api {
WebContentsView::WebContentsView( WebContentsView::WebContentsView(v8::Isolate* isolate,
v8::Isolate* isolate, mate::Handle<WebContents> web_contents,
v8::Local<v8::Value> web_contents_wrapper, brightray::InspectableWebContents* iwc)
brightray::InspectableWebContents* web_contents)
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
: View(new DelayedNativeViewHost(web_contents->GetView()->GetNativeView())), : View(new DelayedNativeViewHost(iwc->GetView()->GetNativeView())),
#else #else
: View(web_contents->GetView()->GetView()), : View(iwc->GetView()->GetView()),
#endif #endif
web_contents_wrapper_(isolate, web_contents_wrapper) { web_contents_(isolate, web_contents->GetWrapper()),
api_web_contents_(web_contents.get()) {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// On macOS a View is created to wrap the NSView, and its lifetime is managed // On macOS a View is created to wrap the NSView, and its lifetime is managed
// by us. // by us.
@ -36,9 +36,17 @@ WebContentsView::WebContentsView(
// On other platforms the View is managed by InspectableWebContents. // On other platforms the View is managed by InspectableWebContents.
set_delete_view(false); set_delete_view(false);
#endif #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 // static
mate::WrappableBase* WebContentsView::New( mate::WrappableBase* WebContentsView::New(
@ -50,7 +58,7 @@ mate::WrappableBase* WebContentsView::New(
v8::Exception::Error(mate::StringToV8(args->isolate(), error))); v8::Exception::Error(mate::StringToV8(args->isolate(), error)));
return nullptr; return nullptr;
} }
auto* view = new WebContentsView(args->isolate(), web_contents->GetWrapper(), auto* view = new WebContentsView(args->isolate(), web_contents,
web_contents->managed_web_contents()); web_contents->managed_web_contents());
view->InitWith(args->isolate(), args->GetThis()); view->InitWith(args->isolate(), args->GetThis());
return view; return view;

View file

@ -6,11 +6,7 @@
#define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_VIEW_H_ #define ATOM_BROWSER_API_ATOM_API_WEB_CONTENTS_VIEW_H_
#include "atom/browser/api/atom_api_view.h" #include "atom/browser/api/atom_api_view.h"
#include "native_mate/handle.h" #include "atom/browser/api/atom_api_web_contents.h"
namespace brightray {
class InspectableWebContents;
}
namespace atom { namespace atom {
@ -18,7 +14,7 @@ namespace api {
class WebContents; class WebContents;
class WebContentsView : public View { class WebContentsView : public View, public ExtendedWebContentsObserver {
public: public:
static mate::WrappableBase* New(mate::Arguments* args, static mate::WrappableBase* New(mate::Arguments* args,
mate::Handle<WebContents> web_contents); mate::Handle<WebContents> web_contents);
@ -28,13 +24,17 @@ class WebContentsView : public View {
protected: protected:
WebContentsView(v8::Isolate* isolate, WebContentsView(v8::Isolate* isolate,
v8::Local<v8::Value> web_contents_wrapper, mate::Handle<WebContents> web_contents,
brightray::InspectableWebContents* web_contents); brightray::InspectableWebContents* iwc);
~WebContentsView() override; ~WebContentsView() override;
// ExtendedWebContentsObserver:
void OnCloseContents() override;
private: private:
// Keep a reference to v8 wrapper. // Keep a reference to v8 wrapper.
v8::Global<v8::Value> web_contents_wrapper_; v8::Global<v8::Value> web_contents_;
api::WebContents* api_web_contents_;
DISALLOW_COPY_AND_ASSIGN(WebContentsView); DISALLOW_COPY_AND_ASSIGN(WebContentsView);
}; };