2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Gecko Layout
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Benjamin Smedberg <bsmedberg@covad.net>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2004
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsLayoutStylesheetCache.h"
|
|
|
|
|
|
|
|
#include "nsAppDirectoryServiceDefs.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 "nsLayoutCID.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
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsLayoutStylesheetCache, nsIObserver)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLayoutStylesheetCache::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const PRUnichar* aData)
|
|
|
|
{
|
|
|
|
if (!strcmp(aTopic, "profile-before-change")) {
|
|
|
|
mUserContentSheet = nsnull;
|
|
|
|
mUserChromeSheet = nsnull;
|
|
|
|
}
|
|
|
|
else if (!strcmp(aTopic, "profile-do-change")) {
|
|
|
|
InitFromProfile();
|
|
|
|
}
|
|
|
|
else if (strcmp(aTopic, "chrome-flush-skin-caches") == 0 ||
|
|
|
|
strcmp(aTopic, "chrome-flush-caches") == 0) {
|
|
|
|
mScrollbarsSheet = nsnull;
|
|
|
|
mFormsSheet = nsnull;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
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)
|
|
|
|
LoadSheet(sheetURI, gStyleCache->mScrollbarsSheet, PR_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)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
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)
|
|
|
|
LoadSheet(sheetURI, gStyleCache->mFormsSheet, PR_TRUE);
|
|
|
|
|
|
|
|
NS_ASSERTION(gStyleCache->mFormsSheet, "Could not load forms.css.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return gStyleCache->mFormsSheet;
|
|
|
|
}
|
|
|
|
|
2010-05-11 13:41:47 -07:00
|
|
|
nsCSSStyleSheet*
|
2007-03-22 10:30:00 -07:00
|
|
|
nsLayoutStylesheetCache::UserContentSheet()
|
|
|
|
{
|
|
|
|
EnsureGlobal();
|
|
|
|
if (!gStyleCache)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
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)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
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)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
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)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
return gStyleCache->mQuirkSheet;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsLayoutStylesheetCache::Shutdown()
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(gCSSLoader);
|
|
|
|
NS_IF_RELEASE(gStyleCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
obsSvc->AddObserver(this, "profile-before-change", PR_FALSE);
|
|
|
|
obsSvc->AddObserver(this, "profile-do-change", PR_FALSE);
|
|
|
|
obsSvc->AddObserver(this, "chrome-flush-skin-caches", PR_FALSE);
|
|
|
|
obsSvc->AddObserver(this, "chrome-flush-caches", PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
LoadSheet(uri, mUASheet, PR_TRUE);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
LoadSheet(uri, mQuirkSheet, PR_TRUE);
|
|
|
|
}
|
|
|
|
NS_ASSERTION(mQuirkSheet, "Could not load quirk.css");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsLayoutStylesheetCache::~nsLayoutStylesheetCache()
|
|
|
|
{
|
|
|
|
gCSSLoader = nsnull;
|
|
|
|
gStyleCache = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsLayoutStylesheetCache::EnsureGlobal()
|
|
|
|
{
|
|
|
|
if (gStyleCache) return;
|
|
|
|
|
|
|
|
gStyleCache = new nsLayoutStylesheetCache();
|
|
|
|
if (!gStyleCache) return;
|
|
|
|
|
|
|
|
NS_ADDREF(gStyleCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsLayoutStylesheetCache::InitFromProfile()
|
|
|
|
{
|
2009-06-22 04:34:41 -07:00
|
|
|
nsCOMPtr<nsIXULRuntime> appInfo = do_GetService("@mozilla.org/xre/app-info;1");
|
|
|
|
if (appInfo) {
|
|
|
|
PRBool inSafeMode = PR_FALSE;
|
|
|
|
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
|
|
|
{
|
|
|
|
PRBool exists = PR_FALSE;
|
|
|
|
aFile->Exists(&exists);
|
|
|
|
|
|
|
|
if (!exists) return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
NS_NewFileURI(getter_AddRefs(uri), aFile);
|
|
|
|
|
|
|
|
LoadSheet(uri, aSheet, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-03-02 13:00:53 -08:00
|
|
|
nsLayoutStylesheetCache::LoadSheet(nsIURI* aURI,
|
2010-05-11 13:41:47 -07:00
|
|
|
nsRefPtr<nsCSSStyleSheet> &aSheet,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRBool aEnableUnsafeRules)
|
|
|
|
{
|
|
|
|
if (!aURI) {
|
|
|
|
NS_ERROR("Null URI. Out of memory?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-02 13:00:53 -08:00
|
|
|
if (!gCSSLoader) {
|
|
|
|
gCSSLoader = new mozilla::css::Loader();
|
|
|
|
NS_IF_ADDREF(gCSSLoader);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (gCSSLoader) {
|
2009-03-16 18:38:36 -07:00
|
|
|
gCSSLoader->LoadSheetSync(aURI, aEnableUnsafeRules, PR_TRUE,
|
|
|
|
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*
|
|
|
|
nsLayoutStylesheetCache::gStyleCache = nsnull;
|
|
|
|
|
2010-03-02 13:00:53 -08:00
|
|
|
mozilla::css::Loader*
|
2007-03-22 10:30:00 -07:00
|
|
|
nsLayoutStylesheetCache::gCSSLoader = nsnull;
|