summaryrefslogtreecommitdiffstats
path: root/comm/suite/components/permissions/content/permissionsManager.js
blob: 9004dc3cdac88a1cac6381dc63e8afac0a640afe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");

var permissions = [];
var removals = [];

var sortColumn;
var sortAscending;

var permissionsTreeView = {
    rowCount: 0,
    setTree: function(tree) {},
    getImageSrc: function(row, column) {},
    getProgressMode: function(row, column) {},
    getCellValue: function(row, column) {},
    getCellText: function(row, column) { return permissions[row][column.id]; },
    isSeparator: function(index) { return false; },
    isSorted: function() { return false; },
    isContainer: function(index) { return false; },
    cycleHeader: function(column) {},
    getRowProperties: function(row, column) { return ""; },
    getColumnProperties: function(column) { return ""; },
    getCellProperties: function(row, column) { return ""; }
  };

var permissionsTree;
var permissionType = "popup";
var gManageCapability;

var permissionsBundle;

function Startup() {
  var introText, windowTitle;

  permissionsTree = document.getElementById("permissionsTree");

  permissionsBundle = document.getElementById("permissionsBundle");

  sortAscending = (permissionsTree.getAttribute("sortAscending") == "true");
  sortColumn = permissionsTree.getAttribute("sortColumn");

  var params = { blockVisible   : true,
                 sessionVisible : true,
                 allowVisible   : true,
                 manageCapability : true
               };

  if (window.arguments && window.arguments[0]) {
    params = window.arguments[0];
    setHost(params.prefilledHost);
    permissionType = params.permissionType;
    gManageCapability = params.manageCapability;
    introText = params.introText;
    windowTitle = params.windowTitle;
  }

  document.getElementById("btnBlock").hidden = !params.blockVisible;
  document.getElementById("btnSession").hidden = !params.sessionVisible;
  document.getElementById("btnAllow").hidden = !params.allowVisible;

  document.getElementById("permissionsText").textContent = introText ||
      permissionsBundle.getString(permissionType + "permissionstext");

  document.title = windowTitle ||
      permissionsBundle.getString(permissionType + "permissionstitle");

  var dialogElement = document.getElementById("permissionsManager");
  dialogElement.setAttribute("windowtype", "permissions-" + permissionType);

  var urlFieldVisible = params.blockVisible ||
                        params.sessionVisible ||
                        params.allowVisible;

  document.getElementById("url").hidden = !urlFieldVisible;
  document.getElementById("urlLabel").hidden = !urlFieldVisible;

  handleHostInput(document.getElementById("url").value);
  loadPermissions();
}

function onAccept() {
  finalizeChanges();
  reInitialize();

  // Don't close the window.
  return false;
}

function onCancel() {
  reInitialize();

  // Don't close the window.
  return false;
}

function reInitialize() {
  permissions = [];
  removals = [];

  // loadPermissions will reverse the sort direction so flip it now.
  sortAscending = !sortAscending;

  // Reload permissions tree.
  loadPermissions();
}

function setHost(aHost) {
  document.getElementById("url").value = aHost;
}

function Permission(id, principal, host, type, capability, perm) {
  this.id = id;
  this.principal = principal;
  this.host = host;
  this.rawHost = host.replace(/^\./, "");
  this.type = type;
  this.capability = capability;
  this.perm = perm;
}

function loadPermissions() {
  var enumerator = Services.perms.enumerator;
  var count = 0;
  var permission;

  try {
    while (enumerator.hasMoreElements()) {
      permission = enumerator.getNext().QueryInterface(Ci.nsIPermission);
      if (permission.type == permissionType &&
          (!gManageCapability || permission.capability == gManageCapability)) {
        permissions.push(new Permission(count++,
                                        permission.principal,
                                        permission.principal.URI.host,
                                        permission.type,
                                        capabilityString(permission.capability),
                                        permission.capability));
      }
    }
  } catch(ex) {
  }

  permissionsTreeView.rowCount = permissions.length;

  // sort and display the table
  permissionsTree.view = permissionsTreeView;
  permissionColumnSort(sortColumn, false);

  // disable "remove all" button if there are none
  document.getElementById("removeAllPermissions").disabled =
    permissions.length == 0;
}

