diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-20 20:24:35 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-20 20:24:35 +0000 |
commit | 79b0f6c1ee5e076fa79d612a8c5cff711e9a5821 (patch) | |
tree | eee1af28a171b868eb24dd280ef1e7e5180d4639 /chrome/test/data | |
parent | e1c8a25d900eaa46bb84462896c91cb3905dd9f0 (diff) | |
download | chromium_src-79b0f6c1ee5e076fa79d612a8c5cff711e9a5821.zip chromium_src-79b0f6c1ee5e076fa79d612a8c5cff711e9a5821.tar.gz chromium_src-79b0f6c1ee5e076fa79d612a8c5cff711e9a5821.tar.bz2 |
Attempting to make the Gmail checker more robust in the face of errors.
The heart of this is that I wrapped up the request to the feed in a timeout and a bunch more error handling and abstracted all that into one function so I could be sure all the errors were getting handled the same way.
Also general cleaning while I was in there.
BUG=11969
TEST=Install Gmail Checker, go offline, go back online. It should start showing message counts again when you go back online.
Review URL: http://codereview.chromium.org/115584
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16522 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/data')
-rw-r--r-- | chrome/test/data/extensions/samples/gmail/gmail_checker.html | 138 |
1 files changed, 91 insertions, 47 deletions
diff --git a/chrome/test/data/extensions/samples/gmail/gmail_checker.html b/chrome/test/data/extensions/samples/gmail/gmail_checker.html index 7f729b1..0f89b33 100644 --- a/chrome/test/data/extensions/samples/gmail/gmail_checker.html +++ b/chrome/test/data/extensions/samples/gmail/gmail_checker.html @@ -2,100 +2,144 @@ <head> <link rel="stylesheet" type="text/css" href="styles.css"> <script> -gmail_atom_href = "http://mail.google.com/mail/feed/atom"; -var poll_timeout = 1000 * 60; // 1 minute +var gmail = "http://mail.google.com/"; +var gmailAtomRef = "http://mail.google.com/mail/feed/atom"; +var pollInterval = 1000 * 60; // 1 minute +var requestTimeout = 1000 * 5; // 5 seconds var unreadCount; -var debug = true; -function gmailNSResolver(prefix) { - if(prefix == 'gmail') { - return 'http://purl.org/atom/ns#'; - } +function init() { + window.setTimeout(startRequest, 0); } -function startFlip() { - document.getElementById("notLoggedIn").style.display = "none"; - document.getElementById("loggedIn").style.display = ""; - document.getElementById("unreadCount").className = 'mid-flip'; - setTimeout("midFlip();", 500); +function scheduleRequest() { + window.setTimeout(startRequest, pollInterval); } -function midFlip() { - document.getElementById("unreadCount").className = 'post-flip'; - document.getElementById("unreadCount").innerHTML = "(" + unreadCount + ")"; - setTimeout("endFlip();", 500); +// ajax stuff +function startRequest() { + getInboxCount( + function(count) { + toggleLoggedInState(); + updateUnreadCount(count); + scheduleRequest(); + }, + function() { + toggleLoggedOutState(); + scheduleRequest(); + } + ); } -function endFlip() { - document.getElementById("unreadCount").className = 'base-flip'; -} +function getInboxCount(onSuccess, onError) { + var xhr = new XMLHttpRequest(); + var abortTimerId = window.setTimeout(function() { + xhr.abort(); + onError(); + }, requestTimeout); -function updateUnreadCount(count) { - if (unreadCount != count) { - unreadCount = count; - startFlip(); + function handleSuccess(count) { + window.clearTimeout(abortTimerId); + onSuccess(count); + } + + function handleError() { + window.clearTimeout(abortTimerId); + onError(); } -} -function requestUnreadFeed() { - var xhr = new XMLHttpRequest(); try { console.log("request.."); xhr.onreadystatechange = function(){ console.log("readystate: " + xhr.readyState); if (xhr.readyState == 4) { + console.log("responseText: " + xhr.responseText.substring(0, 200) + + "..."); if (xhr.responseXML) { - window.clearTimeout(timerId); - - console.log("response.."); var xmlDoc = xhr.responseXML; var fullCountSet = xmlDoc.evaluate("/gmail:feed/gmail:fullcount", xmlDoc, gmailNSResolver, XPathResult.ANY_TYPE, null); var fullCountNode = fullCountSet.iterateNext(); if (fullCountNode) { - updateUnreadCount(fullCountNode.textContent); + handleSuccess(fullCountNode.textContent); } else { console.log("fullcount not found!"); - console.error("Error: feed retrieved, but no <fullcount> node found"); + console.error("Error: feed retrieved, but no <fullcount> node " + + "found"); + handleError(); } + } else { + console.log("No responseXML!"); } - - window.setTimeout(requestUnreadFeed, poll_timeout); } } xhr.onerror = function(error) { - console.log("error: " + error); - window.setTimeout(requestUnreadFeed, poll_timeout); + console.log("error"); + console.log(error); + handleError(); } - xhr.open("GET", gmail_atom_href, true); + xhr.open("GET", gmailAtomRef, true); xhr.send(null); - - // Give up after 5 seconds - var timerId = window.setTimeout(function() { - document.getElementById("loggedIn").style.display = "none"; - document.getElementById("notLoggedIn").style.display = ""; - xhr.abort(); - }, 5000); } catch(e) { console.log("ex: " + e); console.error("exception: " + e); - window.setTimeout(requestUnreadFeed, poll_timeout); + handleError(); + } +} + +function gmailNSResolver(prefix) { + if(prefix == 'gmail') { + return 'http://purl.org/atom/ns#'; + } +} + +// ui stuff +function toggleLoggedInState() { + document.getElementById("notLoggedIn").style.display = "none"; + document.getElementById("loggedIn").style.display = ""; +} + +function toggleLoggedOutState() { + document.getElementById("loggedIn").style.display = "none"; + document.getElementById("notLoggedIn").style.display = ""; +} + +function updateUnreadCount(count) { + if (unreadCount != count) { + unreadCount = count; + startFlip(); } } function goToInbox() { - chrome.tabs.create({url:"http://www.gmail.com/"}); + chrome.tabs.create({url: gmail}); +} + +// animation +function startFlip() { + document.getElementById("unreadCount").className = 'mid-flip'; + setTimeout(midFlip, 500); +} + +function midFlip() { + document.getElementById("unreadCount").className = 'post-flip'; + document.getElementById("unreadCount").innerHTML = "(" + unreadCount + ")"; + setTimeout(endFlip, 500); +} + +function endFlip() { + document.getElementById("unreadCount").className = 'base-flip'; } </script> </head> -<body onload="window.setTimeout(requestUnreadFeed, 0)" onclick="goToInbox()"> +<body onload="init()" onclick="goToInbox()"> <div class="toolstrip-button"> <img src="gmail.png" style="width:auto; height:auto"> <span id="notLoggedIn" style="display:none;">Login</span> - <span id="loggedIn">Gmail - Inbox <span style="display: inline-block;" id="unreadCount" class="base-flip" onclick="startFlip();"></span></span> + <span id="loggedIn">Gmail - Inbox <span style="display: inline-block;" id="unreadCount" class="base-flip"></span></span> </div> </body> </html> |