@@ -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" ]
@@ -361,16 +365,12 @@ SSLObservatory.prototype = {
361365 return ;
362366 }
363367
364- if ( topic == "nsPref:changed" ) {
365- // XXX: We somehow need to only call this once. Right now, we'll make
366- // like 3 calls to getClientASN().. The only thing I can think
367- // of is a timer...
368- if ( data == "network.proxy.ssl" || data == "network.proxy.ssl_port" ||
369- data == "network.proxy.socks" || data == "network.proxy.socks_port" ) {
370- this . log ( INFO , "Proxy settings have changed. Getting new ASN" ) ;
371- this . getClientASN ( ) ;
372- }
373- return ;
368+ if ( "http-on-examine-response" == topic ) {
369+ var channel = subject ;
370+ if ( ! this . observatoryActive ( channel ) ) return ;
371+
372+ var certchain = this . getSSLCertChain ( subject ) ;
373+ this . submitCertChainForChannel ( certchain , channel ) ;
374374 }
375375
376376 if ( topic == "network:offline-status-changed" && data == "online" ) {
@@ -379,14 +379,50 @@ SSLObservatory.prototype = {
379379 return ;
380380 }
381381
382- if ( "http-on-examine-response" == topic ) {
383-
384- var channel = subject ;
385- if ( ! this . observatoryActive ( channel ) ) return ;
386-
387- var certchain = this . getSSLCertChain ( subject ) ;
388- this . submitCertChainForChannel ( certchain , channel ) ;
382+ if ( topic == "nsPref:changed" ) {
383+ // If the user toggles the SSL Observatory settings, we need to add or remove
384+ // our observers
385+ switch ( data ) {
386+ case "network.proxy.ssl" :
387+ case "network.proxy.ssl_port" :
388+ case "network.proxy.socks" :
389+ case "network.proxy.socks_port" :
390+ // XXX: We somehow need to only call this once. Right now, we'll make
391+ // like 3 calls to getClientASN().. The only thing I can think
392+ // of is a timer...
393+ this . log ( INFO , "Proxy settings have changed. Getting new ASN" ) ;
394+ this . getClientASN ( ) ;
395+ break ;
396+ case "extensions.https_everywhere._observatory.enabled" :
397+ if ( this . myGetBoolPref ( "enabled" ) ) {
398+ this . pps . registerFilter ( this , 0 ) ;
399+ OS . addObserver ( this , "cookie-changed" , false ) ;
400+ OS . addObserver ( this , "http-on-examine-response" , false ) ;
401+
402+ var dls = CC [ '@mozilla.org/docloaderservice;1' ]
403+ . getService ( CI . nsIWebProgress ) ;
404+ dls . addProgressListener ( this ,
405+ Ci . nsIWebProgress . NOTIFY_STATE_REQUEST ) ;
406+ this . log ( INFO , "SSL Observatory is now enabled via pref change!" ) ;
407+ } else {
408+ try {
409+ this . pps . unregisterFilter ( this ) ;
410+ OS . removeObserver ( this , "cookie-changed" ) ;
411+ OS . removeObserver ( this , "http-on-examine-response" ) ;
412+
413+ var dls = CC [ '@mozilla.org/docloaderservice;1' ]
414+ . getService ( CI . nsIWebProgress ) ;
415+ dls . removeProgressListener ( this ) ;
416+ this . log ( INFO , "SSL Observatory is now disabled via pref change!" ) ;
417+ } catch ( e ) {
418+ this . log ( WARN , "Removing SSL Observatory observers failed: " + e ) ;
419+ }
420+ }
421+ break ;
422+ }
423+ return ;
389424 }
425+
390426 } ,
391427
392428 submitCertChainForChannel : function ( certchain , channel ) {
@@ -835,6 +871,7 @@ SSLObservatory.prototype = {
835871 if ( req . status == 200 ) {
836872 if ( ! req . responseXML ) {
837873 that . log ( INFO , "Tor check failed: No XML returned by check service." ) ;
874+ that . proxyTestFinished ( ) ;
838875 return ;
839876 }
840877
@@ -859,6 +896,7 @@ SSLObservatory.prototype = {
859896 that . proxy_test_callback ( that . proxy_test_successful ) ;
860897 that . proxy_test_callback = null ;
861898 }
899+ that . proxyTestFinished ( ) ;
862900 }
863901 } ;
864902 req . send ( null ) ;
@@ -872,6 +910,13 @@ SSLObservatory.prototype = {
872910 this . proxy_test_callback ( this . proxy_test_successful ) ;
873911 this . proxy_test_callback = null ;
874912 }
913+ that . proxyTestFinished ( ) ;
914+ }
915+ } ,
916+
917+ proxyTestFinished : function ( ) {
918+ if ( ! this . myGetBoolPref ( "enabled" ) ) {
919+ this . pps . unregisterFilter ( this ) ;
875920 }
876921 } ,
877922
0 commit comments