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

Delta Between Two Patch Sets: chrome/ext/background.js

Issue 5133931611422720: Simplified and removed memory leaks from code dealing with frames on Chrome (Closed)
Left Patch Set: Created Feb. 25, 2014, 5:52 p.m.
Right Patch Set: Addressed comment Created March 31, 2014, 3:43 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | popupBlocker.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2014 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 } 190 }
191 }; 191 };
192 192
193 193
194 /* Web requests */ 194 /* Web requests */
195 195
196 var framesOfTabs = {__proto__: null}; 196 var framesOfTabs = {__proto__: null};
197 197
198 ext.getFrame = function(tabId, frameId) 198 ext.getFrame = function(tabId, frameId)
199 { 199 {
200 return framesOfTabs[tabId][frameId]; 200 return (framesOfTabs[tabId] || {})[frameId];
Wladimir Palant 2014/03/06 15:32:20 What about tabs we don't know?
Sebastian Noack 2014/03/06 19:54:49 Done.
201 }; 201 };
202 202
203 ext.webRequest = { 203 ext.webRequest = {
204 onBeforeRequest: new ext._EventTarget(true), 204 onBeforeRequest: new ext._EventTarget(true),
205 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged 205 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged
206 }; 206 };
207 207
208 chrome.tabs.query({}, function(tabs) 208 chrome.tabs.query({}, function(tabs)
209 { 209 {
210 // unfortunatelly we can't retrieve frames already loaded. 210 tabs.forEach(function(tab)
Sebastian Noack 2014/03/06 19:54:49 Cool. Done.
211 // But we can retrieve the top level url of every tab and 211 {
212 // use it as fallback when we encounter an unknown frame. 212 chrome.webNavigation.getAllFrames({tabId: tab.id}, function(details)
213 for (var i = 0; i < tabs.length; i++) 213 {
Wladimir Palant 2014/03/06 15:32:20 Nit: Please always put brackets around multiline b
214 framesOfTabs[tabs[i].id] = { 214 if (details && details.length > 0)
215 __proto__: null, 215 {
216 216 var frames = framesOfTabs[tab.id] = {__proto__: null};
217 0: { 217
218 url: tabs[i].url, 218 for (var i = 0; i < details.length; i++)
219 parent: null 219 frames[details[i].frameId] = {url: details[i].url, parent: null};
220 } 220
221 }; 221 for (var i = 0; i < details.length; i++)
222 222 {
223 chrome.webRequest.onBeforeRequest.addListener(function(details) 223 var parentFrameId = details[i].parentFrameId;
224 { 224
225 if (details.tabId == -1) 225 if (parentFrameId != -1)
226 return; 226 frames[details[i].frameId].parent = frames[parentFrameId];
227 227 }
228 var isMainFrame = details.type == "main_frame" || ( 228 }
229 // assume that the first request belongs to the top frame. Chrome 229 });
230 // may give the top frame the type "object" instead of "main_frame". 230 });
231 // https://code.google.com/p/chromium/issues/detail?id=281711 231 });
232 details.frameId == 0 && !(details.tabId in framesOfTabs) 232
233 ); 233 chrome.webRequest.onBeforeRequest.addListener(function(details)
234 234 {
235 var frames; 235 if (details.tabId == -1)
236 var frame; 236 return;
237 if (isMainFrame) 237
238 { 238 var isMainFrame = details.type == "main_frame" || (
239 frames = framesOfTabs[details.tabId] = {__proto__: null}; 239 // assume that the first request belongs to the top frame. Chrome
240 frame = null; 240 // may give the top frame the type "object" instead of "main_frame".
241 } 241 // https://code.google.com/p/chromium/issues/detail?id=281711
242 details.frameId == 0 && !(details.tabId in framesOfTabs)
243 );
244
245 var frames = null;
246 if (!isMainFrame)
247 frames = framesOfTabs[details.tabId];
248 if (!frames)
249 frames = framesOfTabs[details.tabId] = {__proto__: null};
250
251 var frame = null;
252 if (!isMainFrame)
253 {
254 // we are looking for the frame that contains the element that
255 // is about to load, however if a frame is loading the surrounding
256 // frame is indicated by parentFrameId instead of frameId
257 var frameId;
258 if (details.type == "sub_frame")
259 frameId = details.parentFrameId;
242 else 260 else
243 { 261 frameId = details.frameId;
244 // we are looking for the frame that contains the element that 262
245 // is about to load, however if a frame is loading the surrounding 263 frame = frames[frameId] || frames[0];
246 // frame is indicated by parentFrameId instead of frameId 264
247 var frameId; 265 if (frame && !ext.webRequest.onBeforeRequest._dispatch(details.url, detail s.type, new Page({id: details.tabId}), frame))
248 if (details.type == "sub_frame") 266 return {cancel: true};
249 frameId = details.parentFrameId; 267 }
250 else 268
251 frameId = details.frameId; 269 if (isMainFrame || details.type == "sub_frame")
252 270 frames[details.frameId] = {url: details.url, parent: frame};
253 frames = framesOfTabs[details.tabId]; 271 }, {urls: ["<all_urls>"]}, ["blocking"]);
254 frame = frames[frameId] || frames[0];
255
256 if (!ext.webRequest.onBeforeRequest._dispatch(details.url, details.type, new Page({id: details.tabId}), frame))
257 return {cancel: true};
258 }
259
260 if (isMainFrame || details.type == "sub_frame")
261 frames[details.frameId] = {url: details.url, parent: frame};
262 }, {urls: ["<all_urls>"]}, ["blocking"]);
263
264
265 /* Message passing */
266
267 ext._setupMessageListener(function(sender)
268 {
269 return {
270 page: new Page(sender.tab),
271 frame: {
272 url: sender.url,
273 get parent()
274 {
275 var frames = framesOfTabs[sender.tab.id];
276
277 for (var frameId in frames)
278 {
279 if (frames[frameId].url == sender.url)
280 return frames[frameId].parent;
281 }
282
283 return frames[0];
284 }
285 }
286 };
287 });
288 });
289 272
290 273
291 /* Context menus */ 274 /* Context menus */
292 275
293 var contextMenuItems = []; 276 var contextMenuItems = [];
294 var isContextMenuHidden = true; 277 var isContextMenuHidden = true;
295 278
296 ext.contextMenus = { 279 ext.contextMenus = {
297 addMenuItem: function(title, contexts, onclick) 280 addMenuItem: function(title, contexts, onclick)
298 { 281 {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 }, 315 },
333 hideMenuItems: function() 316 hideMenuItems: function()
334 { 317 {
335 if (isContextMenuHidden) 318 if (isContextMenuHidden)
336 return; 319 return;
337 320
338 chrome.contextMenus.removeAll(); 321 chrome.contextMenus.removeAll();
339 isContextMenuHidden = true; 322 isContextMenuHidden = true;
340 } 323 }
341 }; 324 };
325
326
327 /* Message passing */
328
329 ext._setupMessageListener(function(sender)
330 {
331 return {
332 page: new Page(sender.tab),
333 frame: {
334 url: sender.url,
335 get parent()
336 {
337 var frames = framesOfTabs[sender.tab.id];
338
339 if (!frames)
340 return null;
341
342 for (var frameId in frames)
343 {
344 if (frames[frameId].url == sender.url)
345 return frames[frameId].parent;
346 }
347
348 return frames[0];
349 }
350 }
351 };
352 });
353
354
355 /* Storage */
356
357 ext.storage = localStorage;
342 })(); 358 })();
LEFTRIGHT
« no previous file | popupBlocker.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld