Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | <!DOCTYPE html> |
michael@0 | 2 | <html> |
michael@0 | 3 | <head> |
michael@0 | 4 | <title>postMessage structured clone page</title> |
michael@0 | 5 | <script type="application/javascript;version=1.7" |
michael@0 | 6 | src="postMessage_structured_clone_helper.js"></script> |
michael@0 | 7 | <script type="application/javascript;version=1.7"> |
michael@0 | 8 | var generator = new getTestContent() |
michael@0 | 9 | |
michael@0 | 10 | function isClone(a, b) { |
michael@0 | 11 | window.dump("Object a: " + a + "\n"); |
michael@0 | 12 | window.dump("Object b: " + b + "\n"); |
michael@0 | 13 | var stack = [[a, b]]; |
michael@0 | 14 | var memory = new WeakMap(); |
michael@0 | 15 | var rmemory = new WeakMap(); |
michael@0 | 16 | |
michael@0 | 17 | while (stack.length > 0) { |
michael@0 | 18 | var pair = stack.pop(); |
michael@0 | 19 | var x = pair[0], y = pair[1]; |
michael@0 | 20 | if (typeof x !== "object" || x === null) { |
michael@0 | 21 | // x is primitive. |
michael@0 | 22 | if (x !== y) { |
michael@0 | 23 | window.dump("Primitives not equal!\n"); |
michael@0 | 24 | return false; |
michael@0 | 25 | } |
michael@0 | 26 | } else if (x instanceof Date) { |
michael@0 | 27 | if (x.getTime() == y.getTime()) |
michael@0 | 28 | return true; |
michael@0 | 29 | window.dump("Dates not equal!\n"); |
michael@0 | 30 | return false; |
michael@0 | 31 | } else if (memory.has(x)) { |
michael@0 | 32 | // x is an object we have seen before in a. |
michael@0 | 33 | if (y !== memory.get(x)) { |
michael@0 | 34 | window.dump("Already seen!?\n"); |
michael@0 | 35 | return false; |
michael@0 | 36 | } |
michael@0 | 37 | if (!(rmemory.get(y) == x)) { |
michael@0 | 38 | window.dump("Not equal??\n"); |
michael@0 | 39 | return false; |
michael@0 | 40 | } |
michael@0 | 41 | } else { |
michael@0 | 42 | // x is an object we have not seen before. |
michael@0 | 43 | // Check that we have not seen y before either. |
michael@0 | 44 | if (rmemory.has(y)) { |
michael@0 | 45 | // If we have seen y before, the only possible outcome |
michael@0 | 46 | // is that x and y are literally the same object. |
michael@0 | 47 | if (y == x) |
michael@0 | 48 | continue; |
michael@0 | 49 | window.dump("Already seen y!?\n"); |
michael@0 | 50 | window.dump(y.toString() + "\n"); |
michael@0 | 51 | return false; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | // x and y must be of the same [[Class]]. |
michael@0 | 55 | var xcls = Object.prototype.toString.call(x); |
michael@0 | 56 | var ycls = Object.prototype.toString.call(y); |
michael@0 | 57 | if (xcls !== ycls) { |
michael@0 | 58 | window.dump("Failing on proto\n"); |
michael@0 | 59 | return false; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | // This function is only designed to check Objects and Arrays. |
michael@0 | 63 | if (!(xcls === "[object Object]" || xcls === "[object Array]")) { |
michael@0 | 64 | window.dump("Not an object!\n"); |
michael@0 | 65 | window.dump(xcls + "\n"); |
michael@0 | 66 | return false; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | // Compare objects. |
michael@0 | 70 | var xk = Object.keys(x), yk = Object.keys(y); |
michael@0 | 71 | if (xk.length !== yk.length) { |
michael@0 | 72 | window.dump("Length mismatch!\n"); |
michael@0 | 73 | return false; |
michael@0 | 74 | } |
michael@0 | 75 | for (var i = 0; i < xk.length; i++) { |
michael@0 | 76 | // We must see the same property names in the same order. |
michael@0 | 77 | if (xk[i] !== yk[i]) { |
michael@0 | 78 | window.dump("wrong order\n"); |
michael@0 | 79 | return false; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | // Put the property values on the stack to compare later. |
michael@0 | 83 | stack.push([x[xk[i]], y[yk[i]]]); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | // Record that we have seen this pair of objects. |
michael@0 | 87 | memory.set(x, y); |
michael@0 | 88 | rmemory.set(y, x); |
michael@0 | 89 | } |
michael@0 | 90 | } |
michael@0 | 91 | return true; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | function receiveMessage(evt) |
michael@0 | 95 | { |
michael@0 | 96 | if (isClone(evt.data, generator.next())) |
michael@0 | 97 | window.parent.postMessage("TEST-PASS", "*"); |
michael@0 | 98 | else |
michael@0 | 99 | window.parent.postMessage("TEST-FAIL", "*"); |
michael@0 | 100 | } |
michael@0 | 101 | window.addEventListener("message", receiveMessage, false); |
michael@0 | 102 | </script> |
michael@0 | 103 | </head> |
michael@0 | 104 | <body> |
michael@0 | 105 | </body> |
michael@0 | 106 | </html> |