@@ -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 ();
@@ -285,9 +291,10 @@ class InjectedScript::ProtocolPromiseHandler {
285291 }
286292
287293 void catchCallback (v8::Local<v8::Value> result) {
294+ m_isActive = true ;
288295 // Hold strongly onto m_evaluationResult now to prevent `cleanup` from
289296 // running in case any code below triggers GC.
290- m_evaluationResult.ClearWeak ();
297+ if (m_evaluationResult. IsWeak ()) m_evaluationResult.ClearWeak ();
291298 V8InspectorSessionImpl* session =
292299 m_inspector->sessionById (m_contextGroupId, m_sessionId);
293300 if (!session) return ;
@@ -393,6 +400,7 @@ class InjectedScript::ProtocolPromiseHandler {
393400 std::unique_ptr<WrapOptions> m_wrapOptions;
394401 bool m_replMode;
395402 bool m_throwOnSideEffect;
403+ bool m_isActive = false ;
396404 std::weak_ptr<EvaluateCallback> m_callback;
397405 v8::Global<v8::Promise> m_evaluationResult;
398406};
@@ -1190,8 +1198,7 @@ template <typename... Args>
11901198PromiseHandlerTracker::Id PromiseHandlerTracker::create (Args&&... args) {
11911199 Id id = m_lastUsedId++;
11921200 InjectedScript::ProtocolPromiseHandler* handler =
1193- new InjectedScript::ProtocolPromiseHandler (id,
1194- std::forward<Args>(args)...);
1201+ new InjectedScript::ProtocolPromiseHandler (std::forward<Args>(args)...);
11951202 m_promiseHandlers.emplace (id, handler);
11961203 return id;
11971204}
@@ -1225,6 +1232,30 @@ InjectedScript::ProtocolPromiseHandler* PromiseHandlerTracker::get(
12251232 return iter->second .get ();
12261233}
12271234
1235+ void PromiseHandlerTracker::makeWeakForContext (int executionContextId) {
1236+ for (auto & [id, handler] : m_promiseHandlers) {
1237+ if (handler->m_executionContextId == executionContextId) {
1238+ handler->makeWeak (id);
1239+ }
1240+ }
1241+ }
1242+
1243+ void PromiseHandlerTracker::makeWeakForObjectGroup (
1244+ int sessionId, const String16& objectGroup) {
1245+ for (auto & [id, handler] : m_promiseHandlers) {
1246+ if (handler->m_sessionId == sessionId &&
1247+ handler->m_objectGroup == objectGroup) {
1248+ handler->makeWeak (id);
1249+ }
1250+ }
1251+ }
1252+
1253+ void PromiseHandlerTracker::makeWeakForSession (int sessionId) {
1254+ for (auto & [id, handler] : m_promiseHandlers) {
1255+ if (handler->m_sessionId == sessionId) handler->makeWeak (id);
1256+ }
1257+ }
1258+
12281259void PromiseHandlerTracker::sendFailure (
12291260 InjectedScript::ProtocolPromiseHandler* handler,
12301261 const protocol::DispatchResponse& response) const {
0 commit comments