@@ -89,7 +89,6 @@ function SSLObservatory() {
8989 // Clear these on cookies-cleared observer event
9090 this . already_submitted = { } ;
9191 this . delayed_submissions = { } ;
92- OS . addObserver ( this , "cookie-changed" , false ) ;
9392
9493 // Figure out the url to submit to
9594 this . submit_host = null ;
@@ -108,17 +107,22 @@ function SSLObservatory() {
108107
109108 this . compatJSON = Cc [ "@mozilla.org/dom/json;1" ] . createInstance ( Ci . nsIJSON ) ;
110109
111- // XXX: We shouldn't register any observers or listeners unless the enabled
112- // pref is set. This goes for the cookie-changed observer above, too..
113- // (But for this, we need a pref-changed observer)
114- //
115- // Register observer
116- OS . addObserver ( this , "http-on-examine-response" , false ) ;
110+ var pref_service = Components . classes [ "@mozilla.org/preferences-service;1" ]
111+ . getService ( Components . interfaces . nsIPrefBranchInternal ) ;
112+ var branch = pref_service . QueryInterface ( Components . interfaces . nsIPrefBranchInternal ) ;
117113
118- var dls = CC [ '@mozilla.org/docloaderservice;1' ]
119- . getService ( CI . nsIWebProgress ) ;
120- dls . addProgressListener ( this ,
121- Ci . nsIWebProgress . NOTIFY_STATE_REQUEST ) ;
114+ branch . addObserver ( "extensions.https_everywhere._observatory.enabled" ,
115+ this , false ) ;
116+
117+ if ( this . myGetBoolPref ( "enabled" ) ) {
118+ OS . addObserver ( this , "cookie-changed" , false ) ;
119+ OS . addObserver ( this , "http-on-examine-response" , false ) ;
120+
121+ var dls = CC [ '@mozilla.org/docloaderservice;1' ]
122+ . getService ( CI . nsIWebProgress ) ;
123+ dls . addProgressListener ( this ,
124+ Ci . nsIWebProgress . NOTIFY_STATE_REQUEST ) ;
125+ }
122126
123127 // Register protocolproxyfilter
124128 this . pps = CC [ "@mozilla.org/network/protocol-proxy-service;1" ]
@@ -341,16 +345,13 @@ SSLObservatory.prototype = {
341345 return ;
342346 }
343347
344- if ( topic == "nsPref:changed" ) {
345- // XXX: We somehow need to only call this once. Right now, we'll make
346- // like 3 calls to getClientASN().. The only thing I can think
347- // of is a timer...
348- if ( data == "network.proxy.ssl" || data == "network.proxy.ssl_port" ||
349- data == "network.proxy.socks" || data == "network.proxy.socks_port" ) {
350- this . log ( INFO , "Proxy settings have changed. Getting new ASN" ) ;
351- this . getClientASN ( ) ;
352- }
353- return ;
348+ if ( "http-on-examine-response" == topic ) {
349+ var channel = subject ;
350+ if ( ! this . observatoryActive ( channel ) ) return ;
351+
352+ var certchain = this . getSSLCertChain ( subject ) ;
353+ var warning = false ;
354+ this . submitCertChainForChannel ( certchain , channel , warning ) ;
354355 }
355356
356357 if ( topic == "network:offline-status-changed" && data == "online" ) {
@@ -359,15 +360,50 @@ SSLObservatory.prototype = {
359360 return ;
360361 }
361362
362- if ( "http-on-examine-response" == topic ) {
363-
364- var channel = subject ;
365- if ( ! this . observatoryActive ( channel ) ) return ;
366-
367- var certchain = this . getSSLCertChain ( subject ) ;
368- var warning = false ;
369- this . submitCertChainForChannel ( certchain , channel , warning ) ;
363+ if ( topic == "nsPref:changed" ) {
364+ // If the user toggles the SSL Observatory settings, we need to add or remove
365+ // our observers
366+ switch ( data ) {
367+ case "network.proxy.ssl" :
368+ case "network.proxy.ssl_port" :
369+ case "network.proxy.socks" :
370+ case "network.proxy.socks_port" :
371+ // XXX: We somehow need to only call this once. Right now, we'll make
372+ // like 3 calls to getClientASN().. The only thing I can think
373+ // of is a timer...
374+ this . log ( INFO , "Proxy settings have changed. Getting new ASN" ) ;
375+ this . getClientASN ( ) ;
376+ break ;
377+ case "extensions.https_everywhere._observatory.enabled" :
378+ if ( this . myGetBoolPref ( "enabled" ) ) {
379+ this . pps . registerFilter ( this , 0 ) ;
380+ OS . addObserver ( this , "cookie-changed" , false ) ;
381+ OS . addObserver ( this , "http-on-examine-response" , false ) ;
382+
383+ var dls = CC [ '@mozilla.org/docloaderservice;1' ]
384+ . getService ( CI . nsIWebProgress ) ;
385+ dls . addProgressListener ( this ,
386+ Ci . nsIWebProgress . NOTIFY_STATE_REQUEST ) ;
387+ this . log ( INFO , "SSL Observatory is now enabled via pref change!" ) ;
388+ } else {
389+ try {
390+ this . pps . unregisterFilter ( this ) ;
391+ OS . removeObserver ( this , "cookie-changed" ) ;
392+ OS . removeObserver ( this , "http-on-examine-response" ) ;
393+
394+ var dls = CC [ '@mozilla.org/docloaderservice;1' ]
395+ . getService ( CI . nsIWebProgress ) ;
396+ dls . removeProgressListener ( this ) ;
397+ this . log ( INFO , "SSL Observatory is now disabled via pref change!" ) ;
398+ } catch ( e ) {
399+ this . log ( WARN , "Removing SSL Observatory observers failed: " + e ) ;
400+ }
401+ }
402+ break ;
403+ }
404+ return ;
370405 }
406+
371407 } ,
372408
373409 submitCertChainForChannel : function ( certchain , channel , warning ) {
@@ -821,6 +857,7 @@ SSLObservatory.prototype = {
821857 if ( req . status == 200 ) {
822858 if ( ! req . responseXML ) {
823859 that . log ( INFO , "Tor check failed: No XML returned by check service." ) ;
860+ that . proxyTestFinished ( ) ;
824861 return ;
825862 }
826863
@@ -845,6 +882,7 @@ SSLObservatory.prototype = {
845882 that . proxy_test_callback ( that . proxy_test_successful ) ;
846883 that . proxy_test_callback = null ;
847884 }
885+ that . proxyTestFinished ( ) ;
848886 }
849887 } ;
850888 req . send ( null ) ;
@@ -858,6 +896,13 @@ SSLObservatory.prototype = {
858896 this . proxy_test_callback ( this . proxy_test_successful ) ;
859897 this . proxy_test_callback = null ;
860898 }
899+ that . proxyTestFinished ( ) ;
900+ }
901+ } ,
902+
903+ proxyTestFinished : function ( ) {
904+ if ( ! this . myGetBoolPref ( "enabled" ) ) {
905+ this . pps . unregisterFilter ( this ) ;
861906 }
862907 } ,
863908
0 commit comments