[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [torbutton/master] Bug 4718: Perform version check periodically in the background



commit 17a284d4444de6bad842809a57322fc3f8cec493
Author: Mike Perry <mikeperry-git@xxxxxxxxxx>
Date:   Wed May 23 18:15:06 2012 -0700

    Bug 4718: Perform version check periodically in the background
    
    We check every 12 hours.
    
    I also made the update check at startup async as part of this.
---
 src/chrome/content/torbutton.js       |   69 ++++++++++++++++++++++++++++++++-
 src/components/cookie-jar-selector.js |   22 ++++++++++
 2 files changed, 89 insertions(+), 2 deletions(-)

diff --git a/src/chrome/content/torbutton.js b/src/chrome/content/torbutton.js
index 653a777..559a754 100644
--- a/src/chrome/content/torbutton.js
+++ b/src/chrome/content/torbutton.js
@@ -885,6 +885,71 @@ function torbutton_restore_nontor_settings()
   torbutton_log(2, 'settings restored');
 }
 
+function torbutton_do_async_versioncheck() {
+  if (!m_tb_tbb || !m_tb_prefs.getBoolPref("extensions.torbutton.versioncheck_enabled")) {
+    return;
+  }
+  torbutton_log(3, "Checking version");
+  try {
+    var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
+                            .createInstance(Components.interfaces.nsIXMLHttpRequest);
+    //var req = new XMLHttpRequest(); Blocked by content policy
+    var url = m_tb_prefs.getCharPref("extensions.torbutton.versioncheck_url");
+    req.open('GET', url, true);
+    req.channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
+    req.overrideMimeType("text/json");
+    req.onreadystatechange = function (oEvent) {  
+      if (req.readyState === 4) {
+        if(req.status == 200) {
+          if(!req.responseText) {
+            torbutton_log(5, "Version check failed! No JSON present!");
+            return -1;
+          }
+          try {
+            var locale = m_tb_prefs.getCharPref("general.useragent.locale");
+            var version_list = JSON.parse(req.responseText);
+            var my_version = m_tb_prefs.getCharPref("torbrowser.version");
+            for (var v in version_list) {
+              if (version_list[v] == my_version) {
+                torbutton_log(3, "Version check passed.");
+                var homepage = m_tb_prefs.getCharPref("browser.startup.homepage");
+                if (homepage.indexOf("https://check.torproject.org/";) == 0) {
+                  m_tb_prefs.setCharPref("browser.startup.homepage",
+                      "https://check.torproject.org/?lang="+locale+"&small=1&uptodate=1";);
+                }
+                return;
+              }
+            }
+            torbutton_log(5, "Your Tor Browser is out of date.");
+            // Not up to date
+            m_tb_prefs.setCharPref("browser.startup.homepage",
+                "https://check.torproject.org/?lang="+locale+"&small=1&uptodate=0";);
+            return;
+          } catch(e) {
+            torbutton_log(5, "Version check failed! JSON parsing error: "+e);
+            return;
+          }
+        } else if (req.status == 404) {
+          // We're going to assume 404 means the service is not implemented yet.
+          torbutton_log(3, "Version check failed. Versions file is 404.");
+          return -1;
+        }
+        torbutton_log(5, "Version check failed! Web server error: "+req.status);
+        return -1;
+      }  
+    };  
+    req.send(null);
+  } catch(e) {
+    if(e.result == 0x80004005) { // NS_ERROR_FAILURE
+      torbutton_log(5, "Version check failed! Is tor running?");
+      return -1;
+    }
+    torbutton_log(5, "Version check failed! Tor internal error: "+e);
+    return -1;
+  }
+
+}
+
 function torbutton_check_version() {
   torbutton_log(3, "Checking version");
   try {
@@ -2967,7 +3032,7 @@ function torbutton_set_launch_state(state, session_restore) {
         torbutton_enable_tor(true);
         torbutton_log(3, "Tor state updated.");
 
-        torbutton_do_versioncheck();
+        torbutton_do_async_versioncheck();
         
         // Load our homepage again. We just killed it via the toggle.
         if (!session_restore) {
@@ -2982,7 +3047,7 @@ function torbutton_set_launch_state(state, session_restore) {
         if(state) torbutton_enable_tor(true);
         else  torbutton_disable_tor();
         
-        torbutton_do_versioncheck();
+        torbutton_do_async_versioncheck();
 
         // Load our homepage again. We just killed it via the toggle.
         if (!session_restore) {
diff --git a/src/components/cookie-jar-selector.js b/src/components/cookie-jar-selector.js
index e26493f..51e4645 100644
--- a/src/components/cookie-jar-selector.js
+++ b/src/components/cookie-jar-selector.js
@@ -494,6 +494,7 @@ function CookieJarSelector() {
   var jarThis = this;
   this.timerCallback = {
     cookie_changed: false,
+    update_counter: 0,
 
     QueryInterface: function(iid) {
        if (!iid.equals(Component.interfaces.nsISupports) &&
@@ -504,6 +505,27 @@ function CookieJarSelector() {
        return this;
     },
     notify: function() {
+       // XXX: ========== WARNING! Ultra-mega hack: ===============
+       // For various reasons, we want to totally refactor Torbutton.
+       // (see #5709). Since there's no great place to place this timer,
+       // I figured we might as well place it here.
+       //
+       // Additionally, mobile should verify that nsITimer.TYPE_REPEATING_SLACK
+       // is smart wrt suspend.
+       //
+       // We want to perform an update check *about* twice per day.
+       // I thought about randomizing it, but the slack will probably
+       // introduce enough noise over the course of 12 hours or so...
+       // Might as well KISS.
+       if (++this.update_counter > 720) {
+           jarThis.logger.log(3, "Performing async version check");
+           this.update_counter = 0;
+           var wm = Cc["@mozilla.org/appshell/window-mediator;1"]
+               .getService(Components.interfaces.nsIWindowMediator);
+           var chrome = wm.getMostRecentWindow("navigator:browser");
+           chrome.torbutton_do_async_versioncheck();
+       }
+
        // this refers to timerCallback object. use jarThis to reference
        // CookieJarSelector object.
        if(!this.cookie_changed) {



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits