@@ -204,8 +204,7 @@ class InjectedScript::ProtocolPromiseHandler {
204204 PromiseHandlerTracker::DiscardReason::kFulfilled );
205205 }
206206
207- ProtocolPromiseHandler (PromiseHandlerTracker::Id id,
208- V8InspectorSessionImpl* session,
207+ ProtocolPromiseHandler (V8InspectorSessionImpl* session,
209208 int executionContextId, const String16& objectGroup,
210209 std::unique_ptr<WrapOptions> wrapOptions,
211210 bool replMode, bool throwOnSideEffect,
@@ -220,7 +219,13 @@ class InjectedScript::ProtocolPromiseHandler {
220219 m_replMode(replMode),
221220 m_throwOnSideEffect(throwOnSideEffect),
222221 m_callback(std::move(callback)),
223- m_evaluationResult(m_inspector->isolate (), evaluationResult) {
222+ m_evaluationResult(m_inspector->isolate (), evaluationResult) {}
223+
224+ void makeWeak (PromiseHandlerTracker::Id id) {
225+ if (m_isActive || m_evaluationResult.IsEmpty () ||
226+ m_evaluationResult.IsWeak ()) {
227+ return ;
228+ }
224229 m_evaluationResult.SetWeak (reinterpret_cast <PromiseHandlerTracker::Id*>(id),
225230 cleanup, v8::WeakCallbackType::kParameter );
226231 }
@@ -238,6 +243,7 @@ class InjectedScript::ProtocolPromiseHandler {
238243 }
239244
240245 void thenCallback (v8::Local<v8::Value> value) {
246+ m_isActive = true ;
241247 // We don't need the m_evaluationResult in the `thenCallback`, but we also
242248 // don't want `cleanup` running in case we re-enter JS.
243249 m_evaluationResult.Reset ();
@@ -286,9 +292,10 @@ class InjectedScript::ProtocolPromiseHandler {
286292 }
287293
288294 void catchCallback (v8::Local<v8::Value> result) {
295+ m_isActive = true ;
289296 // Hold strongly onto m_evaluationResult now to prevent `cleanup` from
290297 // running in case any code below triggers GC.
291- m_evaluationResult.ClearWeak <void >();
298+ if (m_evaluationResult. IsWeak ()) m_evaluationResult.ClearWeak <void >();
292299 V8InspectorSessionImpl* session =
293300 m_inspector->sessionById (m_contextGroupId, m_sessionId);
294301 if (!session) return ;
@@ -397,6 +404,7 @@ class InjectedScript::ProtocolPromiseHandler {
397404 std::unique_ptr<WrapOptions> m_wrapOptions;
398405 bool m_replMode;
399406 bool m_throwOnSideEffect;
407+ bool m_isActive = false ;
400408 std::weak_ptr<EvaluateCallback> m_callback;
401409 v8::Global<v8::Promise> m_evaluationResult;
402410};
@@ -1206,8 +1214,7 @@ template <typename... Args>
12061214PromiseHandlerTracker::Id PromiseHandlerTracker::create (Args&&... args) {
12071215 Id id = m_lastUsedId++;
12081216 InjectedScript::ProtocolPromiseHandler* handler =
1209- new InjectedScript::ProtocolPromiseHandler (id,
1210- std::forward<Args>(args)...);
1217+ new InjectedScript::ProtocolPromiseHandler (std::forward<Args>(args)...);
12111218 m_promiseHandlers.emplace (id, handler);
12121219 return id;
12131220}
@@ -1241,6 +1248,30 @@ InjectedScript::ProtocolPromiseHandler* PromiseHandlerTracker::get(
12411248 return iter->second .get ();
12421249}
12431250
1251+ void PromiseHandlerTracker::makeWeakForContext (int executionContextId) {
1252+ for (auto & [id, handler] : m_promiseHandlers) {
1253+ if (handler->m_executionContextId == executionContextId) {
1254+ handler->makeWeak (id);
1255+ }
1256+ }
1257+ }
1258+
1259+ void PromiseHandlerTracker::makeWeakForObjectGroup (
1260+ int sessionId, const String16& objectGroup) {
1261+ for (auto & [id, handler] : m_promiseHandlers) {
1262+ if (handler->m_sessionId == sessionId &&
1263+ handler->m_objectGroup == objectGroup) {
1264+ handler->makeWeak (id);
1265+ }
1266+ }
1267+ }
1268+
1269+ void PromiseHandlerTracker::makeWeakForSession (int sessionId) {
1270+ for (auto & [id, handler] : m_promiseHandlers) {
1271+ if (handler->m_sessionId == sessionId) handler->makeWeak (id);
1272+ }
1273+ }
1274+
12441275void PromiseHandlerTracker::sendFailure (
12451276 InjectedScript::ProtocolPromiseHandler* handler,
12461277 const protocol::DispatchResponse& response) const {
0 commit comments