Backed out 4 changesets (bug 1879154) for causing bustage on nsUserCharacteristics...
[gecko.git] / dom / script / ScriptLoadHandler.h
blob8357ad75a885b2d9169b5da9c356623266d1a45f
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 /*
8 * A class that handles loading and evaluation of <script> elements.
9 */
11 #ifndef mozilla_dom_ScriptLoadHandler_h
12 #define mozilla_dom_ScriptLoadHandler_h
14 #include "nsIIncrementalStreamLoader.h"
15 #include "nsISupports.h"
16 #include "mozilla/Encoding.h"
17 #include "mozilla/Maybe.h"
18 #include "mozilla/RefPtr.h"
19 #include "mozilla/UniquePtr.h"
21 namespace JS::loader {
22 class ScriptLoadRequest;
25 namespace mozilla {
27 class Decoder;
29 namespace dom {
31 class ScriptLoader;
32 class SRICheckDataVerifier;
34 class ScriptDecoder {
35 public:
36 enum BOMHandling { Ignore, Remove };
38 ScriptDecoder(const Encoding* aEncoding,
39 ScriptDecoder::BOMHandling handleBOM);
41 ~ScriptDecoder() = default;
44 * Once the charset is found by the EnsureDecoder function, we can
45 * incrementally convert the charset to the one expected by the JS Parser.
47 nsresult DecodeRawData(JS::loader::ScriptLoadRequest* aRequest,
48 const uint8_t* aData, uint32_t aDataLength,
49 bool aEndOfStream);
51 private:
53 * Decode the given data into the already-allocated internal
54 * |ScriptTextBuffer<Unit>|.
56 * This function is intended to be called only by |DecodeRawData| after
57 * determining which sort of |ScriptTextBuffer<Unit>| has been allocated.
59 template <typename Unit>
60 nsresult DecodeRawDataHelper(JS::loader::ScriptLoadRequest* aRequest,
61 const uint8_t* aData, uint32_t aDataLength,
62 bool aEndOfStream);
64 // Unicode decoder for charset.
65 mozilla::UniquePtr<mozilla::Decoder> mDecoder;
68 class ScriptLoadHandler final : public nsIIncrementalStreamLoaderObserver {
69 public:
70 explicit ScriptLoadHandler(
71 ScriptLoader* aScriptLoader, JS::loader::ScriptLoadRequest* aRequest,
72 UniquePtr<SRICheckDataVerifier>&& aSRIDataVerifier);
74 NS_DECL_ISUPPORTS
75 NS_DECL_NSIINCREMENTALSTREAMLOADEROBSERVER
77 private:
78 virtual ~ScriptLoadHandler();
81 * Discover the charset by looking at the stream data, the script tag, and
82 * other indicators. Returns true if charset has been discovered.
84 bool EnsureDecoder(nsIIncrementalStreamLoader* aLoader, const uint8_t* aData,
85 uint32_t aDataLength, bool aEndOfStream) {
86 // Check if the decoder has already been created.
87 if (mDecoder) {
88 return true;
91 return TrySetDecoder(aLoader, aData, aDataLength, aEndOfStream);
95 * Attempt to determine how script data will be decoded, when such
96 * determination hasn't already been made. (If you don't know whether it's
97 * been made yet, use |EnsureDecoder| above instead.) Return false if there
98 * isn't enough information yet to make the determination, or true if a
99 * determination was made.
101 bool TrySetDecoder(nsIIncrementalStreamLoader* aLoader, const uint8_t* aData,
102 uint32_t aDataLength, bool aEndOfStream);
105 * When streaming bytecode, we have the opportunity to fallback early if SRI
106 * does not match the expectation of the document.
108 * If SRI hash is decoded, `sriLength` is set to the length of the hash.
110 nsresult MaybeDecodeSRI(uint32_t* sriLength);
112 // Query the channel to find the data type associated with the input stream.
113 nsresult EnsureKnownDataType(nsIIncrementalStreamLoader* aLoader);
115 // ScriptLoader which will handle the parsed script.
116 RefPtr<ScriptLoader> mScriptLoader;
118 // The ScriptLoadRequest for this load. Decoded data are accumulated on it.
119 RefPtr<JS::loader::ScriptLoadRequest> mRequest;
121 // SRI data verifier.
122 UniquePtr<SRICheckDataVerifier> mSRIDataVerifier;
124 // Status of SRI data operations.
125 nsresult mSRIStatus;
127 UniquePtr<ScriptDecoder> mDecoder;
129 // Flipped to true after calling NotifyStart the first time
130 bool mPreloadStartNotified = false;
133 } // namespace dom
134 } // namespace mozilla
136 #endif // mozilla_dom_ScriptLoadHandler_h