Bug 503971 - nsIContentPrefService methods should throw when passed a null setting name; r=myk sr=mconnor
authorGeoff Lankow <geoff@darktrojan.net>
Mon, 24 Aug 2009 19:12:04 +0200
changeset 31788 5e61c13575e78bdea54c73661bbd8264e4d83cd2
parent 31787 b008402ada3fa9c04277a86e51cbb839c5819a07
child 31789 2ea2974edcf45fe799b3a570886fbc03eac59d22
push id8728
push usersgautherie.bz@free.fr
push dateMon, 24 Aug 2009 17:12:52 +0000
treeherdermozilla-central@5e61c13575e7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmyk, mconnor
bugs503971
milestone1.9.3a1pre
Bug 503971 - nsIContentPrefService methods should throw when passed a null setting name; r=myk sr=mconnor
toolkit/components/contentprefs/public/nsIContentPrefService.idl
toolkit/components/contentprefs/src/nsContentPrefService.js
toolkit/components/contentprefs/tests/unit/test_bug503971.js
--- a/toolkit/components/contentprefs/public/nsIContentPrefService.idl
+++ b/toolkit/components/contentprefs/public/nsIContentPrefService.idl
@@ -76,55 +76,60 @@ interface nsIContentPrefService : nsISup
    * to NULL in the database, as well as undefined (nsIDataType::VTYPE_VOID),
    * which means there is no record for this pref in the database.
    *
    * @param    aURI        the URI for which to get the pref, or null to get
    *                       the global pref (applies to all URIs)
    * @param    aName       the name of the pref to get
    *
    * @returns  the value of the pref
+   * @throws   NS_ERROR_ILLEGAL_VALUE if aName is null or an empty string
    */
   nsIVariant getPref(in nsIURI aURI, in AString aName);
   
   /**
    * Set a pref.
    *
    * @param    aURI        the URI for which to set the pref, or null to set
    *                       the global pref (applies to all URIs)
    * @param    aName       the name of the pref to set
    * @param    aValue      the new value of the pref
+   * @throws   NS_ERROR_ILLEGAL_VALUE if aName is null or an empty string
    */
   void setPref(in nsIURI aURI, in AString aName, in nsIVariant aValue);
   
   /**
    * Check whether or not a pref exists.
    *
    * @param    aURI        the URI for which to check for the pref
    * @param    aName       the name of the pref to check for
+   * @throws   NS_ERROR_ILLEGAL_VALUE if aName is null or an empty string
    */
   boolean hasPref(in nsIURI aURI, in AString aName);
   
   /**
    * Remove a pref.
    *
    * @param    aURI        the URI for which to remove the pref
    * @param    aName       the name of the pref to remove
+   * @throws   NS_ERROR_ILLEGAL_VALUE if aName is null or an empty string
    */
   void removePref(in nsIURI aURI, in AString aName);
 
   /**
    * Remove all grouped prefs.  Useful for removing references to the sites
    * the user has visited when the user clears their private data.
    */
   void removeGroupedPrefs();
 
   /**
    * Remove all prefs with the given name.
    *
    * @param    aName        the setting name for which to remove prefs
+   * @throws   NS_ERROR_ILLEGAL_VALUE if aName is null or an empty string
    */
   void removePrefsByName(in AString aName);
 
   /**
    * Get the prefs that apply to the given URI.
    *
    * @param    aURI        the URI for which to retrieve prefs
    * 
@@ -133,16 +138,17 @@ interface nsIContentPrefService : nsISup
   nsIPropertyBag2 getPrefs(in nsIURI aURI);
 
   /**
    * Get the prefs with the given name.
    *
    * @param    aName        the setting name for which to retrieve prefs
    * 
    * @returns  a property bag of prefs
+   * @throws   NS_ERROR_ILLEGAL_VALUE if aName is null or an empty string
    */
   nsIPropertyBag2 getPrefsByName(in AString aName);
   
   /**
    * Add an observer.
    * 
    * @param    aName       the setting to observe, or null to add
    *                       a generic observer that observes all settings
--- a/toolkit/components/contentprefs/src/nsContentPrefService.js
+++ b/toolkit/components/contentprefs/src/nsContentPrefService.js
@@ -123,16 +123,20 @@ ContentPrefService.prototype = {
     }
   },
 
 
   //**************************************************************************//
   // nsIContentPrefService
 
   getPref: function ContentPrefService_getPref(aURI, aName) {
+    if (!aName)
+      throw Components.Exception("aName cannot be null or an empty string",
+                                 Cr.NS_ERROR_ILLEGAL_VALUE);
+
     if (aURI) {
       var group = this.grouper.group(aURI);
       return this._selectPref(group, aName);
     }
 
     return this._selectGlobalPref(aName);
   },
 
