Bug 1318768 - Part 3: Make nsIURIClassifier::ClassifyLocalWithTables() usable in the content process; r=gcp,baku
authorEhsan Akhgari <ehsan@mozilla.com>
Fri, 18 Nov 2016 16:43:28 -0500
changeset 323836 3cb3e4ebc7880788aba31e0cfb35881533bc685f
parent 323835 f80a175c7f3f01252f119c0f1ac7eec633fce174
child 323837 248955c8a9cb68c2a97c103366af6d073e3bc2e0
push id21
push usermaklebus@msu.edu
push dateThu, 01 Dec 2016 06:22:08 +0000
reviewersgcp, baku
bugs1318768
milestone53.0a1
Bug 1318768 - Part 3: Make nsIURIClassifier::ClassifyLocalWithTables() usable in the content process; r=gcp,baku
dom/ipc/ContentParent.cpp
dom/ipc/ContentParent.h
dom/ipc/PContent.ipdl
toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
toolkit/components/url-classifier/tests/mochitest/test_classifier.html
--- a/dom/ipc/ContentParent.cpp
+++ b/dom/ipc/ContentParent.cpp
@@ -4854,8 +4854,29 @@ ContentParent::DeallocPURLClassifierPare
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(aActor);
 
   RefPtr<URLClassifierParent> actor =
     dont_AddRef(static_cast<URLClassifierParent*>(aActor));
   return true;
 }
+
+mozilla::ipc::IPCResult
+ContentParent::RecvClassifyLocal(const URIParams& aURI, const nsCString& aTables,
+                                 nsCString* aResults)
+{
+  MOZ_ASSERT(aResults);
+  nsCOMPtr<nsIURI> uri = DeserializeURI(aURI);
+  if (!uri) {
+    return IPC_FAIL_NO_REASON(this);
+  }
+  nsCOMPtr<nsIURIClassifier> uriClassifier =
+    do_GetService(NS_URICLASSIFIERSERVICE_CONTRACTID);
+  if (!uriClassifier) {
+    return IPC_FAIL_NO_REASON(this);
+  }
+  nsresult rv = uriClassifier->ClassifyLocalWithTables(uri, aTables, *aResults);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return IPC_FAIL(this, "ClassifyLocalWithTables error");
+  }
+  return IPC_OK();
+}
--- a/dom/ipc/ContentParent.h
+++ b/dom/ipc/ContentParent.h
@@ -529,16 +529,21 @@ public:
   virtual mozilla::ipc::IPCResult
   RecvPURLClassifierConstructor(PURLClassifierParent* aActor,
                                 const Principal& aPrincipal,
                                 const bool& aUseTrackingProtection,
                                 bool* aSuccess) override;
   virtual bool
   DeallocPURLClassifierParent(PURLClassifierParent* aActor) override;
 
+  virtual mozilla::ipc::IPCResult
+  RecvClassifyLocal(const URIParams& aURI,
+                    const nsCString& aTables,
+                    nsCString* aResults) override;
+
   // Use the PHangMonitor channel to ask the child to repaint a tab.
   void ForceTabPaint(TabParent* aTabParent, uint64_t aLayerObserverEpoch);
 
 protected:
   void OnChannelConnected(int32_t pid) override;
 
   virtual void ActorDestroy(ActorDestroyReason why) override;
 
--- a/dom/ipc/PContent.ipdl
+++ b/dom/ipc/PContent.ipdl
@@ -825,16 +825,18 @@ parent:
     async PWebrtcGlobal();
 
     async PPresentation();
 
     async PFlyWebPublishedServer(nsString name, FlyWebPublishOptions params);
 
     sync PURLClassifier(Principal principal, bool useTrackingProtection)
         returns (bool success);
+    sync ClassifyLocal(URIParams uri, nsCString tables)
+        returns (nsCString results);
 
     // Services remoting
 
     async StartVisitedQuery(URIParams uri);
     async VisitURI(URIParams uri, OptionalURIParams referrer, uint32_t flags);
     async SetURITitle(URIParams uri, nsString title);
 
     async LoadURIExternal(URIParams uri, PBrowser windowContext);
--- a/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
@@ -48,16 +48,17 @@
 #include "mozilla/Attributes.h"
 #include "nsIPrincipal.h"
 #include "Classifier.h"
 #include "ProtocolParser.h"
 #include "nsContentUtils.h"
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/dom/PermissionMessageUtils.h"
 #include "mozilla/dom/URLClassifierChild.h"
