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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 }, 101 },
102 onLoading: new ext._EventTarget() 102 onLoading: new ext._EventTarget()
103 }; 103 };
104 104
105 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 105 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
106 { 106 {
107 if (changeInfo.status == "loading") 107 if (changeInfo.status == "loading")
108 ext.pages.onLoading._dispatch(new Page(tab)); 108 ext.pages.onLoading._dispatch(new Page(tab));
109 }); 109 });
110 110
111 function forgetTab(tabId)
112 {
113 ext._removeFromAllPageMaps(tabId);
114 delete framesOfTabs[tabId];
115 }
116
111 chrome.webNavigation.onBeforeNavigate.addListener(function(details) 117 chrome.webNavigation.onBeforeNavigate.addListener(function(details)
112 { 118 {
113 if (details.frameId == 0) 119 if (details.frameId == 0)
114 ext._removeFromAllPageMaps(details.tabId); 120 forgetTab(details.tabId);
115 }); 121 });
116 122
117 chrome.tabs.onRemoved.addListener(function(tabId) 123 chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId)
118 { 124 {
119 ext._removeFromAllPageMaps(tabId); 125 forgetTab(removedTabId);
120 delete framesOfTabs[tabId];
121 }); 126 });
122 127
128 chrome.tabs.onRemoved.addListener(forgetTab);
129
123 130
124 /* Browser actions */ 131 /* Browser actions */
125 132
126 var BrowserAction = function(tabId) 133 var BrowserAction = function(tabId)
127 { 134 {
128 this._tabId = tabId; 135 this._tabId = tabId;
136 this._changes = null;
129 }; 137 };
130 BrowserAction.prototype = { 138 BrowserAction.prototype = {
139 _applyChanges: function()
140 {
141 if ("iconPath" in this._changes)
142 {
143 chrome.browserAction.setIcon({
144 tabId: this._tabId,
145 path: {
146 19: this._changes.iconPath.replace("$size", "19"),
147 38: this._changes.iconPath.replace("$size", "38")
148 }
149 });
150 }
151
152 if ("badgeText" in this._changes)
153 {
154 chrome.browserAction.setBadgeText({
155 tabId: this._tabId,
156 text: this._changes.badgeText
157 });
158 }
159
160 if ("badgeColor" in this._changes)
161 {
162 chrome.browserAction.setBadgeBackgroundColor({
163 tabId: this._tabId,
164 color: this._changes.badgeColor
165 });
166 }
167
168 this._changes = null;
169 },
170 _queueChanges: function()
171 {
172 chrome.tabs.get(this._tabId, function()
173 {
174 // If the tab is prerendered, chrome.tabs.get() sets
175 // chrome.runtime.lastError and we have to delay our changes
176 // until the currently visible tab is replaced with the
177 // prerendered tab. Otherwise chrome.browserAction.set* fails.
178 if (chrome.runtime.lastError)
179 {
180 var onReplaced = function(addedTabId, removedTabId)
181 {
182 if (addedTabId == this._tabId)
183 {
184 chrome.tabs.onReplaced.removeListener(onReplaced);
185 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!
186 }
187 }.bind(this);
188 chrome.tabs.onReplaced.addListener(onReplaced);
189 }
190 else
191 {
192 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
193 }
194 }.bind(this));
195 },
196 _addChange: function(name, value)
197 {
198 if (!this._changes)
199 {
200 this._changes = {};
201 this._queueChanges();
202 }
203
204 this._changes[name] = value;
205 },
131 setIcon: function(path) 206 setIcon: function(path)
132 { 207 {
133 var paths = {}; 208 this._addChange("iconPath", path);
134 for (var i = 1; i <= 2; i++)
135 {
136 var size = i * 19;
137 paths[size] = path.replace("$size", size);
138 }
139
140 chrome.browserAction.setIcon({tabId: this._tabId, path: paths});
141 }, 209 },
142 setBadge: function(badge) 210 setBadge: function(badge)
143 { 211 {
144 if (!badge) 212 if (!badge)
145 { 213 {
146 chrome.browserAction.setBadgeText({ 214 this._addChange("badgeText", "");
147 tabId: this._tabId,
148 text: ""
149 });
150 return;
151 } 215 }
216 else
217 {
218 if ("number" in badge)
219 this._addChange("badgeText", badge.number.toString());
152 220
153 if ("color" in badge) 221 if ("color" in badge)
154 { 222 this._addChange("badgeColor", badge.color);
155 chrome.browserAction.setBadgeBackgroundColor({
156 tabId: this._tabId,
157 color: badge.color
158 });
159 }
160
161 if ("number" in badge)
162 {
163 chrome.browserAction.setBadgeText({
164 tabId: this._tabId,
165 text: badge.number.toString()
166 });
167 } 223 }
168 } 224 }
169 }; 225 };
170 226
171 227
172 /* Context menus */ 228 /* Context menus */
173 229
174 var contextMenuItems = new ext.PageMap(); 230 var contextMenuItems = new ext.PageMap();
175 var contextMenuUpdating = false; 231 var contextMenuUpdating = false;
176 232
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 callback(new Page(tab)); 492 callback(new Page(tab));
437 } 493 }
438 else 494 else
439 { 495 {
440 ext.pages.open(optionsUrl, callback); 496 ext.pages.open(optionsUrl, callback);
441 } 497 }
442 }); 498 });
443 }); 499 });
444 }; 500 };
445 })(); 501 })();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld