Bug 1640135 - P3: implement new setter; r=timhuang
authorLiang-Heng Chen <xeonchen@gmail.com>
Thu, 04 Jun 2020 10:51:13 +0000
changeset 533870 bd311336daa756c251f5b6a970ba638f7575a7b0
parent 533869 9a58e5805c830f57aa7d57ad19337803197e0c23
child 533871 64399b8f0d7c0029a4d80b2e723228e7e5347a09
push id37479
push userapavel@mozilla.com
push dateThu, 04 Jun 2020 15:32:20 +0000
treeherdermozilla-central@0d21bdf3fc01 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstimhuang
bugs1640135
milestone79.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 1640135 - P3: implement new setter; r=timhuang Differential Revision: https://phabricator.services.mozilla.com/D77916
caps/OriginAttributes.cpp
--- a/caps/OriginAttributes.cpp
+++ b/caps/OriginAttributes.cpp
@@ -15,90 +15,95 @@
 
 static const char kSourceChar = ':';
 static const char kSanitizedChar = '+';
 
 namespace mozilla {
 
 using dom::URLParams;
 
-void MakeFirstPartyDomain(const nsACString& aScheme, const nsACString& aHost,
-                          int32_t aPort, nsAString& aFirstPartyDomain) {
-  if (!OriginAttributes::UseSiteForFirstPartyDomain()) {
-    aFirstPartyDomain.Assign(NS_ConvertUTF8toUTF16(aHost));
+static void MakeTopLevelInfo(const nsACString& aScheme, const nsACString& aHost,
+                             int32_t aPort, bool aUseSite,
+                             nsAString& aTopLevelInfo) {
+  if (!aUseSite) {
+    aTopLevelInfo.Assign(NS_ConvertUTF8toUTF16(aHost));
     return;
   }
 
   nsAutoCString site;
   site.AssignLiteral("(");
   site.Append(aScheme);
   site.Append(",");
   site.Append(aHost);
   if (aPort != -1) {
     site.Append(",");
     site.AppendInt(aPort);
   }
   site.AppendLiteral(")");
 
-  aFirstPartyDomain.Assign(NS_ConvertUTF8toUTF16(site));
+  aTopLevelInfo.Assign(NS_ConvertUTF8toUTF16(site));
 }
 
-void MakeFirstPartyDomain(const nsACString& aScheme, const nsACString& aHost,
-                          nsAString& aFirstPartyDomain) {
-  MakeFirstPartyDomain(aScheme, aHost, -1, aFirstPartyDomain);
+static void MakeTopLevelInfo(const nsACString& aScheme, const nsACString& aHost,
+                             bool aUseSite, nsAString& aTopLevelInfo) {
+  MakeTopLevelInfo(aScheme, aHost, -1, aUseSite, aTopLevelInfo);
 }
 
-void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
-                                           nsIURI* aURI, bool aForced) {
+static void PopulateTopLevelInfoFromURI(const bool aIsTopLevelDocument,
+                                        nsIURI* aURI, bool aIsFirstPartyEnabled,
+                                        bool aForced, bool aUseSite,
+                                        nsString OriginAttributes::*aTarget,
+                                        OriginAttributes& aOriginAttributes) {
   nsresult rv;
 
   if (!aURI) {
     return;
   }
 
   // If the prefs are off or this is not a top level load, bail out.
-  if ((!IsFirstPartyEnabled() || !aIsTopLevelDocument) && !aForced) {
+  if ((!aIsFirstPartyEnabled || !aIsTopLevelDocument) && !aForced) {
     return;
   }
 
+  nsAString& topLevelInfo = aOriginAttributes.*aTarget;
+
   nsAutoCString scheme;
   rv = aURI->GetScheme(scheme);
   NS_ENSURE_SUCCESS_VOID(rv);
 
   if (scheme.EqualsLiteral("about")) {
-    MakeFirstPartyDomain(scheme,
-                         NS_LITERAL_CSTRING(ABOUT_URI_FIRST_PARTY_DOMAIN),
-                         mFirstPartyDomain);
+    MakeTopLevelInfo(scheme, NS_LITERAL_CSTRING(ABOUT_URI_FIRST_PARTY_DOMAIN),
+                     aUseSite, topLevelInfo);
     return;
   }
 
   // Add-on principals should never get any first-party domain
   // attributes in order to guarantee their storage integrity when switching
   // FPI on and off.
   if (scheme.EqualsLiteral("moz-extension")) {
     return;
   }
 
   nsCOMPtr<nsIPrincipal> blobPrincipal;
   if (dom::BlobURLProtocolHandler::GetBlobURLPrincipal(
           aURI, getter_AddRefs(blobPrincipal))) {
     MOZ_ASSERT(blobPrincipal);
-    mFirstPartyDomain = blobPrincipal->OriginAttributesRef().mFirstPartyDomain;
+    topLevelInfo = blobPrincipal->OriginAttributesRef().*aTarget;
     return;
   }
 
   nsCOMPtr<nsIEffectiveTLDService> tldService =
       do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
   MOZ_ASSERT(tldService);
   NS_ENSURE_TRUE_VOID(tldService);
 
   nsAutoCString baseDomain;
   rv = tldService->GetBaseDomain(aURI, 0, baseDomain);
   if (NS_SUCCEEDED(rv)) {
-    MakeFirstPartyDomain(scheme, baseDomain, mFirstPartyDomain);
+    MakeTopLevelInfo(scheme, baseDomain, aUseSite, topLevelInfo);
     return;
   }
 
   // Saving before rv is overwritten.
   bool isIpAddress = (rv == NS_ERROR_HOST_IS_IP_ADDRESS);
   bool isInsufficientDomainLevels = (rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS);
 
   int32_t port;
@@ -106,68 +111,78 @@ void OriginAttributes::SetFirstPartyDoma
   NS_ENSURE_SUCCESS_VOID(rv);
 
   nsAutoCString host;
   rv = aURI->GetHost(host);
   NS_ENSURE_SUCCESS_VOID(rv);
 
   if (isIpAddress) {
     // If the host is an IPv4/IPv6 address, we still accept it as a
-    // valid firstPartyDomain.
+    // valid topLevelInfo.
     nsAutoCString ipAddr;
 
     if (net_IsValidIPv6Addr(host)) {
       // According to RFC2732, the host of an IPv6 address should be an
       // IPv6reference. The GetHost() of nsIURI will only return the IPv6
       // address. So, we need to convert it back to IPv6reference here.
       ipAddr.AssignLiteral("[");
       ipAddr.Append(host);
       ipAddr.AppendLiteral("]");
     } else {
       ipAddr = host;
     }
 
-    MakeFirstPartyDomain(scheme, ipAddr, port, mFirstPartyDomain);
+    MakeTopLevelInfo(scheme, ipAddr, port, aUseSite, topLevelInfo);
     return;
   }
 
-  if (OriginAttributes::UseSiteForFirstPartyDomain()) {
-    MakeFirstPartyDomain(scheme, host, port, mFirstPartyDomain);
+  if (aUseSite) {
+    MakeTopLevelInfo(scheme, host, port, aUseSite, topLevelInfo);
     return;
   }
 
   if (isInsufficientDomainLevels) {
     nsAutoCString publicSuffix;
     rv = tldService->GetPublicSuffix(aURI, publicSuffix);
     if (NS_SUCCEEDED(rv)) {
-      MakeFirstPartyDomain(scheme, publicSuffix, port, mFirstPartyDomain);
+      MakeTopLevelInfo(scheme, publicSuffix, port, aUseSite, topLevelInfo);
       return;
     }
   }
 }
 
 void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
+                                           nsIURI* aURI, bool aForced) {
+  PopulateTopLevelInfoFromURI(
+      aIsTopLevelDocument, aURI, IsFirstPartyEnabled(), aForced,
+      StaticPrefs::privacy_firstparty_isolate_use_site(),
+      &OriginAttributes::mFirstPartyDomain, *this);
+}
+
+void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
                                            const nsACString& aDomain) {
   SetFirstPartyDomain(aIsTopLevelDocument, NS_ConvertUTF8toUTF16(aDomain));
 }
 
 void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
                                            const nsAString& aDomain,
                                            bool aForced) {
   // If the pref is off or this is not a top level load, bail out.
   if ((!IsFirstPartyEnabled() || !aIsTopLevelDocument) && !aForced) {
     return;
   }
 
   mFirstPartyDomain = aDomain;
 }
 
 void OriginAttributes::SetPartitionKey(nsIURI* aURI) {
-  // TODO: implement this function
-  MOZ_ASSERT(false);
+  PopulateTopLevelInfoFromURI(
+      false /* aIsTopLevelDocument */, aURI, IsFirstPartyEnabled(),
+      true /* aForced */, StaticPrefs::privacy_dynamic_firstparty_use_site(),
+      &OriginAttributes::mPartitionKey, *this);
 }
 
 void OriginAttributes::SetPartitionKey(const nsACString& aDomain) {
   SetPartitionKey(NS_ConvertUTF8toUTF16(aDomain));
 }
 
 void OriginAttributes::SetPartitionKey(const nsAString& aDomain) {
   mPartitionKey = aDomain;