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
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.
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"
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
)
53 nsLayoutStylesheetCache::Observe(nsISupports
* aSubject
,
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")) {
64 else if (strcmp(aTopic
, "chrome-flush-skin-caches") == 0 ||
65 strcmp(aTopic
, "chrome-flush-caches") == 0) {
66 mScrollbarsSheet
= nsnull
;
70 NS_NOTREACHED("Unexpected observer topic.");
76 nsLayoutStylesheetCache::ScrollbarsSheet()
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
89 LoadSheet(sheetURI
, gStyleCache
->mScrollbarsSheet
, PR_FALSE
);
90 NS_ASSERTION(gStyleCache
->mScrollbarsSheet
, "Could not load scrollbars.css.");
93 return gStyleCache
->mScrollbarsSheet
;
97 nsLayoutStylesheetCache::FormsSheet()
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
110 LoadSheet(sheetURI
, gStyleCache
->mFormsSheet
, PR_TRUE
);
112 NS_ASSERTION(gStyleCache
->mFormsSheet
, "Could not load forms.css.");
115 return gStyleCache
->mFormsSheet
;
119 nsLayoutStylesheetCache::UserContentSheet()
125 return gStyleCache
->mUserContentSheet
;
129 nsLayoutStylesheetCache::UserChromeSheet()
135 return gStyleCache
->mUserChromeSheet
;
139 nsLayoutStylesheetCache::UASheet()
145 return gStyleCache
->mUASheet
;
149 nsLayoutStylesheetCache::QuirkSheet()
155 return gStyleCache
->mQuirkSheet
;
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?");
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
);
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");
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");
191 LoadSheet(uri
, mQuirkSheet
, PR_TRUE
);
193 NS_ASSERTION(mQuirkSheet
, "Could not load quirk.css");
196 nsLayoutStylesheetCache::~nsLayoutStylesheetCache()
199 gStyleCache
= nsnull
;
203 nsLayoutStylesheetCache::EnsureGlobal()
205 if (gStyleCache
) return;
207 gStyleCache
= new nsLayoutStylesheetCache();
208 if (!gStyleCache
) return;
210 NS_ADDREF(gStyleCache
);
214 nsLayoutStylesheetCache::InitFromProfile()
216 nsCOMPtr
<nsIXULRuntime
> appInfo
= do_GetService("@mozilla.org/xre/app-info;1");
218 PRBool inSafeMode
= PR_FALSE
;
219 appInfo
->GetInSafeMode(&inSafeMode
);
223 nsCOMPtr
<nsIFile
> contentFile
;
224 nsCOMPtr
<nsIFile
> chromeFile
;
226 NS_GetSpecialDirectory(NS_APP_USER_CHROME_DIR
,
227 getter_AddRefs(contentFile
));
229 // if we don't have a profile yet, that's OK!
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
);
244 nsLayoutStylesheetCache::LoadSheetFile(nsIFile
* aFile
, nsRefPtr
<nsCSSStyleSheet
> &aSheet
)
246 PRBool exists
= PR_FALSE
;
247 aFile
->Exists(&exists
);
251 nsCOMPtr
<nsIURI
> uri
;
252 NS_NewFileURI(getter_AddRefs(uri
), aFile
);
254 LoadSheet(uri
, aSheet
, PR_FALSE
);
258 nsLayoutStylesheetCache::LoadSheet(nsIURI
* aURI
,
259 nsRefPtr
<nsCSSStyleSheet
> &aSheet
,
260 PRBool aEnableUnsafeRules
)
263 NS_ERROR("Null URI. Out of memory?");
268 gCSSLoader
= new mozilla::css::Loader();
269 NS_IF_ADDREF(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
;