2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
9 * Interface to nsIPermissionManager, which controls the popup
10 * blocking whitelist, among other things.
13 let permission_manager = Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager);
15 let permission_types = {
16 popup : {desc: "specifies a whitelist and blacklist for the popup blocker",
17 values: [ ["allow", Ci.nsIPermissionManager.ALLOW_ACTION],
18 ["deny", Ci.nsIPermissionManager.DENY_ACTION] ],
20 [ "dom.disable_open_during_load",
21 "This preference defines the default behavior of whether " +
22 "unrequested popups are allowed for sites not listed in the permission list." ],
23 [ "dom.popup_maximum", "The number of pop-ups to allow from a single non-click event."] ]
26 cookie : {desc: "specifies per-host cookie policies",
28 ["allow", Ci.nsIPermissionManager.ALLOW_ACTION],
29 ["session", Ci.nsICookiePermission.ACCESS_SESSION, "expire matching cookies when the browser exits"],
30 ["deny", Ci.nsIPermissionManager.DENY_ACTION] ],
32 [ "network.cookie.lifetime.behavior.enabled" ],
33 [ "network.cookie.cookieBehavior",
34 "This preference defines the default cookie behavior for sites not listed in the " +
35 "permission list. The value 0 means to enable all cookies, 1 means to reject " +
36 "only third-party cookies, and 2 means to reject all cookies." ],
37 [ "network.cookie.lifetime.behavior",
38 "If network.cookie.lifetime.behavior.enabled is set to true, a value of 0 means all " +
39 "cookies expire at the end of the current session, while a value of 1 means that " +
40 "cookies expire after the number of days specified by network.cookie.lifetime.days."
42 [ "network.cookie.lifetime.days" ] ]
46 image : {desc: "specifies per-host image automatic loading policies",
48 ["allow", Ci.nsIPermissionManager.ALLOW_ACTION],
49 ["deny", Ci.nsIPermissionManager.DENY_ACTION] ],
51 [ "permissions.default.image", "This prefreence defines the default image loading policy "
52 + "for sites not listed in the permission list. The value "
53 + "1 means all images should be loaded, 2 means no images "
54 + "should be loaded, and 3 means only third-party images "
58 install : {desc: "specifies a whitelist of sites from which XPI files may be opened",
60 ["allow", Ci.nsIPermissionManager.ALLOW_ACTION] ]
70 * dom.popup_maximum - The number of pop-ups to allow from a single non-click event
72 * dom.popup_allowed_events
74 * dom.disable_open_during_load - This preference defines the default behavior of whether unrequested popups are allowed for sites not listed in the permission list.
79 interactive("permission-manager", "View or edit the host-specific "
80 + "permission list.\nThis list is used for the popup"
81 + "blocker whitelist, among other things.",
83 I.minibuffer.message("Save the file and close the editor when done editing permissions.");
85 let existing_perms = {};
89 "# Permission list\n\n";
93 let e = permission_manager.enumerator;
97 while (e.hasMoreElements()) {
98 let p = e.getNext().QueryInterface(Ci.nsIPermission);
101 let cap = p.capability;
102 if (max_host_len < host.length)
103 max_host_len = host.length;
104 if (max_type_len < type.length)
105 max_type_len = type.length;
106 arr.push([host, type, cap]);
107 existing_perms[""+[host, type]] = cap;
111 let max_host = get_spaces(max_host_len);
112 let max_type = get_spaces(max_type_len);
113 for (let i = 0; i < arr.length; ++i) {
114 let [host, type, cap] = arr[i];
115 if (permission_types.hasOwnProperty(type)) {
116 let values = permission_types[type].values;
117 for (let j = 0; j < values.length; ++j) {
118 if (cap == values[j][1]) {
124 file_buf += host + max_host.substr(host.length) + type + max_type.substr(type.length) + cap + "\n";
132 "# entry syntax (one per line): <domain> <type> <permission>\n\n" +
133 "# example: google.com popup allow\n\n" +
135 word_wrap("The <domain> must be a valid domain name. Depending on the <type>, only exact " +
136 "matches may be used, or alternatively it may match any sub-domain if a more " +
137 "specific entry is not found.", 80, "# ") + "\n" +
138 "# The possible values for the permission <type> include:\n";
139 for (let type in permission_types) {
140 let data = permission_types[type];
141 file_buf += "# " + type + " - " + data.desc + "\n\n";
142 file_buf += "# Supported <permission> values:\n";
143 for (let i = 0; i < data.values.length; ++i) {
144 let x = data.values[i];
145 file_buf += "# " + x[0] + " (" + x[1] + ")";
147 file_buf += " - " + x[3];
150 if (data.related_prefs && data.related_prefs.length > 0) {
151 file_buf += "\n# Related Mozilla preferences:\n";
152 for (let i = 0; i < data.related_prefs.length; ++i) {
153 let x = data.related_prefs[i];
154 file_buf += "# " + x[0] + " = " + get_pref(x[0]) + "\n";
156 file_buf += word_wrap(x[1], 80, "# ", "#");
164 file = get_temporary_file("permissions.txt");
169 write_text_file(file, file_buf);
170 yield open_file_with_external_editor(file, $line = line);
172 let new_buf = read_text_file(file);
173 if (new_buf == file_buf) {
174 I.minibuffer.message("No permission changes made.");
179 let lines = new_buf.split("\n");
180 if (lines[lines.length - 1].length == 0) // Remove extra line at end
181 lines.length = lines.length - 1;
183 let prev_entries = {};
184 for (let i = 0; i < lines.length; ++i) {
185 // Parse each line, checking for syntax errors
187 let idx = x.indexOf('#');
189 x = x.substr(0, idx);
190 let parts = x.split(/\s+/);
191 if (parts.length == 1 && parts[0].length == 0)
192 continue; // ignore blank line
195 if (!/[a-zA-Z0-9-_]+(\.[a-zA-Z0-9-_]+)*/.test(host))
196 throw "invalid host name: " + host;
197 if (parts.length < 2)
198 throw "missing permission type";
200 if (parts.length < 3)
201 throw "missing permission value";
203 if (permission_types.hasOwnProperty(type)) {
204 let values = permission_types[type].values;
205 for (let i = 0; i < values.length; ++i) {
206 if (cap == values[i][0]) {
212 if (!/([0-9]+)/.test(cap))
213 throw "invalid permission value: " + cap;
215 if (parts.length > 3)
216 throw "too many terms";
217 if (prev_entries[""+[host,type]])
218 throw "duplicate entry";
219 prev_entries[""+[host,type]] = true;
220 arr.push([host,type,cap]);
221 } catch (syntax_err) {
223 lines.splice(i+1, 0, "# ERROR on previous line: " + syntax_err, "");
224 file_buf = lines.join("\n") + "\n";
225 I.minibuffer.message("Correct the syntax error in the permissions list, " +
226 "or close the editor to abort.");
232 for (let i = 0; i < arr.length; ++i) {
233 let [host,type,cap] = arr[i];
234 let x = existing_perms[""+[host,type]];
236 if (x === undefined) {
244 delete existing_perms[""+[host,type]];
247 permission_manager.add(make_uri("http://" + host), type, cap);
250 for (let [k,v] in Iterator(existing_perms)) {
251 let [host,type] = k.split(",",2);
253 permission_manager.remove(host,type);
256 if (num_added == 0 && num_changed == 0 && num_removed == 0)
257 msg = "No permission changes made.";
259 msg = "Updated permissions list: " +
260 [["added", num_added],
261 ["changed", num_changed],
262 ["removed", num_removed]].
263 filter(function ([caption, count]) count > 0).
264 map(function ([caption, count]) {
266 return "1 entry " + caption;
267 return count + " entries " + caption;
271 I.minibuffer.message(msg);
276 throw interactive_error("Failed to edit permissions list in external editor.");
282 provide("permission-manager");