Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: chrome/ext/background.js

Issue 6089170179063808: Issue 1976 - Handle prerendered tabs on Chrome (Closed)
Patch Set: Created Feb. 8, 2015, 11:47 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/ext/background.js
===================================================================
--- a/chrome/ext/background.js
+++ b/chrome/ext/background.js
@@ -108,62 +108,118 @@
ext.pages.onLoading._dispatch(new Page(tab));
});
+ function forgetTab(tabId)
+ {
+ ext._removeFromAllPageMaps(tabId);
+ delete framesOfTabs[tabId];
+ }
+
chrome.webNavigation.onBeforeNavigate.addListener(function(details)
{
if (details.frameId == 0)
- ext._removeFromAllPageMaps(details.tabId);
+ forgetTab(details.tabId);
});
- chrome.tabs.onRemoved.addListener(function(tabId)
+ chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId)
{
- ext._removeFromAllPageMaps(tabId);
- delete framesOfTabs[tabId];
+ forgetTab(removedTabId);
});
+ chrome.tabs.onRemoved.addListener(forgetTab);
+
/* Browser actions */
var BrowserAction = function(tabId)
{
this._tabId = tabId;
+ this._changes = null;
};
BrowserAction.prototype = {
+ _applyChanges: function()
+ {
+ if ("iconPath" in this._changes)
+ {
+ chrome.browserAction.setIcon({
+ tabId: this._tabId,
+ path: {
+ 19: this._changes.iconPath.replace("$size", "19"),
+ 38: this._changes.iconPath.replace("$size", "38")
+ }
+ });
+ }
+
+ if ("badgeText" in this._changes)
+ {
+ chrome.browserAction.setBadgeText({
+ tabId: this._tabId,
+ text: this._changes.badgeText
+ });
+ }
+
+ if ("badgeColor" in this._changes)
+ {
+ chrome.browserAction.setBadgeBackgroundColor({
+ tabId: this._tabId,
+ color: this._changes.badgeColor
+ });
+ }
+
+ this._changes = null;
+ },
+ _queueChanges: function()
+ {
+ chrome.tabs.get(this._tabId, function()
+ {
+ // If the tab is prerendered, chrome.tabs.get() sets
+ // chrome.runtime.lastError and we have to delay our changes
+ // until the currently visible tab is replaced with the
+ // prerendered tab. Otherwise chrome.browserAction.set* fails.
+ if (chrome.runtime.lastError)
+ {
+ var onReplaced = function(addedTabId, removedTabId)
+ {
+ if (addedTabId == this._tabId)
+ {
+ chrome.tabs.onReplaced.removeListener(onReplaced);
+ this._applyChanges();
kzar 2015/02/09 16:38:40 Woudln't `this` in this context be onReplaced inst
Sebastian Noack 2015/02/09 17:03:24 Mind the .bind() below. ;)
kzar 2015/02/09 17:05:25 Whoops OK, missed that!
+ }
+ }.bind(this);
+ chrome.tabs.onReplaced.addListener(onReplaced);
+ }
+ else
+ {
+ this._applyChanges();
kzar 2015/02/09 16:38:40 Nit: Don't really need the braces for the else cla
Sebastian Noack 2015/02/09 17:03:24 Some time ago Wladimir told me to add braces for t
kzar 2015/02/09 17:05:25 Fair enough
+ }
+ }.bind(this));
+ },
+ _addChange: function(name, value)
+ {
+ if (!this._changes)
+ {
+ this._changes = {};
+ this._queueChanges();
+ }
+
+ this._changes[name] = value;
+ },
setIcon: function(path)
{
- var paths = {};
- for (var i = 1; i <= 2; i++)
- {
- var size = i * 19;
- paths[size] = path.replace("$size", size);
- }
-
- chrome.browserAction.setIcon({tabId: this._tabId, path: paths});
+ this._addChange("iconPath", path);
},
setBadge: function(badge)
{
if (!badge)
{
- chrome.browserAction.setBadgeText({
- tabId: this._tabId,
- text: ""
- });
- return;
+ this._addChange("badgeText", "");
}
+ else
+ {
+ if ("number" in badge)
+ this._addChange("badgeText", badge.number.toString());
- if ("color" in badge)
- {
- chrome.browserAction.setBadgeBackgroundColor({
- tabId: this._tabId,
- color: badge.color
- });
- }
-
- if ("number" in badge)
- {
- chrome.browserAction.setBadgeText({
- tabId: this._tabId,
- text: badge.number.toString()
- });
+ if ("color" in badge)
+ this._addChange("badgeColor", badge.color);
}
}
};
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld