permissions.js
Go to the documentation of this file.
1 # -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 # ***** BEGIN LICENSE BLOCK *****
3 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 #
5 # The contents of this file are subject to the Mozilla Public License Version
6 # 1.1 (the "License"); you may not use this file except in compliance with
7 # the License. You may obtain a copy of the License at
8 # http://www.mozilla.org/MPL/
9 #
10 # Software distributed under the License is distributed on an "AS IS" basis,
11 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 # for the specific language governing rights and limitations under the
13 # License.
14 #
15 # The Original Code is the Firefox Preferences System.
16 #
17 # The Initial Developer of the Original Code is
18 # Ben Goodger.
19 # Portions created by the Initial Developer are Copyright (C) 2005
20 # the Initial Developer. All Rights Reserved.
21 #
22 # Contributor(s):
23 # Ben Goodger <ben@mozilla.org>
24 # Blake Ross <firefox@blakeross.com>
25 #
26 # Alternatively, the contents of this file may be used under the terms of
27 # either the GNU General Public License Version 2 or later (the "GPL"), or
28 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 # in which case the provisions of the GPL or the LGPL are applicable instead
30 # of those above. If you wish to allow use of your version of this file only
31 # under the terms of either the GPL or the LGPL, and not to allow others to
32 # use your version of this file under the terms of the MPL, indicate your
33 # decision by deleting the provisions above and replace them with the notice
34 # and other provisions required by the GPL or the LGPL. If you do not delete
35 # the provisions above, a recipient may use your version of this file under
36 # the terms of any one of the MPL, the GPL or the LGPL.
37 #
38 # ***** END LICENSE BLOCK *****
39 
40 const nsIPermissionManager = Components.interfaces.nsIPermissionManager;
41 const nsICookiePermission = Components.interfaces.nsICookiePermission;
42 
43 function Permission(host, rawHost, type, capability, perm)
44 {
45  this.host = host;
46  this.rawHost = rawHost;
47  this.type = type;
48  this.capability = capability;
49  this.perm = perm;
50 }
51 
53  _type : "",
54  _permissions : [],
55  _pm : Components.classes["@mozilla.org/permissionmanager;1"]
56  .getService(Components.interfaces.nsIPermissionManager),
57  _bundle : null,
58  _tree : null,
59 
60  _view: {
61  _rowCount: 0,
62  get rowCount()
63  {
64  return this._rowCount;
65  },
66  getCellText: function (aRow, aColumn)
67  {
68  if (aColumn.id == "siteCol")
69  return gPermissionManager._permissions[aRow].rawHost;
70  else if (aColumn.id == "statusCol")
71  return gPermissionManager._permissions[aRow].capability;
72  return "";
73  },
74 
75  isSeparator: function(aIndex) { return false; },
76  isSorted: function() { return false; },
77  isContainer: function(aIndex) { return false; },
78  setTree: function(aTree){},
79  getImageSrc: function(aRow, aColumn) {},
80  getProgressMode: function(aRow, aColumn) {},
81  getCellValue: function(aRow, aColumn) {},
82  cycleHeader: function(column) {},
83  getRowProperties: function(row,prop){},
84  getColumnProperties: function(column,prop){},
85  getCellProperties: function(row,column,prop){
86  if (column.element.getAttribute("id") == "siteCol")
87  prop.AppendElement(this._ltrAtom);
88  }
89  },
90 
91  _getCapabilityString: function (aCapability)
92  {
93  var stringKey = null;
94  switch (aCapability) {
95  case nsIPermissionManager.ALLOW_ACTION:
96  stringKey = "can";
97  break;
98  case nsIPermissionManager.DENY_ACTION:
99  stringKey = "cannot";
100  break;
101  case nsICookiePermission.ACCESS_SESSION:
102  stringKey = "canSession";
103  break;
104  }
105  return this._bundle.getString(stringKey);
106  },
107 
108  addPermission: function (aCapability)
109  {
110  var textbox = document.getElementById("url");
111  var host = textbox.value.replace(/^\s*([-\w]*:\/+)?/, ""); // trim any leading space and scheme
112  try {
113  var ioService = Components.classes["@mozilla.org/network/io-service;1"]
114  .getService(Components.interfaces.nsIIOService);
115  var uri = ioService.newURI("http://"+host, null, null);
116  host = uri.host;
117  } catch(ex) {
118  var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
119  .getService(Components.interfaces.nsIPromptService);
120  var message = this._bundle.getString("invalidURI");
121  var title = this._bundle.getString("invalidURITitle");
122  promptService.alert(window, title, message);
123  return;
124  }
125 
126  var capabilityString = this._getCapabilityString(aCapability);
127 
128  // check whether the permission already exists, if not, add it
129  var exists = false;
130  for (var i = 0; i < this._permissions.length; ++i) {
131  if (this._permissions[i].rawHost == host) {
132  // Avoid calling the permission manager if the capability settings are
133  // the same. Otherwise allow the call to the permissions manager to
134  // update the listbox for us.
135  exists = this._permissions[i].perm == aCapability;
136  break;
137  }
138  }
139 
140  if (!exists) {
141  host = (host.charAt(0) == ".") ? host.substring(1,host.length) : host;
142  var uri = ioService.newURI("http://" + host, null, null);
143  this._pm.add(uri, this._type, aCapability);
144  }
145  textbox.value = "";
146  textbox.focus();
147 
148  // covers a case where the site exists already, so the buttons don't disable
149  this.onHostInput(textbox);
150 
151  // enable "remove all" button as needed
152  document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
153  },
154 
155  onHostInput: function (aSiteField)
156  {
157  document.getElementById("btnSession").disabled = !aSiteField.value;
158  document.getElementById("btnBlock").disabled = !aSiteField.value;
159  document.getElementById("btnAllow").disabled = !aSiteField.value;
160  },
161 
162  onHostKeyPress: function (aEvent)
163  {
164  if (aEvent.keyCode == KeyEvent.DOM_VK_RETURN)
165  document.getElementById("btnAllow").click();
166  },
167 
168  onLoad: function ()
169  {
170  this._bundle = document.getElementById("bundlePreferences");
171  var params = window.arguments[0];
172  this.init(params);
173  },
174 
175  init: function (aParams)
176  {
177  if (this._type) {
178  // reusing an open dialog, clear the old observer
179  this.uninit();
180  }
181 
182  this._type = aParams.permissionType;
183  this._manageCapability = aParams.manageCapability;
184 
185  var permissionsText = document.getElementById("permissionsText");
186  while (permissionsText.hasChildNodes())
187  permissionsText.removeChild(permissionsText.firstChild);
188  permissionsText.appendChild(document.createTextNode(aParams.introText));
189 
190  document.title = aParams.windowTitle;
191 
192  document.getElementById("btnBlock").hidden = !aParams.blockVisible;
193  document.getElementById("btnSession").hidden = !aParams.sessionVisible;
194  document.getElementById("btnAllow").hidden = !aParams.allowVisible;
195 
196  var urlFieldVisible = (aParams.blockVisible || aParams.sessionVisible || aParams.allowVisible);
197 
198  var urlField = document.getElementById("url");
199  urlField.value = aParams.prefilledHost;
200  urlField.hidden = !urlFieldVisible;
201 
202  this.onHostInput(urlField);
203 
204  var urlLabel = document.getElementById("urlLabel");
205  urlLabel.hidden = !urlFieldVisible;
206 
207  var os = Components.classes["@mozilla.org/observer-service;1"]
208  .getService(Components.interfaces.nsIObserverService);
209  os.addObserver(this, "perm-changed", false);
210 
211  if (this._type == "install") {
212  var enumerator = this._pm.enumerator;
213  if (!enumerator.hasMoreElements())
214  this._updatePermissions();
215  }
216 
217  this._loadPermissions();
218 
219  urlField.focus();
220 
221  this._ltrAtom = Components.classes["@mozilla.org/atom-service;1"]
222  .getService(Components.interfaces.nsIAtomService)
223  .getAtom("ltr");
224  },
225 
226  uninit: function ()
227  {
228  var os = Components.classes["@mozilla.org/observer-service;1"]
229  .getService(Components.interfaces.nsIObserverService);
230  os.removeObserver(this, "perm-changed");
231  },
232 
233  observe: function (aSubject, aTopic, aData)
234  {
235  if (aTopic == "perm-changed") {
236  var permission = aSubject.QueryInterface(Components.interfaces.nsIPermission);
237  if (aData == "added") {
238  this._addPermissionToList(permission);
239  ++this._view._rowCount;
240  this._tree.treeBoxObject.rowCountChanged(this._view.rowCount - 1, 1);
241  // Re-do the sort, since we inserted this new item at the end.
242  gTreeUtils.sort(this._tree, this._view, this._permissions,
243  this._lastPermissionSortColumn,
244  this._lastPermissionSortAscending);
245  }
246  else if (aData == "changed") {
247  for (var i = 0; i < this._permissions.length; ++i) {
248  if (this._permissions[i].host == permission.host) {
249  this._permissions[i].capability = this._getCapabilityString(permission.capability);
250  break;
251  }
252  }
253  // Re-do the sort, if the status changed from Block to Allow
254  // or vice versa, since if we're sorted on status, we may no
255  // longer be in order.
256  if (this._lastPermissionSortColumn.id == "statusCol") {
257  gTreeUtils.sort(this._tree, this._view, this._permissions,
258  this._lastPermissionSortColumn,
259  this._lastPermissionSortAscending);
260  }
261  this._tree.treeBoxObject.invalidate();
262  }
263  // No UI other than this window causes this method to be sent a "deleted"
264  // notification, so we don't need to implement it since Delete is handled
265  // directly by the Permission Removal handlers. If that ever changes, those
266  // implementations will have to move into here.
267  }
268  },
269 
270  onPermissionSelected: function ()
271  {
272  var hasSelection = this._tree.view.selection.count > 0;
273  var hasRows = this._tree.view.rowCount > 0;
274  document.getElementById("removePermission").disabled = !hasRows || !hasSelection;
275  document.getElementById("removeAllPermissions").disabled = !hasRows;
276  },
277 
278  onPermissionDeleted: function ()
279  {
280  if (!this._view.rowCount)
281  return;
282  var removedPermissions = [];
283  gTreeUtils.deleteSelectedItems(this._tree, this._view, this._permissions, removedPermissions);
284  for (var i = 0; i < removedPermissions.length; ++i) {
285  var p = removedPermissions[i];
286  this._pm.remove(p.host, p.type);
287  }
288  document.getElementById("removePermission").disabled = !this._permissions.length;
289  document.getElementById("removeAllPermissions").disabled = !this._permissions.length;
290  },
291 
292  onAllPermissionsDeleted: function ()
293  {
294  if (!this._view.rowCount)
295  return;
296  var removedPermissions = [];
297  gTreeUtils.deleteAll(this._tree, this._view, this._permissions, removedPermissions);
298  for (var i = 0; i < removedPermissions.length; ++i) {
299  var p = removedPermissions[i];
300  this._pm.remove(p.host, p.type);
301  }
302  document.getElementById("removePermission").disabled = true;
303  document.getElementById("removeAllPermissions").disabled = true;
304  },
305 
306  onPermissionKeyPress: function (aEvent)
307  {
308  if (aEvent.keyCode == 46)
309  this.onPermissionDeleted();
310  },
311 
312  _lastPermissionSortColumn: "",
313  _lastPermissionSortAscending: false,
314 
315  onPermissionSort: function (aColumn)
316  {
317  this._lastPermissionSortAscending = gTreeUtils.sort(this._tree,
318  this._view,
319  this._permissions,
320  aColumn,
321  this._lastPermissionSortColumn,
322  this._lastPermissionSortAscending);
323  this._lastPermissionSortColumn = aColumn;
324  },
325 
326  _loadPermissions: function ()
327  {
328  this._tree = document.getElementById("permissionsTree");
329  this._permissions = [];
330 
331  // load permissions into a table
332  var count = 0;
333  var enumerator = this._pm.enumerator;
334  while (enumerator.hasMoreElements()) {
335  var nextPermission = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission);
336  this._addPermissionToList(nextPermission);
337  }
338 
339  this._view._rowCount = this._permissions.length;
340 
341  // sort and display the table
342  this._tree.treeBoxObject.view = this._view;
343  this.onPermissionSort("rawHost", false);
344 
345  // disable "remove all" button if there are none
346  document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
347  },
348 
349  _addPermissionToList: function (aPermission)
350  {
351  if (aPermission.type == this._type &&
352  (!this._manageCapability ||
353  (aPermission.capability == this._manageCapability))) {
354 
355  var host = aPermission.host;
356  var capabilityString = this._getCapabilityString(aPermission.capability);
357  var p = new Permission(host,
358  (host.charAt(0) == ".") ? host.substring(1,host.length) : host,
359  aPermission.type,
360  capabilityString,
361  aPermission.capability);
362  this._permissions.push(p);
363  }
364  },
365 
366  _updatePermissions: function ()
367  {
368  try {
369  var ioService = Components.classes["@mozilla.org/network/io-service;1"]
370  .getService(Components.interfaces.nsIIOService);
371  var pbi = Components.classes["@mozilla.org/preferences-service;1"]
372  .getService(Components.interfaces.nsIPrefBranch2);
373  var prefList = [["xpinstall.whitelist.add", nsIPermissionManager.ALLOW_ACTION],
374  ["xpinstall.whitelist.add.36", nsIPermissionManager.ALLOW_ACTION],
375  ["xpinstall.blacklist.add", nsIPermissionManager.DENY_ACTION]];
376 
377  for (var i = 0; i < prefList.length; ++i) {
378  try {
379  // this pref is a comma-delimited list of hosts
380  var hosts = pbi.getCharPref(prefList[i][0]);
381  } catch(ex) {
382  continue;
383  }
384 
385  if (!hosts)
386  continue;
387 
388  hostList = hosts.split(",");
389  var capability = prefList[i][1];
390  for (var j = 0; j < hostList.length; ++j) {
391  // trim leading and trailing spaces
392  var host = hostList[j].replace(/^\s*/,"").replace(/\s*$/,"");
393  try {
394  var uri = ioService.newURI("http://" + host, null, null);
395  this._pm.add(uri, this._type, capability);
396  } catch(ex) { }
397  }
398  pbi.setCharPref(prefList[i][0], "");
399  }
400  } catch(ex) { }
401  },
402 
403  setHost: function (aHost)
404  {
405  document.getElementById("url").value = aHost;
406  }
407 };
408 
409 function setHost(aHost)
410 {
411  gPermissionManager.setHost(aHost);
412 }
413 
414 function initWithParams(aParams)
415 {
416  gPermissionManager.init(aParams);
417 }
418 
const nsIPermissionManager
Definition: permissions.js:40
var gPermissionManager
Definition: permissions.js:52
_updateCookies aHost
var ioService
let window
gImageView getCellProperties
Definition: pageInfo.js:171
function initWithParams(aParams)
Definition: permissions.js:414
_window init
Definition: FeedWriter.js:1144
var count
Definition: test_bug7406.js:32
function setHost(aHost)
Definition: permissions.js:409
Lastfm onLoad
Definition: mini.js:36
var gTreeUtils
GstMessage * message
return null
Definition: FeedWriter.js:1143
var os
var uri
Definition: FeedWriter.js:1135
let promptService
function Permission(host, rawHost, type, capability, perm)
Definition: permissions.js:43
const nsICookiePermission
Definition: permissions.js:41
function uninit(aEvent)
Definition: aboutDialog.js:89
_getSelectedPageStyle s i
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe