browser_bug1108547.js (4564B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 requestLongerTimeout(2); 5 6 function test() { 7 waitForExplicitFinish(); 8 9 runPass("file_bug1108547-2.html", function () { 10 runPass("file_bug1108547-3.html", function () { 11 finish(); 12 }); 13 }); 14 } 15 16 function runPass(getterFile, finishedCallback) { 17 var rootDir = "http://mochi.test:8888/browser/dom/html/test/"; 18 var testBrowser; 19 var privateWin; 20 21 function whenDelayedStartupFinished(win, callback) { 22 let topic = "browser-delayed-startup-finished"; 23 Services.obs.addObserver(function onStartup(aSubject) { 24 if (win != aSubject) { 25 return; 26 } 27 28 Services.obs.removeObserver(onStartup, topic); 29 executeSoon(callback); 30 }, topic); 31 } 32 33 // First, set the cookie in a normal window. 34 gBrowser.selectedTab = BrowserTestUtils.addTab( 35 gBrowser, 36 rootDir + "file_bug1108547-1.html" 37 ); 38 BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser).then( 39 afterOpenCookieSetter 40 ); 41 42 function afterOpenCookieSetter() { 43 gBrowser.removeCurrentTab(); 44 45 // Now, open a private window. 46 privateWin = OpenBrowserWindow({ private: true }); 47 whenDelayedStartupFinished(privateWin, afterPrivateWindowOpened); 48 } 49 50 function afterPrivateWindowOpened() { 51 // In the private window, open the getter file, and wait for a new tab to be opened. 52 privateWin.gBrowser.selectedTab = BrowserTestUtils.addTab( 53 privateWin.gBrowser, 54 rootDir + getterFile 55 ); 56 testBrowser = privateWin.gBrowser.selectedBrowser; 57 privateWin.gBrowser.tabContainer.addEventListener( 58 "TabOpen", 59 onNewTabOpened, 60 true 61 ); 62 } 63 64 function fetchResult() { 65 return SpecialPowers.spawn(testBrowser, [], function () { 66 return content.document.getElementById("result").textContent; 67 }); 68 } 69 70 function onNewTabOpened() { 71 // When the new tab with js URI is opened, wait for it to load. 72 privateWin.gBrowser.tabContainer.removeEventListener( 73 "TabOpen", 74 onNewTabOpened, 75 true 76 ); 77 BrowserTestUtils.browserLoaded( 78 privateWin.gBrowser.tabs[privateWin.gBrowser.tabs.length - 1] 79 .linkedBrowser, 80 { wantLoad: "about:blank" } // js URIs have about:blank as location 81 ) 82 .then(fetchResult) 83 .then(onNewTabLoaded); 84 } 85 86 function onNewTabLoaded(result) { 87 // Now, ensure that the private tab doesn't have access to the cookie set in normal mode. 88 is(result, "", "Shouldn't have access to the cookies"); 89 90 // We're done with the private window, close it. 91 privateWin.close(); 92 93 // Clear all cookies. 94 Cc["@mozilla.org/cookiemanager;1"] 95 .getService(Ci.nsICookieManager) 96 .removeAll(); 97 98 // Open a new private window, this time to set a cookie inside it. 99 privateWin = OpenBrowserWindow({ private: true }); 100 whenDelayedStartupFinished(privateWin, afterPrivateWindowOpened2); 101 } 102 103 function afterPrivateWindowOpened2() { 104 // In the private window, open the setter file, and wait for it to load. 105 privateWin.gBrowser.selectedTab = BrowserTestUtils.addTab( 106 privateWin.gBrowser, 107 rootDir + "file_bug1108547-1.html" 108 ); 109 BrowserTestUtils.browserLoaded(privateWin.gBrowser.selectedBrowser).then( 110 afterOpenCookieSetter2 111 ); 112 } 113 114 function afterOpenCookieSetter2() { 115 // We're done with the private window now, close it. 116 privateWin.close(); 117 118 // Now try to read the cookie in a normal window, and wait for a new tab to be opened. 119 gBrowser.selectedTab = BrowserTestUtils.addTab( 120 gBrowser, 121 rootDir + getterFile 122 ); 123 testBrowser = gBrowser.selectedBrowser; 124 gBrowser.tabContainer.addEventListener("TabOpen", onNewTabOpened2, true); 125 } 126 127 function onNewTabOpened2() { 128 // When the new tab with js URI is opened, wait for it to load. 129 gBrowser.tabContainer.removeEventListener("TabOpen", onNewTabOpened2, true); 130 BrowserTestUtils.browserLoaded( 131 gBrowser.tabs[gBrowser.tabs.length - 1].linkedBrowser, 132 { wantLoad: "about:blank" } // js URIs have about:blank as location 133 ) 134 .then(fetchResult) 135 .then(onNewTabLoaded2); 136 } 137 138 function onNewTabLoaded2(result) { 139 // Now, ensure that the normal tab doesn't have access to the cookie set in private mode. 140 is(result, "", "Shouldn't have access to the cookies"); 141 142 // Remove both of the tabs opened here. 143 gBrowser.removeCurrentTab(); 144 gBrowser.removeCurrentTab(); 145 146 privateWin = null; 147 testBrowser = null; 148 149 finishedCallback(); 150 } 151 }