Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / security / SRIMetadata.h
blobcaa3ba25f3cfa01a9f9cab30646f99c2aad9ca53
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
10 #include "nsTArray.h"
11 #include "nsString.h"
12 #include "SRICheck.h"
14 namespace mozilla::dom {
16 class SRIMetadata final {
17 friend class SRICheck;
19 public:
20 static const uint32_t MAX_ALTERNATE_HASHES = 256;
21 static const int8_t UNKNOWN_ALGORITHM = -1;
23 /**
24 * Create an empty metadata object.
26 SRIMetadata() : mAlgorithmType(UNKNOWN_ALGORITHM), mEmpty(true) {}
28 /**
29 * Split a string token into the components of an SRI metadata
30 * attribute.
32 explicit SRIMetadata(const nsACString& aToken);
34 /**
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;
40 /**
41 * Not implemented. Should not be used.
43 bool operator>(const SRIMetadata& aOther) const;
45 /**
46 * Add another metadata's hash to this one.
48 SRIMetadata& operator+=(const SRIMetadata& aOther);
50 /**
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; }
69 // Return true if:
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
77 // also satisfied.
78 bool CanTrustBeDelegatedTo(const SRIMetadata& aOther) const;
80 private:
81 CopyableTArray<nsCString> mHashes;
82 nsString mIntegrityString;
83 nsCString mAlgorithm;
84 int8_t mAlgorithmType;
85 bool mEmpty;
88 } // namespace mozilla::dom
90 #endif // mozilla_dom_SRIMetadata_h