Bug 1517089 - Part 8: Add GetBaseDomainFromSchemeHost to ThirdPartyUtil and make ThirdPartyUtil ref counting thread safe; r=asuth
authorJan Varga <jan.varga@gmail.com>
Fri, 08 Feb 2019 21:01:46 +0100
changeset 520801 6dab164824a85614c96d1169d8a16e43f79f5502
parent 520800 b5b99eeb0467d8bdc3fd9e53d1ebf01e52108262
child 520802 5adb364e4ddfb240c8d5a02b03f32365bba628d0
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersasuth
bugs1517089
milestone67.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 1517089 - Part 8: Add GetBaseDomainFromSchemeHost to ThirdPartyUtil and make ThirdPartyUtil ref counting thread safe; r=asuth Differential Revision: https://phabricator.services.mozilla.com/D19202
dom/base/ThirdPartyUtil.cpp
dom/base/ThirdPartyUtil.h
layout/build/nsLayoutStatics.cpp
netwerk/base/mozIThirdPartyUtil.idl
--- a/dom/base/ThirdPartyUtil.cpp
+++ b/dom/base/ThirdPartyUtil.cpp
@@ -10,16 +10,17 @@
 #include "nsIChannel.h"
 #include "nsIServiceManager.h"
 #include "nsIHttpChannelInternal.h"
 #include "nsIDOMWindow.h"
 #include "nsILoadContext.h"
 #include "nsIPrincipal.h"
 #include "nsIScriptObjectPrincipal.h"
 #include "nsIURI.h"
+#include "nsReadableUtils.h"
 #include "nsThreadUtils.h"
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/Logging.h"
 #include "mozilla/StaticPtr.h"
 #include "mozilla/Unused.h"
 #include "nsPIDOMWindow.h"
 
 NS_IMPL_ISUPPORTS(ThirdPartyUtil, mozIThirdPartyUtil)
@@ -28,16 +29,24 @@ NS_IMPL_ISUPPORTS(ThirdPartyUtil, mozITh
 // MOZ_LOG=thirdPartyUtil:5
 //
 static mozilla::LazyLogModule gThirdPartyLog("thirdPartyUtil");
 #undef LOG
 #define LOG(args) MOZ_LOG(gThirdPartyLog, mozilla::LogLevel::Debug, args)
 
 static mozilla::StaticRefPtr<ThirdPartyUtil> gService;
 
+// static
+void ThirdPartyUtil::Startup() {
+  nsCOMPtr<mozIThirdPartyUtil> tpu;
+  if (NS_WARN_IF(!(tpu = do_GetService(THIRDPARTYUTIL_CONTRACTID)))) {
+    NS_WARNING("Failed to get third party util!");
+  }
+}
+
 nsresult ThirdPartyUtil::Init() {
   NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_AVAILABLE);
 
   MOZ_ASSERT(!gService);
   gService = this;
   mozilla::ClearOnShutdown(&gService);
 
   mTLDService = nsEffectiveTLDService::GetInstance();
@@ -343,8 +352,42 @@ ThirdPartyUtil::GetBaseDomain(nsIURI* aH
     aHostURI->SchemeIs("file", &isFileURI);
     if (!isFileURI) {
       return NS_ERROR_INVALID_ARG;
     }
   }
 
   return NS_OK;
 }
+
+NS_IMETHODIMP
+ThirdPartyUtil::GetBaseDomainFromSchemeHost(const nsACString& aScheme,
+                                            const nsACString& aAsciiHost,
+                                            nsACString& aBaseDomain) {
+  MOZ_DIAGNOSTIC_ASSERT(IsASCII(aAsciiHost));
+
+  // 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->GetBaseDomainFromHost(aAsciiHost, 0, aBaseDomain);
+  if (rv == NS_ERROR_HOST_IS_IP_ADDRESS ||
+      rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) {
+    // aMozURL 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.
+    aBaseDomain = aAsciiHost;
+    rv = NS_OK;
+  }
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // aMozURL (and thus aBaseDomain) may be the string '.'. If so, fail.
+  if (aBaseDomain.Length() == 1 && aBaseDomain.Last() == '.')
+    return NS_ERROR_INVALID_ARG;
+
+  // Reject any URLs without a host that aren't file:// URLs. 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 URLs where "not foreign"
+  // means "the involved URLs are all file://".
+  if (aBaseDomain.IsEmpty() && !aScheme.EqualsLiteral("file")) {
+    return NS_ERROR_INVALID_ARG;
+  }
+
+  return NS_OK;
+}
--- a/dom/base/ThirdPartyUtil.h
+++ b/dom/base/ThirdPartyUtil.h
@@ -12,21 +12,22 @@
 #include "mozIThirdPartyUtil.h"
 #include "nsEffectiveTLDService.h"
 #include "mozilla/Attributes.h"
 
 class nsIURI;
 
 class ThirdPartyUtil final : public mozIThirdPartyUtil {
  public:
-  NS_DECL_ISUPPORTS
+  NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_MOZITHIRDPARTYUTIL
 
   nsresult Init();
 
+  static void Startup();
   static ThirdPartyUtil* GetInstance();
 
  private:
   ~ThirdPartyUtil();
 
   bool IsThirdPartyInternal(const nsCString& aFirstDomain,
                             const nsCString& aSecondDomain) {
     // Check strict equality.
--- a/layout/build/nsLayoutStatics.cpp
+++ b/layout/build/nsLayoutStatics.cpp
@@ -302,16 +302,18 @@ nsresult nsLayoutStatics::Initialize() {
 
   // Reporting API.
   ReportingHeader::Initialize();
 
   if (XRE_IsParentProcess()) {
     InitializeLocalStorage();
   }
 
+  ThirdPartyUtil::Startup();
+
   return NS_OK;
 }
 
 void nsLayoutStatics::Shutdown() {
   // Don't need to shutdown nsWindowMemoryReporter, that will be done by the
   // memory reporter manager.
 
   if (XRE_IsParentProcess() || XRE_IsContentProcess()) {
--- a/netwerk/base/mozIThirdPartyUtil.idl
+++ b/netwerk/base/mozIThirdPartyUtil.idl
@@ -138,16 +138,36 @@ interface mozIThirdPartyUtil : nsISuppor
    * @param aHostURI
    *        The URI to analyze.
    *
    * @return the base domain.
    */
   AUTF8String getBaseDomain(in nsIURI aHostURI);
 
   /**
+   * NOTE: Long term, this method won't be needed once bug 922464 is fixed which
+   * will make it possible to parse all URI's off the main thread.
+   *
+   * getBaseDomainFromSchemeHost
+   *
+   * Get the base domain for aScheme and aHost. Otherwise identical to
+   * getBaseDomain().
+   *
+   * @param aScheme
+   *        The scheme to analyze.
+   *
+   * @param aAsciiHost
+   *        The host to analyze.
+   *
+   * @return the base domain.
+   */
+  AUTF8String getBaseDomainFromSchemeHost(in AUTF8String aScheme,
+                                          in AUTF8String aAsciiHost);
+
+  /**
    * getURIFromWindow
    *
    * Returns the URI associated with the script object principal for the
    * window.
    */
   nsIURI getURIFromWindow(in mozIDOMWindowProxy aWindow);
 
   /**