refactor: clean up Session with CleanedUpAtExit (#24603)
This commit is contained in:
parent
e5cb22b7f9
commit
f0953902db
4 changed files with 17 additions and 11 deletions
|
@ -942,8 +942,6 @@ gin::Handle<Session> Session::CreateFrom(
|
||||||
// The Sessions should never be garbage collected, since the common pattern is
|
// The Sessions should never be garbage collected, since the common pattern is
|
||||||
// to use partition strings, instead of using the Session object directly.
|
// to use partition strings, instead of using the Session object directly.
|
||||||
handle->Pin(isolate);
|
handle->Pin(isolate);
|
||||||
ElectronBrowserMainParts::Get()->RegisterDestructionCallback(
|
|
||||||
base::BindOnce([](Session* session) { delete session; }, handle.get()));
|
|
||||||
|
|
||||||
App::Get()->EmitCustomEvent("session-created",
|
App::Get()->EmitCustomEvent("session-created",
|
||||||
handle.ToV8().As<v8::Object>());
|
handle.ToV8().As<v8::Object>());
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include "gin/wrappable.h"
|
#include "gin/wrappable.h"
|
||||||
#include "shell/browser/event_emitter_mixin.h"
|
#include "shell/browser/event_emitter_mixin.h"
|
||||||
#include "shell/browser/net/resolve_proxy_helper.h"
|
#include "shell/browser/net/resolve_proxy_helper.h"
|
||||||
|
#include "shell/common/gin_helper/cleaned_up_at_exit.h"
|
||||||
#include "shell/common/gin_helper/error_thrower.h"
|
#include "shell/common/gin_helper/error_thrower.h"
|
||||||
#include "shell/common/gin_helper/function_template_extensions.h"
|
#include "shell/common/gin_helper/function_template_extensions.h"
|
||||||
#include "shell/common/gin_helper/pinnable.h"
|
#include "shell/common/gin_helper/pinnable.h"
|
||||||
|
@ -51,6 +52,7 @@ namespace api {
|
||||||
class Session : public gin::Wrappable<Session>,
|
class Session : public gin::Wrappable<Session>,
|
||||||
public gin_helper::Pinnable<Session>,
|
public gin_helper::Pinnable<Session>,
|
||||||
public gin_helper::EventEmitterMixin<Session>,
|
public gin_helper::EventEmitterMixin<Session>,
|
||||||
|
public gin_helper::CleanedUpAtExit,
|
||||||
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
||||||
public SpellcheckHunspellDictionary::Observer,
|
public SpellcheckHunspellDictionary::Observer,
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -46,7 +46,6 @@ JavascriptEnvironment::~JavascriptEnvironment() {
|
||||||
{
|
{
|
||||||
v8::Locker locker(isolate_);
|
v8::Locker locker(isolate_);
|
||||||
v8::HandleScope scope(isolate_);
|
v8::HandleScope scope(isolate_);
|
||||||
gin_helper::CleanedUpAtExit::DoCleanup();
|
|
||||||
context_.Get(isolate_)->Exit();
|
context_.Get(isolate_)->Exit();
|
||||||
}
|
}
|
||||||
isolate_->Exit();
|
isolate_->Exit();
|
||||||
|
@ -97,6 +96,11 @@ void JavascriptEnvironment::OnMessageLoopCreated() {
|
||||||
|
|
||||||
void JavascriptEnvironment::OnMessageLoopDestroying() {
|
void JavascriptEnvironment::OnMessageLoopDestroying() {
|
||||||
DCHECK(microtasks_runner_);
|
DCHECK(microtasks_runner_);
|
||||||
|
{
|
||||||
|
v8::Locker locker(isolate_);
|
||||||
|
v8::HandleScope scope(isolate_);
|
||||||
|
gin_helper::CleanedUpAtExit::DoCleanup();
|
||||||
|
}
|
||||||
base::MessageLoopCurrent::Get()->RemoveTaskObserver(microtasks_runner_.get());
|
base::MessageLoopCurrent::Get()->RemoveTaskObserver(microtasks_runner_.get());
|
||||||
platform_->DrainTasks(isolate_);
|
platform_->DrainTasks(isolate_);
|
||||||
platform_->UnregisterIsolate(isolate_);
|
platform_->UnregisterIsolate(isolate_);
|
||||||
|
|
|
@ -4,29 +4,31 @@
|
||||||
|
|
||||||
#include "shell/common/gin_helper/cleaned_up_at_exit.h"
|
#include "shell/common/gin_helper/cleaned_up_at_exit.h"
|
||||||
|
|
||||||
#include "base/containers/flat_set.h"
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "base/no_destructor.h"
|
#include "base/no_destructor.h"
|
||||||
|
|
||||||
namespace gin_helper {
|
namespace gin_helper {
|
||||||
|
|
||||||
base::flat_set<CleanedUpAtExit*>& GetDoomed() {
|
std::vector<CleanedUpAtExit*>& GetDoomed() {
|
||||||
static base::NoDestructor<base::flat_set<CleanedUpAtExit*>> doomed;
|
static base::NoDestructor<std::vector<CleanedUpAtExit*>> doomed;
|
||||||
return *doomed;
|
return *doomed;
|
||||||
}
|
}
|
||||||
CleanedUpAtExit::CleanedUpAtExit() {
|
CleanedUpAtExit::CleanedUpAtExit() {
|
||||||
GetDoomed().insert(this);
|
GetDoomed().emplace_back(this);
|
||||||
}
|
}
|
||||||
CleanedUpAtExit::~CleanedUpAtExit() {
|
CleanedUpAtExit::~CleanedUpAtExit() {
|
||||||
GetDoomed().erase(this);
|
auto& doomed = GetDoomed();
|
||||||
|
doomed.erase(std::remove(doomed.begin(), doomed.end(), this), doomed.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void CleanedUpAtExit::DoCleanup() {
|
void CleanedUpAtExit::DoCleanup() {
|
||||||
auto& doomed = GetDoomed();
|
auto& doomed = GetDoomed();
|
||||||
while (!doomed.empty()) {
|
while (!doomed.empty()) {
|
||||||
auto iter = doomed.begin();
|
CleanedUpAtExit* next = doomed.back();
|
||||||
delete *iter;
|
delete next;
|
||||||
// It removed itself from the list.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue