Bug 683280 - Workers: creating workers from 'localhost' or an IP address fails. r=sicking, a=clegnitto.
authorBen Turner <bent.mozilla@gmail.com>
Tue, 15 Nov 2011 14:30:32 -0800
changeset 79203 2e0d543637d3ba5a9dc6139b26bc9016729d9d16
parent 79202 0b06a096b18083948a848568a51c58d7f10e4b80
child 79204 54c9b55a4c505ffd8694da3f715d7b881521bbb1
push id78
push userclegnitto@mozilla.com
push dateFri, 16 Dec 2011 17:32:24 +0000
treeherdermozilla-release@79d24e644fdd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssicking, clegnitto
bugs683280
milestone9.0
Bug 683280 - Workers: creating workers from 'localhost' or an IP address fails. r=sicking, a=clegnitto.
content/base/src/ThirdPartyUtil.cpp
content/base/src/ThirdPartyUtil.h
dom/workers/WorkerPrivate.cpp
netwerk/base/public/mozIThirdPartyUtil.idl
--- a/content/base/src/ThirdPartyUtil.cpp
+++ b/content/base/src/ThirdPartyUtil.cpp
@@ -40,68 +40,29 @@
 #include "nsIServiceManager.h"
 #include "nsIHttpChannelInternal.h"
 #include "nsIDOMWindow.h"
 #include "nsILoadContext.h"
 #include "nsIPrincipal.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsThreadUtils.h"
 
-NS_IMPL_ISUPPORTS1(ThirdPartyUtil, mozIThirdPartyUtil)
+NS_IMPL_ISUPPORTS2(ThirdPartyUtil, mozIThirdPartyUtil,
+                                   mozIThirdPartyUtil_BRANCH)
 
 nsresult
 ThirdPartyUtil::Init()
 {
   NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_AVAILABLE);
 
   nsresult rv;
   mTLDService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID, &rv);
   return rv;
 }
 
-// Get the base domain for aHostURI; e.g. for "www.bbc.co.uk", this would be
-// "bbc.co.uk". Only properly-formed URI's are tolerated, though a trailing
-// dot may be present. If aHostURI is an IP address, an alias such as
-// 'localhost', an eTLD such as 'co.uk', or the empty string, aBaseDomain will
-// be the exact host. The result of this function should only be used in exact
-// string comparisons, since substring comparisons will not be valid for the
-// special cases elided above.
-nsresult
-ThirdPartyUtil::GetBaseDomain(nsIURI* aHostURI,
-                              nsCString& aBaseDomain)
-{
-  // Get the base domain. this will fail if the host contains a leading dot,
-  // more than one trailing dot, or is otherwise malformed.
-  nsresult rv = mTLDService->GetBaseDomain(aHostURI, 0, aBaseDomain);
-  if (rv == NS_ERROR_HOST_IS_IP_ADDRESS ||
-      rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) {
-    // aHostURI is either an IP address, an alias such as 'localhost', an eTLD
-    // such as 'co.uk', or the empty string. Uses the normalized host in such
-    // cases.
-    rv = aHostURI->GetAsciiHost(aBaseDomain);
-  }
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  // aHostURI (and thus aBaseDomain) may be the string '.'. If so, fail.
-  if (aBaseDomain.Length() == 1 && aBaseDomain.Last() == '.')
-    return NS_ERROR_INVALID_ARG;
-
-  // Reject any URIs without a host that aren't file:// URIs. This makes it the
-  // only way we can get a base domain consisting of the empty string, which
-  // means we can safely perform foreign tests on such URIs where "not foreign"
-  // means "the involved URIs are all file://".
-  if (aBaseDomain.IsEmpty()) {
-    PRBool isFileURI = PR_FALSE;
-    aHostURI->SchemeIs("file", &isFileURI);
-    NS_ENSURE_TRUE(isFileURI, NS_ERROR_INVALID_ARG);
-  }
-
-  return NS_OK;
-}
-
 // Determine if aFirstDomain is a different base domain to aSecondURI; or, if
 // the concept of base domain does not apply, determine if the two hosts are not
 // string-identical.
 nsresult
 ThirdPartyUtil::IsThirdPartyInternal(const nsCString& aFirstDomain,
                                      nsIURI* aSecondURI,
                                      PRBool* aResult)
 {
@@ -311,8 +272,47 @@ ThirdPartyUtil::IsThirdPartyChannel(nsIC
     ourWin = parentWin;
   }
 
   // Check the window hierarchy. This covers most cases for an ordinary page
   // load from the location bar.
   return IsThirdPartyWindow(ourWin, channelURI, aResult);
 }
 
