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 "nsICSSLoader.h"
43 #include "nsLayoutCID.h"
44 #include "nsNetUtil.h"
45 #include "nsIObserverService.h"
46 #include "nsServiceManagerUtils.h"
48 NS_IMPL_ISUPPORTS1(nsLayoutStylesheetCache
, nsIObserver
)
51 nsLayoutStylesheetCache::Observe(nsISupports
* aSubject
,
53 const PRUnichar
* aData
)
55 if (!strcmp(aTopic
, "profile-before-change")) {
56 mUserContentSheet
= nsnull
;
57 mUserChromeSheet
= nsnull
;
59 else if (!strcmp(aTopic
, "profile-do-change")) {
62 else if (strcmp(aTopic
, "chrome-flush-skin-caches") == 0 ||
63 strcmp(aTopic
, "chrome-flush-caches") == 0) {
64 mScrollbarsSheet
= nsnull
;
68 NS_NOTREACHED("Unexpected observer topic.");
74 nsLayoutStylesheetCache::ScrollbarsSheet()
80 if (!gStyleCache
->mScrollbarsSheet
) {
81 nsCOMPtr
<nsIURI
> sheetURI
;
82 NS_NewURI(getter_AddRefs(sheetURI
),
83 NS_LITERAL_CSTRING("chrome://global/skin/scrollbars.css"));
85 // Scrollbars don't need access to unsafe rules
87 LoadSheet(sheetURI
, gStyleCache
->mScrollbarsSheet
, PR_FALSE
);
88 NS_ASSERTION(gStyleCache
->mScrollbarsSheet
, "Could not load scrollbars.css.");
91 return gStyleCache
->mScrollbarsSheet
;
95 nsLayoutStylesheetCache::FormsSheet()
101 if (!gStyleCache
->mFormsSheet
) {
102 nsCOMPtr
<nsIURI
> sheetURI
;
103 NS_NewURI(getter_AddRefs(sheetURI
),
104 NS_LITERAL_CSTRING("resource://gre/res/forms.css"));
106 // forms.css needs access to unsafe rules
108 LoadSheet(sheetURI
, gStyleCache
->mFormsSheet
, PR_TRUE
);
110 NS_ASSERTION(gStyleCache
->mFormsSheet
, "Could not load forms.css.");
113 return gStyleCache
->mFormsSheet
;
117 nsLayoutStylesheetCache::UserContentSheet()
123 return gStyleCache
->mUserContentSheet
;
127 nsLayoutStylesheetCache::UserChromeSheet()
133 return gStyleCache
->mUserChromeSheet
;
137 nsLayoutStylesheetCache::Shutdown()
139 NS_IF_RELEASE(gCSSLoader
);
140 NS_IF_RELEASE(gStyleCache
);
143 nsLayoutStylesheetCache::nsLayoutStylesheetCache()
145 nsCOMPtr
<nsIObserverService
> obsSvc
=
146 do_GetService("@mozilla.org/observer-service;1");
147 NS_ASSERTION(obsSvc
, "No global observer service?");
150 obsSvc
->AddObserver(this, "profile-before-change", PR_FALSE
);
151 obsSvc
->AddObserver(this, "profile-do-change", PR_FALSE
);
152 obsSvc
->AddObserver(this, "chrome-flush-skin-caches", PR_FALSE
);
153 obsSvc
->AddObserver(this, "chrome-flush-caches", PR_FALSE
);
159 nsLayoutStylesheetCache::~nsLayoutStylesheetCache()
162 gStyleCache
= nsnull
;
166 nsLayoutStylesheetCache::EnsureGlobal()
168 if (gStyleCache
) return;
170 gStyleCache
= new nsLayoutStylesheetCache();
171 if (!gStyleCache
) return;
173 NS_ADDREF(gStyleCache
);
177 nsLayoutStylesheetCache::InitFromProfile()
179 nsCOMPtr
<nsIFile
> contentFile
;
180 nsCOMPtr
<nsIFile
> chromeFile
;
182 NS_GetSpecialDirectory(NS_APP_USER_CHROME_DIR
,
183 getter_AddRefs(contentFile
));
185 // if we don't have a profile yet, that's OK!
189 contentFile
->Clone(getter_AddRefs(chromeFile
));
190 if (!chromeFile
) return;
192 contentFile
->Append(NS_LITERAL_STRING("userContent.css"));
193 chromeFile
->Append(NS_LITERAL_STRING("userChrome.css"));
195 LoadSheetFile(contentFile
, mUserContentSheet
);
196 LoadSheetFile(chromeFile
, mUserChromeSheet
);
200 nsLayoutStylesheetCache::LoadSheetFile(nsIFile
* aFile
, nsCOMPtr
<nsICSSStyleSheet
> &aSheet
)
202 PRBool exists
= PR_FALSE
;
203 aFile
->Exists(&exists
);
207 nsCOMPtr
<nsIURI
> uri
;
208 NS_NewFileURI(getter_AddRefs(uri
), aFile
);
210 LoadSheet(uri
, aSheet
, PR_FALSE
);
214 nsLayoutStylesheetCache::LoadSheet(nsIURI
* aURI
, nsCOMPtr
<nsICSSStyleSheet
> &aSheet
,
215 PRBool aEnableUnsafeRules
)
218 NS_ERROR("Null URI. Out of memory?");
223 NS_NewCSSLoader(&gCSSLoader
);
226 gCSSLoader
->LoadSheetSync(aURI
, aEnableUnsafeRules
, getter_AddRefs(aSheet
));
230 nsLayoutStylesheetCache
*
231 nsLayoutStylesheetCache::gStyleCache
= nsnull
;
234 nsLayoutStylesheetCache::gCSSLoader
= nsnull
;