browser_object_follow: do not give $opener for following non-element/window in new...
[conkeror.git] / modules / permission-manager.js
blob36767093a765379ef7ed9ef5e6fada5e88d543bd
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 in_module(null);
15 let permission_manager = Cc["@mozilla.org/permissionmanager;1"].getService(Ci.nsIPermissionManager);
17 let permission_types = {
18     popup : {desc: "specifies a whitelist and blacklist for the popup blocker",
19              values: [ ["allow", Ci.nsIPermissionManager.ALLOW_ACTION],
20                        ["deny", Ci.nsIPermissionManager.DENY_ACTION] ],
21              related_prefs: [
22                  [ "dom.disable_open_during_load",
23                    "This preference defines the default behavior of whether " +
24                    "unrequested popups are allowed for sites not listed in the permission list." ],
25                  [ "dom.popup_maximum", "The number of pop-ups to allow from a single non-click event."] ]
26             },
28     cookie : {desc: "specifies per-host cookie policies",
29               values: [
30                   ["allow", Ci.nsIPermissionManager.ALLOW_ACTION],
31                   ["session", Ci.nsICookiePermission.ACCESS_SESSION, "expire matching cookies when the browser exits"],
32                   ["deny", Ci.nsIPermissionManager.DENY_ACTION] ],
33               related_prefs: [
34                   [ "network.cookie.lifetime.behavior.enabled" ],
35                   [ "network.cookie.cookieBehavior",
36                     "This preference defines the default cookie behavior for sites not listed in the " +
37                     "permission list.  The value 0 means to enable all cookies, 1 means to reject " +
38                     "only third-party cookies, and 2 means to reject all cookies." ],
39                   [ "network.cookie.lifetime.behavior",
40                     "If network.cookie.lifetime.behavior.enabled is set to true, a value of 0 means all " +
41                     "cookies expire at the end of the current session,  while a value of 1 means that " +
42                     "cookies expire after the number of days specified by network.cookie.lifetime.days."
43                   ],
44                   [ "network.cookie.lifetime.days" ] ]
45              },
48     image : {desc: "specifies per-host image automatic loading policies",
49               values: [
50                   ["allow", Ci.nsIPermissionManager.ALLOW_ACTION],
51                   ["deny", Ci.nsIPermissionManager.DENY_ACTION] ],
52               related_prefs: [
53                   [ "permissions.default.image", "This prefreence defines the default image loading policy "
54                                                  + "for sites not listed in the permission list.  The value "
55                                                  + "1 means all images should be loaded, 2 means no images "
56                                                  + "should be loaded, and 3 means only third-party images "
57                                                  + "are blocked." ] ]
58             },
60     install : {desc: "specifies a whitelist of sites from which XPI files may be opened",
61               values: [
62                   ["allow", Ci.nsIPermissionManager.ALLOW_ACTION] ]
63               }
67  * cookie
68  *
69  *
70  *
71  * popup
72  *   dom.popup_maximum  -  The number of pop-ups to allow from a single non-click event
73  *
74  *   dom.popup_allowed_events
75  *
76  *   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.
77  *
78  *
79  */
81 interactive("permission-manager", "View or edit the host-specific "
82             + "permission list.\nThis list is used for the popup"
83             + "blocker whitelist, among other things.",
84             function (I) {
85                 I.minibuffer.message("Save the file and close the editor when done editing permissions.");
87                 let existing_perms = new string_hashmap();
89                 var file_buf =
90                     "# -*- conf -*-\n" +
91                     "# Permission list\n\n";
94                 {
95                     let e = permission_manager.enumerator;
96                     let arr = [];
97                     let max_host_len = 0;
98                     let max_type_len = 0;
99                     while (e.hasMoreElements()) {
100                         let p = e.getNext().QueryInterface(Ci.nsIPermission);
101                         let host = p.host;
102                         let type = p.type;
103                         let cap = p.capability;
104                         if (max_host_len < host.length)
105                             max_host_len = host.length;
106                         if (max_type_len < type.length)
107                             max_type_len = type.length;
108                         arr.push([host, type, cap]);
109                         existing_perms.put([host, type], cap);
110                     }
111                     ++max_host_len;
112                     ++max_type_len;
113                     let max_host = get_spaces(max_host_len);
114                     let max_type = get_spaces(max_type_len);
115                     for (let i = 0; i < arr.length; ++i) {
116                         let [host, type, cap] = arr[i];
117                         if (permission_types.hasOwnProperty(type)) {
118                             let values = permission_types[type].values;
119                             for (let j = 0; j < values.length; ++j) {
120                                 if (cap == values[j][1]) {
121                                     cap = values[j][0];
122                                     break;
123                                 }
124                             }
125                         }
126                         file_buf += host + max_host.substr(host.length) + type + max_type.substr(type.length) + cap + "\n";
127                     }
129                     if (arr.length == 0)
130                         file_buf += "\n";
131                 }
133                 file_buf += "\n" +
134                     "# entry syntax (one per line): <domain> <type> <permission>\n\n" +
135                     "# example: google.com popup allow\n\n" +
137                     word_wrap("The <domain> must be a valid domain name.  Depending on the <type>, only exact " +
138                               "matches may be used, or alternatively it may match any sub-domain if a more " +
139                               "specific entry is not found.", 80, "# ") + "\n" +
140                     "# The possible values for the permission <type> include:\n";
141                 for (let type in permission_types) {
142                     let data = permission_types[type];
143                     file_buf += "#   " + type + " - " + data.desc + "\n\n";
144                     file_buf += "#     Supported <permission> values:\n";
145                     for (let i = 0; i < data.values.length; ++i) {
146                         let x = data.values[i];
147                         file_buf += "#       " + x[0] + " (" + x[1] + ")";
148                         if (x[3])
149                             file_buf += " - " + x[3];
150                         file_buf += "\n";
151                     }
152                     if (data.related_prefs && data.related_prefs.length > 0) {
153                         file_buf += "\n#     Related Mozilla preferences:\n";
154                         for (let i = 0; i < data.related_prefs.length; ++i) {
155                             let x = data.related_prefs[i];
156                             file_buf += "#       " + x[0] + " = " + get_pref(x[0]) + "\n";
157                             if (x.length > 1) {
158                                 file_buf += word_wrap(x[1], 80, "#         ", "#");
159                             }
160                             file_buf += "\n";
161                         }
162                     }
163                 }
164                 var file = null;
165                 try {
166                     file = get_temporary_file("permissions.txt");
167                     let line = 4;
169                     outer: while (1) {
171                         write_text_file(file, file_buf);
172                         yield open_file_with_external_editor(file, $line = line);
174                         let new_buf = read_text_file(file);
175                         if (new_buf == file_buf) {
176                             I.minibuffer.message("No permission changes made.");
177                             break;
178                         }
180                         // Parse
181                         let lines = new_buf.split("\n");
182                         if (lines[lines.length - 1].length == 0) // Remove extra line at end
183                             lines.length = lines.length - 1;
184                         let arr = [];
185                         let prev_entries = new string_hashset();
186                         for (let i = 0; i < lines.length; ++i) {
187                             // Parse each line, checking for syntax errors
188                             let x = lines[i];
189                             let idx = x.indexOf('#');
190                             if (idx != -1)
191                                 x = x.substr(0, idx);
192                             let parts = x.split(/\s+/);
193                             if (parts.length == 1 && parts[0].length == 0)
194                                 continue; // ignore blank line
195                             try {
196                                 let host = parts[0];
197                                 if (!/[a-zA-Z0-9-_]+(\.[a-zA-Z0-9-_]+)*/.test(host))
198                                     throw "invalid host name: " + host;
199                                 if (parts.length < 2)
200                                     throw "missing permission type";
201                                 let type = parts[1];
202                                 if (parts.length < 3)
203                                     throw "missing permission value";
204                                 let cap = parts[2];
205                                 if (permission_types.hasOwnProperty(type)) {
206                                     let values = permission_types[type].values;
207                                     for (let i = 0; i < values.length; ++i) {
208                                         if (cap == values[i][0]) {
209                                             cap = values[i][1];
210                                             break;
211                                         }
212                                     }
213                                 }
214                                 if (!/([0-9]+)/.test(cap))
215                                     throw "invalid permission value: " + cap;
216                                 cap = parseInt(cap);
217                                 if (parts.length > 3)
218                                     throw "too many terms";
219                                 if (prev_entries.contains([host,type]))
220                                     throw "duplicate entry";
221                                 prev_entries.add([host,type]);
222                                 arr.push([host,type,cap]);
223                             } catch (syntax_err) {
224                                 line = i + 1;
225                                 lines.splice(i+1, 0, "# ERROR on previous line: " + syntax_err, "");
226                                 file_buf = lines.join("\n") + "\n";
227                                 I.minibuffer.message("Correct the syntax error in the permissions list, " +
228                                                      "or close the editor to abort.");
229                                 continue outer;
230                             }
231                         }
232                         let num_added = 0;
233                         let num_changed = 0;
234                         for (let i = 0; i < arr.length; ++i) {
235                             let [host,type,cap] = arr[i];
236                             let x = existing_perms.get([host,type]);
237                             let add = false;
238                             if (x === undefined) {
239                                 ++num_added;
240                                 add = true;
241                             } else {
242                                 if (x != cap) {
243                                     ++num_changed;
244                                     add = true;
245                                 }
246                                 existing_perms.remove([host,type]);
247                             }
248                             if (add)
249                                 permission_manager.add(make_uri("http://" + host), type, cap);
250                         }
251                         let num_removed = 0;
252                         for (let k in existing_perms.iterator(true)) {
253                             let [host,type] = k.split(",",2);
254                             ++num_removed;
255                             permission_manager.remove(host,type);
256                         }
257                         let msg;
258                         if (num_added == 0 && num_changed == 0 && num_removed == 0)
259                             msg = "No permission changes made.";
260                         else {
261                             msg = "Updated permissions list: " +
262                                 [["added", num_added],
263                                  ["changed", num_changed],
264                                  ["removed", num_removed]].
265                                 filter(function ([caption, count]) count > 0).
266                                 map(function ([caption, count]) {
267                                         if (count == 1)
268                                              return "1 entry " + caption;
269                                         return count + " entries " + caption;
270                                     }).join(", ") +
271                                 ".";
272                         }
273                         I.minibuffer.message(msg);
274                         break;
275                     }
276                 } catch (e) {
277                     dump_error(e);
278                     throw interactive_error("Failed to edit permissions list in external editor.");
279                 } finally {
280                     file.remove(false);
281                 }
282             });
284 provide("permission-manager");