electron/shell/common/api/object_life_monitor.cc

41 lines
1 KiB
C++
Raw Normal View History

// Copyright (c) 2013 GitHub, Inc.
// Copyright (c) 2012 Intel Corp. All rights reserved.
2014-04-25 09:49:37 +00:00
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/api/object_life_monitor.h"
2015-08-27 08:41:51 +00:00
#include "base/bind.h"
namespace electron {
2015-08-27 08:41:51 +00:00
ObjectLifeMonitor::ObjectLifeMonitor(v8::Isolate* isolate,
v8::Local<v8::Object> target)
2018-04-18 01:55:30 +00:00
: target_(isolate, target), weak_ptr_factory_(this) {
2015-08-27 08:41:51 +00:00
target_.SetWeak(this, OnObjectGC, v8::WeakCallbackType::kParameter);
}
ObjectLifeMonitor::~ObjectLifeMonitor() {
if (target_.IsEmpty())
return;
target_.ClearWeak();
target_.Reset();
}
// static
2015-08-27 08:41:51 +00:00
void ObjectLifeMonitor::OnObjectGC(
const v8::WeakCallbackInfo<ObjectLifeMonitor>& data) {
ObjectLifeMonitor* self = data.GetParameter();
self->target_.Reset();
self->RunDestructor();
data.SetSecondPassCallback(Free);
}
// static
void ObjectLifeMonitor::Free(
const v8::WeakCallbackInfo<ObjectLifeMonitor>& data) {
delete data.GetParameter();
2015-08-27 08:41:51 +00:00
}
} // namespace electron