fix: dangling raw_ptr<Session> in UserDataLink (#42786)
* fix: dangling raw_ptr<Session> in UserDataLink * chore: make linter happy
This commit is contained in:
parent
cb2e7f130b
commit
e89da2bad2
2 changed files with 10 additions and 5 deletions
|
@ -530,9 +530,10 @@ class DictionaryObserver final : public SpellcheckCustomDictionary::Observer {
|
||||||
#endif // BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
#endif // BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
||||||
|
|
||||||
struct UserDataLink : base::SupportsUserData::Data {
|
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;
|
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());
|
protocol_.Reset(isolate, Protocol::Create(isolate, browser_context).ToV8());
|
||||||
|
|
||||||
browser_context->SetUserData(kElectronApiSessionKey,
|
browser_context->SetUserData(
|
||||||
std::make_unique<UserDataLink>(this));
|
kElectronApiSessionKey,
|
||||||
|
std::make_unique<UserDataLink>(weak_factory_.GetWeakPtr()));
|
||||||
|
|
||||||
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
||||||
if (auto* service =
|
if (auto* service =
|
||||||
|
@ -1511,7 +1513,7 @@ bool Session::IsSpellCheckerEnabled() const {
|
||||||
Session* Session::FromBrowserContext(content::BrowserContext* context) {
|
Session* Session::FromBrowserContext(content::BrowserContext* context) {
|
||||||
auto* data =
|
auto* data =
|
||||||
static_cast<UserDataLink*>(context->GetUserData(kElectronApiSessionKey));
|
static_cast<UserDataLink*>(context->GetUserData(kElectronApiSessionKey));
|
||||||
return data ? data->session : nullptr;
|
return data ? data->session.get() : nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "base/memory/raw_ptr.h"
|
#include "base/memory/raw_ptr.h"
|
||||||
|
#include "base/memory/weak_ptr.h"
|
||||||
#include "base/values.h"
|
#include "base/values.h"
|
||||||
#include "content/public/browser/download_manager.h"
|
#include "content/public/browser/download_manager.h"
|
||||||
#include "electron/buildflags/buildflags.h"
|
#include "electron/buildflags/buildflags.h"
|
||||||
|
@ -218,6 +219,8 @@ class Session : public gin::Wrappable<Session>,
|
||||||
base::UnguessableToken network_emulation_token_;
|
base::UnguessableToken network_emulation_token_;
|
||||||
|
|
||||||
const raw_ref<ElectronBrowserContext> browser_context_;
|
const raw_ref<ElectronBrowserContext> browser_context_;
|
||||||
|
|
||||||
|
base::WeakPtrFactory<Session> weak_factory_{this};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
Loading…
Reference in a new issue