Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / layout / style / nsLayoutStylesheetCache.cpp
blobb5bb0935a058d5941d9e5521e77a969401290e21
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Gecko Layout
17 * The Initial Developer of the Original Code is
18 * Benjamin Smedberg <bsmedberg@covad.net>
19 * Portions created by the Initial Developer are Copyright (C) 2004
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsLayoutStylesheetCache.h"
40 #include "nsAppDirectoryServiceDefs.h"
41 #include "mozilla/css/Loader.h"
42 #include "nsIFile.h"
43 #include "nsLayoutCID.h"
44 #include "nsNetUtil.h"
45 #include "nsIObserverService.h"
46 #include "nsServiceManagerUtils.h"
47 #include "nsIXULRuntime.h"
48 #include "nsCSSStyleSheet.h"
50 NS_IMPL_ISUPPORTS1(nsLayoutStylesheetCache, nsIObserver)
52 nsresult
53 nsLayoutStylesheetCache::Observe(nsISupports* aSubject,
54 const char* aTopic,
55 const PRUnichar* aData)
57 if (!strcmp(aTopic, "profile-before-change")) {
58 mUserContentSheet = nsnull;
59 mUserChromeSheet = nsnull;
61 else if (!strcmp(aTopic, "profile-do-change")) {
62 InitFromProfile();
64 else if (strcmp(aTopic, "chrome-flush-skin-caches") == 0 ||
65 strcmp(aTopic, "chrome-flush-caches") == 0) {
66 mScrollbarsSheet = nsnull;
67 mFormsSheet = nsnull;
69 else {
70 NS_NOTREACHED("Unexpected observer topic.");
72 return NS_OK;
75 nsCSSStyleSheet*
76 nsLayoutStylesheetCache::ScrollbarsSheet()
78 EnsureGlobal();
79 if (!gStyleCache)
80 return nsnull;
82 if (!gStyleCache->mScrollbarsSheet) {
83 nsCOMPtr<nsIURI> sheetURI;
84 NS_NewURI(getter_AddRefs(sheetURI),
85 NS_LITERAL_CSTRING("chrome://global/skin/scrollbars.css"));
87 // Scrollbars don't need access to unsafe rules
88 if (sheetURI)
89 LoadSheet(sheetURI, gStyleCache->mScrollbarsSheet, PR_FALSE);
90 NS_ASSERTION(gStyleCache->mScrollbarsSheet, "Could not load scrollbars.css.");
93 return gStyleCache->mScrollbarsSheet;
96 nsCSSStyleSheet*
97 nsLayoutStylesheetCache::FormsSheet()
99 EnsureGlobal();
100 if (!gStyleCache)
101 return nsnull;
103 if (!gStyleCache->mFormsSheet) {
104 nsCOMPtr<nsIURI> sheetURI;
105 NS_NewURI(getter_AddRefs(sheetURI),
106 NS_LITERAL_CSTRING("resource://gre-resources/forms.css"));
108 // forms.css needs access to unsafe rules
109 if (sheetURI)
110 LoadSheet(sheetURI, gStyleCache->mFormsSheet, PR_TRUE);
112 NS_ASSERTION(gStyleCache->mFormsSheet, "Could not load forms.css.");
115 return gStyleCache->mFormsSheet;
118 nsCSSStyleSheet*
119 nsLayoutStylesheetCache::UserContentSheet()
121 EnsureGlobal();
122 if (!gStyleCache)
123 return nsnull;
125 return gStyleCache->mUserContentSheet;
128 nsCSSStyleSheet*
129 nsLayoutStylesheetCache::UserChromeSheet()
131 EnsureGlobal();
132 if (!gStyleCache)
133 return nsnull;
135 return gStyleCache->mUserChromeSheet;
138 nsCSSStyleSheet*
139 nsLayoutStylesheetCache::UASheet()
141 EnsureGlobal();
142 if (!gStyleCache)
143 return nsnull;
145 return gStyleCache->mUASheet;
148 nsCSSStyleSheet*
149 nsLayoutStylesheetCache::QuirkSheet()
151 EnsureGlobal();
152 if (!gStyleCache)
153 return nsnull;
155 return gStyleCache->mQuirkSheet;
158 void
159 nsLayoutStylesheetCache::Shutdown()
161 NS_IF_RELEASE(gCSSLoader);
162 NS_IF_RELEASE(gStyleCache);
165 nsLayoutStylesheetCache::nsLayoutStylesheetCache()
167 nsCOMPtr<nsIObserverService> obsSvc =
168 mozilla::services::GetObserverService();
169 NS_ASSERTION(obsSvc, "No global observer service?");
171 if (obsSvc) {
172 obsSvc->AddObserver(this, "profile-before-change", PR_FALSE);
173 obsSvc->AddObserver(this, "profile-do-change", PR_FALSE);
174 obsSvc->AddObserver(this, "chrome-flush-skin-caches", PR_FALSE);
175 obsSvc->AddObserver(this, "chrome-flush-caches", PR_FALSE);
178 InitFromProfile();
180 // And make sure that we load our UA sheets. No need to do this
181 // per-profile, since they're profile-invariant.
182 nsCOMPtr<nsIURI> uri;
183 NS_NewURI(getter_AddRefs(uri), "resource://gre-resources/ua.css");
184 if (uri) {
185 LoadSheet(uri, mUASheet, PR_TRUE);
187 NS_ASSERTION(mUASheet, "Could not load ua.css");
189 NS_NewURI(getter_AddRefs(uri), "resource://gre-resources/quirk.css");
190 if (uri) {
191 LoadSheet(uri, mQuirkSheet, PR_TRUE);
193 NS_ASSERTION(mQuirkSheet, "Could not load quirk.css");
196 nsLayoutStylesheetCache::~nsLayoutStylesheetCache()
198 gCSSLoader = nsnull;
199 gStyleCache = nsnull;
202 void
203 nsLayoutStylesheetCache::EnsureGlobal()
205 if (gStyleCache) return;
207 gStyleCache = new nsLayoutStylesheetCache();
208 if (!gStyleCache) return;
210 NS_ADDREF(gStyleCache);
213 void
214 nsLayoutStylesheetCache::InitFromProfile()
216 nsCOMPtr<nsIXULRuntime> appInfo = do_GetService("@mozilla.org/xre/app-info;1");
217 if (appInfo) {
218 PRBool inSafeMode = PR_FALSE;
219 appInfo->GetInSafeMode(&inSafeMode);
220 if (inSafeMode)
221 return;
223 nsCOMPtr<nsIFile> contentFile;
224 nsCOMPtr<nsIFile> chromeFile;
226 NS_GetSpecialDirectory(NS_APP_USER_CHROME_DIR,
227 getter_AddRefs(contentFile));
228 if (!contentFile) {
229 // if we don't have a profile yet, that's OK!
230 return;
233 contentFile->Clone(getter_AddRefs(chromeFile));
234 if (!chromeFile) return;
236 contentFile->Append(NS_LITERAL_STRING("userContent.css"));
237 chromeFile->Append(NS_LITERAL_STRING("userChrome.css"));
239 LoadSheetFile(contentFile, mUserContentSheet);
240 LoadSheetFile(chromeFile, mUserChromeSheet);
243 void
244 nsLayoutStylesheetCache::LoadSheetFile(nsIFile* aFile, nsRefPtr<nsCSSStyleSheet> &aSheet)
246 PRBool exists = PR_FALSE;
247 aFile->Exists(&exists);
249 if (!exists) return;
251 nsCOMPtr<nsIURI> uri;
252 NS_NewFileURI(getter_AddRefs(uri), aFile);
254 LoadSheet(uri, aSheet, PR_FALSE);
257 void
258 nsLayoutStylesheetCache::LoadSheet(nsIURI* aURI,
259 nsRefPtr<nsCSSStyleSheet> &aSheet,
260 PRBool aEnableUnsafeRules)
262 if (!aURI) {
263 NS_ERROR("Null URI. Out of memory?");
264 return;
267 if (!gCSSLoader) {
268 gCSSLoader = new mozilla::css::Loader();
269 NS_IF_ADDREF(gCSSLoader);
272 if (gCSSLoader) {
273 gCSSLoader->LoadSheetSync(aURI, aEnableUnsafeRules, PR_TRUE,
274 getter_AddRefs(aSheet));
278 nsLayoutStylesheetCache*
279 nsLayoutStylesheetCache::gStyleCache = nsnull;
281 mozilla::css::Loader*
282 nsLayoutStylesheetCache::gCSSLoader = nsnull;