2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2013-01-17 16:45:12 -08:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsLayoutStylesheetCache.h"
|
|
|
|
|
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2014-02-26 04:26:14 -08:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-06-28 15:49:35 -07:00
|
|
|
#include "mozilla/css/Loader.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2009-06-22 04:34:41 -07:00
|
|
|
#include "nsIXULRuntime.h"
|
2010-05-11 13:41:47 -07:00
|
|
|
#include "nsCSSStyleSheet.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-26 04:26:14 -08:00
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
static bool sNumberControlEnabled;
|
|
|
|
|
|
|
|
#define NUMBER_CONTROL_PREF "dom.forms.number"
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(
|
2013-12-07 22:09:10 -08:00
|
|
|
nsLayoutStylesheetCache, nsIObserver, nsIMemoryReporter)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLayoutStylesheetCache::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* aData)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!strcmp(aTopic, "profile-before-change")) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mUserContentSheet = nullptr;
|
|
|
|
mUserChromeSheet = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else if (!strcmp(aTopic, "profile-do-change")) {
|
|
|
|
InitFromProfile();
|
|
|
|
}
|
|
|
|
else if (strcmp(aTopic, "chrome-flush-skin-caches") == 0 ||
|
|
|
|
strcmp(aTopic, "chrome-flush-caches") == 0) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mScrollbarsSheet = nullptr;
|
|
|
|
mFormsSheet = nullptr;
|
2014-02-26 04:26:14 -08:00
|
|
|
mNumberControlSheet = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_NOTREACHED("Unexpected observer topic.");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet*
|
2007-03-22 10:30:00 -07:00
|
|
|
nsLayoutStylesheetCache::ScrollbarsSheet()
|
|
|
|
{
|
|
|
|
EnsureGlobal();
|
|
|
|
if (!gStyleCache)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!gStyleCache->mScrollbarsSheet) {
|
|
|
|
nsCOMPtr<nsIURI> sheetURI;
|
|
|
|
NS_NewURI(getter_AddRefs(sheetURI),
|
2008-03-14 08:24:50 -07:00
|
|
|
NS_LITERAL_CSTRING("chrome://global/skin/scrollbars.css"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Scrollbars don't need access to unsafe rules
|
|
|
|
if (sheetURI)
|
2011-10-17 07:59:28 -07:00
|
|
|
LoadSheet(sheetURI, gStyleCache->mScrollbarsSheet, false);
|
2008-03-14 08:24:50 -07:00
|
|
|
NS_ASSERTION(gStyleCache->mScrollbarsSheet, "Could not load scrollbars.css.");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return gStyleCache->mScrollbarsSheet;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet*
|
2007-03-22 10:30:00 -07:00
|
|
|
nsLayoutStylesheetCache::FormsSheet()
|
|
|
|
{
|
|
|
|
EnsureGlobal();
|
|
|
|
if (!gStyleCache)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!gStyleCache->mFormsSheet) {
|
|
|
|
nsCOMPtr<nsIURI> sheetURI;
|
|
|
|
NS_NewURI(getter_AddRefs(sheetURI),
|
2009-10-12 12:31:50 -07:00
|
|
|
NS_LITERAL_CSTRING("resource://gre-resources/forms.css"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// forms.css needs access to unsafe rules
|
|
|
|
if (sheetURI)
|
2011-10-17 07:59:28 -07:00
|
|
|
LoadSheet(sheetURI, gStyleCache->mFormsSheet, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(gStyleCache->mFormsSheet, "Could not load forms.css.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return gStyleCache->mFormsSheet;
|
|
|
|
}
|
|
|
|
|
2014-02-26 04:26:14 -08:00
|
|
|
nsCSSStyleSheet*
|
|
|
|
nsLayoutStylesheetCache::NumberControlSheet()
|
|
|
|
{
|
|
|
|
EnsureGlobal();
|
|
|
|
if (!gStyleCache)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (!sNumberControlEnabled) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gStyleCache->mNumberControlSheet) {
|
|
|
|
nsCOMPtr<nsIURI> sheetURI;
|
|
|
|
NS_NewURI(getter_AddRefs(sheetURI),
|
|
|
|
NS_LITERAL_CSTRING("resource://gre-resources/number-control.css"));
|
|
|
|
|
|
|
|
if (sheetURI)
|
|
|
|
LoadSheet(sheetURI, gStyleCache->mNumberControlSheet, false);
|
|
|
|
|
|
|
|
NS_ASSERTION(gStyleCache->mNumberControlSheet, "Could not load number-control.css");
|
|
|
|
}
|
|
|
|
|
|
|
|
return gStyleCache->mNumberControlSheet;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet*
|
2007-03-22 10:30:00 -07:00
|
|
|
nsLayoutStylesheetCache::UserContentSheet()
|
|
|
|
{
|
|
|
|
EnsureGlobal();
|
|
|
|
if (!gStyleCache)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return gStyleCache->mUserContentSheet;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet*
|
2007-03-22 10:30:00 -07:00
|
|
|
nsLayoutStylesheetCache::UserChromeSheet()
|
|
|
|
{
|
|
|
|
EnsureGlobal();
|
|
|
|
if (!gStyleCache)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return gStyleCache->mUserChromeSheet;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet*
|
2008-11-02 18:29:51 -08:00
|
|
|
nsLayoutStylesheetCache::UASheet()
|
|
|
|
{
|
|
|
|
EnsureGlobal();
|
|
|
|
if (!gStyleCache)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2008-11-02 18:29:51 -08:00
|
|
|
|
|
|
|
return gStyleCache->mUASheet;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet*
|
2008-11-02 18:29:51 -08:00
|
|
|
nsLayoutStylesheetCache::QuirkSheet()
|
|
|
|
{
|
|
|
|
EnsureGlobal();
|
|
|
|
if (!gStyleCache)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2008-11-02 18:29:51 -08:00
|
|
|
|
|
|
|
return gStyleCache->mQuirkSheet;
|
|
|
|
}
|
|
|
|
|
2011-11-29 14:00:31 -08:00
|
|
|
nsCSSStyleSheet*
|
|
|
|
nsLayoutStylesheetCache::FullScreenOverrideSheet()
|
|
|
|
{
|
|
|
|
EnsureGlobal();
|
|
|
|
if (!gStyleCache)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2011-11-29 14:00:31 -08:00
|
|
|
|
|
|
|
return gStyleCache->mFullScreenOverrideSheet;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsLayoutStylesheetCache::Shutdown()
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(gCSSLoader);
|
|
|
|
NS_IF_RELEASE(gStyleCache);
|
|
|
|
}
|
|
|
|
|
2013-12-07 22:09:10 -08:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(LayoutStylesheetCacheMallocSizeOf)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLayoutStylesheetCache::CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData)
|
2012-01-02 18:19:14 -08:00
|
|
|
{
|
2013-12-07 22:09:10 -08:00
|
|
|
return MOZ_COLLECT_REPORT(
|
|
|
|
"explicit/layout/style-sheet-cache", KIND_HEAP, UNITS_BYTES,
|
|
|
|
SizeOfIncludingThis(LayoutStylesheetCacheMallocSizeOf),
|
|
|
|
"Memory used for some built-in style sheets.");
|
2012-01-02 18:19:14 -08:00
|
|
|
}
|
|
|
|
|
2013-12-07 22:09:10 -08:00
|
|
|
|
2012-01-02 18:19:14 -08:00
|
|
|
size_t
|
2013-11-06 21:35:30 -08:00
|
|
|
nsLayoutStylesheetCache::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2012-01-02 18:19:14 -08:00
|
|
|
{
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
|
|
|
|
#define MEASURE(s) n += s ? s->SizeOfIncludingThis(aMallocSizeOf) : 0;
|
|
|
|
|
|
|
|
MEASURE(mScrollbarsSheet);
|
|
|
|
MEASURE(mFormsSheet);
|
2014-02-26 04:26:14 -08:00
|
|
|
MEASURE(mNumberControlSheet);
|
2012-01-02 18:19:14 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsLayoutStylesheetCache::nsLayoutStylesheetCache()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obsSvc =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
mozilla::services::GetObserverService();
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(obsSvc, "No global observer service?");
|
|
|
|
|
|
|
|
if (obsSvc) {
|
2011-10-17 07:59:28 -07:00
|
|
|
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);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
InitFromProfile();
|
2008-11-02 18:29:51 -08:00
|
|
|
|
|
|
|
// And make sure that we load our UA sheets. No need to do this
|
|
|
|
// per-profile, since they're profile-invariant.
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2009-10-12 12:31:50 -07:00
|
|
|
NS_NewURI(getter_AddRefs(uri), "resource://gre-resources/ua.css");
|
2008-11-02 18:29:51 -08:00
|
|
|
if (uri) {
|
2011-10-17 07:59:28 -07:00
|
|
|
LoadSheet(uri, mUASheet, true);
|
2008-11-02 18:29:51 -08:00
|
|
|
}
|
|
|
|
NS_ASSERTION(mUASheet, "Could not load ua.css");
|
|
|
|
|
2009-10-12 12:31:50 -07:00
|
|
|
NS_NewURI(getter_AddRefs(uri), "resource://gre-resources/quirk.css");
|
2008-11-02 18:29:51 -08:00
|
|
|
if (uri) {
|
2011-10-17 07:59:28 -07:00
|
|
|
LoadSheet(uri, mQuirkSheet, true);
|
2008-11-02 18:29:51 -08:00
|
|
|
}
|
|
|
|
NS_ASSERTION(mQuirkSheet, "Could not load quirk.css");
|
2011-11-29 14:00:31 -08:00
|
|
|
|
|
|
|
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");
|
2012-01-02 18:19:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsLayoutStylesheetCache::~nsLayoutStylesheetCache()
|
|
|
|
{
|
2013-12-07 22:09:10 -08:00
|
|
|
mozilla::UnregisterWeakMemoryReporter(this);
|
2013-01-17 16:45:12 -08:00
|
|
|
gStyleCache = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-11-06 21:35:30 -08:00
|
|
|
void
|
|
|
|
nsLayoutStylesheetCache::InitMemoryReporter()
|
|
|
|
{
|
2013-12-07 22:09:10 -08:00
|
|
|
mozilla::RegisterWeakMemoryReporter(this);
|
2013-11-06 21:35:30 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsLayoutStylesheetCache::EnsureGlobal()
|
|
|
|
{
|
|
|
|
if (gStyleCache) return;
|
|
|
|
|
|
|
|
gStyleCache = new nsLayoutStylesheetCache();
|
|
|
|
if (!gStyleCache) return;
|
|
|
|
|
|
|
|
NS_ADDREF(gStyleCache);
|
2013-11-06 21:35:30 -08:00
|
|
|
|
|
|
|
gStyleCache->InitMemoryReporter();
|
2014-02-26 04:26:14 -08:00
|
|
|
|
|
|
|
Preferences::AddBoolVarCache(&sNumberControlEnabled, NUMBER_CONTROL_PREF,
|
|
|
|
true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsLayoutStylesheetCache::InitFromProfile()
|
|
|
|
{
|
2009-06-22 04:34:41 -07:00
|
|
|
nsCOMPtr<nsIXULRuntime> appInfo = do_GetService("@mozilla.org/xre/app-info;1");
|
|
|
|
if (appInfo) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool inSafeMode = false;
|
2009-06-22 04:34:41 -07:00
|
|
|
appInfo->GetInSafeMode(&inSafeMode);
|
|
|
|
if (inSafeMode)
|
|
|
|
return;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
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
|
2010-05-11 13:41:47 -07:00
|
|
|
nsLayoutStylesheetCache::LoadSheetFile(nsIFile* aFile, nsRefPtr<nsCSSStyleSheet> &aSheet)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool exists = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
aFile->Exists(&exists);
|
|
|
|
|
|
|
|
if (!exists) return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
NS_NewFileURI(getter_AddRefs(uri), aFile);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
LoadSheet(uri, aSheet, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-03-02 13:00:53 -08:00
|
|
|
nsLayoutStylesheetCache::LoadSheet(nsIURI* aURI,
|
2010-05-11 13:41:47 -07:00
|
|
|
nsRefPtr<nsCSSStyleSheet> &aSheet,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aEnableUnsafeRules)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!aURI) {
|
|
|
|
NS_ERROR("Null URI. Out of memory?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-17 16:45:12 -08:00
|
|
|
if (!gCSSLoader) {
|
2010-03-02 13:00:53 -08:00
|
|
|
gCSSLoader = new mozilla::css::Loader();
|
|
|
|
NS_IF_ADDREF(gCSSLoader);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (gCSSLoader) {
|
2011-10-17 07:59:28 -07:00
|
|
|
gCSSLoader->LoadSheetSync(aURI, aEnableUnsafeRules, true,
|
2009-03-16 18:38:36 -07:00
|
|
|
getter_AddRefs(aSheet));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-03-02 13:00:53 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsLayoutStylesheetCache*
|
2012-07-30 07:20:58 -07:00
|
|
|
nsLayoutStylesheetCache::gStyleCache = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-02 13:00:53 -08:00
|
|
|
mozilla::css::Loader*
|
2012-07-30 07:20:58 -07:00
|
|
|
nsLayoutStylesheetCache::gCSSLoader = nullptr;
|