|
|
|
|
1 |
// Bug 380852 - Delete permission manager entries in Clear Recent History |
2 |
|
3 |
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm"); |
4 |
const {Sanitizer} = ChromeUtils.import("resource:///modules/Sanitizer.jsm", {}); |
5 |
|
6 |
XPCOMUtils.defineLazyServiceGetter(this, "sas", |
7 |
"@mozilla.org/storage/activity-service;1", |
8 |
"nsIStorageActivityService"); |
9 |
XPCOMUtils.defineLazyServiceGetter(this, "swm", |
10 |
"@mozilla.org/serviceworkers/manager;1", |
11 |
"nsIServiceWorkerManager"); |
12 |
XPCOMUtils.defineLazyServiceGetter(this, "quotaManagerService", |
13 |
"@mozilla.org/dom/quota-manager-service;1", |
14 |
"nsIQuotaManagerService"); |
15 |
|
16 |
const oneHour = 3600000000; |
17 |
const fiveHours = oneHour * 5; |
18 |
|
19 |
const itemsToClear = [ "cookies", "offlineApps" ]; |
20 |
|
21 |
function waitForUnregister(host) { |
22 |
return new Promise(resolve => { |
23 |
let listener = { |
24 |
onUnregister: registration => { |
25 |
if (registration.principal.URI.host != host) { |
26 |
return; |
27 |
} |
28 |
let swm = Cc["@mozilla.org/serviceworkers/manager;1"] |
29 |
.getService(Ci.nsIServiceWorkerManager); |
30 |
swm.removeListener(listener); |
31 |
resolve(registration); |
32 |
} |
33 |
}; |
34 |
swm.addListener(listener); |
35 |
}); |
36 |
} |
37 |
|
38 |
async function createData(host) { |
39 |
let pageURL = getRootDirectory(gTestPath).replace("chrome://mochitests/content", "http://" + host) + "sanitize.html"; |
40 |
|
41 |
return BrowserTestUtils.withNewTab(pageURL, async function(browser) { |
42 |
await ContentTask.spawn(browser, null, () => { |
43 |
return new content.window.Promise(resolve => { |
44 |
let id = content.window.setInterval(() => { |
45 |
if ("foobar" in content.window.localStorage) { |
46 |
content.window.clearInterval(id); |
47 |
resolve(true); |
48 |
} |
49 |
}, 1000); |
50 |
}); |
51 |
}); |
52 |
}); |
53 |
} |
54 |
|
55 |
function moveOriginInTime(principals, endDate, host) { |
56 |
for (let i = 0; i < principals.length; ++i) { |
57 |
let principal = principals.queryElementAt(i, Ci.nsIPrincipal); |
58 |
if (principal.URI.host == host) { |
59 |
sas.moveOriginInTime(principal, endDate - fiveHours); |
60 |
return true; |
61 |
} |
62 |
} |
63 |
return false; |
64 |
} |
65 |
|
66 |
async function getData(host) { |
67 |
let dummyURL = getRootDirectory(gTestPath).replace("chrome://mochitests/content", "http://" + host) + "dummy_page.html"; |
68 |
|
69 |
// LocalStorage + IndexedDB |
70 |
let data = await BrowserTestUtils.withNewTab(dummyURL, async function(browser) { |
71 |
return ContentTask.spawn(browser, null, () => { |
72 |
return new content.window.Promise(resolve => { |
73 |
let obj = { |
74 |
localStorage: "foobar" in content.window.localStorage, |
75 |
indexedDB: true, |
76 |
serviceWorker: false, |
77 |
}; |
78 |
|
79 |
let request = content.window.indexedDB.open("sanitizer_test", 1); |
80 |
request.onupgradeneeded = event => { |
81 |
obj.indexedDB = false; |
82 |
}; |
83 |
request.onsuccess = event => { |
84 |
resolve(obj); |
85 |
}; |
86 |
}); |
87 |
}); |
88 |
}); |
89 |
|
90 |
// ServiceWorkers |
91 |
let serviceWorkers = swm.getAllRegistrations(); |
92 |
for (let i = 0; i < serviceWorkers.length; i++) { |
93 |
let sw = serviceWorkers.queryElementAt(i, Ci.nsIServiceWorkerRegistrationInfo); |
94 |
if (sw.principal.URI.host == host) { |
95 |
data.serviceWorker = true; |
96 |
break; |
97 |
} |
98 |
} |
99 |
|
100 |
return data; |
101 |
} |
102 |
|
103 |
add_task(async function testWithRange() { |
104 |
await SpecialPowers.pushPrefEnv({"set": [ |
105 |
["dom.serviceWorkers.enabled", true], |
106 |
["dom.serviceWorkers.exemptFromPerDomainMax", true], |
107 |
["dom.serviceWorkers.testing.enabled", true] |
108 |
]}); |
109 |
|
110 |
// The service may have picked up activity from prior tests in this run. |
111 |
// Clear it. |
112 |
sas.testOnlyReset(); |
113 |
|
114 |
let endDate = Date.now() * 1000; |
115 |
let principals = sas.getActiveOrigins(endDate - oneHour, endDate); |
116 |
is(principals.length, 0, "starting from clear activity state"); |
117 |
|
118 |
info("sanitize: " + itemsToClear.join(", ")); |
119 |
await Sanitizer.sanitize(itemsToClear, {ignoreTimespan: false}); |
120 |
|
121 |
await createData("example.org"); |
122 |
await createData("example.com"); |
123 |
|
124 |
endDate = Date.now() * 1000; |
125 |
principals = sas.getActiveOrigins(endDate - oneHour, endDate); |
126 |
ok(!!principals, "We have an active origin."); |
127 |
ok(principals.length >= 2, "We have an active origin."); |
128 |
|
129 |
let found = 0; |
130 |
for (let i = 0; i < principals.length; ++i) { |
131 |
let principal = principals.queryElementAt(i, Ci.nsIPrincipal); |
132 |
if (principal.URI.host == "example.org" || |
133 |
principal.URI.host == "example.com") { |
134 |
found++; |
135 |
} |
136 |
} |
137 |
|
138 |
is(found, 2, "Our origins are active."); |
139 |
|
140 |
let dataPre = await getData("example.org"); |
141 |
ok(dataPre.localStorage, "We have localStorage data"); |
142 |
ok(dataPre.indexedDB, "We have indexedDB data"); |
143 |
ok(dataPre.serviceWorker, "We have serviceWorker data"); |
144 |
|
145 |
dataPre = await getData("example.com"); |
146 |
ok(dataPre.localStorage, "We have localStorage data"); |
147 |
ok(dataPre.indexedDB, "We have indexedDB data"); |
148 |
ok(dataPre.serviceWorker, "We have serviceWorker data"); |
149 |
|
150 |
// Let's move example.com in the past. |
151 |
ok(moveOriginInTime(principals, endDate, "example.com"), "Operation completed!"); |
152 |
|
153 |
let p = waitForUnregister("example.org"); |
154 |
|
155 |
// Clear it |
156 |
info("sanitize: " + itemsToClear.join(", ")); |
157 |
await Sanitizer.sanitize(itemsToClear, {ignoreTimespan: false}); |
158 |
await p; |
159 |
|
160 |
let dataPost = await getData("example.org"); |
161 |
ok(!dataPost.localStorage, "We don't have localStorage data"); |
162 |
ok(!dataPost.indexedDB, "We don't have indexedDB data"); |
163 |
ok(!dataPost.serviceWorker, "We don't have serviceWorker data"); |
164 |
|
165 |
dataPost = await getData("example.com"); |
166 |
ok(dataPost.localStorage, "We still have localStorage data"); |
167 |
ok(dataPost.indexedDB, "We still have indexedDB data"); |
168 |
ok(dataPost.serviceWorker, "We still have serviceWorker data"); |
169 |
|
170 |
// We have to move example.com in the past because how we check IDB triggers |
171 |
// a storage activity. |
172 |
ok(moveOriginInTime(principals, endDate, "example.com"), "Operation completed!"); |
173 |
|
174 |
// Let's call the clean up again. |
175 |
info("sanitize again to ensure clearing doesn't expand the activity scope"); |
176 |
await Sanitizer.sanitize(itemsToClear, {ignoreTimespan: false}); |
177 |
|
178 |
dataPost = await getData("example.com"); |
179 |
ok(dataPost.localStorage, "We still have localStorage data"); |
180 |
ok(dataPost.indexedDB, "We still have indexedDB data"); |
181 |
ok(dataPost.serviceWorker, "We still have serviceWorker data"); |
182 |
|
183 |
dataPost = await getData("example.org"); |
184 |
ok(!dataPost.localStorage, "We don't have localStorage data"); |
185 |
ok(!dataPost.indexedDB, "We don't have indexedDB data"); |
186 |
ok(!dataPost.serviceWorker, "We don't have serviceWorker data"); |
187 |
|
188 |
sas.testOnlyReset(); |
189 |
}); |