home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / browser.jar / content / browser / cookieviewer / CookieExceptions.js next >
Encoding:
Text File  |  2005-07-29  |  9.3 KB  |  264 lines

  1. const nsIPermissionManager = Components.interfaces.nsIPermissionManager;
  2. const nsICookiePermission = Components.interfaces.nsICookiePermission;
  3.  
  4. function Permission(id, host, rawHost, type, capability, perm) 
  5. {
  6.   this.id = id;
  7.   this.host = host;
  8.   this.rawHost = rawHost;
  9.   this.type = type;
  10.   this.capability = capability;
  11.   this.perm = perm;
  12. }
  13.  
  14. var gPermissionManager = {
  15.   _type                 : "",
  16.   _addedPermissions     : [],
  17.   _removedPermissions   : [],
  18.   _pm                   : Components.classes["@mozilla.org/permissionmanager;1"]
  19.                                     .getService(Components.interfaces.nsIPermissionManager),
  20.   _bundle               : null,
  21.   _tree                 : null,
  22.   
  23.   _view: {
  24.     _rowCount: 0,
  25.     get rowCount() 
  26.     { 
  27.       return this._rowCount; 
  28.     },
  29.     getCellText: function (aRow, aColumn)
  30.     {
  31.       if (aColumn == "siteCol")
  32.         return gPermissionManager._addedPermissions[aRow].rawHost;
  33.       else if (aColumn == "statusCol")
  34.         return gPermissionManager._addedPermissions[aRow].capability;
  35.       return "";
  36.     },
  37.  
  38.     isSeparator: function(aIndex) { return false; },
  39.     isSorted: function() { return false; },
  40.     isContainer: function(aIndex) { return false; },
  41.     setTree: function(aTree){},
  42.     getImageSrc: function(aRow, aColumn) {},
  43.     getProgressMode: function(aRow, aColumn) {},
  44.     getCellValue: function(aRow, aColumn) {},
  45.     cycleHeader: function(aColId, aElt) {},
  46.     getRowProperties: function(aRow, aColumn, aProperty) {},
  47.     getColumnProperties: function(aColumn, aColumnElement, aProperty) {},
  48.     getCellProperties: function(aRow, aProperty) {}
  49.   },
  50.   
  51.   onOK: function ()
  52.   {
  53.     var pm = Components.classes["@mozilla.org/permissionmanager;1"]
  54.                        .getService(Components.interfaces.nsIPermissionManager);
  55.     for (var i = 0; i < this._removedPermissions.length; ++i) {
  56.       var p = this._removedPermissions[i];
  57.       pm.remove(p.host, p.type);
  58.     }
  59.  
  60.     var uri = Components.classes["@mozilla.org/network/standard-url;1"]
  61.                         .createInstance(Components.interfaces.nsIURI);    
  62.     for (var i = 0; i < this._addedPermissions.length; ++i) {
  63.       var p = this._addedPermissions[i];
  64.       uri.spec = p.host;
  65.       pm.add(uri, p.type, p.perm);
  66.     }
  67.   },
  68.   
  69.   addPermission: function (aPermission)
  70.   {
  71.     var textbox = document.getElementById("url");
  72.     var host = textbox.value.replace(/^\s*([-\w]*:\/+)?/, ""); // trim any leading space and scheme
  73.     try {
  74.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  75.                                 .getService(Components.interfaces.nsIIOService);
  76.       var uri = ioService.newURI("http://"+host, null, null);
  77.       host = uri.host;
  78.     } catch(ex) {
  79.       var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  80.                                     .getService(Components.interfaces.nsIPromptService);
  81.       var message = stringBundle.getString("invalidURI");
  82.       var title = stringBundle.getString("invalidURITitle");
  83.       promptservice.alert(window,title,message);
  84.     }
  85.  
  86.     // we need this whether the perm exists or not
  87.     var stringKey = null;
  88.     switch (aPermission) {
  89.       case nsIPermissionManager.ALLOW_ACTION:
  90.         stringKey = "can";
  91.         break;
  92.       case nsIPermissionManager.DENY_ACTION:
  93.         stringKey = "cannot";
  94.         break;
  95.       case nsICookiePermission.ACCESS_SESSION:
  96.         stringKey = "canSession";
  97.         break;
  98.       default:
  99.         break;
  100.     } 
  101.     // check whether the permission already exists, if not, add it
  102.     var exists = false;
  103.     for (var i = 0; i < this._addedPermissions.length; ++i) {
  104.       if (this._addedPermissions[i].rawHost == host) {
  105.         exists = true;
  106.         this._addedPermissions[i].capability = this._bundle.getString(stringKey);
  107.         this._addedPermissions[i].perm = aPermission;
  108.         break;
  109.       }
  110.     }
  111.     
  112.     if (!exists) {
  113.       var p = new Permission(this._addedPermissions.length, 
  114.                              host, 
  115.                              (host.charAt(0) == ".") ? host.substring(1,host.length) : host, 
  116.                              this._type, 
  117.                              this._bundle.getString(stringKey), 
  118.                              aPermission);
  119.       this._addedPermissions.push(p);
  120.       
  121.       this._view._rowCount = this._addedPermissions.length;
  122.       this._tree.treeBoxObject.rowCountChanged(this._addedPermissions.length-1, 1);
  123.       this._tree.treeBoxObject.ensureRowIsVisible(this._addedPermissions.length-1);
  124.     }
  125.     textbox.value = "";
  126.     textbox.focus();
  127.  
  128.     // covers a case where the site exists already, so the buttons don't disable
  129.     this.onHostInput(textbox);
  130.  
  131.     // enable "remove all" button as needed
  132.     document.getElementById("removeAllPermissions").disabled = this._addedPermissions.length == 0;
  133.   },
  134.   
  135.   onHostInput: function (aSiteField)
  136.   {
  137.     document.getElementById("btnSession").disabled = !aSiteField.value;
  138.     document.getElementById("btnBlock").disabled = !aSiteField.value;
  139.     document.getElementById("btnAllow").disabled = !aSiteField.value;
  140.   },
  141.   
  142.   onLoad: function ()
  143.   {
  144.     this._type = window.arguments[0].permissionType;
  145.     this._bundle = document.getElementById("permBundle");
  146.  
  147.     var permissionsText = document.getElementById("permissionsText");
  148.     while (permissionsText.hasChildNodes())
  149.       permissionsText.removeChild(permissionsText.firstChild);
  150.     
  151.     var introString = this._bundle.getString(this._type + "permissionstext");
  152.     permissionsText.appendChild(document.createTextNode(introString));
  153.  
  154.     var titleString = this._bundle.getString(this._type + "permissionstitle");
  155.     document.documentElement.setAttribute("title", titleString);
  156.     
  157.     document.getElementById("btnBlock").hidden = !window.arguments[0].blockVisible;
  158.     document.getElementById("btnSession").hidden = !window.arguments[0].sessionVisible;
  159.     document.getElementById("btnAllow").hidden = !window.arguments[0].allowVisible;
  160.     document.getElementById("url").value = window.arguments[0].prefilledHost;
  161.     this.onHostInput(document.getElementById("url"));
  162.  
  163.     this._loadPermissions();
  164.   },
  165.   
  166.   onPermissionSelected: function ()
  167.   {
  168.     var selections = GetTreeSelections(this._tree);
  169.     document.getElementById("removePermission").disabled = (selections.length < 1);
  170.   },
  171.   
  172.   onPermissionDeleted: function ()
  173.   {
  174.     DeleteSelectedItemFromTree(this._tree, this._view,
  175.                                this._addedPermissions, this._removedPermissions,
  176.                                "removePermission", "removeAllPermissions");
  177.   },
  178.   
  179.   onAllPermissionsDeleted: function ()
  180.   {
  181.     DeleteAllFromTree(this._tree, this._view,
  182.                       this._addedPermissions, this._removedPermissions,
  183.                       "removePermission", "removeAllPermissions");
  184.   },
  185.   
  186.   onPermissionKeyPress: function (aEvent)
  187.   {
  188.     if (aEvent.keyCode == 46)
  189.       this.onPermissionDeleted();
  190.   },
  191.   
  192.   _lastPermissionSortColumn: "",
  193.   _lastPermissionSortAscending: false,
  194.   
  195.   onPermissionSort: function (aColumn, aUpdateSelection)
  196.   {
  197.     this._lastPermissionSortAscending = SortTree(this._tree, 
  198.                                                  this._view, 
  199.                                                  this._addedPermissions,
  200.                                                  aColumn, 
  201.                                                  this._lastPermissionSortColumn, 
  202.                                                  this._lastPermissionSortAscending, 
  203.                                                  aUpdateSelection);
  204.     this._lastPermissionSortColumn = aColumn;
  205.   },
  206.   
  207.   _loadPermissions: function ()
  208.   {
  209.     this._tree = document.getElementById("permissionsTree");
  210.  
  211.     // load permissions into a table
  212.     var count = 0;
  213.     var enumerator = this._pm.enumerator;
  214.     while (enumerator.hasMoreElements()) {
  215.       var nextPermission = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission);
  216.       if (nextPermission.type == this._type) {
  217.         var host = nextPermission.host;
  218.         var capability = null;
  219.         switch (nextPermission.capability) {
  220.           case nsIPermissionManager.ALLOW_ACTION:
  221.             capability = "can";
  222.             break;
  223.           case nsIPermissionManager.DENY_ACTION:
  224.             capability = "cannot";
  225.             break;
  226.           // we should only ever hit this for cookies
  227.           case nsICookiePermission.ACCESS_SESSION:
  228.             capability = "canSession";
  229.             break;
  230.           default:
  231.             break;
  232.         } 
  233.         var capabilityString = this._bundle.getString(capability);
  234.         var p = new Permission(count++, host,
  235.                                (host.charAt(0) == ".") ? host.substring(1,host.length) : host,
  236.                                nextPermission.type,
  237.                                capabilityString, 
  238.                                nextPermission.capability);
  239.         this._addedPermissions.push(p);
  240.       }
  241.     }
  242.    
  243.     this._view._rowCount = this._addedPermissions.length;
  244.  
  245.     // sort and display the table
  246.     this._tree.treeBoxObject.view = this._view;
  247.     this.onPermissionSort("rawHost", false);
  248.  
  249.     // disable "remove all" button if there are none
  250.     document.getElementById("removeAllPermissions").disabled = this._addedPermissions.length == 0;
  251.   },
  252.   
  253.   setHost: function (aHost)
  254.   {
  255.     document.getElementById("url").value = aHost;
  256.   }
  257. };
  258.  
  259. function setHost(aHost)
  260. {
  261.   gPermissionManager.setHost(aHost);
  262. }
  263.  
  264.