Bug 1144786: Create ClassifyLocalWithTables (r=gcp,francois)
authorMonica Chew <mmc@mozilla.com>
Thu, 26 Mar 2015 15:24:19 -0700
changeset 266354 de296010ea9e40dff400d9c2a925d288aaf0e44c
parent 266353 28dca8aee5844b99d18954af858fdbdeb6e2bfc3
child 266355 130f0523865dbb28aa20830ba726229624ff44b9
push id830
push userraliiev@mozilla.com
push dateFri, 19 Jun 2015 19:24:37 +0000
treeherdermozilla-release@932614382a68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgcp, francois
bugs1144786
milestone39.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 1144786: Create ClassifyLocalWithTables (r=gcp,francois)
netwerk/base/nsIURIClassifier.idl
toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
--- a/netwerk/base/nsIURIClassifier.idl
+++ b/netwerk/base/nsIURIClassifier.idl
@@ -25,17 +25,17 @@ interface nsIURIClassifierCallback : nsI
    */
   void onClassifyComplete(in nsresult aErrorCode);
 };
 
 /**
  * The URI classifier service checks a URI against lists of phishing
  * and malware sites.
  */
-[scriptable, uuid(03d26681-0ef5-4718-9777-33c9e1ee3e32)]
+[scriptable, uuid(750c4313-f689-4a4c-87c2-a04710a2395b)]
 interface nsIURIClassifier : nsISupports
 {
   /**
    * Classify a Principal using its URI.
    *
    * @param aPrincipal
    *        The principal that should be checked by the URI classifier.
    * @param aTrackingProtectionEnabled
@@ -57,9 +57,17 @@ interface nsIURIClassifier : nsISupports
 
   /**
    * Synchronously classify a Principal locally using its URI. This does not
    * make network requests. The result is an error code with which the channel
    * should be cancelled, or NS_OK if no result was found.
    */
   nsresult classifyLocal(in nsIPrincipal aPrincipal,
                          in boolean aTrackingProtectionEnabled);
+
+  /**
+   * Synchronously classify a Principal locally using its URI with a
+   * comma-separated string containing the given tables. This does not make
+   * network requests. The result is a comma-separated string of tables that match.
+   */
+  ACString classifyLocalWithTables(in nsIPrincipal aPrincipal,
+                                   in ACString aTables);
 };
--- a/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
@@ -167,41 +167,34 @@ nsUrlClassifierDBServiceWorker::DoLocalL
   // results that were found than fail.
   mClassifier->Check(spec, tables, gFreshnessGuarantee, *results);
 
   LOG(("Found %d results.", results->Length()));
   return NS_OK;
 }
 
 static nsresult
