Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | // Remember the window size in non-fullscreen mode. |
michael@0 | 2 | var normalSize = new function() { |
michael@0 | 3 | this.w = window.outerWidth; |
michael@0 | 4 | this.h = window.outerHeight; |
michael@0 | 5 | }(); |
michael@0 | 6 | |
michael@0 | 7 | // Returns true if the window occupies the entire screen. |
michael@0 | 8 | // Note this only returns true once the transition from normal to |
michael@0 | 9 | // fullscreen mode is complete. |
michael@0 | 10 | function inFullscreenMode() { |
michael@0 | 11 | return window.outerWidth == window.screen.width && |
michael@0 | 12 | window.outerHeight == window.screen.height; |
michael@0 | 13 | } |
michael@0 | 14 | |
michael@0 | 15 | // Returns true if the window is in normal mode, i.e. non fullscreen mode. |
michael@0 | 16 | // Note this only returns true once the transition from fullscreen back to |
michael@0 | 17 | // normal mode is complete. |
michael@0 | 18 | function inNormalMode() { |
michael@0 | 19 | return window.outerWidth == normalSize.w && |
michael@0 | 20 | window.outerHeight == normalSize.h; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | function ok(condition, msg) { |
michael@0 | 24 | opener.ok(condition, "[rollback] " + msg); |
michael@0 | 25 | if (!condition) { |
michael@0 | 26 | opener.finish(); |
michael@0 | 27 | } |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | // On Linux we sometimes receive fullscreenchange events before the window |
michael@0 | 31 | // has finished transitioning to fullscreen. This can cause problems and |
michael@0 | 32 | // test failures, so work around it on Linux until we can get a proper fix. |
michael@0 | 33 | const workAroundFullscreenTransition = navigator.userAgent.indexOf("Linux") != -1; |
michael@0 | 34 | |
michael@0 | 35 | // Adds a listener that will be called once a fullscreen transition |
michael@0 | 36 | // is complete. When type==='enter', callback is called when we've |
michael@0 | 37 | // received a fullscreenchange event, and the fullscreen transition is |
michael@0 | 38 | // complete. When type==='exit', callback is called when we've |
michael@0 | 39 | // received a fullscreenchange event and the window dimensions match |
michael@0 | 40 | // the window dimensions when the window opened (so don't resize the |
michael@0 | 41 | // window while running your test!). inDoc is the document which |
michael@0 | 42 | // the listeners are added on, if absent, the listeners are added to |
michael@0 | 43 | // the current document. |
michael@0 | 44 | function addFullscreenChangeContinuation(type, callback, inDoc) { |
michael@0 | 45 | var doc = inDoc || document; |
michael@0 | 46 | var listener = null; |
michael@0 | 47 | if (type === "enter") { |
michael@0 | 48 | // when entering fullscreen, ensure we don't call 'callback' until the |
michael@0 | 49 | // enter transition is complete. |
michael@0 | 50 | listener = function(event) { |
michael@0 | 51 | doc.removeEventListener("mozfullscreenchange", listener, false); |
michael@0 | 52 | if (!workAroundFullscreenTransition) { |
michael@0 | 53 | callback(event); |
michael@0 | 54 | return; |
michael@0 | 55 | } |
michael@0 | 56 | if (!inFullscreenMode()) { |
michael@0 | 57 | opener.todo(false, "fullscreenchange before entering fullscreen complete! " + |
michael@0 | 58 | " window.fullScreen=" + window.fullScreen + |
michael@0 | 59 | " normal=(" + normalSize.w + "," + normalSize.h + ")" + |
michael@0 | 60 | " outerWidth=" + window.outerWidth + " width=" + window.screen.width + |
michael@0 | 61 | " outerHeight=" + window.outerHeight + " height=" + window.screen.height); |
michael@0 | 62 | setTimeout(function(){listener(event);}, 100); |
michael@0 | 63 | return; |
michael@0 | 64 | } |
michael@0 | 65 | setTimeout(function(){callback(event)}, 0); |
michael@0 | 66 | }; |
michael@0 | 67 | } else if (type === "exit") { |
michael@0 | 68 | listener = function(event) { |
michael@0 | 69 | doc.removeEventListener("mozfullscreenchange", listener, false); |
michael@0 | 70 | if (!workAroundFullscreenTransition) { |
michael@0 | 71 | callback(event); |
michael@0 | 72 | return; |
michael@0 | 73 | } |
michael@0 | 74 | if (!document.mozFullScreenElement && !inNormalMode()) { |
michael@0 | 75 | opener.todo(false, "fullscreenchange before exiting fullscreen complete! " + |
michael@0 | 76 | " window.fullScreen=" + window.fullScreen + |
michael@0 | 77 | " normal=(" + normalSize.w + "," + normalSize.h + ")" + |
michael@0 | 78 | " outerWidth=" + window.outerWidth + " width=" + window.screen.width + |
michael@0 | 79 | " outerHeight=" + window.outerHeight + " height=" + window.screen.height); |
michael@0 | 80 | // 'document' (*not* 'doc') has no fullscreen element, so we're trying |
michael@0 | 81 | // to completely exit fullscreen mode. Wait until the transition |
michael@0 | 82 | // to normal mode is complete before calling callback. |
michael@0 | 83 | setTimeout(function(){listener(event);}, 100); |
michael@0 | 84 | return; |
michael@0 | 85 | } |
michael@0 | 86 | opener.info("[rollback] Exited fullscreen"); |
michael@0 | 87 | setTimeout(function(){callback(event);}, 0); |
michael@0 | 88 | }; |
michael@0 | 89 | } else { |
michael@0 | 90 | throw "'type' must be either 'enter', or 'exit'."; |
michael@0 | 91 | } |
michael@0 | 92 | doc.addEventListener("mozfullscreenchange", listener, false); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | // Calls |callback| when the next fullscreenerror is dispatched to inDoc||document. |
michael@0 | 96 | function addFullscreenErrorContinuation(callback, inDoc) { |
michael@0 | 97 | var doc = inDoc || document; |
michael@0 | 98 | var listener = function(event) { |
michael@0 | 99 | doc.removeEventListener("mozfullscreenerror", listener, false); |
michael@0 | 100 | setTimeout(function(){callback(event);}, 0); |
michael@0 | 101 | }; |
michael@0 | 102 | doc.addEventListener("mozfullscreenerror", listener, false); |
michael@0 | 103 | } |
michael@0 | 104 |