|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 let pm; |
|
5 |
|
6 // Create a principal based on the { origin, appId, browserElement }. |
|
7 function createPrincipal(aOrigin, aAppId, aBrowserElement) |
|
8 { |
|
9 return Services.scriptSecurityManager.getAppCodebasePrincipal(NetUtil.newURI(aOrigin), aAppId, aBrowserElement); |
|
10 } |
|
11 |
|
12 // Return the subject required by 'webapps-clear-data' notification. |
|
13 function getSubject(aAppId, aBrowserOnly) |
|
14 { |
|
15 return { |
|
16 appId: aAppId, |
|
17 browserOnly: aBrowserOnly, |
|
18 QueryInterface: XPCOMUtils.generateQI([Ci.mozIApplicationClearPrivateDataParams]) |
|
19 }; |
|
20 } |
|
21 |
|
22 // Use aEntries to create principals, add permissions to them and check that they have them. |
|
23 // Then, it is notifying 'webapps-clear-data' with the given aSubject and check if the permissions |
|
24 // of principals[i] matches the permission in aResults[i]. |
|
25 function test(aEntries, aSubject, aResults) |
|
26 { |
|
27 let principals = []; |
|
28 |
|
29 for (entry of aEntries) { |
|
30 principals.push(createPrincipal(entry.origin, entry.appId, entry.browserElement)); |
|
31 } |
|
32 |
|
33 for (principal of principals) { |
|
34 do_check_eq(pm.testPermissionFromPrincipal(principal, "test/webapps-clear"), pm.UNKNOWN_ACTION); |
|
35 pm.addFromPrincipal(principal, "test/webapps-clear", pm.ALLOW_ACTION, pm.EXPIRE_NEVER, 0); |
|
36 do_check_eq(pm.testPermissionFromPrincipal(principal, "test/webapps-clear"), pm.ALLOW_ACTION); |
|
37 } |
|
38 |
|
39 Services.obs.notifyObservers(aSubject, 'webapps-clear-data', null); |
|
40 |
|
41 var length = aEntries.length; |
|
42 for (let i=0; i<length; ++i) { |
|
43 do_check_eq(pm.testPermissionFromPrincipal(principals[i], 'test/webapps-clear'), aResults[i]); |
|
44 |
|
45 // Remove allowed actions. |
|
46 if (aResults[i] == pm.ALLOW_ACTION) { |
|
47 pm.removeFromPrincipal(principals[i], 'test/webapps-clear'); |
|
48 } |
|
49 } |
|
50 } |
|
51 |
|
52 function run_test() |
|
53 { |
|
54 do_get_profile(); |
|
55 |
|
56 pm = Cc["@mozilla.org/permissionmanager;1"] |
|
57 .getService(Ci.nsIPermissionManager); |
|
58 |
|
59 let entries = [ |
|
60 { origin: 'http://example.com', appId: 1, browserElement: false }, |
|
61 { origin: 'http://example.com', appId: 1, browserElement: true }, |
|
62 { origin: 'http://example.com', appId: Ci.nsIScriptSecurityManager.NO_APPID, browserElement: false }, |
|
63 { origin: 'http://example.com', appId: 2, browserElement: false }, |
|
64 ]; |
|
65 |
|
66 // In that case, all permissions from app 1 should be removed but not the other ones. |
|
67 test(entries, getSubject(1, false), [ pm.UNKNOWN_ACTION, pm.UNKNOWN_ACTION, pm.ALLOW_ACTION, pm.ALLOW_ACTION ]); |
|
68 |
|
69 // In that case, only the permissions of app 1 related to a browserElement should be removed. |
|
70 // All the other permissions should stay. |
|
71 test(entries, getSubject(1, true), [ pm.ALLOW_ACTION, pm.UNKNOWN_ACTION, pm.ALLOW_ACTION, pm.ALLOW_ACTION ]); |
|
72 } |