From 05c8b0e46748122d9b2e01ac245e23a4b7924ba6 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sun, 25 Jun 2006 20:14:11 +0000 Subject: [PATCH] Fixes #60, make sure it works well offline - Added detection for network failure -- debug message is output and noNetwork property is added to the xmlhttp object - Removed onStatus callback from HTTP.doGet and HTTP.doPost -- that was copied over from the Piggy Bank API, but the onDone callback has to handle errors anyway, so it can just check the status code if it actually cares to differentiate non-200 status codes from any other error - Added error handling for empty responseXML to Schema._updateScrapersRemoteCallback - Renamed SCHOLAR_CONFIG['REPOSITORY_CHECK_RETRY'] to SCHOLAR_CONFIG['REPOSITORY_RETRY_INTERVAL'] --- .../content/scholar/xpcom/schema.js | 13 ++++-- .../content/scholar/xpcom/scholar.js | 42 +++++++++---------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/chrome/chromeFiles/content/scholar/xpcom/schema.js b/chrome/chromeFiles/content/scholar/xpcom/schema.js index 79b0d199b..8f34c69cc 100644 --- a/chrome/chromeFiles/content/scholar/xpcom/schema.js +++ b/chrome/chromeFiles/content/scholar/xpcom/schema.js @@ -80,12 +80,12 @@ Scholar.Schema = new function(){ + 'version=' + Scholar.version; Scholar.debug('Checking repository for updates (' + url + ')'); - var get = Scholar.HTTP.doGet(url, false, _updateScrapersRemoteCallback); + var get = Scholar.HTTP.doGet(url, _updateScrapersRemoteCallback); // TODO: instead, add an observer to start and stop timer on online state change if (!get){ Scholar.debug('Browser is offline -- skipping check'); - _setRepositoryTimer(SCHOLAR_CONFIG['REPOSITORY_CHECK_RETRY']); + _setRepositoryTimer(SCHOLAR_CONFIG['REPOSITORY_RETRY_INTERVAL']); } } @@ -279,7 +279,14 @@ Scholar.Schema = new function(){ * Process the response from the repository **/ function _updateScrapersRemoteCallback(xmlhttp){ - // TODO: error handling + if (!xmlhttp.responseXML){ + if (!xmlhttp.noNetwork){ + Scholar.debug('Invalid response from repository', 2); + } + _setRepositoryTimer(SCHOLAR_CONFIG['REPOSITORY_RETRY_INTERVAL']); + return false; + } + var currentTime = xmlhttp.responseXML. getElementsByTagName('currentTime')[0].firstChild.nodeValue; var updates = xmlhttp.responseXML.getElementsByTagName('scraper'); diff --git a/chrome/chromeFiles/content/scholar/xpcom/scholar.js b/chrome/chromeFiles/content/scholar/xpcom/scholar.js index cc84708df..69c0d2b4f 100644 --- a/chrome/chromeFiles/content/scholar/xpcom/scholar.js +++ b/chrome/chromeFiles/content/scholar/xpcom/scholar.js @@ -6,7 +6,7 @@ const SCHOLAR_CONFIG = { DEBUG_TO_CONSOLE: true, // dump debug messages to console rather than (much slower) Debug Logger REPOSITORY_URL: 'http://chnm.gmu.edu/firefoxscholar/repo', REPOSITORY_CHECK_INTERVAL: 3600, // temp -- 86400, // 24 hours - REPOSITORY_CHECK_RETRY: 3600 // 1 hour + REPOSITORY_RETRY_INTERVAL: 3600 // 1 hour }; /* @@ -466,19 +466,20 @@ Scholar.HTTP = new function(){ * * Returns false if browser is offline **/ - function doGet(url, onStatus, onDone){ + function doGet(url, onDone){ if (this.browserIsOffline()){ return false; } var xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] - .createInstance(); + .createInstance(); - xmlhttp.open('GET', url, true); + var test = xmlhttp.open('GET', url, true); xmlhttp.onreadystatechange = function(){ - _stateChange(xmlhttp, onStatus, onDone); + _stateChange(xmlhttp, onDone); }; + xmlhttp.send(null); return true; @@ -490,7 +491,7 @@ Scholar.HTTP = new function(){ * * Returns false if browser is offline **/ - function doPost(url, body, onStatus, onDone){ + function doPost(url, body, onDone){ if (this.browserIsOffline()){ return false; } @@ -501,8 +502,9 @@ Scholar.HTTP = new function(){ xmlhttp.open('POST', url, true); xmlhttp.onreadystatechange = function(){ - _stateChange(xmlhttp, onStatus, onDone); + _stateChange(xmlhttp, onDone); }; + xmlhttp.send(body); return true; @@ -515,30 +517,28 @@ Scholar.HTTP = new function(){ } - function _stateChange(xmlhttp, onStatus, onDone){ + function _stateChange(xmlhttp, onDone){ switch (xmlhttp.readyState){ - // Request not yet made case 1: break; // Contact established with server but nothing downloaded yet case 2: + // Accessing status will throw an exception if no network connection try { - // Check for HTTP status 200 - if (xmlhttp.status != 200){ - if (onStatus) { - onStatus( - xmlhttp.status, - xmlhttp.statusText, - xmlhttp - ); - xmlhttp.abort(); - } - } + xmlhttp.status; } catch (e){ - Scholar.debug(e, 2); + Scholar.debug('No network connection'); + xmlhttp.noNetwork = true; + return false; + } + + // Check for HTTP status 200 + if (xmlhttp.status != 200){ + Scholar.debug('XMLHTTPRequest received HTTP response code ' + + xmlhttp.status); } break;