function capabilityString(aCapability) {
  var capability = null;
  switch (aCapability) {
    case Ci.nsIPermissionManager.ALLOW_ACTION:
      capability = "can";
      break;
    case Ci.nsIPermissionManager.DENY_ACTION:
      capability = "cannot";
      break;
    // we should only ever hit this for cookies
    case Ci.nsICookiePermission.ACCESS_SESSION:
      capability = "canSession";
      break;
    default:
      break;
  }
  return permissionsBundle.getString(capability);
}

function permissionColumnSort(aColumn, aUpdateSelection) {
  sortAscending =
    SortTree(permissionsTree, permissionsTreeView, permissions,
             aColumn, sortColumn, sortAscending, aUpdateSelection);
  sortColumn = aColumn;

  SetSortDirection(permissionsTree, aColumn, sortAscending);
}

function deletePermissions() {
  DeleteSelectedItemFromTree(permissionsTree, permissionsTreeView,
                             permissions, removals,
                             "removePermission", "removeAllPermissions");
}

function deleteAllPermissions() {
  DeleteAllFromTree(permissionsTree, permissionsTreeView, permissions,
                    removals, "removePermission", "removeAllPermissions");
}

function finalizeChanges() {
  let p;

  for (let i in permissions) {
    p = permissions[i];
    try {
      // Principal is null so a permission we just added in this session.
      if (p.principal == null) {
        let uri = Services.io.newURI("https://" + p.host);
        Services.perms.add(uri, p.type, p.perm);
      }
    } catch(ex) {
    }
  }

  for (let i in removals) {
    p = removals[i];
    try {
      // Principal is not null so not a permission we just added in this
      // session.
      if (p.principal) {
        Services.perms.removeFromPrincipal(p.principal,
                                           p.type);
      }
    } catch(ex) {
    }
  }
}

function handlePermissionKeyPress(e) {
  if (e.keyCode == KeyEvent.DOM_VK_DELETE ||
      (AppConstants.platform == "macosx" &&
       e.keyCode == KeyEvent.DOM_VK_BACK_SPACE)) {
    deletePermissions();
  }
}

function addPermission(aPermission) {
  var textbox = document.getElementById("url");
  // trim any leading and trailing spaces and scheme
  var host = trimSpacesAndScheme(textbox.value);
  try {
    let uri = Services.io.newURI("https://" + host);
    host = uri.host;
  } catch(ex) {
    var message = permissionsBundle.getFormattedString("alertInvalid", [host]);
    var title = permissionsBundle.getString("alertInvalidTitle");
    Services.prompt.alert(window, title, message);
    textbox.value = "";
    textbox.focus();
    handleHostInput("");
    return;
  }

  // we need this whether the perm exists or not
  var stringCapability = capabilityString(aPermission);

  // check whether the permission already exists, if not, add it
  var exists = false;
  for (var i in permissions) {
    if (permissions[i].rawHost == host) {
      // Avoid calling the permission manager if the capability settings are
      // the same. Otherwise allow the call to the permissions manager to
      // update the listbox for us.
      exists = permissions[i].perm == aPermission;
      break;
    }
  }

  if (!exists) {
    permissions.push(new Permission(permissions.length, null, host,
                                    permissionType, stringCapability,
                                    aPermission));

    permissionsTreeView.rowCount = permissions.length;
    permissionsTree.treeBoxObject.rowCountChanged(permissions.length - 1, 1);
    permissionsTree.treeBoxObject.ensureRowIsVisible(permissions.length - 1);
  }
  textbox.value = "";
  textbox.focus();

  // covers a case where the site exists already, so the buttons don't disable
  handleHostInput("");

  // enable "remove all" button as needed
  document.getElementById("removeAllPermissions").disabled = permissions.length == 0;
}

function doHelpButton() {
  openHelp(permissionsBundle.getString(permissionType + "permissionshelp"), "chrome://communicator/locale/help/suitehelp.rdf");
  return true;
}