Back to index

lightning-sunbird  0.9+nobinonly
nsLayoutStylesheetCache.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Gecko Layout
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Benjamin Smedberg <bsmedberg@covad.net>
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsLayoutStylesheetCache.h"
00039 
00040 #include "nsAppDirectoryServiceDefs.h"
00041 #include "nsICSSLoader.h"
00042 #include "nsIFile.h"
00043 #include "nsLayoutCID.h"
00044 #include "nsNetUtil.h"
00045 #include "nsIObserverService.h"
00046 #include "nsServiceManagerUtils.h"
00047 
00048 NS_IMPL_ISUPPORTS1(nsLayoutStylesheetCache, nsIObserver)
00049 
00050 nsresult
00051 nsLayoutStylesheetCache::Observe(nsISupports* aSubject,
00052                             const char* aTopic,
00053                             const PRUnichar* aData)
00054 {
00055   if (!strcmp(aTopic, "profile-before-change")) {
00056     mUserContentSheet = nsnull;
00057     mUserChromeSheet  = nsnull;
00058   }
00059   else if (!strcmp(aTopic, "profile-do-change")) {
00060     InitFromProfile();
00061   }
00062   else if (strcmp(aTopic, "chrome-flush-skin-caches") == 0 ||
00063            strcmp(aTopic, "chrome-flush-caches") == 0) {
00064     mScrollbarsSheet = nsnull;
00065     mFormsSheet = nsnull;
00066   }
00067   else {
00068     NS_NOTREACHED("Unexpected observer topic.");
00069   }
00070   return NS_OK;
00071 }
00072 
00073 nsICSSStyleSheet*
00074 nsLayoutStylesheetCache::ScrollbarsSheet()
00075 {
00076   EnsureGlobal();
00077   if (!gStyleCache)
00078     return nsnull;
00079 
00080   if (!gStyleCache->mScrollbarsSheet) {
00081     nsCOMPtr<nsIURI> sheetURI;
00082     NS_NewURI(getter_AddRefs(sheetURI),
00083 #ifdef XP_MACOSX
00084               NS_LITERAL_CSTRING("chrome://global/skin/nativescrollbars.css"));
00085 #else
00086               NS_LITERAL_CSTRING("chrome://global/skin/xulscrollbars.css"));
00087 #endif
00088 
00089     // Scrollbars don't need access to unsafe rules
00090     if (sheetURI)
00091       LoadSheet(sheetURI, gStyleCache->mScrollbarsSheet, PR_FALSE);
00092 #ifdef XP_MACOSX
00093     NS_ASSERTION(gStyleCache->mScrollbarsSheet, "Could not load nativescrollbars.css.");
00094 #else
00095     NS_ASSERTION(gStyleCache->mScrollbarsSheet, "Could not load xulscrollbars.css.");
00096 #endif
00097   }
00098 
00099   return gStyleCache->mScrollbarsSheet;
00100 }
00101 
00102 nsICSSStyleSheet*
00103 nsLayoutStylesheetCache::FormsSheet()
00104 {
00105   EnsureGlobal();
00106   if (!gStyleCache)
00107     return nsnull;
00108 
00109   if (!gStyleCache->mFormsSheet) {
00110     nsCOMPtr<nsIURI> sheetURI;
00111       NS_NewURI(getter_AddRefs(sheetURI),
00112                 NS_LITERAL_CSTRING("resource://gre/res/forms.css"));
00113 
00114     // forms.css needs access to unsafe rules
00115     if (sheetURI)
00116       LoadSheet(sheetURI, gStyleCache->mFormsSheet, PR_TRUE);
00117 
00118     NS_ASSERTION(gStyleCache->mFormsSheet, "Could not load forms.css.");
00119   }
00120 
00121   return gStyleCache->mFormsSheet;
00122 }
00123 
00124 nsICSSStyleSheet*
00125 nsLayoutStylesheetCache::UserContentSheet()
00126 {
00127   EnsureGlobal();
00128   if (!gStyleCache)
00129     return nsnull;
00130 
00131   return gStyleCache->mUserContentSheet;
00132 }
00133 
00134 nsICSSStyleSheet*
00135 nsLayoutStylesheetCache::UserChromeSheet()
00136 {
00137   EnsureGlobal();
00138   if (!gStyleCache)
00139     return nsnull;
00140 
00141   return gStyleCache->mUserChromeSheet;
00142 }
00143 
00144 void
00145 nsLayoutStylesheetCache::Shutdown()
00146 {
00147   NS_IF_RELEASE(gStyleCache);
00148 }
00149 
00150 nsLayoutStylesheetCache::nsLayoutStylesheetCache()
00151 {
00152   nsCOMPtr<nsIObserverService> obsSvc =
00153     do_GetService("@mozilla.org/observer-service;1");
00154   NS_ASSERTION(obsSvc, "No global observer service?");
00155 
00156   if (obsSvc) {
00157     obsSvc->AddObserver(this, "profile-before-change", PR_FALSE);
00158     obsSvc->AddObserver(this, "profile-do-change", PR_FALSE);
00159     obsSvc->AddObserver(this, "chrome-flush-skin-caches", PR_FALSE);
00160     obsSvc->AddObserver(this, "chrome-flush-caches", PR_FALSE);
00161   }
00162 
00163   InitFromProfile();
00164 }
00165 
00166 nsLayoutStylesheetCache::~nsLayoutStylesheetCache()
00167 {
00168   gStyleCache = nsnull;
00169 }
00170 
00171 void
00172 nsLayoutStylesheetCache::EnsureGlobal()
00173 {
00174   if (gStyleCache) return;
00175 
00176   gStyleCache = new nsLayoutStylesheetCache();
00177   if (!gStyleCache) return;
00178 
00179   NS_ADDREF(gStyleCache);
00180 }
00181 
00182 void
00183 nsLayoutStylesheetCache::InitFromProfile()
00184 {
00185   nsCOMPtr<nsIFile> contentFile;
00186   nsCOMPtr<nsIFile> chromeFile;
00187 
00188   NS_GetSpecialDirectory(NS_APP_USER_CHROME_DIR,
00189                          getter_AddRefs(contentFile));
00190   if (!contentFile) {
00191     // if we don't have a profile yet, that's OK!
00192     return;
00193   }
00194 
00195   contentFile->Clone(getter_AddRefs(chromeFile));
00196   if (!chromeFile) return;
00197 
00198   contentFile->Append(NS_LITERAL_STRING("userContent.css"));
00199   chromeFile->Append(NS_LITERAL_STRING("userChrome.css"));
00200 
00201   LoadSheetFile(contentFile, mUserContentSheet);
00202   LoadSheetFile(chromeFile, mUserChromeSheet);
00203 }
00204 
00205 void
00206 nsLayoutStylesheetCache::LoadSheetFile(nsIFile* aFile, nsCOMPtr<nsICSSStyleSheet> &aSheet)
00207 {
00208   PRBool exists = PR_FALSE;
00209   aFile->Exists(&exists);
00210 
00211   if (!exists) return;
00212 
00213   nsCOMPtr<nsIURI> uri;
00214   NS_NewFileURI(getter_AddRefs(uri), aFile);
00215 
00216   LoadSheet(uri, aSheet, PR_FALSE);
00217 }
00218 
00219 static NS_DEFINE_CID(kCSSLoaderCID, NS_CSS_LOADER_CID);
00220 
00221 void
00222 nsLayoutStylesheetCache::LoadSheet(nsIURI* aURI, nsCOMPtr<nsICSSStyleSheet> &aSheet,
00223                                    PRBool aEnableUnsafeRules)
00224 {
00225   if (!aURI) {
00226     NS_ERROR("Null URI. Out of memory?");
00227     return;
00228   }
00229 
00230   // note: CSS Loader is treated as a service here... slightly unusual,
00231   // but within the rules.
00232   nsCOMPtr<nsICSSLoader_MOZILLA_1_8_BRANCH> cssLoader = do_GetService(kCSSLoaderCID);
00233   if (!cssLoader) return;
00234 
00235   cssLoader->LoadSheetSync(aURI, aEnableUnsafeRules, getter_AddRefs(aSheet));
00236 }  
00237 
00238 nsLayoutStylesheetCache*
00239 nsLayoutStylesheetCache::gStyleCache = nsnull;