forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsLayoutStylesheetCache.cpp
319 lines (260 loc) · 7.75 KB
/
nsLayoutStylesheetCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsLayoutStylesheetCache.h"
#include "nsAppDirectoryServiceDefs.h"
#include "mozilla/css/Loader.h"
#include "nsIFile.h"
#include "nsLayoutCID.h"
#include "nsIMemoryReporter.h"
#include "nsNetUtil.h"
#include "nsIObserverService.h"
#include "nsServiceManagerUtils.h"
#include "nsIXULRuntime.h"
#include "nsCSSStyleSheet.h"
NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(LayoutStyleSheetCacheMallocSizeOf)
static int64_t
GetStylesheetCacheSize()
{
return nsLayoutStylesheetCache::SizeOfIncludingThis(
LayoutStyleSheetCacheMallocSizeOf);
}
NS_MEMORY_REPORTER_IMPLEMENT(StyleSheetCache,
"explicit/layout/style-sheet-cache",
KIND_HEAP,
nsIMemoryReporter::UNITS_BYTES,
GetStylesheetCacheSize,
"Memory used for some built-in style sheets.")
NS_IMPL_ISUPPORTS1(nsLayoutStylesheetCache, nsIObserver)
nsresult
nsLayoutStylesheetCache::Observe(nsISupports* aSubject,
const char* aTopic,
const PRUnichar* aData)
{
if (!strcmp(aTopic, "profile-before-change")) {
mUserContentSheet = nullptr;
mUserChromeSheet = nullptr;
}
else if (!strcmp(aTopic, "profile-do-change")) {
InitFromProfile();
}
else if (strcmp(aTopic, "chrome-flush-skin-caches") == 0 ||
strcmp(aTopic, "chrome-flush-caches") == 0) {
mScrollbarsSheet = nullptr;
mFormsSheet = nullptr;
}
else {
NS_NOTREACHED("Unexpected observer topic.");
}
return NS_OK;
}
nsCSSStyleSheet*
nsLayoutStylesheetCache::ScrollbarsSheet()
{
EnsureGlobal();
if (!gStyleCache)
return nullptr;
if (!gStyleCache->mScrollbarsSheet) {
nsCOMPtr<nsIURI> sheetURI;
NS_NewURI(getter_AddRefs(sheetURI),
NS_LITERAL_CSTRING("chrome://global/skin/scrollbars.css"));
// Scrollbars don't need access to unsafe rules
if (sheetURI)
LoadSheet(sheetURI, gStyleCache->mScrollbarsSheet, false);
NS_ASSERTION(gStyleCache->mScrollbarsSheet, "Could not load scrollbars.css.");
}
return gStyleCache->mScrollbarsSheet;
}
nsCSSStyleSheet*
nsLayoutStylesheetCache::FormsSheet()
{
EnsureGlobal();
if (!gStyleCache)
return nullptr;
if (!gStyleCache->mFormsSheet) {
nsCOMPtr<nsIURI> sheetURI;
NS_NewURI(getter_AddRefs(sheetURI),
NS_LITERAL_CSTRING("resource://gre-resources/forms.css"));
// forms.css needs access to unsafe rules
if (sheetURI)
LoadSheet(sheetURI, gStyleCache->mFormsSheet, true);
NS_ASSERTION(gStyleCache->mFormsSheet, "Could not load forms.css.");
}
return gStyleCache->mFormsSheet;
}
nsCSSStyleSheet*
nsLayoutStylesheetCache::UserContentSheet()
{
EnsureGlobal();
if (!gStyleCache)
return nullptr;
return gStyleCache->mUserContentSheet;
}
nsCSSStyleSheet*
nsLayoutStylesheetCache::UserChromeSheet()
{
EnsureGlobal();
if (!gStyleCache)
return nullptr;
return gStyleCache->mUserChromeSheet;
}
nsCSSStyleSheet*
nsLayoutStylesheetCache::UASheet()
{
EnsureGlobal();
if (!gStyleCache)
return nullptr;
return gStyleCache->mUASheet;
}
nsCSSStyleSheet*
nsLayoutStylesheetCache::QuirkSheet()
{
EnsureGlobal();
if (!gStyleCache)
return nullptr;
return gStyleCache->mQuirkSheet;
}
nsCSSStyleSheet*
nsLayoutStylesheetCache::FullScreenOverrideSheet()
{
EnsureGlobal();
if (!gStyleCache)
return nullptr;
return gStyleCache->mFullScreenOverrideSheet;
}
void
nsLayoutStylesheetCache::Shutdown()
{
NS_IF_RELEASE(gCSSLoader);
NS_IF_RELEASE(gStyleCache);
}
size_t
nsLayoutStylesheetCache::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf)
{
if (!nsLayoutStylesheetCache::gStyleCache) {
return 0;
}
return nsLayoutStylesheetCache::gStyleCache->
SizeOfIncludingThisHelper(aMallocSizeOf);
}
size_t
nsLayoutStylesheetCache::SizeOfIncludingThisHelper(nsMallocSizeOfFun aMallocSizeOf) const
{
size_t n = aMallocSizeOf(this);
#define MEASURE(s) n += s ? s->SizeOfIncludingThis(aMallocSizeOf) : 0;
MEASURE(mScrollbarsSheet);
MEASURE(mFormsSheet);
MEASURE(mUserContentSheet);
MEASURE(mUserChromeSheet);
MEASURE(mUASheet);
MEASURE(mQuirkSheet);
MEASURE(mFullScreenOverrideSheet);
// Measurement of the following members may be added later if DMD finds it is
// worthwhile:
// - gCSSLoader
return n;
}
nsLayoutStylesheetCache::nsLayoutStylesheetCache()
{
nsCOMPtr<nsIObserverService> obsSvc =
mozilla::services::GetObserverService();
NS_ASSERTION(obsSvc, "No global observer service?");
if (obsSvc) {
obsSvc->AddObserver(this, "profile-before-change", false);
obsSvc->AddObserver(this, "profile-do-change", false);
obsSvc->AddObserver(this, "chrome-flush-skin-caches", false);
obsSvc->AddObserver(this, "chrome-flush-caches", false);
}
InitFromProfile();
// And make sure that we load our UA sheets. No need to do this
// per-profile, since they're profile-invariant.
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), "resource://gre-resources/ua.css");
if (uri) {
LoadSheet(uri, mUASheet, true);
}
NS_ASSERTION(mUASheet, "Could not load ua.css");
NS_NewURI(getter_AddRefs(uri), "resource://gre-resources/quirk.css");
if (uri) {
LoadSheet(uri, mQuirkSheet, true);
}
NS_ASSERTION(mQuirkSheet, "Could not load quirk.css");
NS_NewURI(getter_AddRefs(uri), "resource://gre-resources/full-screen-override.css");
if (uri) {
LoadSheet(uri, mFullScreenOverrideSheet, true);
}
NS_ASSERTION(mFullScreenOverrideSheet, "Could not load full-screen-override.css");
mReporter = new NS_MEMORY_REPORTER_NAME(StyleSheetCache);
(void)::NS_RegisterMemoryReporter(mReporter);
}
nsLayoutStylesheetCache::~nsLayoutStylesheetCache()
{
(void)::NS_UnregisterMemoryReporter(mReporter);
mReporter = nullptr;
}
void
nsLayoutStylesheetCache::EnsureGlobal()
{
if (gStyleCache) return;
gStyleCache = new nsLayoutStylesheetCache();
if (!gStyleCache) return;
NS_ADDREF(gStyleCache);
}
void
nsLayoutStylesheetCache::InitFromProfile()
{
nsCOMPtr<nsIXULRuntime> appInfo = do_GetService("@mozilla.org/xre/app-info;1");
if (appInfo) {
bool inSafeMode = false;
appInfo->GetInSafeMode(&inSafeMode);
if (inSafeMode)
return;
}
nsCOMPtr<nsIFile> contentFile;
nsCOMPtr<nsIFile> chromeFile;
NS_GetSpecialDirectory(NS_APP_USER_CHROME_DIR,
getter_AddRefs(contentFile));
if (!contentFile) {
// if we don't have a profile yet, that's OK!
return;
}
contentFile->Clone(getter_AddRefs(chromeFile));
if (!chromeFile) return;
contentFile->Append(NS_LITERAL_STRING("userContent.css"));
chromeFile->Append(NS_LITERAL_STRING("userChrome.css"));
LoadSheetFile(contentFile, mUserContentSheet);
LoadSheetFile(chromeFile, mUserChromeSheet);
}
void
nsLayoutStylesheetCache::LoadSheetFile(nsIFile* aFile, nsRefPtr<nsCSSStyleSheet> &aSheet)
{
bool exists = false;
aFile->Exists(&exists);
if (!exists) return;
nsCOMPtr<nsIURI> uri;
NS_NewFileURI(getter_AddRefs(uri), aFile);
LoadSheet(uri, aSheet, false);
}
void
nsLayoutStylesheetCache::LoadSheet(nsIURI* aURI,
nsRefPtr<nsCSSStyleSheet> &aSheet,
bool aEnableUnsafeRules)
{
if (!aURI) {
NS_ERROR("Null URI. Out of memory?");
return;
}
if (!gCSSLoader) {
gCSSLoader = new mozilla::css::Loader();
NS_IF_ADDREF(gCSSLoader);
}
if (gCSSLoader) {
gCSSLoader->LoadSheetSync(aURI, aEnableUnsafeRules, true,
getter_AddRefs(aSheet));
}
}
nsLayoutStylesheetCache*
nsLayoutStylesheetCache::gStyleCache = nullptr;
mozilla::css::Loader*
nsLayoutStylesheetCache::gCSSLoader = nullptr;