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_css_SheetLoadData_h
8 #define mozilla_css_SheetLoadData_h
10 #include "mozilla/css/Loader.h"
11 #include "mozilla/css/SheetParsingMode.h"
12 #include "mozilla/Encoding.h"
13 #include "mozilla/PreloaderBase.h"
14 #include "mozilla/SharedSubResourceCache.h"
15 #include "mozilla/NotNull.h"
16 #include "nsProxyRelease.h"
19 class AsyncEventDispatcher
;
21 } // namespace mozilla
22 class nsICSSLoaderObserver
;
26 class nsIReferrerInfo
;
28 namespace mozilla::css
{
30 /*********************************************
31 * Data needed to properly load a stylesheet *
32 *********************************************/
34 static_assert(eAuthorSheetFeatures
== 0 && eUserSheetFeatures
== 1 &&
35 eAgentSheetFeatures
== 2,
36 "sheet parsing mode constants won't fit "
37 "in SheetLoadData::mParsingMode");
39 enum class SyncLoad
: bool { No
, Yes
};
41 class SheetLoadData final
42 : public PreloaderBase
,
43 public SharedSubResourceCacheLoadingValueBase
<SheetLoadData
> {
44 using MediaMatched
= dom::LinkStyle::MediaMatched
;
45 using IsAlternate
= dom::LinkStyle::IsAlternate
;
46 using UseSystemPrincipal
= css::Loader::UseSystemPrincipal
;
49 virtual ~SheetLoadData();
52 static void PrioritizeAsPreload(nsIChannel
* aChannel
);
54 // If this is a deferred load, start it now.
55 void StartPendingLoad();
57 // Data for loading a sheet linked from a document
58 SheetLoadData(css::Loader
*, const nsAString
& aTitle
, nsIURI
*, StyleSheet
*,
59 SyncLoad
, nsINode
* aOwningNode
, IsAlternate
, MediaMatched
,
60 StylePreloadKind
, nsICSSLoaderObserver
* aObserver
,
61 nsIPrincipal
* aTriggeringPrincipal
, nsIReferrerInfo
*,
62 const nsAString
& aNonce
);
64 // Data for loading a sheet linked from an @import rule
65 SheetLoadData(css::Loader
*, nsIURI
*, StyleSheet
*, SheetLoadData
* aParentData
,
66 nsICSSLoaderObserver
* aObserver
,
67 nsIPrincipal
* aTriggeringPrincipal
, nsIReferrerInfo
*);
69 // Data for loading a non-document sheet
70 SheetLoadData(css::Loader
*, nsIURI
*, StyleSheet
*, SyncLoad
,
71 UseSystemPrincipal
, StylePreloadKind
,
72 const Encoding
* aPreloadEncoding
,
73 nsICSSLoaderObserver
* aObserver
,
74 nsIPrincipal
* aTriggeringPrincipal
, nsIReferrerInfo
*,
75 const nsAString
& aNonce
);
77 nsIReferrerInfo
* ReferrerInfo() const { return mReferrerInfo
; }
79 const nsString
& Nonce() const { return mNonce
; }
81 already_AddRefed
<AsyncEventDispatcher
> PrepareLoadEventIfNeeded();
83 NotNull
<const Encoding
*> DetermineNonBOMEncoding(const nsACString
& aSegment
,
86 // The caller may have the bytes for the stylesheet split across two strings,
87 // so aBytes1 and aBytes2 refer to those pieces.
88 nsresult
VerifySheetReadyToParse(nsresult aStatus
, const nsACString
& aBytes1
,
89 const nsACString
& aBytes2
,
90 nsIChannel
* aChannel
);
94 css::Loader
& Loader() { return *mLoader
; }
96 void DidCancelLoad() { mIsCancelled
= true; }
98 // Hold a ref to the CSSLoader so we can call back to it to let it
99 // know the load finished
100 const RefPtr
<css::Loader
> mLoader
;
102 // Title needed to pull datas out of the pending datas table when
103 // the preferred title is changed
104 const nsString mTitle
;
106 // The encoding we decided to use for the sheet
107 const Encoding
* mEncoding
;
109 // URI we're loading. Null for inline or constructable sheets.
110 nsCOMPtr
<nsIURI
> mURI
;
112 // The sheet we're loading data for
113 const RefPtr
<StyleSheet
> mSheet
;
115 // Load data for the sheet that @import-ed us if we were @import-ed
117 const RefPtr
<SheetLoadData
> mParentData
;
119 // The expiration time of the channel that has loaded this data, if
121 uint32_t mExpirationTime
= 0;
123 // Number of sheets we @import-ed that are still loading
124 uint32_t mPendingChildren
;
126 // mSyncLoad is true when the load needs to be synchronous.
127 // For LoadSheetSync, <link> to chrome stylesheets in UA Widgets,
128 // and children of sync loads.
129 const bool mSyncLoad
: 1;
131 // mIsNonDocumentSheet is true if the load was triggered by LoadSheetSync or
132 // LoadSheet or an @import from such a sheet. Non-document sheet loads can
133 // proceed even if we have no document.
134 const bool mIsNonDocumentSheet
: 1;
136 // Whether this stylesheet is for a child sheet load. This is necessary
137 // because the sheet could be detached mid-load by CSSOM.
138 const bool mIsChildSheet
: 1;
140 // mIsBeingParsed is true if this stylesheet is currently being parsed.
141 bool mIsBeingParsed
: 1;
143 // mIsLoading is set to true when a sheet load is initiated. This field is
144 // also used by the SharedSubResourceCache to avoid having multiple loads for
145 // the same resource.
148 // mIsCancelled is set to true when a sheet load is stopped by
149 // Stop() or StopLoadingSheet() (which was removed in Bug 556446).
150 // SheetLoadData::OnStreamComplete() checks this to avoid parsing
151 // sheets that have been cancelled and such.
152 bool mIsCancelled
: 1;
154 // mMustNotify is true if the load data is being loaded async and the original
155 // function call that started the load has returned.
157 // This applies only to observer notifications; load/error events are fired
158 // for any SheetLoadData that has a non-null owner node (though mMustNotify is
159 // used to avoid an event loop round-trip in that case).
160 bool mMustNotify
: 1;
162 // Whether we had an owner node at the point of creation. This allows
163 // differentiating between "Link" header stylesheets and LinkStyle-owned
165 const bool mHadOwnerNode
: 1;
167 // mWasAlternate is true if the sheet was an alternate
168 // (https://html.spec.whatwg.org/#rel-alternate) when the load data was
170 const bool mWasAlternate
: 1;
172 // mMediaMatched is true if the sheet matched its medialist when the load data
174 const bool mMediaMatched
: 1;
176 // mUseSystemPrincipal is true if the system principal should be used for
177 // this sheet, no matter what the channel principal is. Only true for sync
179 const bool mUseSystemPrincipal
: 1;
181 // If true, this SheetLoadData is being used as a way to handle
182 // async observer notification for an already-complete sheet.
183 bool mSheetAlreadyComplete
: 1;
185 // If true, the sheet is being loaded cross-origin without CORS permissions.
186 // This is completely normal and CORS isn't needed for such loads. This
187 // flag is simply useful in determining whether to set mBlockResourceTiming
188 // for a child sheet.
189 bool mIsCrossOriginNoCORS
: 1;
191 // If this flag is true, LoadSheet will call SetReportResourceTiming(false)
192 // on the timedChannel. This is to mark resources that are loaded by a
193 // cross-origin stylesheet with a no-cors policy.
194 // https://www.w3.org/TR/resource-timing/#processing-model
195 bool mBlockResourceTiming
: 1;
197 // Boolean flag indicating whether the load has failed. This will be set
198 // to true if this load, or the load of any descendant import, fails.
199 bool mLoadFailed
: 1;
201 // Whether this is a preload, and which kind of preload it is.
203 // TODO(emilio): This can become a bitfield once we build with a GCC version
204 // that has the fix for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61414,
205 // which causes a false positive warning here.
206 const StylePreloadKind mPreloadKind
;
208 nsINode
* GetRequestingNode() const;
210 // The observer that wishes to be notified of load completion
211 nsCOMPtr
<nsICSSLoaderObserver
> mObserver
;
213 // The principal that identifies who started loading us.
214 const nsCOMPtr
<nsIPrincipal
> mTriggeringPrincipal
;
216 // Referrer info of the load.
217 const nsCOMPtr
<nsIReferrerInfo
> mReferrerInfo
;
219 // The cryptographic nonce of the load used for CSP checks.
220 const nsString mNonce
;
222 // The encoding guessed from attributes and the document character set.
223 const NotNull
<const Encoding
*> mGuessedEncoding
;
225 // The quirks mode of the loader at the time the load was triggered.
226 const nsCompatibility mCompatMode
;
228 // Whether SheetComplete was called.
229 bool mSheetCompleteCalled
= false;
231 // Whether we intentionally are not calling SheetComplete because nobody is
232 // listening for the load.
233 bool mIntentionallyDropped
= false;
235 bool ShouldDefer() const { return mWasAlternate
|| !mMediaMatched
; }
237 RefPtr
<StyleSheet
> ValueForCache() const;
238 uint32_t ExpirationTime() const { return mExpirationTime
; }
240 // If there are no child sheets outstanding, mark us as complete.
241 // Otherwise, the children are holding strong refs to the data
242 // and will call SheetComplete() on it when they complete.
243 void SheetFinishedParsingAsync() {
244 MOZ_ASSERT(mIsBeingParsed
);
245 mIsBeingParsed
= false;
246 if (!mPendingChildren
) {
247 mLoader
->SheetComplete(*this, NS_OK
);
251 bool IsPreload() const { return mPreloadKind
!= StylePreloadKind::None
; }
252 bool IsLinkRelPreload() const { return css::IsLinkRelPreload(mPreloadKind
); }
254 bool BlocksLoadEvent() const {
255 const auto& root
= RootLoadData();
256 return !root
.IsLinkRelPreload() && !root
.IsSyncLoad();
259 bool IsSyncLoad() const override
{ return mSyncLoad
; }
260 bool IsLoading() const override
{ return mIsLoading
; }
261 bool IsCancelled() const override
{ return mIsCancelled
; }
263 void StartLoading() override
{ mIsLoading
= true; }
264 void SetLoadCompleted() override
{ mIsLoading
= false; }
265 void Cancel() override
{ mIsCancelled
= true; }
268 const SheetLoadData
& RootLoadData() const {
269 const auto* top
= this;
270 while (top
->mParentData
) {
271 top
= top
->mParentData
;
277 using SheetLoadDataHolder
= nsMainThreadPtrHolder
<SheetLoadData
>;
279 } // namespace mozilla::css
281 #endif // mozilla_css_SheetLoadData_h