@@ -179,40 +179,71 @@ void RuntimeTarget::notifyDomainStateChanged(
179179 Domain domain,
180180 bool enabled,
181181 const RuntimeAgent& notifyingAgent) {
182- bool runtimeAndLogStatusBefore = false , runtimeAndLogStatusAfter = false ;
183- if (domain == Domain::Log || domain == Domain::Runtime) {
184- runtimeAndLogStatusBefore =
185- agentsByEnabledDomain_[Domain::Runtime].contains (¬ifyingAgent) &&
186- agentsByEnabledDomain_[Domain::Log].contains (¬ifyingAgent);
187- }
188-
189- if (enabled) {
190- agentsByEnabledDomain_[domain].insert (¬ifyingAgent);
191- } else {
192- agentsByEnabledDomain_[domain].erase (¬ifyingAgent);
193- }
194- threadSafeDomainStatus_[domain] = !agentsByEnabledDomain_[domain].empty ();
195-
196- if (domain == Domain::Log || domain == Domain::Runtime) {
197- runtimeAndLogStatusAfter =
198- agentsByEnabledDomain_[Domain::Runtime].contains (¬ifyingAgent) &&
199- agentsByEnabledDomain_[Domain::Log].contains (¬ifyingAgent);
182+ auto [domainStateChangedLocally, domainStateChangedGlobally] =
183+ processDomainChange (domain, enabled, notifyingAgent);
184+
185+ switch (domain) {
186+ case Domain::Log:
187+ case Domain::Runtime: {
188+ auto otherDomain = domain == Domain::Log ? Domain::Runtime : Domain::Log;
189+ // There should be an agent that enables both Log and Runtime domains.
190+ if (!agentsByEnabledDomain_[otherDomain].contains (¬ifyingAgent)) {
191+ break ;
192+ }
200193
201- if (runtimeAndLogStatusBefore != runtimeAndLogStatusAfter) {
202- if (runtimeAndLogStatusAfter) {
194+ if (domainStateChangedGlobally && enabled) {
195+ assert (agentsWithRuntimeAndLogDomainsEnabled_ == 0 );
196+ emitDebuggerSessionCreated ();
197+ ++agentsWithRuntimeAndLogDomainsEnabled_;
198+ } else if (domainStateChangedGlobally) {
199+ assert (agentsWithRuntimeAndLogDomainsEnabled_ == 1 );
200+ emitDebuggerSessionDestroyed ();
201+ --agentsWithRuntimeAndLogDomainsEnabled_;
202+ } else if (domainStateChangedLocally && enabled) {
203+ // This is a case when given domain was already enabled by other Agent,
204+ // so global state didn't change.
203205 if (++agentsWithRuntimeAndLogDomainsEnabled_ == 1 ) {
204206 emitDebuggerSessionCreated ();
205207 }
206- } else {
207- assert (agentsWithRuntimeAndLogDomainsEnabled_ > 0 );
208+ } else if (domainStateChangedLocally) {
208209 if (--agentsWithRuntimeAndLogDomainsEnabled_ == 0 ) {
209210 emitDebuggerSessionDestroyed ();
210211 }
211212 }
213+
214+ break ;
215+ }
216+ case Domain::Network:
217+ break ;
218+ case Domain::kMaxValue : {
219+ throw std::logic_error (" Unexpected kMaxValue domain value provided" );
212220 }
213221 }
214222}
215223
224+ std::pair<bool , bool > RuntimeTarget::processDomainChange (
225+ Domain domain,
226+ bool enabled,
227+ const RuntimeAgent& notifyingAgent) {
228+ bool domainHadAgentsBefore = !agentsByEnabledDomain_[domain].empty ();
229+ bool domainHasBeenEnabledBefore =
230+ agentsByEnabledDomain_[domain].contains (¬ifyingAgent);
231+
232+ if (enabled) {
233+ agentsByEnabledDomain_[domain].insert (¬ifyingAgent);
234+ } else {
235+ agentsByEnabledDomain_[domain].erase (¬ifyingAgent);
236+ }
237+ threadSafeDomainStatus_[domain] = !agentsByEnabledDomain_[domain].empty ();
238+
239+ bool domainHasAgentsAfter = !agentsByEnabledDomain_[domain].empty ();
240+
241+ return {
242+ domainHasBeenEnabledBefore ^ enabled,
243+ domainHadAgentsBefore ^ domainHasAgentsAfter,
244+ };
245+ }
246+
216247bool RuntimeTarget::isDomainEnabled (Domain domain) const {
217248 return threadSafeDomainStatus_[domain];
218249}
0 commit comments