PageRenderTime 56ms CodeModel.GetById 12ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

/security/manager/pki/resources/content/crlManager.js

http://github.com/zpao/v8monkey
JavaScript | 255 lines | 179 code | 25 blank | 51 comment | 16 complexity | 937f21cebcfbbb1d340416d9f939e72d MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is mozilla.org code.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Netscape Communications Corporation.
 18 * Portions created by the Initial Developer are Copyright (C) 2001
 19 * the Initial Developer. All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *   David Drinan <ddrinan@netscape.com>
 23 *
 24 * Alternatively, the contents of this file may be used under the terms of
 25 * either the GNU General Public License Version 2 or later (the "GPL"), or
 26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 27 * in which case the provisions of the GPL or the LGPL are applicable instead
 28 * of those above. If you wish to allow use of your version of this file only
 29 * under the terms of either the GPL or the LGPL, and not to allow others to
 30 * use your version of this file under the terms of the MPL, indicate your
 31 * decision by deleting the provisions above and replace them with the notice
 32 * and other provisions required by the GPL or the LGPL. If you do not delete
 33 * the provisions above, a recipient may use your version of this file under
 34 * the terms of any one of the MPL, the GPL or the LGPL.
 35 *
 36 * ***** END LICENSE BLOCK ***** */
 37
 38const nsICRLManager = Components.interfaces.nsICRLManager;
 39const nsCRLManager  = "@mozilla.org/security/crlmanager;1";
 40const nsICRLInfo = Components.interfaces.nsICRLInfo;
 41const nsISupportsArray = Components.interfaces.nsISupportsArray;
 42const nsIPKIParamBlock    = Components.interfaces.nsIPKIParamBlock;
 43const nsPKIParamBlock    = "@mozilla.org/security/pkiparamblock;1";
 44const nsIPrefService      = Components.interfaces.nsIPrefService;
 45
 46var crlManager;
 47var crls;
 48var prefService;
 49var prefBranch;
 50
 51var autoupdateEnabledBaseString   = "security.crl.autoupdate.enable.";
 52var autoupdateTimeTypeBaseString  = "security.crl.autoupdate.timingType.";
 53var autoupdateTimeBaseString      = "security.crl.autoupdate.nextInstant.";
 54var autoupdateURLBaseString       = "security.crl.autoupdate.url.";
 55var autoupdateErrCntBaseString    = "security.crl.autoupdate.errCount.";
 56var autoupdateErrDetailBaseString = "security.crl.autoupdate.errDetail.";
 57var autoupdateDayCntString        = "security.crl.autoupdate.dayCnt.";
 58var autoupdateFreqCntString       = "security.crl.autoupdate.freqCnt.";
 59
 60function onLoad()
 61{
 62  var crlEntry;
 63  var i;
 64
 65  crlManager = Components.classes[nsCRLManager].getService(nsICRLManager);
 66  crls = crlManager.getCrls();
 67  prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(nsIPrefService);
 68  prefBranch = prefService.getBranch(null);
 69  var bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
 70  var autoupdateEnabledString;
 71  var autoupdateErrCntString;
 72
 73  for (i=0; i<crls.length; i++) {
 74    crlEntry = crls.queryElementAt(i, nsICRLInfo);
 75    var org = crlEntry.organization;
 76    var orgUnit = crlEntry.organizationalUnit;
 77    var lastUpdate = crlEntry.lastUpdateLocale;
 78    var nextUpdate = crlEntry.nextUpdateLocale;
 79    autoupdateEnabledString    = autoupdateEnabledBaseString + crlEntry.nameInDb;
 80    autoupdateErrCntString    = autoupdateErrCntBaseString + crlEntry.nameInDb;
 81    var enabled = false;
 82    var enabledStr = bundle.GetStringFromName("crlAutoupdateNotEnabled");
 83    var status = "";
 84    try{
 85      enabled = prefBranch.getBoolPref(autoupdateEnabledString)
 86      if(enabled){
 87        enabledStr = bundle.GetStringFromName("crlAutoupdateEnabled");
 88      }
 89      var cnt;
 90      cnt = prefBranch.getIntPref(autoupdateErrCntString);
 91      if(cnt > 0){
 92        status = bundle.GetStringFromName("crlAutoupdateFailed");
 93      } else {
 94        status = bundle.GetStringFromName("crlAutoupdateOk");
 95      }
 96    }catch(exception){}
 97    
 98    AddItem("crlList", [org, orgUnit, lastUpdate, nextUpdate, enabledStr, status], "crltree_", i);
 99  }
100}
101
102function AddItem(children,cells,prefix,idfier)
103{
104  var kids = document.getElementById(children);
105  var item  = document.createElement("treeitem");
106  var row   = document.createElement("treerow");
107  for(var i = 0; i < cells.length; i++)
108  {
109    var cell  = document.createElement("treecell");
110    cell.setAttribute("class", "propertylist");
111    cell.setAttribute("label", cells[i])
112    row.appendChild(cell);
113  }
114  item.appendChild(row);
115  item.setAttribute("id",prefix + idfier);
116  kids.appendChild(item);
117}
118
119function DeleteCrlSelected() {
120  var crlEntry;
121
122  // delete selected item
123  var crltree = document.getElementById("crltree");
124  var i = crltree.currentIndex;
125  if(i<0){
126    return;
127  }
128  crlEntry = crls.queryElementAt(i, nsICRLInfo);
129    
130  var autoupdateEnabled = false;
131  var autoupdateParamAvailable = false;
132  var id = crlEntry.nameInDb;
133  
134  //First, check if autoupdate was enabled for this crl
135  try {
136    autoupdateEnabled = prefBranch.getBoolPref(autoupdateEnabledBaseString + id);
137    //Note, if the pref is not present, we get an exception right here,
138    //and autoupdateEnabled remains false
139    autoupdateParamAvailable = true;
140    prefBranch.clearUserPref(autoupdateEnabledBaseString + id);
141    prefBranch.clearUserPref(autoupdateTimeTypeBaseString + id);
142    prefBranch.clearUserPref(autoupdateTimeBaseString + id);
143    prefBranch.clearUserPref(autoupdateURLBaseString + id);
144    prefBranch.clearUserPref(autoupdateDayCntString + id);
145    prefBranch.clearUserPref(autoupdateFreqCntString + id);
146    prefBranch.clearUserPref(autoupdateErrCntBaseString + id);
147    prefBranch.clearUserPref(autoupdateErrDetailBaseString + id);
148  } catch(Exception){}
149
150  //Once we have deleted the prefs that can be deleted, we save the 
151  //file if relevant, restart the scheduler, and once we are successful 
152  //in doind that, we try to delete the crl 
153  try{
154    if(autoupdateParamAvailable){
155      prefService.savePrefFile(null);
156    }
157
158    if(autoupdateEnabled){
159      crlManager.rescheduleCRLAutoUpdate();
160    }
161          
162    // Now, try to delete it
163    crlManager.deleteCrl(i);
164    DeleteItemSelected("crltree", "crltree_", "crlList");
165    //To do: If delete fails, we should be able to retrieve the deleted
166    //settings
167    //XXXXXXXXXXXXXXXXXXXXX
168  
169  }catch(exception) {
170    //To Do: Possibly show an error ...
171    //XXXXXXXXXXXX
172  }
173
174  EnableCrlActions();
175}
176
177function EnableCrlActions() {
178  var tree = document.getElementById("crltree");
179  if (tree.view.selection.count) {
180    document.getElementById("deleteCrl").removeAttribute("disabled");
181    document.getElementById("editPrefs").removeAttribute("disabled");
182    document.getElementById("updateCRL").removeAttribute("disabled");
183  } else {
184    document.getElementById("deleteCrl").setAttribute("disabled", "true");
185    document.getElementById("editPrefs").setAttribute("disabled", "true");
186    document.getElementById("updateCRL").setAttribute("disabled", "true");
187  }
188}
189
190function DeleteItemSelected(tree, prefix, kids) {
191  var i;
192  var delnarray = [];
193  var rv = "";
194  var cookietree = document.getElementById(tree);
195  var rangeCount = cookietree.view.selection.getRangeCount();
196  for(i = 0; i < rangeCount; ++i) 
197  { 
198    var start = {}, end = {};
199    cookietree.view.selection.getRangeAt(i, start, end);
200    for (var k = start.value; k <= end.value; ++k) {
201      var item = cookietree.contentView.getItemAtIndex(k);
202      delnarray[i] = document.getElementById(item.id);
203      var itemid = parseInt(item.id.substring(prefix.length, item.id.length));
204      rv += (itemid + ",");
205    }
206  }
207  for(i = 0; i < delnarray.length; i++) 
208  { 
209    document.getElementById(kids).removeChild(delnarray[i]);
210  }
211  return rv;
212}
213
214function EditAutoUpdatePrefs() {
215  var crlEntry;
216
217  // delete selected item
218  var crltree = document.getElementById("crltree");
219  var i = crltree.currentIndex;
220  if(i<0){
221    return;
222  }
223  crlEntry = crls.queryElementAt(i, nsICRLInfo);
224  var params = Components.classes[nsPKIParamBlock].createInstance(nsIPKIParamBlock);
225  params.setISupportAtIndex(1, crlEntry);
226  window.openDialog("chrome://pippki/content/pref-crlupdate.xul","",
227                    "chrome,centerscreen,modal", params);
228}
229
230function UpdateCRL()
231{
232  var crlEntry;
233  var crltree = document.getElementById("crltree");
234  var i = crltree.currentIndex;
235  if(i<0){
236    return;
237  }
238  crlEntry = crls.queryElementAt(i, nsICRLInfo);
239  crlManager.updateCRLFromURL(crlEntry.lastFetchURL, crlEntry.nameInDb);
240}
241
242function ImportCRL()
243{
244  // prompt for the URL to import from
245  var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
246  
247  var CRLLocation = {value:null};
248  var dummy = { value: 0 };
249  var strBundle = document.getElementById('pippki_bundle');
250  var addCRL = promptService.prompt(window, strBundle.getString('crlImportNewCRLTitle'), 
251                                    strBundle.getString('crlImportNewCRLLabel'),  CRLLocation, null, dummy);
252
253  if (addCRL)
254    crlManager.updateCRLFromURL(CRLLocation.value, "");
255}