Bug 949639 - Move CanAddURI to nsAndroidHistory. r=blassey, a=bajaj
authorMark Finkle <mfinkle@mozilla.com>
Thu, 12 Dec 2013 23:09:16 -0500
changeset 167875 ff7059bb9c3eba1750d6ae77f54b4f14edee225f
parent 167874 4a876256830eaff08db8c206ead3ddd1302ac66d
child 167876 0366c03687fa785b9d0f0e5fa889fa454923673e
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblassey, bajaj
bugs949639
milestone27.0
Bug 949639 - Move CanAddURI to nsAndroidHistory. r=blassey, a=bajaj
mobile/android/base/GlobalHistory.java
mobile/android/components/build/nsAndroidHistory.cpp
mobile/android/components/build/nsAndroidHistory.h
--- a/mobile/android/base/GlobalHistory.java
+++ b/mobile/android/base/GlobalHistory.java
@@ -89,59 +89,22 @@ class GlobalHistory {
     public void addToGeckoOnly(String uri) {
         Set<String> visitedSet = mVisitedCache.get();
         if (visitedSet != null) {
             visitedSet.add(uri);
         }
         GeckoAppShell.notifyUriVisited(uri);
     }
 
-    // Logic ported from nsNavHistory::CanAddURI.
-    // http://mxr.mozilla.org/mozilla-central/source/toolkit/components/places/nsNavHistory.cpp#1272
-    private boolean canAddURI(String uri) {
-        if (uri == null || uri.length() == 0)
-            return false;
-
-        // First, heck the most common cases (HTTP, HTTPS) to avoid most of the work.
-        if (uri.startsWith("http:") || uri.startsWith("https:"))
-            return true;
-
-        String scheme = Uri.parse(uri).getScheme();
-        if (scheme == null)
-            return false;
-
-        // Now check for all bad things.
-        if (scheme.equals("about") ||
-            scheme.equals("imap") ||
-            scheme.equals("news") ||
-            scheme.equals("mailbox") ||
-            scheme.equals("moz-anno") ||
-            scheme.equals("view-source") ||
-            scheme.equals("chrome") ||
-            scheme.equals("resource") ||
-            scheme.equals("data") ||
-            scheme.equals("wyciwyg") ||
-            scheme.equals("javascript"))
-            return false;
-
-        return true;
-    }
-
     public void add(String uri) {
-        if (!canAddURI(uri))
-            return;
-
         BrowserDB.updateVisitedHistory(GeckoAppShell.getContext().getContentResolver(), uri);
         addToGeckoOnly(uri);
     }
 
     public void update(String uri, String title) {
-        if (!canAddURI(uri))
-            return;
-
         BrowserDB.updateHistoryTitle(GeckoAppShell.getContext().getContentResolver(), uri, title);
     }
 
     public void checkUriVisited(final String uri) {
         mHandler.post(new Runnable() {
             @Override
             public void run() {
                 // this runs on the same handler thread as the processing loop,
--- a/mobile/android/components/build/nsAndroidHistory.cpp
+++ b/mobile/android/components/build/nsAndroidHistory.cpp
@@ -82,39 +82,55 @@ nsAndroidHistory::UnregisterVisitedCallb
 }
 
 NS_IMETHODIMP
 nsAndroidHistory::VisitURI(nsIURI *aURI, nsIURI *aLastVisitedURI, uint32_t aFlags)
 {
   if (!aURI)
     return NS_OK;
 
+  // Silently return if URI is something we shouldn't add to DB.
+  bool canAdd;
+  nsresult rv = CanAddURI(aURI, &canAdd);
+  NS_ENSURE_SUCCESS(rv, rv);
+  if (!canAdd) {
+    return NS_OK;
+  }
+
   if (!(aFlags & VisitFlags::TOP_LEVEL))
     return NS_OK;
 
   if (aFlags & VisitFlags::REDIRECT_SOURCE)
     return NS_OK;
 
   if (aFlags & VisitFlags::UNRECOVERABLE_ERROR)
     return NS_OK;
 
   AndroidBridge *bridge = AndroidBridge::Bridge();
   if (bridge) {
     nsAutoCString uri;
-    nsresult rv = aURI->GetSpec(uri);
+    rv = aURI->GetSpec(uri);
     if (NS_FAILED(rv)) return rv;
     NS_ConvertUTF8toUTF16 uriString(uri);
     bridge->MarkURIVisited(uriString);
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsAndroidHistory::SetURITitle(nsIURI *aURI, const nsAString& aTitle)
 {
+  // Silently return if URI is something we shouldn't add to DB.
+  bool canAdd;
+  nsresult rv = CanAddURI(aURI, &canAdd);
+  NS_ENSURE_SUCCESS(rv, rv);
+  if (!canAdd) {
+    return NS_OK;
+  }
+
   AndroidBridge *bridge = AndroidBridge::Bridge();
   if (bridge) {
     nsAutoCString uri;
     nsresult rv = aURI->GetSpec(uri);
     if (NS_FAILED(rv)) return rv;
     NS_ConvertUTF8toUTF16 uriString(uri);
     bridge->SetURITitle(uriString, aTitle);
   }
@@ -146,8 +162,58 @@ nsAndroidHistory::Run()
       // as per the IHistory interface contract, remove the
       // Link pointers once they have been notified
       mListeners.Remove(uriString);
       delete list;
     }
   }
   return NS_OK;
 }
+
+// Filter out unwanted URIs such as "chrome:", "mailbox:", etc.
+//
+// The model is if we don't know differently then add which basically means
+// we are suppose to try all the things we know not to allow in and then if
+// we don't bail go on and allow it in.
+//
+// Logic ported from nsNavHistory::CanAddURI.
+
+NS_IMETHODIMP
+nsAndroidHistory::CanAddURI(nsIURI* aURI, bool* canAdd)
+{
+  NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread");
+  NS_ENSURE_ARG(aURI);
+  NS_ENSURE_ARG_POINTER(canAdd);
+
+  nsAutoCString scheme;
+  nsresult rv = aURI->GetScheme(scheme);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // first check the most common cases (HTTP, HTTPS) to allow in to avoid most
+  // of the work
+  if (scheme.EqualsLiteral("http")) {
+    *canAdd = true;
+    return NS_OK;
+  }
+  if (scheme.EqualsLiteral("https")) {
+    *canAdd = true;
+    return NS_OK;
+  }
+
+  // now check for all bad things
+  if (scheme.EqualsLiteral("about") ||
+      scheme.EqualsLiteral("imap") ||
+      scheme.EqualsLiteral("news") ||
+      scheme.EqualsLiteral("mailbox") ||
+      scheme.EqualsLiteral("moz-anno") ||
+      scheme.EqualsLiteral("view-source") ||
+      scheme.EqualsLiteral("chrome") ||
+      scheme.EqualsLiteral("resource") ||
+      scheme.EqualsLiteral("data") ||
+      scheme.EqualsLiteral("wyciwyg") ||
+      scheme.EqualsLiteral("javascript") ||
+      scheme.EqualsLiteral("blob")) {
+    *canAdd = false;
+    return NS_OK;
+  }
+  *canAdd = true;
+  return NS_OK;
+}
--- a/mobile/android/components/build/nsAndroidHistory.h
+++ b/mobile/android/components/build/nsAndroidHistory.h
@@ -29,11 +29,13 @@ public:
 
   nsAndroidHistory();
 
 private:
   static nsAndroidHistory* sHistory;
 
   nsDataHashtable<nsStringHashKey, nsTArray<mozilla::dom::Link *> *> mListeners;
   nsTPriorityQueue<nsString> mPendingURIs;
+
+  nsresult CanAddURI(nsIURI* aURI, bool* canAdd);
 };
 
 #endif