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 "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<View> {
class View : public mate::TrackableObject<View> {
public:
static mate::WrappableBase* New(mate::Arguments* args);

View file

@ -18,16 +18,16 @@ namespace atom {
namespace api {
WebContentsView::WebContentsView(
v8::Isolate* isolate,
v8::Local<v8::Value> web_contents_wrapper,
brightray::InspectableWebContents* web_contents)
WebContentsView::WebContentsView(v8::Isolate* isolate,
mate::Handle<WebContents> 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;

View file

@ -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<WebContents> web_contents);
@ -28,13 +24,17 @@ class WebContentsView : public View {
protected:
WebContentsView(v8::Isolate* isolate,
v8::Local<v8::Value> web_contents_wrapper,
brightray::InspectableWebContents* web_contents);
mate::Handle<WebContents> web_contents,
brightray::InspectableWebContents* iwc);
~WebContentsView() override;
// ExtendedWebContentsObserver:
void OnCloseContents() override;
private:
// 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);
};