+#include "mozilla/ipc/URIUtils.h"
 
 namespace mozilla {
 namespace safebrowsing {
 
 nsresult
 TablesToResponse(const nsACString& tables)
 {
   if (tables.IsEmpty()) {
@@ -1461,18 +1462,35 @@ nsUrlClassifierDBService::Classify(nsIPr
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsUrlClassifierDBService::ClassifyLocalWithTables(nsIURI *aURI,
                                                   const nsACString & aTables,
                                                   nsACString & aTableResults)
 {
+  MOZ_ASSERT(NS_IsMainThread(), "ClassifyLocalWithTables must be on main thread");
+
+  if (XRE_IsContentProcess()) {
+    using namespace mozilla::dom;
+    using namespace mozilla::ipc;
+    URIParams uri;
+    SerializeURI(aURI, uri);
+    nsAutoCString tables(aTables);
+    nsAutoCString results;
+    bool result = ContentChild::GetSingleton()->SendClassifyLocal(uri, tables,
+                                                                  &results);
+    if (result) {
+      aTableResults = results;
+      return NS_OK;
+    }
+    return NS_ERROR_FAILURE;
+  }
+
   PROFILER_LABEL_FUNC(js::ProfileEntry::Category::OTHER);
-  MOZ_ASSERT(NS_IsMainThread(), "ClassifyLocalWithTables must be on main thread");
 
   nsCOMPtr<nsIURI> uri = NS_GetInnermostURI(aURI);
   NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
 
   nsAutoCString key;
   // Canonicalize the url
   nsCOMPtr<nsIUrlClassifierUtils> utilsService =
     do_GetService(NS_URLCLASSIFIERUTILS_CONTRACTID);
--- a/toolkit/components/url-classifier/tests/mochitest/test_classifier.html
+++ b/toolkit/components/url-classifier/tests/mochitest/test_classifier.html
@@ -38,60 +38,72 @@ var testData = [
 
 const Cc = SpecialPowers.Cc;
 const Ci = SpecialPowers.Ci;
 const Cr = SpecialPowers.Cr;
 
 var testURLs = [
   { url: "http://example.com",
     trackingProtection: false,
+    table: "",
     result: Cr.NS_OK
   },
   { url: "http://example.com",
     trackingProtection: true,
+    table: "",
     result: Cr.NS_OK
   },
   { url: "http://malware.example.com",
     trackingProtection: false,
+    table: "test-malware-simple",
     result: Cr.NS_ERROR_MALWARE_URI,
   },
   { url: "http://malware.example.com",
     trackingProtection: true,
+    table: "test-malware-simple",
     result: Cr.NS_ERROR_MALWARE_URI,
   },
   { url: "http://unwanted.example.com",
     trackingProtection: false,
+    table: "test-unwanted-simple",
     result: Cr.NS_ERROR_UNWANTED_URI
   },
   { url: "http://unwanted.example.com",
     trackingProtection: true,
+    table: "test-unwanted-simple",
     result: Cr.NS_ERROR_UNWANTED_URI
   },
   { url: "http://itisatrap.org/firefox/its-a-trap.html",
     trackingProtection: false,
+    table: "test-phish-simple",
     result: Cr.NS_ERROR_PHISHING_URI
   },
   { url: "http://itisatrap.org/firefox/its-a-trap.html",
     trackingProtection: true,
+    table: "test-phish-simple",
     result: Cr.NS_ERROR_PHISHING_URI
   },
   { url: "http://tracking.example.com",
     trackingProtection: false,
+    table: "test-track-simple",
     result: Cr.NS_OK
   },
   { url: "http://tracking.example.com",
     trackingProtection: true,
+    table: "test-track-simple",
     result: Cr.NS_ERROR_TRACKING_URI
   },
   { url: "http://blocked.example.com",
     trackingProtection: false,
+    table: "test-block-simple",
     result: Cr.NS_ERROR_BLOCKED_URI
   },
   { url: "http://blocked.example.com",
     trackingProtection: true,
+    table: "test-block-simple",
     result: Cr.NS_ERROR_BLOCKED_URI
   }
 ];
 
 function loadTestFrame() {
   document.getElementById("testFrame").src = "classifierFrame.html";
 }
 
@@ -118,19 +130,24 @@ function testService() {
     let ios = Cc["@mozilla.org/network/io-service;1"].
                 getService(Ci.nsIIOService);
     function runNextTest() {
       if (!testURLs.length) {
         resolve();
         return;
       }
       let test = testURLs.shift();
-      let prin = ssm.createCodebasePrincipal(ios.newURI(test.url, null, null), {});
+      let tables = "test-malware-simple,test-unwanted-simple,test-phish-simple,test-track-simple,test-block-simple";
+      let uri = ios.newURI(test.url, null, null);
+      let prin = ssm.createCodebasePrincipal(uri, {});
+      is(service.classifyLocalWithTables(uri, tables), test.table,
+         `Successful synchronous classification of ${test.url} with TP=${test.trackingProtection}`);
       let result = service.classify(prin, test.trackingProtection, function(errorCode) {
-        is(errorCode, test.result, `Successful classification of ${test.url} with TP=${test.trackingProtection}`);
+        is(errorCode, test.result,
+           `Successful asynchronous classification of ${test.url} with TP=${test.trackingProtection}`);
         runNextTest();
       });
     }
     runNextTest(resolve);
   });
 }
 
 SpecialPowers.pushPrefEnv(