Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/css/SheetLoadData.h
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#ifndef mozilla_css_SheetLoadData_h
8
#define mozilla_css_SheetLoadData_h
9
10
#include "mozilla/css/Loader.h"
11
#include "mozilla/css/SheetParsingMode.h"
12
#include "mozilla/Encoding.h"
13
#include "mozilla/NotNull.h"
14
#include "nsIThreadInternal.h"
15
#include "nsProxyRelease.h"
16
17
namespace mozilla {
18
class StyleSheet;
19
}
20
class nsICSSLoaderObserver;
21
class nsINode;
22
class nsIPrincipal;
23
class nsIURI;
24
25
namespace mozilla {
26
namespace css {
27
28
/*********************************************
29
 * Data needed to properly load a stylesheet *
30
 *********************************************/
31
32
static_assert(eAuthorSheetFeatures == 0 && eUserSheetFeatures == 1 &&
33
                eAgentSheetFeatures == 2,
34
              "sheet parsing mode constants won't fit "
35
              "in SheetLoadData::mParsingMode");
36
37
class SheetLoadData final
38
  : public nsIRunnable
39
  , public nsIThreadObserver
40
{
41
  typedef nsIStyleSheetLinkingElement::MediaMatched MediaMatched;
42
  typedef nsIStyleSheetLinkingElement::IsAlternate IsAlternate;
43
protected:
44
  virtual ~SheetLoadData();
45
46
public:
47
  // Data for loading a sheet linked from a document
48
  SheetLoadData(Loader* aLoader,
49
                const nsAString& aTitle,
50
                nsIURI* aURI,
51
                StyleSheet* aSheet,
52
                bool aSyncLoad,
53
                nsIStyleSheetLinkingElement* aOwningElement,
54
                IsAlternate aIsAlternate,
55
                MediaMatched aMediaMatched,
56
                nsICSSLoaderObserver* aObserver,
57
                nsIPrincipal* aLoaderPrincipal,
58
                nsINode* aRequestingNode);
59
60
  // Data for loading a sheet linked from an @import rule
61
  SheetLoadData(Loader* aLoader,
62
                nsIURI* aURI,
63
                StyleSheet* aSheet,
64
                SheetLoadData* aParentData,
65
                nsICSSLoaderObserver* aObserver,
66
                nsIPrincipal* aLoaderPrincipal,
67
                nsINode* aRequestingNode);
68
69
  // Data for loading a non-document sheet
70
  SheetLoadData(Loader* aLoader,
71
                nsIURI* aURI,
72
                StyleSheet* aSheet,
73
                bool aSyncLoad,
74
                bool aUseSystemPrincipal,
75
                const Encoding* aPreloadEncoding,
76
                nsICSSLoaderObserver* aObserver,
77
                nsIPrincipal* aLoaderPrincipal,
78
                nsINode* aRequestingNode);
79
80
  already_AddRefed<nsIURI> GetReferrerURI();
81
82
  void ScheduleLoadEventIfNeeded();
83
84
  NotNull<const Encoding*> DetermineNonBOMEncoding(nsACString const& aSegment,
85
                                                   nsIChannel* aChannel);
86
87
  // The caller may have the bytes for the stylesheet split across two strings,
88
  // so aBytes1 and aBytes2 refer to those pieces.
89
  nsresult VerifySheetReadyToParse(nsresult aStatus,
90
                                   const nsACString& aBytes1,
91
                                   const nsACString& aBytes2,
92
                                   nsIChannel* aChannel);
93
94
  void SetReferrerPolicyFromHeader(nsIChannel* aChannel);
95
96
  NS_DECL_ISUPPORTS
97
  NS_DECL_NSIRUNNABLE
98
  NS_DECL_NSITHREADOBSERVER
99
100
  // Hold a ref to the CSSLoader so we can call back to it to let it
101
  // know the load finished
102
  RefPtr<Loader> mLoader;
103
104
  // Title needed to pull datas out of the pending datas table when
105
  // the preferred title is changed
106
  nsString mTitle;
107
108
  // The encoding we decided to use for the sheet
109
  const Encoding* mEncoding;
110
111
  // URI we're loading.  Null for inline sheets
112
  nsCOMPtr<nsIURI> mURI;
113
114
  // Should be 1 for non-inline sheets.
115
  uint32_t mLineNumber;
116
117
  // The sheet we're loading data for
118
  RefPtr<StyleSheet> mSheet;
119
120
  // Linked list of datas for the same URI as us
121
  SheetLoadData* mNext; // strong ref
122
123
  // Load data for the sheet that @import-ed us if we were @import-ed
124
  // during the parse
125
  RefPtr<SheetLoadData> mParentData;
126
127
  // Number of sheets we @import-ed that are still loading
128
  uint32_t mPendingChildren;
129
130
  // mSyncLoad is true when the load needs to be synchronous.
131
  // For LoadSheetSync, <link> to chrome stylesheets in UA Widgets,
132
  // and children of sync loads.
133
  bool mSyncLoad : 1;
134
135
  // mIsNonDocumentSheet is true if the load was triggered by LoadSheetSync or
136
  // LoadSheet or an @import from such a sheet.  Non-document sheet loads can
137
  // proceed even if we have no document.
138
  bool mIsNonDocumentSheet : 1;
139
140
  // mIsLoading is true from the moment we are placed in the loader's
141
  // "loading datas" table (right after the async channel is opened)
142
  // to the moment we are removed from said table (due to the load
143
  // completing or being cancelled).
144
  bool mIsLoading : 1;
145
146
  // mIsBeingParsed is true if this stylesheet is currently being parsed.
147
  bool mIsBeingParsed : 1;
148
149
  // mIsCancelled is set to true when a sheet load is stopped by
150
  // Stop() or StopLoadingSheet() (which was removed in Bug 556446).
151
  // SheetLoadData::OnStreamComplete() checks this to avoid parsing
152
  // sheets that have been cancelled and such.
153
  bool mIsCancelled : 1;
154
155
  // mMustNotify is true if the load data is being loaded async and
156
  // the original function call that started the load has returned.
157
  // This applies only to observer notifications; load/error events
158
  // are fired for any SheetLoadData that has a non-null
159
  // mOwningElement.
160
  bool mMustNotify : 1;
161
162
  // mWasAlternate is true if the sheet was an alternate when the load data was
163
  // created.
164
  bool mWasAlternate : 1;
165
166
  // mMediaMatched is true if the sheet matched its medialist when the load data
167
  // was created.
168
  bool mMediaMatched : 1;
169
170
  // mUseSystemPrincipal is true if the system principal should be used for
171
  // this sheet, no matter what the channel principal is.  Only true for sync
172
  // loads.
173
  bool mUseSystemPrincipal : 1;
174
175
  // If true, this SheetLoadData is being used as a way to handle
176
  // async observer notification for an already-complete sheet.
177
  bool mSheetAlreadyComplete : 1;
178
179
  // If true, the sheet is being loaded cross-origin without CORS permissions.
180
  // This is completely normal and CORS isn't needed for such loads.  This
181
  // flag is simply useful in determining whether to set mBlockResourceTiming
182
  // for a child sheet.
183
  bool mIsCrossOriginNoCORS : 1;
184
185
  // If this flag is true, LoadSheet will call SetReportResourceTiming(false)
186
  // on the timedChannel. This is to mark resources that are loaded by a
187
  // cross-origin stylesheet with a no-cors policy.
188
  // https://www.w3.org/TR/resource-timing/#processing-model
189
  bool mBlockResourceTiming : 1;
190
191
  // Boolean flag indicating whether the load has failed.  This will be set
192
  // to true if this load, or the load of any descendant import, fails.
193
  bool mLoadFailed : 1;
194
195
  // This is the element that imported the sheet.  Needed to get the
196
  // charset set on it and to fire load/error events.
197
  nsCOMPtr<nsIStyleSheetLinkingElement> mOwningElement;
198
199
  // The observer that wishes to be notified of load completion
200
  nsCOMPtr<nsICSSLoaderObserver> mObserver;
201
202
  // The principal that identifies who started loading us.
203
  nsCOMPtr<nsIPrincipal> mLoaderPrincipal;
204
205
  // The node that identifies who started loading us.
206
  nsCOMPtr<nsINode> mRequestingNode;
207
208
  // The encoding to use for preloading Must be empty if mOwningElement
209
  // is non-null.
210
  const Encoding* mPreloadEncoding;
211
212
  bool ShouldDefer() const
213
0
  {
214
0
    return mWasAlternate || !mMediaMatched;
215
0
  }
216
217
private:
218
  void FireLoadEvent(nsIThreadInternal* aThread);
219
};
220
221
typedef nsMainThreadPtrHolder<SheetLoadData> SheetLoadDataHolder;
222
223
} // namespace css
224
} // namespace mozilla
225
226
/**
227
 * Casting SheetLoadData to nsISupports is ambiguous.
228
 * This method handles that.
229
 */
230
inline nsISupports*
231
ToSupports(mozilla::css::SheetLoadData* p)
232
0
{
233
0
  return NS_ISUPPORTS_CAST(nsIRunnable*, p);
234
0
}
235
236
#endif // mozilla_css_SheetLoadData_h