-TablesToResponse(const nsACString& tables,
-                 bool checkMalware,
-                 bool checkPhishing,
-                 bool checkTracking)
+TablesToResponse(const nsACString& tables)
 {
-  if (checkMalware &&
-      FindInReadable(NS_LITERAL_CSTRING("-malware-"), tables)) {
+  // We don't check mCheckMalware and friends because BuildTables never
+  // includes a table that is not enabled.
+  if (FindInReadable(NS_LITERAL_CSTRING("-malware-"), tables)) {
     return NS_ERROR_MALWARE_URI;
   }
-  if (checkPhishing &&
-    FindInReadable(NS_LITERAL_CSTRING("-phish-"), tables)) {
+  if (FindInReadable(NS_LITERAL_CSTRING("-phish-"), tables)) {
     return NS_ERROR_PHISHING_URI;
   }
-  if (checkTracking &&
-    FindInReadable(NS_LITERAL_CSTRING("-track-"), tables)) {
+  if (FindInReadable(NS_LITERAL_CSTRING("-track-"), tables)) {
     return NS_ERROR_TRACKING_URI;
   }
   return NS_OK;
 }
 
-static nsresult
-ProcessLookupResults(LookupResultArray* results,
-                     bool checkMalware,
-                     bool checkPhishing,
-                     bool checkTracking)
+static nsCString
+ProcessLookupResults(LookupResultArray* results)
 {
   // Build a stringified list of result tables.
   nsTArray<nsCString> tables;
   for (uint32_t i = 0; i < results->Length(); i++) {
     LookupResult& result = results->ElementAt(i);
     MOZ_ASSERT(!result.mNoise, "Lookup results should not have noise added");
     LOG(("Found result from table %s", result.mTableName.get()));
     if (tables.IndexOf(result.mTableName) == nsTArray<nsCString>::NoIndex) {
@@ -209,17 +202,17 @@ ProcessLookupResults(LookupResultArray* 
     }
   }
   nsAutoCString tableStr;
   for (uint32_t i = 0; i < tables.Length(); i++) {
     if (i != 0)
       tableStr.Append(',');
     tableStr.Append(tables[i]);
   }
-  return TablesToResponse(tableStr, checkMalware, checkPhishing, checkTracking);
+  return tableStr;
 }
 
 /**
  * Lookup up a key in the database is a two step process:
  *
  * a) First we look for any Entries in the database that might apply to this
  *    url.  For each URL there are one or two possible domain names to check:
  *    the two-part domain name (example.com) and the three-part name
@@ -976,38 +969,31 @@ public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIURLCLASSIFIERCALLBACK
 
   nsUrlClassifierClassifyCallback(nsIURIClassifierCallback *c,
                                   bool checkMalware,
                                   bool checkPhishing,
                                   bool checkTracking)
     : mCallback(c)
-    , mCheckMalware(checkMalware)
-    , mCheckPhishing(checkPhishing)
-    , mCheckTracking(checkTracking)
     {}
 
 private:
   ~nsUrlClassifierClassifyCallback() {}
 
   nsCOMPtr<nsIURIClassifierCallback> mCallback;
-  bool mCheckMalware;
-  bool mCheckPhishing;
-  bool mCheckTracking;
 };
 
 NS_IMPL_ISUPPORTS(nsUrlClassifierClassifyCallback,
                   nsIUrlClassifierCallback)
 
 NS_IMETHODIMP
 nsUrlClassifierClassifyCallback::HandleEvent(const nsACString& tables)
 {
-  nsresult response = TablesToResponse(tables, mCheckMalware,
-                                       mCheckPhishing, mCheckTracking);
+  nsresult response = TablesToResponse(tables);
   mCallback->OnClassifyComplete(response);
   return NS_OK;
 }
 
 
 // -------------------------------------------------------------------------
 // Proxy class implementation
 
@@ -1250,16 +1236,29 @@ NS_IMETHODIMP
 nsUrlClassifierDBService::ClassifyLocal(nsIPrincipal* aPrincipal,
                                         bool aTrackingProtectionEnabled,
                                         nsresult* aResponse)
 {
   MOZ_ASSERT(NS_IsMainThread(), "ClassifyLocal must be on main thread");
   *aResponse = NS_OK;
   nsAutoCString tables;
   BuildTables(aTrackingProtectionEnabled, tables);
+  nsAutoCString results;
+  nsresult rv = ClassifyLocalWithTables(aPrincipal, tables, results);
+  NS_ENSURE_SUCCESS(rv, rv);
+  *aResponse = TablesToResponse(results);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsUrlClassifierDBService::ClassifyLocalWithTables(nsIPrincipal *aPrincipal,
+                                                  const nsACString & aTables,
+                                                  nsACString & aTableResults)
+{
+  MOZ_ASSERT(NS_IsMainThread(), "ClassifyLocalWithTables must be on main thread");
 
   nsCOMPtr<nsIURI> uri;
   nsresult rv = aPrincipal->GetURI(getter_AddRefs(uri));
   NS_ENSURE_SUCCESS(rv, rv);
   NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
 
   uri = NS_GetInnermostURI(uri);
   NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
@@ -1272,21 +1271,19 @@ nsUrlClassifierDBService::ClassifyLocal(
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsAutoPtr<LookupResultArray> results(new LookupResultArray());
   if (!results) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
 
   // In unittests, we may not have been initalized, so don't crash.
-  rv = mWorkerProxy->DoLocalLookup(key, tables, results);
+  rv = mWorkerProxy->DoLocalLookup(key, aTables, results);
   if (NS_SUCCEEDED(rv)) {
-    rv = ProcessLookupResults(results, mCheckMalware, mCheckPhishing,
-                              mCheckTracking);
-    *aResponse = rv;
+    aTableResults = ProcessLookupResults(results);
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsUrlClassifierDBService::Lookup(nsIPrincipal* aPrincipal,
                                  const nsACString& tables,
                                  nsIUrlClassifierCallback* c)