fix: ensure persistence store still exists when GC runs (#21402)
Fix a bad access crash that happens when a render frame is deleted (window closed) and garbage collection runs afterward.
This commit is contained in:
parent
4037b12511
commit
8a06ef11cf
2 changed files with 10 additions and 6 deletions
|
@ -20,7 +20,7 @@ class CachedProxyLifeMonitor final : public ObjectLifeMonitor {
|
||||||
public:
|
public:
|
||||||
static void BindTo(v8::Isolate* isolate,
|
static void BindTo(v8::Isolate* isolate,
|
||||||
v8::Local<v8::Object> target,
|
v8::Local<v8::Object> target,
|
||||||
RenderFramePersistenceStore* store,
|
base::WeakPtr<RenderFramePersistenceStore> store,
|
||||||
WeakGlobalPairNode* node,
|
WeakGlobalPairNode* node,
|
||||||
int hash) {
|
int hash) {
|
||||||
new CachedProxyLifeMonitor(isolate, target, store, node, hash);
|
new CachedProxyLifeMonitor(isolate, target, store, node, hash);
|
||||||
|
@ -29,7 +29,7 @@ class CachedProxyLifeMonitor final : public ObjectLifeMonitor {
|
||||||
protected:
|
protected:
|
||||||
CachedProxyLifeMonitor(v8::Isolate* isolate,
|
CachedProxyLifeMonitor(v8::Isolate* isolate,
|
||||||
v8::Local<v8::Object> target,
|
v8::Local<v8::Object> target,
|
||||||
RenderFramePersistenceStore* store,
|
base::WeakPtr<RenderFramePersistenceStore> store,
|
||||||
WeakGlobalPairNode* node,
|
WeakGlobalPairNode* node,
|
||||||
int hash)
|
int hash)
|
||||||
: ObjectLifeMonitor(isolate, target),
|
: ObjectLifeMonitor(isolate, target),
|
||||||
|
@ -38,6 +38,9 @@ class CachedProxyLifeMonitor final : public ObjectLifeMonitor {
|
||||||
hash_(hash) {}
|
hash_(hash) {}
|
||||||
|
|
||||||
void RunDestructor() override {
|
void RunDestructor() override {
|
||||||
|
if (!store_)
|
||||||
|
return;
|
||||||
|
|
||||||
if (node_->detached) {
|
if (node_->detached) {
|
||||||
delete node_;
|
delete node_;
|
||||||
return;
|
return;
|
||||||
|
@ -56,7 +59,7 @@ class CachedProxyLifeMonitor final : public ObjectLifeMonitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RenderFramePersistenceStore* store_;
|
base::WeakPtr<RenderFramePersistenceStore> store_;
|
||||||
WeakGlobalPairNode* node_;
|
WeakGlobalPairNode* node_;
|
||||||
int hash_;
|
int hash_;
|
||||||
};
|
};
|
||||||
|
@ -98,11 +101,11 @@ void RenderFramePersistenceStore::CacheProxiedObject(
|
||||||
auto iter = proxy_map_.find(hash);
|
auto iter = proxy_map_.find(hash);
|
||||||
auto* node = new WeakGlobalPairNode(
|
auto* node = new WeakGlobalPairNode(
|
||||||
std::make_tuple(std::move(global_from), std::move(global_proxy)));
|
std::make_tuple(std::move(global_from), std::move(global_proxy)));
|
||||||
CachedProxyLifeMonitor::BindTo(v8::Isolate::GetCurrent(), obj, this, node,
|
CachedProxyLifeMonitor::BindTo(v8::Isolate::GetCurrent(), obj,
|
||||||
hash);
|
weak_factory_.GetWeakPtr(), node, hash);
|
||||||
CachedProxyLifeMonitor::BindTo(v8::Isolate::GetCurrent(),
|
CachedProxyLifeMonitor::BindTo(v8::Isolate::GetCurrent(),
|
||||||
v8::Local<v8::Object>::Cast(proxy_value),
|
v8::Local<v8::Object>::Cast(proxy_value),
|
||||||
this, node, hash);
|
weak_factory_.GetWeakPtr(), node, hash);
|
||||||
if (iter == proxy_map_.end()) {
|
if (iter == proxy_map_.end()) {
|
||||||
proxy_map_.emplace(hash, node);
|
proxy_map_.emplace(hash, node);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -60,6 +60,7 @@ class RenderFramePersistenceStore final : public content::RenderFrameObserver {
|
||||||
|
|
||||||
// object_identity ==> [from_value, proxy_value]
|
// object_identity ==> [from_value, proxy_value]
|
||||||
std::map<int, WeakGlobalPairNode*> proxy_map_;
|
std::map<int, WeakGlobalPairNode*> proxy_map_;
|
||||||
|
base::WeakPtrFactory<RenderFramePersistenceStore> weak_factory_{this};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace context_bridge
|
} // namespace context_bridge
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue