Reviewers: jasvir,
Description:
the clock demo works fine, but if you run something after running
the clock demo, the js console starts spitting out errors every
second, because the clock demo set an interval handler that's still
firing every second.
afaict, there's no security implication to this leak, since the
interval handler is as sandboxed as it would be if it were just
one gadget on a page full of gadgets, but the unintentional
resource leak is annoying, and maybe caja should automatically
cancel timers when a gadget is unloaded.
in the meantime, this change makes the clock demo cancel the
interval handler when it notices it's been unloaded.
Please review this at http://codereview.appspot.com/4306048/
Affected files:
M src/com/google/caja/demos/playground/examples/clock.html
D src/com/google/caja/demos/playground/setup-valija.js
Index: src/com/google/caja/demos/playground/setup-valija.js
===================================================================
--- src/com/google/caja/demos/playground/setup-valija.js (revision 4403)
+++ src/com/google/caja/demos/playground/setup-valija.js (working copy)
@@ -1,5 +0,0 @@
-(function(){
- var imports = ___.getNewModuleHandler().getImports();
- imports.loader = {provide:___.markFuncFreeze(function(v){valijaMaker =
v;})};
- imports.outers = imports;
-})();
Index: src/com/google/caja/demos/playground/examples/clock.html
===================================================================
--- src/com/google/caja/demos/playground/examples/clock.html (revision 4403)
+++ src/com/google/caja/demos/playground/examples/clock.html (working copy)
@@ -1,13 +1,20 @@
<html>
<head>
<script type="application/javascript">
- function init(){
+var intervalId;
+function init(){
clock();
- setInterval(clock,1000);
+ intervalId = setInterval(clock, 1000);
}
function clock(){
+ // no notification when we get unloaded/replaced, so check here
+ var canv = document.getElementById('canvas');
+ if (!canv) {
+ clearInterval(intervalId);
+ return;
+ }
var now = new Date();
- var ctx = document.getElementById('canvas').getContext('2d');
+ var ctx = canv.getContext('2d');
ctx.save();
ctx.clearRect(0,0,150,150);
ctx.translate(75,75);