Bug 330879 - Check whether the node parameter is a valid Places node. r=mak
authorAmod Narvekar <amod.narvekar@gmail.com>
Wed, 16 Apr 2014 13:50:36 -0400
changeset 197405 566c01fe0d987d9a0115f206401307999a717601
parent 197404 1ebef0fc77de07dd28b8db8cdcd65871d45211ad
child 197406 68ba8f6f4f03f6700e4ed2a8d9ce04980ead824b
push id3624
push userasasaki@mozilla.com
push dateMon, 09 Jun 2014 21:49:01 +0000
treeherdermozilla-beta@b1a5da15899a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs330879
milestone31.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 330879 - Check whether the node parameter is a valid Places node. r=mak
toolkit/components/places/PlacesUtils.jsm
toolkit/components/places/tests/unit/test_PlacesUtils_nodeIsXXX_invalidArg.js
toolkit/components/places/tests/unit/xpcshell.ini
--- a/toolkit/components/places/PlacesUtils.jsm
+++ b/toolkit/components/places/PlacesUtils.jsm
@@ -151,58 +151,73 @@ this.PlacesUtils = {
 
   /**
    * Determines whether or not a ResultNode is a Bookmark folder.
    * @param   aNode
    *          A result node
    * @returns true if the node is a Bookmark folder, false otherwise
    */
   nodeIsFolder: function PU_nodeIsFolder(aNode) {
+    if (!(aNode instanceof Ci.nsINavHistoryResultNode)) {
+      throw new Error("Invalid Places node");
+    }
     return (aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER ||
             aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT);
   },
 
   /**
    * Determines whether or not a ResultNode represents a bookmarked URI.
    * @param   aNode
    *          A result node
    * @returns true if the node represents a bookmarked URI, false otherwise
    */
   nodeIsBookmark: function PU_nodeIsBookmark(aNode) {
+    if (!(aNode instanceof Ci.nsINavHistoryResultNode)) {
+      throw new Error("Invalid Places node");
+    }
     return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_URI &&
            aNode.itemId != -1;
   },
 
   /**
    * Determines whether or not a ResultNode is a Bookmark separator.
    * @param   aNode
    *          A result node
    * @returns true if the node is a Bookmark separator, false otherwise
    */
   nodeIsSeparator: function PU_nodeIsSeparator(aNode) {
+    if (!(aNode instanceof Ci.nsINavHistoryResultNode)) {
+      throw new Error("Invalid Places node");
+    }
     return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_SEPARATOR;
   },
 
   /**
    * Determines whether or not a ResultNode is a URL item.
    * @param   aNode
    *          A result node
    * @returns true if the node is a URL item, false otherwise
    */
   nodeIsURI: function PU_nodeIsURI(aNode) {
+    if (!(aNode instanceof Ci.nsINavHistoryResultNode)) {
+      throw new Error("Invalid Places node");
+    }
     return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_URI;
   },
 
   /**
    * Determines whether or not a ResultNode is a Query item.
    * @param   aNode
    *          A result node
    * @returns true if the node is a Query item, false otherwise
    */
   nodeIsQuery: function PU_nodeIsQuery(aNode) {
+    if (!(aNode instanceof Ci.nsINavHistoryResultNode)) {
+      throw new Error("Invalid Places node");
+    }
     return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY;
   },
 
   /**
    * Generator for a node's ancestors.
    * @param aNode
    *        A result node
    */
@@ -352,16 +367,19 @@ this.PlacesUtils = {
   /**
    * Determines if a node is read only (children cannot be inserted, sometimes
    * they cannot be removed depending on the circumstance)
    * @param   aNode
    *          A result node
    * @returns true if the node is readonly, false otherwise
    */
   nodeIsReadOnly: function PU_nodeIsReadOnly(aNode) {
+    if (!(aNode instanceof Ci.nsINavHistoryResultNode)) {
+      throw new Error("Invalid Places node");
+    }
     let itemId = aNode.itemId;
     if (itemId != -1) {
       return this._readOnly.indexOf(itemId) != -1;
     }
 
     if (this.nodeIsQuery(aNode) &&
         asQuery(aNode).queryOptions.resultType !=
         Ci.nsINavHistoryQueryOptions.RESULTS_AS_TAG_CONTENTS)
@@ -371,59 +389,71 @@ this.PlacesUtils = {
 
   /**
    * Determines whether or not a ResultNode is a host container.
    * @param   aNode
    *          A result node
    * @returns true if the node is a host container, false otherwise
    */
   nodeIsHost: function PU_nodeIsHost(aNode) {
+    if (!(aNode instanceof Ci.nsINavHistoryResultNode)) {
+      throw new Error("Invalid Places node");
+    }
     return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY &&
            aNode.parent &&
            asQuery(aNode.parent).queryOptions.resultType ==
              Ci.nsINavHistoryQueryOptions.RESULTS_AS_SITE_QUERY;
   },
 
   /**
    * Determines whether or not a ResultNode is a day container.
    * @param   node
    *          A NavHistoryResultNode
    * @returns true if the node is a day container, false otherwise
    */
   nodeIsDay: function PU_nodeIsDay(aNode) {
+    if (!(aNode instanceof Ci.nsINavHistoryResultNode)) {
+      throw new Error("Invalid Places node");
+    }
     var resultType;
     return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY &&
            aNode.parent &&
            ((resultType = asQuery(aNode.parent).queryOptions.resultType) ==
                Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_QUERY ||
              resultType == Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_SITE_QUERY);
   },
 
   /**
    * Determines whether or not a result-node is a tag container.
    * @param   aNode
    *          A result-node
    * @returns true if the node is a tag container, false otherwise
    */
   nodeIsTagQuery: function PU_nodeIsTagQuery(aNode) {
+    if (!(aNode instanceof Ci.nsINavHistoryResultNode)) {
+      throw new Error("Invalid Places node");
+    }
     return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY &&
            asQuery(aNode).queryOptions.resultType ==
              Ci.nsINavHistoryQueryOptions.RESULTS_AS_TAG_CONTENTS;
   },
 
   /**
    * Determines whether or not a ResultNode is a container.
    * @param   aNode
    *          A result node
    * @returns true if the node is a container item, false otherwise
    */
   containerTypes: [Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER,
                    Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT,
                    Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY],
   nodeIsContainer: function PU_nodeIsContainer(aNode) {
+    if (!(aNode instanceof Ci.nsINavHistoryResultNode)) {
+      throw new Error("Invalid Places node");
+    }
     return this.containerTypes.indexOf(aNode.type) != -1;
   },
 
   /**
    * Determines whether or not a ResultNode is an history related container.
    * @param   node
    *          A result node
    * @returns true if the node is an history related container, false otherwise
new file mode 100644
--- /dev/null
+++ b/toolkit/components/places/tests/unit/test_PlacesUtils_nodeIsXXX_invalidArg.js
@@ -0,0 +1,24 @@
+
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+function run_test() {
+ let nodeIsMethods = [
+  "nodeIsFolder",
+  "nodeIsBookmark",
+  "nodeIsSeparator",
+  "nodeIsURI",
+  "nodeIsQuery",
+  "nodeIsReadOnly",
+  "nodeIsHost",
+  "nodeIsDay",
+  "nodeIsTagQuery",
+  "nodeIsContainer",
+  "nodeIsHistoryContainer",
+  "nodeIsQuery"
+ ];
+ for (let methodName of nodeIsMethods) {
+  Assert.throws(() => PlacesUtils[methodName](true), /Invalid Places node/);
+ }
+}
+
--- a/toolkit/components/places/tests/unit/xpcshell.ini
+++ b/toolkit/components/places/tests/unit/xpcshell.ini
@@ -112,16 +112,17 @@ fail-if = os == "android"
 skip-if = true
 [test_null_interfaces.js]
 [test_onItemChanged_tags.js]
 [test_pageGuid_bookmarkGuid.js]
 [test_frecency_observers.js]
 [test_placeURIs.js]
 [test_PlacesUtils_asyncGetBookmarkIds.js]
 [test_PlacesUtils_lazyobservers.js]
+[test_PlacesUtils_nodeIsXXX_invalidArg.js]
 [test_placesTxn.js]
 [test_preventive_maintenance.js]
 # Bug 676989: test hangs consistently on Android
 skip-if = os == "android"
 [test_preventive_maintenance_checkAndFixDatabase.js]
 # Bug 676989: test hangs consistently on Android
 skip-if = os == "android"
 [test_preventive_maintenance_runTasks.js]