Index: chrome/common/extensions/docs/examples/extensions/gmail/background.js |
diff --git a/chrome/common/extensions/docs/examples/extensions/gmail/background.js b/chrome/common/extensions/docs/examples/extensions/gmail/background.js |
index 3f2a366a0d270bf47d67e34992a46511923d52e5..d0f389cd87d3b03adc9174939c83b3617456ac05 100644 |
--- a/chrome/common/extensions/docs/examples/extensions/gmail/background.js |
+++ b/chrome/common/extensions/docs/examples/extensions/gmail/background.js |
@@ -87,26 +87,23 @@ LoadingAnimation.prototype.stop = function() { |
this.timerId_ = 0; |
} |
- |
-chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) { |
- if (changeInfo.url && isGmailUrl(changeInfo.url)) { |
- getInboxCount(function(count) { |
- updateUnreadCount(count); |
- }); |
- } |
-}); |
- |
- |
function init() { |
canvas = document.getElementById('canvas'); |
loggedInImage = document.getElementById('logged_in'); |
canvasContext = canvas.getContext('2d'); |
+ updateIcon(); |
+} |
- chrome.browserAction.setBadgeBackgroundColor({color:[208, 0, 24, 255]}); |
- chrome.browserAction.setIcon({path: "gmail_logged_in.png"}); |
- loadingAnimation.start(); |
- |
- startRequest(); |
+function updateIcon() { |
+ if (unreadCount == -1) { |
+ chrome.browserAction.setIcon({path:"gmail_not_logged_in.png"}); |
+ chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]}); |
+ chrome.browserAction.setBadgeText({text:"?"}); |
+ } else { |
+ chrome.browserAction.setIcon({path: "gmail_logged_in.png"}); |
+ chrome.browserAction.setBadgeBackgroundColor({color:[208, 0, 24, 255]}); |
+ chrome.browserAction.setBadgeText({text:unreadCount}); |
+ } |
} |
function scheduleRequest() { |
@@ -119,23 +116,33 @@ function scheduleRequest() { |
} |
// ajax stuff |
-function startRequest(alarm) { |
+function startRequest(params) { |
+ function doCallback() { |
+ if (params && params.scheduleRequest) scheduleRequest(); |
+ } |
+ |
+ function stopLoadingAnimation() { |
+ if (params && params.showLoadingAnimation) loadingAnimation.stop(); |
+ } |
+ |
+ if (params && params.showLoadingAnimation) |
+ loadingAnimation.start(); |
+ |
getInboxCount( |
function(count) { |
- loadingAnimation.stop(); |
+ stopLoadingAnimation(); |
updateUnreadCount(count); |
- scheduleRequest(); |
+ doCallback(); |
}, |
function() { |
- loadingAnimation.stop(); |
- showLoggedOut(); |
- scheduleRequest(); |
+ stopLoadingAnimation(); |
+ unreadCount = -1; |
+ updateIcon(); |
+ doCallback(); |
} |
); |
} |
-chrome.alarms.onAlarm.addListener(startRequest); |
- |
function getInboxCount(onSuccess, onError) { |
var xhr = new XMLHttpRequest(); |
var abortTimerId = window.setTimeout(function() { |
@@ -198,10 +205,11 @@ function gmailNSResolver(prefix) { |
} |
function updateUnreadCount(count) { |
- if (unreadCount != count) { |
- unreadCount = count; |
+ var changed = unreadCount != count; |
+ unreadCount = count; |
+ updateIcon(); |
+ if (changed) |
animateFlip(); |
- } |
} |
@@ -225,13 +233,6 @@ function animateFlip() { |
} |
} |
-function showLoggedOut() { |
- unreadCount = -1; |
- chrome.browserAction.setIcon({path:"gmail_not_logged_in.png"}); |
- chrome.browserAction.setBadgeBackgroundColor({color:[190, 190, 190, 230]}); |
- chrome.browserAction.setBadgeText({text:"?"}); |
-} |
- |
function drawIconAtRotation() { |
canvasContext.save(); |
canvasContext.clearRect(0, 0, canvas.width, canvas.height); |
@@ -260,9 +261,25 @@ function goToInbox() { |
}); |
} |
-// Called when the user clicks on the browser action. |
-chrome.browserAction.onClicked.addListener(function(tab) { |
- goToInbox(); |
+document.addEventListener('DOMContentLoaded', init); |
+ |
+chrome.runtime.onInstalled.addListener(function() { |
+ startRequest({scheduleRequest:true, showLoadingAnimation:true}); |
}); |
-document.addEventListener('DOMContentLoaded', init); |
+chrome.alarms.onAlarm.addListener(function() { |
+ startRequest({scheduleRequest:true, showLoadingAnimation:false}); |
+}); |
+ |
+var filters = { |
+ // TODO(aa): Cannot use urlPrefix because all the url fields lack the protocol |
+ // part. See crbug.com/140238. |
+ url: [{urlContains: getGmailUrl().replace(/^https?\:\/\//, '')}] |
+}; |
+ |
+chrome.webNavigation.onDOMContentLoaded.addListener(function(changeInfo) { |
+ if (changeInfo.url && isGmailUrl(changeInfo.url)) |
+ startRequest({scheduleRequest:false, showLoadingAnimation:false}); |
+}, filters); |
+ |
+chrome.browserAction.onClicked.addListener(goToInbox); |