1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
// Bug 380852 - Delete permission manager entries in Clear Recent History
const { Sanitizer } = ChromeUtils.import("resource:///modules/Sanitizer.jsm");
const { SiteDataTestUtils } = ChromeUtils.import(
"resource://testing-common/SiteDataTestUtils.jsm"
);
const { PromiseTestUtils } = ChromeUtils.import(
"resource://testing-common/PromiseTestUtils.jsm"
);
XPCOMUtils.defineLazyServiceGetter(
this,
"sas",
"@mozilla.org/storage/activity-service;1",
"nsIStorageActivityService"
);
XPCOMUtils.defineLazyServiceGetter(
this,
"swm",
"@mozilla.org/serviceworkers/manager;1",
"nsIServiceWorkerManager"
);
const oneHour = 3600000000;
const fiveHours = oneHour * 5;
const itemsToClear = ["cookies", "offlineApps"];
function hasIndexedDB(origin) {
return new Promise(resolve => {
let hasData = true;
let uri = Services.io.newURI(origin);
let principal = Services.scriptSecurityManager.createContentPrincipal(
uri,
{}
);
let request = indexedDB.openForPrincipal(principal, "TestDatabase", 1);
request.onupgradeneeded = function(e) {
hasData = false;
};
request.onsuccess = function(e) {
resolve(hasData);
};
});
}
function waitForUnregister(host) {
return new Promise(resolve => {
let listener = {
onUnregister: registration => {
if (registration.principal.host != host) {
return;
}
swm.removeListener(listener);
resolve(registration);
},
};
swm.addListener(listener);
});
}
async function createData(host) {
let origin = "https://" + host;
let dummySWURL =
getRootDirectory(gTestPath).replace("chrome://mochitests/content", origin) +
"dummy.js";
await SiteDataTestUtils.addToIndexedDB(origin);
await SiteDataTestUtils.addServiceWorker(dummySWURL);
}
function moveOriginInTime(principals, endDate, host) {
for (let i = 0; i < principals.length; ++i) {
let principal = principals.queryElementAt(i, Ci.nsIPrincipal);
if (principal.host == host) {
sas.moveOriginInTime(principal, endDate - fiveHours);
return true;
}
}
return false;
}
add_task(async function testWithRange() {
// We have intermittent occurrences of NS_ERROR_ABORT being
// thrown at closing database instances when using Santizer.sanitize().
// This does not seem to impact cleanup, since our tests run fine anyway.
PromiseTestUtils.allowMatchingRejectionsGlobally(/NS_ERROR_ABORT/);
await SpecialPowers.pushPrefEnv({
set: [
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.testing.enabled", true],
],
});
// The service may have picked up activity from prior tests in this run.
// Clear it.
sas.testOnlyReset();
let endDate = Date.now() * 1000;
let principals = sas.getActiveOrigins(endDate - oneHour, endDate);
is(principals.length, 0, "starting from clear activity state");
info("sanitize: " + itemsToClear.join(", "));
await Sanitizer.sanitize(itemsToClear, { ignoreTimespan: false });
await createData("example.org");
await createData("example.com");
endDate = Date.now() * 1000;
principals = sas.getActiveOrigins(endDate - oneHour, endDate);
ok(!!principals, "We have an active origin.");
ok(principals.length >= 2, "We have an active origin.");
let found = 0;
for (let i = 0; i < principals.length; ++i) {
let principal = principals.queryElementAt(i, Ci.nsIPrincipal);
if (principal.host == "example.org" || principal.host == "example.com") {
found++;
}
}
is(found, 2, "Our origins are active.");
ok(
await hasIndexedDB("https://example.org"),
"We have indexedDB data for example.org"
);
ok(
SiteDataTestUtils.hasServiceWorkers("https://example.org"),
"We have serviceWorker data for example.org"
);
ok(
await hasIndexedDB("https://example.com"),
"We have indexedDB data for example.com"
);
ok(
SiteDataTestUtils.hasServiceWorkers("https://example.com"),
"We have serviceWorker data for example.com"
);
// Let's move example.com in the past.
ok(
moveOriginInTime(principals, endDate, "example.com"),
"Operation completed!"
);
let p = waitForUnregister("example.org");
// Clear it
info("sanitize: " + itemsToClear.join(", "));
await Sanitizer.sanitize(itemsToClear, { ignoreTimespan: false });
await p;
ok(
!(await hasIndexedDB("https://example.org")),
"We don't have indexedDB data for example.org"
);
ok(
!SiteDataTestUtils.hasServiceWorkers("https://example.org"),
"We don't have serviceWorker data for example.org"
);
ok(
await hasIndexedDB("https://example.com"),
"We still have indexedDB data for example.com"
);
ok(
SiteDataTestUtils.hasServiceWorkers("https://example.com"),
"We still have serviceWorker data for example.com"
);
// We have to move example.com in the past because how we check IDB triggers
// a storage activity.
ok(
moveOriginInTime(principals, endDate, "example.com"),
"Operation completed!"
);
// Let's call the clean up again.
info("sanitize again to ensure clearing doesn't expand the activity scope");
await Sanitizer.sanitize(itemsToClear, { ignoreTimespan: false });
ok(
await hasIndexedDB("https://example.com"),
"We still have indexedDB data for example.com"
);
ok(
SiteDataTestUtils.hasServiceWorkers("https://example.com"),
"We still have serviceWorker data for example.com"
);
ok(
!(await hasIndexedDB("https://example.org")),
"We don't have indexedDB data for example.org"
);
ok(
!SiteDataTestUtils.hasServiceWorkers("https://example.org"),
"We don't have serviceWorker data for example.org"
);
sas.testOnlyReset();
// Clean up.
await Sanitizer.sanitize(itemsToClear);
});
|