1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_SRIMetadata_h
8 #define mozilla_dom_SRIMetadata_h
14 namespace mozilla::dom
{
16 class SRIMetadata final
{
17 friend class SRICheck
;
20 static const uint32_t MAX_ALTERNATE_HASHES
= 256;
21 static const int8_t UNKNOWN_ALGORITHM
= -1;
24 * Create an empty metadata object.
26 SRIMetadata() : mAlgorithmType(UNKNOWN_ALGORITHM
), mEmpty(true) {}
29 * Split a string token into the components of an SRI metadata
32 explicit SRIMetadata(const nsACString
& aToken
);
35 * Returns true when this object's hash algorithm is weaker than the
36 * other object's hash algorithm.
38 bool operator<(const SRIMetadata
& aOther
) const;
41 * Not implemented. Should not be used.
43 bool operator>(const SRIMetadata
& aOther
) const;
46 * Add another metadata's hash to this one.
48 SRIMetadata
& operator+=(const SRIMetadata
& aOther
);
51 * Returns true when the two metadata use the same hash algorithm.
53 bool operator==(const SRIMetadata
& aOther
) const;
55 bool IsEmpty() const { return mEmpty
; }
56 bool IsMalformed() const { return mHashes
.IsEmpty() || mAlgorithm
.IsEmpty(); }
57 bool IsAlgorithmSupported() const {
58 return mAlgorithmType
!= UNKNOWN_ALGORITHM
;
60 bool IsValid() const { return !IsMalformed() && IsAlgorithmSupported(); }
62 uint32_t HashCount() const { return mHashes
.Length(); }
63 void GetHash(uint32_t aIndex
, nsCString
* outHash
) const;
64 void GetAlgorithm(nsCString
* outAlg
) const { *outAlg
= mAlgorithm
; }
65 void GetHashType(int8_t* outType
, uint32_t* outLength
) const;
67 const nsString
& GetIntegrityString() const { return mIntegrityString
; }
70 // - this integrity metadata is empty, or
71 // - the other integrity metadata has the same hash algorithm and also the
72 // same set of values otherwise, return false.
74 // This method simply checks if the other integrity metadata is identical to
75 // this one (if it exists), so that a load that has been checked against that
76 // other integrity metadata implies that the current integrity metadata is
78 bool CanTrustBeDelegatedTo(const SRIMetadata
& aOther
) const;
81 CopyableTArray
<nsCString
> mHashes
;
82 nsString mIntegrityString
;
84 int8_t mAlgorithmType
;
88 } // namespace mozilla::dom
90 #endif // mozilla_dom_SRIMetadata_h