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

View file

@ -10,6 +10,7 @@
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "content/public/browser/download_manager.h"
#include "electron/buildflags/buildflags.h"
@ -218,6 +219,8 @@ class Session : public gin::Wrappable<Session>,
base::UnguessableToken network_emulation_token_;
const raw_ref<ElectronBrowserContext> browser_context_;
base::WeakPtrFactory<Session> weak_factory_{this};
};
} // namespace api