77const { Ci, Cu } = require ( "chrome" ) ;
88const { Actor, ActorClassWithSpec } = require ( "devtools/shared/protocol" ) ;
99const { accessibleSpec } = require ( "devtools/shared/specs/accessibility" ) ;
10+ const { accessibility : { AUDIT_TYPE } } = require ( "devtools/shared/constants" ) ;
1011
1112loader . lazyRequireGetter ( this , "getContrastRatioFor" , "devtools/server/actors/accessibility/contrast" , true ) ;
1213loader . lazyRequireGetter ( this , "isDefunct" , "devtools/server/actors/utils/accessibility" , true ) ;
1314loader . lazyRequireGetter ( this , "findCssSelector" , "devtools/shared/inspector/css-logic" , true ) ;
15+ loader . lazyRequireGetter ( this , "events" , "devtools/shared/event-emitter" ) ;
1416
1517const RELATIONS_TO_IGNORE = new Set ( [
1618 Ci . nsIAccessibleRelation . RELATION_CONTAINING_APPLICATION ,
@@ -155,6 +157,10 @@ const AccessibleActor = ActorClassWithSpec(accessibleSpec, {
155157 this . rawAccessible = null ;
156158 } ,
157159
160+ get isDestroyed ( ) {
161+ return this . actorID == null ;
162+ } ,
163+
158164 get role ( ) {
159165 if ( this . isDefunct ) {
160166 return null ;
@@ -369,6 +375,7 @@ const AccessibleActor = ActorClassWithSpec(accessibleSpec, {
369375 states : this . states ,
370376 actions : this . actions ,
371377 attributes : this . attributes ,
378+ checks : this . _lastAudit ,
372379 } ;
373380 } ,
374381
@@ -388,13 +395,16 @@ const AccessibleActor = ActorClassWithSpec(accessibleSpec, {
388395
389396 const { DOMNode : rawNode } = this . rawAccessible ;
390397 const win = rawNode . ownerGlobal ;
391- this . walker . clearStyles ( win ) ;
398+ // Keep the reference to the walker actor in case the actor gets destroyed
399+ // during the colour contrast ratio calculation.
400+ const { walker } = this ;
401+ walker . clearStyles ( win ) ;
392402 const contrastRatio = await getContrastRatioFor ( rawNode . parentNode , {
393403 bounds : this . bounds ,
394404 win,
395405 } ) ;
396406
397- this . walker . restoreStyles ( win ) ;
407+ walker . restoreStyles ( win ) ;
398408
399409 return contrastRatio ;
400410 } ,
@@ -405,7 +415,7 @@ const AccessibleActor = ActorClassWithSpec(accessibleSpec, {
405415 * @return {Object|null }
406416 * Audit results for the accessible object.
407417 */
408- async audit ( ) {
418+ audit ( ) {
409419 if ( this . _auditing ) {
410420 return this . _auditing ;
411421 }
@@ -418,12 +428,23 @@ const AccessibleActor = ActorClassWithSpec(accessibleSpec, {
418428 ] ) . then ( ( [
419429 contrastRatio ,
420430 ] ) => {
421- const audit = this . isDefunct ? null : {
422- contrastRatio,
423- } ;
431+ let audit = null ;
432+ if ( ! this . isDefunct && ! this . isDestroyed ) {
433+ audit = {
434+ [ AUDIT_TYPE . CONTRAST ] : contrastRatio ,
435+ } ;
436+ this . _lastAudit = audit ;
437+ events . emit ( this , "audited" , audit ) ;
438+ }
424439
425- this . _auditing = null ;
426440 return audit ;
441+ } ) . catch ( error => {
442+ if ( ! this . isDefunct && ! this . isDestroyed ) {
443+ throw error ;
444+ }
445+ return null ;
446+ } ) . finally ( ( ) => {
447+ this . _auditing = null ;
427448 } ) ;
428449
429450 return this . _auditing ;
0 commit comments