tests/simple/page-mode-activation.js: update a variable name
[conkeror/arlinius.git] / modules / permission-manager.js
blobb2d54e1570f87a9d0346f281e4a8b25e60c6729f
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 /**
9  * Interface to nsIPermissionManager, which controls the popup
10  * blocking whitelist, among other things.
11  */
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] ],
19              related_prefs: [
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."] ]
24             },
26     cookie : {desc: "specifies per-host cookie policies",
27               values: [
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] ],
31               related_prefs: [
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."
41                   ],
42                   [ "network.cookie.lifetime.days" ] ]
43              },
46     image : {desc: "specifies per-host image automatic loading policies",
47               values: [
48                   ["allow", Ci.nsIPermissionManager.ALLOW_ACTION],
49                   ["deny", Ci.nsIPermissionManager.DENY_ACTION] ],
50               related_prefs: [
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 "
55                                                  + "are blocked." ] ]
56             },
58     install : {desc: "specifies a whitelist of sites from which XPI files may be opened",
59               values: [
60                   ["allow", Ci.nsIPermissionManager.ALLOW_ACTION] ]
61               }
65  * cookie
66  *
67  *
68  *
69  * popup
70  *   dom.popup_maximum  -  The number of pop-ups to allow from a single non-click event
71  *
72  *   dom.popup_allowed_events
73  *
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.
75  *
76  *
77  */
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.",
82             function (I) {
83                 I.minibuffer.message("Save the file and close the editor when done editing permissions.");
85                 let existing_perms = {};
87                 var file_buf =
88                     "# -*- conf -*-\n" +
89                     "# Permission list\n\n";
92                 {
93                     let e = permission_manager.enumerator;
94                     let arr = [];
95                     let max_host_len = 0;
96                     let max_type_len = 0;
97                     while (e.hasMoreElements()) {
98                         let p = e.getNext().QueryInterface(Ci.nsIPermission);
99                         let host = p.host;
100                         let type = p.type;
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;
108                     }
109                     ++max_host_len;
110                     ++max_type_len;
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]) {
119                                     cap = values[j][0];
120                                     break;
121                                 }
122                             }
123                         }
124                         file_buf += host + max_host.substr(host.length) + type + max_type.substr(type.length) + cap + "\n";
125                     }
127                     if (arr.length == 0)
128                         file_buf += "\n";
129                 }
131                 file_buf += "\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] + ")";
146                         if (x[3])
147                             file_buf += " - " + x[3];
148                         file_buf += "\n";
149                     }
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";
155                             if (x.length > 1) {
156                                 file_buf += word_wrap(x[1], 80, "#         ", "#");
157                             }
158                             file_buf += "\n";
159                         }
160                     }
161                 }
162                 var file = null;
163                 try {
164                     file = get_temporary_file("permissions.txt");
165                     let line = 4;
167                     outer: while (1) {
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.");
175                             break;
176                         }
178                         // Parse
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;
182                         let arr = [];
183                         let prev_entries = {};
184                         for (let i = 0; i < lines.length; ++i) {
185                             // Parse each line, checking for syntax errors
186                             let x = lines[i];
187                             let idx = x.indexOf('#');
188                             if (idx != -1)
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
193                             try {
194                                 let host = parts[0];
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";
199                                 let type = parts[1];
200                                 if (parts.length < 3)
201                                     throw "missing permission value";
202                                 let cap = parts[2];
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]) {
207                                             cap = values[i][1];
208                                             break;
209                                         }
210                                     }
211                                 }
212                                 if (!/([0-9]+)/.test(cap))
213                                     throw "invalid permission value: " + cap;
214                                 cap = parseInt(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) {
222                                 line = i + 1;
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.");
227                                 continue outer;
228                             }
229                         }
230                         let num_added = 0;
231                         let num_changed = 0;
232                         for (let i = 0; i < arr.length; ++i) {
233                             let [host,type,cap] = arr[i];
234                             let x = existing_perms[""+[host,type]];
235                             let add = false;
236                             if (x === undefined) {
237                                 ++num_added;
238                                 add = true;
239                             } else {
240                                 if (x != cap) {
241                                     ++num_changed;
242                                     add = true;
243                                 }
244                                 delete existing_perms[""+[host,type]];
245                             }
246                             if (add)
247                                 permission_manager.add(make_uri("http://" + host), type, cap);
248                         }
249                         let num_removed = 0;
250                         for (let [k,v] in Iterator(existing_perms)) {
251                             let [host,type] = k.split(",",2);
252                             ++num_removed;
253                             permission_manager.remove(host,type);
254                         }
255                         let msg;
256                         if (num_added == 0 && num_changed == 0 && num_removed == 0)
257                             msg = "No permission changes made.";
258                         else {
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]) {
265                                         if (count == 1)
266                                              return "1 entry " + caption;
267                                         return count + " entries " + caption;
268                                     }).join(", ") +
269                                 ".";
270                         }
271                         I.minibuffer.message(msg);
272                         break;
273                     }
274                 } catch (e) {
275                     dump_error(e);
276                     throw interactive_error("Failed to edit permissions list in external editor.");
277                 } finally {
278                     file.remove(false);
279                 }
280             });
282 provide("permission-manager");