home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / firefox-3.0.14 / chrome / browser.jar / content / browser / preferences / permissions.js < prev    next >
Encoding:
Text File  |  2008-02-22  |  12.9 KB  |  375 lines

  1. //@line 39 "/build/buildd/firefox-3.0-3.0.14+build2+nobinonly/build-tree/mozilla/browser/components/preferences/permissions.js"
  2.  
  3. const nsIPermissionManager = Components.interfaces.nsIPermissionManager;
  4. const nsICookiePermission = Components.interfaces.nsICookiePermission;
  5.  
  6. function Permission(host, rawHost, type, capability, perm) 
  7. {
  8.   this.host = host;
  9.   this.rawHost = rawHost;
  10.   this.type = type;
  11.   this.capability = capability;
  12.   this.perm = perm;
  13. }
  14.  
  15. var gPermissionManager = {
  16.   _type         : "",
  17.   _permissions  : [],
  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.id == "siteCol")
  32.         return gPermissionManager._permissions[aRow].rawHost;
  33.       else if (aColumn.id == "statusCol")
  34.         return gPermissionManager._permissions[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(column) {},
  46.     getRowProperties: function(row,prop){},
  47.     getColumnProperties: function(column,prop){},
  48.     getCellProperties: function(row,column,prop){}
  49.   },
  50.   
  51.   _getCapabilityString: function (aCapability)
  52.   {
  53.     var stringKey = null;
  54.     switch (aCapability) {
  55.     case nsIPermissionManager.ALLOW_ACTION:
  56.       stringKey = "can";
  57.       break;
  58.     case nsIPermissionManager.DENY_ACTION:
  59.       stringKey = "cannot";
  60.       break;
  61.     case nsICookiePermission.ACCESS_SESSION:
  62.       stringKey = "canSession";
  63.       break;
  64.     }
  65.     return this._bundle.getString(stringKey);
  66.   },
  67.   
  68.   addPermission: function (aCapability)
  69.   {
  70.     var textbox = document.getElementById("url");
  71.     var host = textbox.value.replace(/^\s*([-\w]*:\/+)?/, ""); // trim any leading space and scheme
  72.     try {
  73.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  74.                                 .getService(Components.interfaces.nsIIOService);
  75.       var uri = ioService.newURI("http://"+host, null, null);
  76.       host = uri.host;
  77.     } catch(ex) {
  78.       var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  79.                                     .getService(Components.interfaces.nsIPromptService);
  80.       var message = this._bundle.getString("invalidURI");
  81.       var title = this._bundle.getString("invalidURITitle");
  82.       promptService.alert(window, title, message);
  83.       return;
  84.     }
  85.  
  86.     var capabilityString = this._getCapabilityString(aCapability);
  87.  
  88.     // check whether the permission already exists, if not, add it
  89.     var exists = false;
  90.     for (var i = 0; i < this._permissions.length; ++i) {
  91.       if (this._permissions[i].rawHost == host) {
  92.         // Avoid calling the permission manager if the capability settings are
  93.         // the same. Otherwise allow the call to the permissions manager to
  94.         // update the listbox for us.
  95.         exists = this._permissions[i].perm == aCapability;
  96.         break;
  97.       }
  98.     }
  99.     
  100.     if (!exists) {
  101.       host = (host.charAt(0) == ".") ? host.substring(1,host.length) : host;
  102.       var uri = ioService.newURI("http://" + host, null, null);
  103.       this._pm.add(uri, this._type, aCapability);
  104.     }
  105.     textbox.value = "";
  106.     textbox.focus();
  107.  
  108.     // covers a case where the site exists already, so the buttons don't disable
  109.     this.onHostInput(textbox);
  110.  
  111.     // enable "remove all" button as needed
  112.     document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
  113.   },
  114.   
  115.   onHostInput: function (aSiteField)
  116.   {
  117.     document.getElementById("btnSession").disabled = !aSiteField.value;
  118.     document.getElementById("btnBlock").disabled = !aSiteField.value;
  119.     document.getElementById("btnAllow").disabled = !aSiteField.value;
  120.   },
  121.   
  122.   onHostKeyPress: function (aEvent)
  123.   {
  124.     if (aEvent.keyCode == KeyEvent.DOM_VK_RETURN)
  125.       document.getElementById("btnAllow").click();
  126.   },
  127.   
  128.   onLoad: function ()
  129.   {
  130.     this._bundle = document.getElementById("bundlePreferences");
  131.     var params = window.arguments[0];
  132.     this.init(params);
  133.   },
  134.   
  135.   init: function (aParams)
  136.   {
  137.     if (this._type) {
  138.       // reusing an open dialog, clear the old observer
  139.       this.uninit();
  140.     }
  141.  
  142.     this._type = aParams.permissionType;
  143.     this._manageCapability = aParams.manageCapability;
  144.     
  145.     var permissionsText = document.getElementById("permissionsText");
  146.     while (permissionsText.hasChildNodes())
  147.       permissionsText.removeChild(permissionsText.firstChild);
  148.     permissionsText.appendChild(document.createTextNode(aParams.introText));
  149.  
  150.     document.title = aParams.windowTitle;
  151.     
  152.     document.getElementById("btnBlock").hidden    = !aParams.blockVisible;
  153.     document.getElementById("btnSession").hidden  = !aParams.sessionVisible;
  154.     document.getElementById("btnAllow").hidden    = !aParams.allowVisible;
  155.  
  156.     var urlFieldVisible = (aParams.blockVisible || aParams.sessionVisible || aParams.allowVisible);
  157.  
  158.     var urlField = document.getElementById("url");
  159.     urlField.value = aParams.prefilledHost;
  160.     urlField.hidden = !urlFieldVisible;
  161.  
  162.     this.onHostInput(urlField);
  163.  
  164.     var urlLabel = document.getElementById("urlLabel");
  165.     urlLabel.hidden = !urlFieldVisible;
  166.  
  167.     var os = Components.classes["@mozilla.org/observer-service;1"]
  168.                        .getService(Components.interfaces.nsIObserverService);
  169.     os.addObserver(this, "perm-changed", false);
  170.  
  171.     if (this._type == "install") {
  172.       var enumerator = this._pm.enumerator;
  173.       if (!enumerator.hasMoreElements())
  174.         this._updatePermissions();
  175.     }
  176.  
  177.     this._loadPermissions();
  178.     
  179.     urlField.focus();
  180.   },
  181.   
  182.   uninit: function ()
  183.   {
  184.     var os = Components.classes["@mozilla.org/observer-service;1"]
  185.                        .getService(Components.interfaces.nsIObserverService);
  186.     os.removeObserver(this, "perm-changed");
  187.   },
  188.   
  189.   observe: function (aSubject, aTopic, aData)
  190.   {
  191.     if (aTopic == "perm-changed") {
  192.       var permission = aSubject.QueryInterface(Components.interfaces.nsIPermission);
  193.       if (aData == "added") {
  194.         this._addPermissionToList(permission);
  195.         ++this._view._rowCount;
  196.         this._tree.treeBoxObject.rowCountChanged(this._view.rowCount - 1, 1);        
  197.         // Re-do the sort, since we inserted this new item at the end. 
  198.         gTreeUtils.sort(this._tree, this._view, this._permissions, 
  199.                         this._lastPermissionSortColumn, 
  200.                         this._lastPermissionSortAscending);        
  201.       }
  202.       else if (aData == "changed") {
  203.         for (var i = 0; i < this._permissions.length; ++i) {
  204.           if (this._permissions[i].host == permission.host) {
  205.             this._permissions[i].capability = this._getCapabilityString(permission.capability);
  206.             break;
  207.           }
  208.         }
  209.         // Re-do the sort, if the status changed from Block to Allow
  210.         // or vice versa, since if we're sorted on status, we may no
  211.         // longer be in order. 
  212.         if (this._lastPermissionSortColumn.id == "statusCol") {
  213.           gTreeUtils.sort(this._tree, this._view, this._permissions, 
  214.                           this._lastPermissionSortColumn, 
  215.                           this._lastPermissionSortAscending);
  216.         }
  217.         this._tree.treeBoxObject.invalidate();
  218.       }
  219.       // No UI other than this window causes this method to be sent a "deleted"
  220.       // notification, so we don't need to implement it since Delete is handled
  221.       // directly by the Permission Removal handlers. If that ever changes, those
  222.       // implementations will have to move into here. 
  223.     }
  224.   },
  225.   
  226.   onPermissionSelected: function ()
  227.   {
  228.     var hasSelection = this._tree.view.selection.count > 0;
  229.     var hasRows = this._tree.view.rowCount > 0;
  230.     document.getElementById("removePermission").disabled = !hasRows || !hasSelection;
  231.     document.getElementById("removeAllPermissions").disabled = !hasRows;
  232.   },
  233.   
  234.   onPermissionDeleted: function ()
  235.   {
  236.     if (!this._view.rowCount)
  237.       return;
  238.     var removedPermissions = [];
  239.     gTreeUtils.deleteSelectedItems(this._tree, this._view, this._permissions, removedPermissions);
  240.     for (var i = 0; i < removedPermissions.length; ++i) {
  241.       var p = removedPermissions[i];
  242.       this._pm.remove(p.host, p.type);
  243.     }    
  244.     document.getElementById("removePermission").disabled = !this._permissions.length;
  245.     document.getElementById("removeAllPermissions").disabled = !this._permissions.length;
  246.   },
  247.   
  248.   onAllPermissionsDeleted: function ()
  249.   {
  250.     if (!this._view.rowCount)
  251.       return;
  252.     var removedPermissions = [];
  253.     gTreeUtils.deleteAll(this._tree, this._view, this._permissions, removedPermissions);
  254.     for (var i = 0; i < removedPermissions.length; ++i) {
  255.       var p = removedPermissions[i];
  256.       this._pm.remove(p.host, p.type);
  257.     }    
  258.     document.getElementById("removePermission").disabled = true;
  259.     document.getElementById("removeAllPermissions").disabled = true;
  260.   },
  261.   
  262.   onPermissionKeyPress: function (aEvent)
  263.   {
  264.     if (aEvent.keyCode == 46)
  265.       this.onPermissionDeleted();
  266.   },
  267.   
  268.   _lastPermissionSortColumn: "",
  269.   _lastPermissionSortAscending: false,
  270.   
  271.   onPermissionSort: function (aColumn)
  272.   {
  273.     this._lastPermissionSortAscending = gTreeUtils.sort(this._tree, 
  274.                                                         this._view, 
  275.                                                         this._permissions,
  276.                                                         aColumn, 
  277.                                                         this._lastPermissionSortColumn, 
  278.                                                         this._lastPermissionSortAscending);
  279.     this._lastPermissionSortColumn = aColumn;
  280.   },
  281.   
  282.   _loadPermissions: function ()
  283.   {
  284.     this._tree = document.getElementById("permissionsTree");
  285.     this._permissions = [];
  286.  
  287.     // load permissions into a table
  288.     var count = 0;
  289.     var enumerator = this._pm.enumerator;
  290.     while (enumerator.hasMoreElements()) {
  291.       var nextPermission = enumerator.getNext().QueryInterface(Components.interfaces.nsIPermission);
  292.       this._addPermissionToList(nextPermission);
  293.     }
  294.    
  295.     this._view._rowCount = this._permissions.length;
  296.  
  297.     // sort and display the table
  298.     this._tree.treeBoxObject.view = this._view;
  299.     this.onPermissionSort("rawHost", false);
  300.  
  301.     // disable "remove all" button if there are none
  302.     document.getElementById("removeAllPermissions").disabled = this._permissions.length == 0;
  303.   },
  304.   
  305.   _addPermissionToList: function (aPermission)
  306.   {
  307.     if (aPermission.type == this._type &&
  308.         (!this._manageCapability ||
  309.          (aPermission.capability == this._manageCapability))) {
  310.  
  311.       var host = aPermission.host;
  312.       var capabilityString = this._getCapabilityString(aPermission.capability);
  313.       var p = new Permission(host,
  314.                              (host.charAt(0) == ".") ? host.substring(1,host.length) : host,
  315.                              aPermission.type,
  316.                              capabilityString, 
  317.                              aPermission.capability);
  318.       this._permissions.push(p);
  319.     }  
  320.   },
  321.   
  322.   _updatePermissions: function ()
  323.   {
  324.     try {
  325.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  326.                                 .getService(Components.interfaces.nsIIOService);
  327.       var pbi = Components.classes["@mozilla.org/preferences-service;1"]
  328.                           .getService(Components.interfaces.nsIPrefBranch2);
  329.       var prefList = [["xpinstall.whitelist.add", nsIPermissionManager.ALLOW_ACTION],
  330.                       ["xpinstall.whitelist.add.103", nsIPermissionManager.ALLOW_ACTION],
  331.                       ["xpinstall.blacklist.add", nsIPermissionManager.DENY_ACTION]];
  332.  
  333.       for (var i = 0; i < prefList.length; ++i) {
  334.         try {
  335.           // this pref is a comma-delimited list of hosts
  336.           var hosts = pbi.getCharPref(prefList[i][0]);
  337.         } catch(ex) {
  338.           continue;
  339.         }
  340.  
  341.         if (!hosts)
  342.           continue;
  343.  
  344.         hostList = hosts.split(",");
  345.         var capability = prefList[i][1];
  346.         for (var j = 0; j < hostList.length; ++j) {
  347.           // trim leading and trailing spaces
  348.           var host = hostList[j].replace(/^\s*/,"").replace(/\s*$/,"");
  349.           try {
  350.             var uri = ioService.newURI("http://" + host, null, null);
  351.             this._pm.add(uri, this._type, capability);
  352.           } catch(ex) { }
  353.         }
  354.         pbi.setCharPref(prefList[i][0], "");
  355.       }
  356.     } catch(ex) { }
  357.   },
  358.   
  359.   setHost: function (aHost)
  360.   {
  361.     document.getElementById("url").value = aHost;
  362.   }
  363. };
  364.  
  365. function setHost(aHost)
  366. {
  367.   gPermissionManager.setHost(aHost);
  368. }
  369.  
  370. function initWithParams(aParams)
  371. {
  372.   gPermissionManager.init(aParams);
  373. }
  374.  
  375.