@@ -227,16 +231,20 @@ ContentPrefService.prototype = {
     }
     catch(ex) {
       this._dbConnection.rollbackTransaction();
       throw ex;
     }
   },
 
   removePrefsByName: function ContentPrefService_removePrefsByName(aName) {
+    if (!aName)
+      throw Components.Exception("aName cannot be null or an empty string",
+                                 Cr.NS_ERROR_ILLEGAL_VALUE);
+
     var settingID = this._selectSettingID(aName);
     if (!settingID) {
       return;
     }
     
     var selectGroupsStmt = this._dbCreateStatement(
       "SELECT groups.name AS groupName " +
       "FROM prefs " +
@@ -275,16 +283,20 @@ ContentPrefService.prototype = {
       var group = this.grouper.group(aURI);
       return this._selectPrefs(group);
     }
 
     return this._selectGlobalPrefs();
   },
 
   getPrefsByName: function ContentPrefService_getPrefsByName(aName) {
+    if (!aName)
+      throw Components.Exception("aName cannot be null or an empty string",
+                                 Cr.NS_ERROR_ILLEGAL_VALUE);
+
     return this._selectPrefsByName(aName);
   },
 
   // A hash of arrays of observers, indexed by setting name.
   _observers: {},
 
   // An array of generic observers, which observe all settings.
   _genericObservers: [],
new file mode 100644
--- /dev/null
+++ b/toolkit/components/contentprefs/tests/unit/test_bug503971.js
@@ -0,0 +1,68 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Content Preferences (cpref).
+ *
+ * The Initial Developer of the Original Code is Mozilla.
+ * Portions created by the Initial Developer are Copyright (C) 2007
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Geoff Lankow <geoff@darktrojan.net>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+function run_test() {
+  var cps = Cc["@mozilla.org/content-pref/service;1"].
+            getService(Ci.nsIContentPrefService);
+
+  var uri = ContentPrefTest.getURI("http://www.example.com/");
+  
+  do_check_thrown(function () { cps.setPref(uri, null, 8); });
+  do_check_thrown(function () { cps.hasPref(uri, null); });
+  do_check_thrown(function () { cps.getPref(uri, null); });
+  do_check_thrown(function () { cps.removePref(uri, null); });
+  do_check_thrown(function () { cps.getPrefsByName(null); });
+  do_check_thrown(function () { cps.removePrefsByName(null); });
+
+  do_check_thrown(function () { cps.setPref(uri, "", 21); });
+  do_check_thrown(function () { cps.hasPref(uri, ""); });
+  do_check_thrown(function () { cps.getPref(uri, ""); });
+  do_check_thrown(function () { cps.removePref(uri, ""); });
+  do_check_thrown(function () { cps.getPrefsByName(""); });
+  do_check_thrown(function () { cps.removePrefsByName(""); });
+}
+
+function do_check_thrown (aCallback) {
+  var exThrown = false;
+  try {
+    aCallback();
+    do_throw("NS_ERROR_ILLEGAL_VALUE should have been thrown here");
+  } catch (e) {
+    do_check_eq(e.result, Cr.NS_ERROR_ILLEGAL_VALUE);
+    exThrown = true;
+  }
+  do_check_true(exThrown);
+}