Bug 706740 - Backout bug 667075. r=dcamp
authorGian-Carlo Pascutto <gpascutto@mozilla.com>
Fri, 02 Dec 2011 10:47:43 +0100
changeset 81958 4be41994deb70f69eee8d14ab85819ae60f9e721
parent 81957 cddc8b0ba0b64339f82d4eb39bfaa6776389850b
child 81959 aeb035da53283c56370992f254e4f79d7dd180f8
push idunknown
push userunknown
push dateunknown
reviewersdcamp
bugs706740, 667075
milestone11.0a1
Bug 706740 - Backout bug 667075. r=dcamp
toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
--- a/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
@@ -57,17 +57,16 @@
 #include "nsIObserverService.h"
 #include "nsIPermissionManager.h"
 #include "nsIPrefBranch.h"
 #include "nsIPrefBranch2.h"
 #include "nsIPrefService.h"
 #include "nsIProperties.h"
 #include "nsToolkitCompsCID.h"
 #include "nsIUrlClassifierUtils.h"
-#include "nsIRandomGenerator.h"
 #include "nsUrlClassifierDBService.h"
 #include "nsUrlClassifierUtils.h"
 #include "nsUrlClassifierProxies.h"
 #include "nsURILoader.h"
 #include "nsString.h"
 #include "nsReadableUtils.h"
 #include "nsTArray.h"
 #include "nsNetUtil.h"
@@ -483,19 +482,23 @@ public:
 
   // Read a certain number of rows adjacent to the requested rowid that
   // don't have complete hash data.
   nsresult ReadNoiseEntries(PRInt64 rowID,
                             PRUint32 numRequested,
                             bool before,
                             nsTArray<nsUrlClassifierEntry> &entries);
 
+  // Ask the db for a random number.  This is temporary, and should be
+  // replaced with nsIRandomGenerator when 419739 is fixed.
+  nsresult RandomNumber(PRInt64 *randomNum);
   // Return an array with all Prefixes known
   nsresult ReadPrefixes(nsTArray<PRUint32>& array, PRUint32 aKey);
 
+
 protected:
   nsresult ReadEntries(mozIStorageStatement *statement,
                        nsTArray<nsUrlClassifierEntry>& entries);
   nsUrlClassifierDBServiceWorker *mWorker;
   nsCOMPtr<mozIStorageConnection> mConnection;
 
   nsCOMPtr<mozIStorageStatement> mLookupWithIDStatement;
 
@@ -504,16 +507,17 @@ protected:
   nsCOMPtr<mozIStorageStatement> mDeleteStatement;
   nsCOMPtr<mozIStorageStatement> mExpireStatement;
 
   nsCOMPtr<mozIStorageStatement> mPartialEntriesStatement;
   nsCOMPtr<mozIStorageStatement> mPartialEntriesAfterStatement;
   nsCOMPtr<mozIStorageStatement> mLastPartialEntriesStatement;
   nsCOMPtr<mozIStorageStatement> mPartialEntriesBeforeStatement;
 
+  nsCOMPtr<mozIStorageStatement> mRandomStatement;
   nsCOMPtr<mozIStorageStatement> mAllPrefixStatement;
 };
 
 nsresult
 nsUrlClassifierStore::Init(nsUrlClassifierDBServiceWorker *worker,
                            mozIStorageConnection *connection,
                            const nsACString& entriesName)
 {
@@ -562,17 +566,21 @@ nsUrlClassifierStore::Init(nsUrlClassifi
   rv = mConnection->CreateStatement
     (NS_LITERAL_CSTRING("SELECT * FROM ") + entriesName +
      NS_LITERAL_CSTRING(" WHERE id < ?1 AND complete_data ISNULL"
                         " ORDER BY id DESC LIMIT ?2"),
      getter_AddRefs(mPartialEntriesBeforeStatement));
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = mConnection->CreateStatement
-    (NS_LITERAL_CSTRING("SELECT domain, partial_data, complete_data FROM ")
+    (NS_LITERAL_CSTRING("SELECT abs(random())"),
+     getter_AddRefs(mRandomStatement));
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  rv = mConnection->CreateStatement(NS_LITERAL_CSTRING("SELECT domain, partial_data, complete_data FROM ")
      + entriesName,
      getter_AddRefs(mAllPrefixStatement));
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 void
@@ -584,16 +592,17 @@ nsUrlClassifierStore::Close()
   mUpdateStatement = nsnull;
   mDeleteStatement = nsnull;
   mExpireStatement = nsnull;
 
   mPartialEntriesStatement = nsnull;
   mPartialEntriesAfterStatement = nsnull;
   mPartialEntriesBeforeStatement = nsnull;
   mLastPartialEntriesStatement = nsnull;
+  mRandomStatement = nsnull;
 
   mAllPrefixStatement = nsnull;
 
   mConnection = nsnull;
 }
 
 
 bool
@@ -768,16 +777,31 @@ nsUrlClassifierStore::ReadNoiseEntries(P
   mozStorageStatementScoper wraparoundScoper(wraparoundStatement);
 
   rv = wraparoundStatement->BindInt32ByIndex(0, numRequested - numRead);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return ReadEntries(wraparoundStatement, entries);
 }
 
+nsresult
+nsUrlClassifierStore::RandomNumber(PRInt64 *randomNum)
+{
+  mozStorageStatementScoper randScoper(mRandomStatement);
+  PRBool exists;
+  nsresult rv = mRandomStatement->ExecuteStep(&exists);
+  NS_ENSURE_SUCCESS(rv, rv);
+  if (!exists)
+    return NS_ERROR_NOT_AVAILABLE;
+
+  *randomNum = mRandomStatement->AsInt64(0);
+
+  return NS_OK;
+}
+
 // -------------------------------------------------------------------------
 // nsUrlClassifierAddStore class implementation
 
 // This class accesses the moz_classifier table.
 class nsUrlClassifierAddStore: public nsUrlClassifierStore
 {
 public:
   nsUrlClassifierAddStore() {};
@@ -1762,26 +1786,19 @@ nsresult
 nsUrlClassifierDBServiceWorker::AddNoise(PRInt64 nearID,
                                          PRInt32 count,
                                          nsTArray<nsUrlClassifierLookupResult>& results)
 {
   if (count < 1) {
     return NS_OK;
   }
 
-  nsCOMPtr<nsIRandomGenerator> rg =
-    do_GetService("@mozilla.org/security/random-generator;1");
-  NS_ENSURE_STATE(rg);
-
-  PRInt32 randomNum;
-  PRUint8 *temp;
-  nsresult rv = rg->GenerateRandomBytes(sizeof(randomNum), &temp);
+  PRInt64 randomNum;
+  nsresult rv = mMainStore.RandomNumber(&randomNum);
   NS_ENSURE_SUCCESS(rv, rv);
-  memcpy(&randomNum, temp, sizeof(randomNum));
-  NS_Free(temp);
 
   PRInt32 numBefore = randomNum % count;
 
   nsTArray<nsUrlClassifierEntry> noiseEntries;
   rv = mMainStore.ReadNoiseEntries(nearID, numBefore, true, noiseEntries);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = mMainStore.ReadNoiseEntries(nearID, count - numBefore, false, noiseEntries);