2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2013-04-25 10:25:18 +00:00
|
|
|
// 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
|
2013-04-25 10:25:18 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-05-12 16:32:04 +00:00
|
|
|
#include "shell/common/api/object_life_monitor.h"
|
2013-04-25 10:25:18 +00:00
|
|
|
|
2023-02-03 11:43:42 +00:00
|
|
|
#include "base/functional/bind.h"
|
2014-06-28 14:33:00 +00:00
|
|
|
|
2013-04-25 10:25:18 +00:00
|
|
|
namespace electron {
|
|
|
|
|
2015-08-27 08:41:51 +00:00
|
|
|
ObjectLifeMonitor::ObjectLifeMonitor(v8::Isolate* isolate,
|
2016-04-26 07:10:27 +00:00
|
|
|
v8::Local<v8::Object> target)
|
2021-01-26 18:16:21 +00:00
|
|
|
: target_(isolate, target) {
|
2015-08-27 08:41:51 +00:00
|
|
|
target_.SetWeak(this, OnObjectGC, v8::WeakCallbackType::kParameter);
|
2013-04-25 10:25:18 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 07:10:27 +00:00
|
|
|
ObjectLifeMonitor::~ObjectLifeMonitor() {
|
|
|
|
if (target_.IsEmpty())
|
|
|
|
return;
|
|
|
|
target_.ClearWeak();
|
|
|
|
target_.Reset();
|
|
|
|
}
|
|
|
|
|
2013-04-25 10:25:18 +00:00
|
|
|
// static
|
2015-08-27 08:41:51 +00:00
|
|
|
void ObjectLifeMonitor::OnObjectGC(
|
|
|
|
const v8::WeakCallbackInfo<ObjectLifeMonitor>& data) {
|
|
|
|
ObjectLifeMonitor* self = data.GetParameter();
|
|
|
|
self->target_.Reset();
|
2016-04-26 07:10:27 +00:00
|
|
|
self->RunDestructor();
|
2016-03-21 12:42:12 +00:00
|
|
|
data.SetSecondPassCallback(Free);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void ObjectLifeMonitor::Free(
|
|
|
|
const v8::WeakCallbackInfo<ObjectLifeMonitor>& data) {
|
|
|
|
delete data.GetParameter();
|
2015-08-27 08:41:51 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 10:25:18 +00:00
|
|
|
} // namespace electron
|