+// Get the base domain for aHostURI; e.g. for "www.bbc.co.uk", this would be
+// "bbc.co.uk". Only properly-formed URI's are tolerated, though a trailing
+// dot may be present. If aHostURI is an IP address, an alias such as
+// 'localhost', an eTLD such as 'co.uk', or the empty string, aBaseDomain will
+// be the exact host. The result of this function should only be used in exact
+// string comparisons, since substring comparisons will not be valid for the
+// special cases elided above.
+NS_IMETHODIMP
+ThirdPartyUtil::GetBaseDomain(nsIURI* aHostURI,
+                              nsACString& aBaseDomain)
+{
+  // Get the base domain. this will fail if the host contains a leading dot,
+  // more than one trailing dot, or is otherwise malformed.
+  nsresult rv = mTLDService->GetBaseDomain(aHostURI, 0, aBaseDomain);
+  if (rv == NS_ERROR_HOST_IS_IP_ADDRESS ||
+      rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) {
+    // aHostURI is either an IP address, an alias such as 'localhost', an eTLD
+    // such as 'co.uk', or the empty string. Uses the normalized host in such
+    // cases.
+    rv = aHostURI->GetAsciiHost(aBaseDomain);
+  }
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // aHostURI (and thus aBaseDomain) may be the string '.'. If so, fail.
+  if (aBaseDomain.Length() == 1 && aBaseDomain.Last() == '.')
+    return NS_ERROR_INVALID_ARG;
+
+  // Reject any URIs without a host that aren't file:// URIs. This makes it the
+  // only way we can get a base domain consisting of the empty string, which
+  // means we can safely perform foreign tests on such URIs where "not foreign"
+  // means "the involved URIs are all file://".
+  if (aBaseDomain.IsEmpty()) {
+    PRBool isFileURI = false;
+    aHostURI->SchemeIs("file", &isFileURI);
+    NS_ENSURE_TRUE(isFileURI, NS_ERROR_INVALID_ARG);
+  }
+
+  return NS_OK;
+}
--- a/content/base/src/ThirdPartyUtil.h
+++ b/content/base/src/ThirdPartyUtil.h
@@ -42,26 +42,26 @@
 #include "nsString.h"
 #include "mozIThirdPartyUtil.h"
 #include "nsIEffectiveTLDService.h"
 
 class nsIURI;
 class nsIChannel;
 class nsIDOMWindow;
 
-class ThirdPartyUtil : public mozIThirdPartyUtil
+class ThirdPartyUtil : public mozIThirdPartyUtil_BRANCH
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_MOZITHIRDPARTYUTIL
+  NS_DECL_MOZITHIRDPARTYUTIL_BRANCH
 
   nsresult Init();
 
 private:
-  nsresult GetBaseDomain(nsIURI* aHostURI, nsCString& aBaseDomain);
   nsresult IsThirdPartyInternal(const nsCString& aFirstDomain,
     nsIURI* aSecondURI, PRBool* aResult);
   static already_AddRefed<nsIURI> GetURIFromWindow(nsIDOMWindow* aWin);
 
   nsCOMPtr<nsIEffectiveTLDService> mTLDService;
 };
 
 #endif
--- a/dom/workers/WorkerPrivate.cpp
+++ b/dom/workers/WorkerPrivate.cpp
@@ -34,21 +34,21 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "WorkerPrivate.h"
 
+#include "mozIThirdPartyUtil.h"
 #include "nsIClassInfo.h"
 #include "nsIConsoleService.h"
 #include "nsIDOMFile.h"
 #include "nsIDocument.h"
-#include "nsIEffectiveTLDService.h"
 #include "nsIJSContextStack.h"
 #include "nsIMemoryReporter.h"
 #include "nsIScriptError.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsPIDOMWindow.h"
 #include "nsITextToSubURI.h"
 #include "nsITimer.h"
@@ -2321,24 +2321,24 @@ WorkerPrivate::Create(JSContext* aCx, JS
           return nsnull;
         }
 
         if (isFile) {
           // XXX Fix this, need a real domain here.
           domain = file;
         }
         else {
-          nsCOMPtr<nsIEffectiveTLDService> tldService =
-            do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
-          if (!tldService) {
-            JS_ReportError(aCx, "Could not get TLD service!");
+          nsCOMPtr<mozIThirdPartyUtil_BRANCH> thirdPartyUtil =
+            do_GetService(THIRDPARTYUTIL_CONTRACTID);
+          if (!thirdPartyUtil) {
+            JS_ReportError(aCx, "Could not get third party helper service!");
             return nsnull;
           }
 
-          if (NS_FAILED(tldService->GetBaseDomain(codebase, 0, domain))) {
+          if (NS_FAILED(thirdPartyUtil->GetBaseDomain(codebase, domain))) {
             JS_ReportError(aCx, "Could not get domain!");
             return nsnull;
           }
         }
       }
     }
     else {
       // Not a window
--- a/netwerk/base/public/mozIThirdPartyUtil.idl
+++ b/netwerk/base/public/mozIThirdPartyUtil.idl
@@ -152,16 +152,43 @@ interface mozIThirdPartyUtil : nsISuppor
    * @throws if 'aChannel' is null; the channel has no notification callbacks or
    *         an associated window; or isThirdPartyWindow throws.
    *
    * @see isThirdPartyWindow
    */
   boolean isThirdPartyChannel(in nsIChannel aChannel, [optional] in nsIURI aURI);
 };
 
+/**
+ * Interface extension for branches only to avoid iid rev of mozIThirdPartyUtil.
+ * This interface has been merged with mozIThirdPartyUtil on trunk and will
+ * cease to exist in Firefox 10.
+ */
+[scriptable, uuid(bc92a019-c595-439a-9d1c-353d3ff80780)]
+interface mozIThirdPartyUtil_BRANCH : mozIThirdPartyUtil
+{
+  /**
+   * getBaseDomain
+   *
+   * Get the base domain for aHostURI; e.g. for "www.bbc.co.uk", this would be
+   * "bbc.co.uk". Only properly-formed URI's are tolerated, though a trailing
+   * dot may be present. If aHostURI is an IP address, an alias such as
+   * 'localhost', an eTLD such as 'co.uk', or the empty string, aBaseDomain will
+   * be the exact host. The result of this function should only be used in exact
+   * string comparisons, since substring comparisons will not be valid for the
+   * special cases elided above.
+   *
+   * @param aHostURI
+   *        The URI to analyze.
+   *
+   * @return the base domain.
+   */
+  AUTF8String getBaseDomain(in nsIURI aHostURI);
+};
+
 %{ C++
 /**
  * The mozIThirdPartyUtil implementation is an XPCOM service registered
  * under the ContractID:
  */
 #define THIRDPARTYUTIL_CONTRACTID "@mozilla.org/thirdpartyutil;1"
 %}