fix: dangling raw_ptr<Session> in UserDataLink (#42786)

* fix: dangling raw_ptr<Session> in UserDataLink

* chore: make linter happy
This commit is contained in:
Charles Kerr 2024-07-08 05:55:48 -05:00 committed by GitHub
parent cb2e7f130b
commit e89da2bad2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 5 deletions

View file

@ -530,9 +530,10 @@ class DictionaryObserver final : public SpellcheckCustomDictionary::Observer {
#endif // BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
struct UserDataLink : base::SupportsUserData::Data {
explicit UserDataLink(Session* ses) : session(ses) {}
explicit UserDataLink(base::WeakPtr<Session> session_in)
: session{std::move(session_in)} {}
raw_ptr<Session> session;
base::WeakPtr<Session> session;
};
const void* kElectronApiSessionKey = &kElectronApiSessionKey;
@ -553,8 +554,9 @@ Session::Session(v8::Isolate* isolate, ElectronBrowserContext* browser_context)
protocol_.Reset(isolate, Protocol::Create(isolate, browser_context).ToV8());
browser_context->SetUserData(kElectronApiSessionKey,
std::make_unique<UserDataLink>(this));
browser_context->SetUserData(
kElectronApiSessionKey,
std::make_unique<UserDataLink>(weak_factory_.GetWeakPtr()));
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
if (auto* service =
@ -1511,7 +1513,7 @@ bool Session::IsSpellCheckerEnabled() const {
Session* Session::FromBrowserContext(content::BrowserContext* context) {
auto* data =
static_cast<UserDataLink*>(context->GetUserData(kElectronApiSessionKey));
return data ? data->session : nullptr;
return data ? data->session.get() : nullptr;
}
// static