/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef mozilla_BounceTrackingSite_h #define mozilla_BounceTrackingSite_h #include "mozilla/HashFunctions.h" #include "mozilla/OriginAttributes.h" #include "mozilla/Result.h" #include "nsHashKeys.h" #include "nsString.h" #include "fmt/format.h" class nsIPrincipal; class nsIURI; namespace mozilla { // A schemeless site together with the storage partition it ran in, which is // the OriginAttributes stripped of first party domain and partition key. BTP // state is kept per site and partition. Either a site or the null site, which // stands in for documents BTP does not track and belongs to no partition. class BounceTrackingSite final { public: // The null site. BounceTrackingSite() = default; // The site of aPrincipal in its own partition, or the null site if BTP does // not track aPrincipal. static Result FromPrincipal( nsIPrincipal* aPrincipal); // The site of aURI loaded in aOriginAttributes, or the null site if aURI is // not http(s) or has no site. static Result FromURI( nsIURI* aURI, const OriginAttributes& aOriginAttributes); // For sites persisted as a site host or passed in as one over XPCOM. Prefer // FromPrincipal or FromURI. aSiteHost must already be a schemeless site, // e.g. "example.com" rather than "www.example.com", or empty for the null // site. static BounceTrackingSite FromSiteHost( const nsACString& aSiteHost, const OriginAttributes& aOriginAttributes); // BTP state is partitioned by neither first party domain nor partition key. // Returns the OriginAttributes BTP keys its state on. static OriginAttributes NormalizeOriginAttributes( const OriginAttributes& aOriginAttributes) { OriginAttributes oa = aOriginAttributes; oa.StripAttributes(OriginAttributes::STRIP_FIRST_PARTY_DOMAIN | OriginAttributes::STRIP_PARTITION_KEY); return oa; } // Whether BTP keeps state for aPrincipal's site. Only http(s) content // principals have one. static bool ShouldTrackPrincipal(nsIPrincipal* aPrincipal); bool IsNull() const { return mSiteHost.IsEmpty(); } // Empty for the null site. const nsCString& SiteHost() const { return mSiteHost; } const OriginAttributes& OriginAttributesRef() const { MOZ_ASSERT(!IsNull()); return mOriginAttributes; } bool operator==(const BounceTrackingSite& aOther) const { return mSiteHost == aOther.mSiteHost && mOriginAttributes == aOther.mOriginAttributes; } bool operator!=(const BounceTrackingSite& aOther) const { return !(*this == aOther); } PLDHashNumber Hash() const { return AddToHash(HashString(mSiteHost), mOriginAttributes.Hash()); } private: BounceTrackingSite(const nsACString& aSiteHost, const OriginAttributes& aOriginAttributes); nsCString mSiteHost; OriginAttributes mOriginAttributes; }; using BounceTrackingSiteHashKey = nsGenericHashKey; } // namespace mozilla template <> struct fmt::formatter : fmt::formatter { auto format(const mozilla::BounceTrackingSite& aSite, fmt::format_context& aCtx) const { if (aSite.IsNull()) { return fmt::format_to(aCtx.out(), "null"); } nsAutoCString suffix; aSite.OriginAttributesRef().CreateSuffix(suffix); return fmt::format_to(aCtx.out(), "{}{}", aSite.SiteHost(), suffix); } }; #endif