Bug 451040 ? Passwords Manager Empty after convert to MozStorage. r=gavin
[wine-gecko.git] / layout / style / nsLayoutStylesheetCache.cpp
blob814c1b1c903bfaa3c189af5dd589c5659d2aa30c
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 "nsICSSLoader.h"
42 #include "nsIFile.h"
43 #include "nsLayoutCID.h"
44 #include "nsNetUtil.h"
45 #include "nsIObserverService.h"
46 #include "nsServiceManagerUtils.h"
48 NS_IMPL_ISUPPORTS1(nsLayoutStylesheetCache, nsIObserver)
50 nsresult
51 nsLayoutStylesheetCache::Observe(nsISupports* aSubject,
52 const char* aTopic,
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")) {
60 InitFromProfile();
62 else if (strcmp(aTopic, "chrome-flush-skin-caches") == 0 ||
63 strcmp(aTopic, "chrome-flush-caches") == 0) {
64 mScrollbarsSheet = nsnull;
65 mFormsSheet = nsnull;
67 else {
68 NS_NOTREACHED("Unexpected observer topic.");
70 return NS_OK;
73 nsICSSStyleSheet*
74 nsLayoutStylesheetCache::ScrollbarsSheet()
76 EnsureGlobal();
77 if (!gStyleCache)
78 return nsnull;
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
86 if (sheetURI)
87 LoadSheet(sheetURI, gStyleCache->mScrollbarsSheet, PR_FALSE);
88 NS_ASSERTION(gStyleCache->mScrollbarsSheet, "Could not load scrollbars.css.");
91 return gStyleCache->mScrollbarsSheet;
94 nsICSSStyleSheet*
95 nsLayoutStylesheetCache::FormsSheet()
97 EnsureGlobal();
98 if (!gStyleCache)
99 return nsnull;
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
107 if (sheetURI)
108 LoadSheet(sheetURI, gStyleCache->mFormsSheet, PR_TRUE);
110 NS_ASSERTION(gStyleCache->mFormsSheet, "Could not load forms.css.");
113 return gStyleCache->mFormsSheet;
116 nsICSSStyleSheet*
117 nsLayoutStylesheetCache::UserContentSheet()
119 EnsureGlobal();
120 if (!gStyleCache)
121 return nsnull;
123 return gStyleCache->mUserContentSheet;
126 nsICSSStyleSheet*
127 nsLayoutStylesheetCache::UserChromeSheet()
129 EnsureGlobal();
130 if (!gStyleCache)
131 return nsnull;
133 return gStyleCache->mUserChromeSheet;
136 void
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?");
149 if (obsSvc) {
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);
156 InitFromProfile();
159 nsLayoutStylesheetCache::~nsLayoutStylesheetCache()
161 gCSSLoader = nsnull;
162 gStyleCache = nsnull;
165 void
166 nsLayoutStylesheetCache::EnsureGlobal()
168 if (gStyleCache) return;
170 gStyleCache = new nsLayoutStylesheetCache();
171 if (!gStyleCache) return;
173 NS_ADDREF(gStyleCache);
176 void
177 nsLayoutStylesheetCache::InitFromProfile()
179 nsCOMPtr<nsIFile> contentFile;
180 nsCOMPtr<nsIFile> chromeFile;
182 NS_GetSpecialDirectory(NS_APP_USER_CHROME_DIR,
183 getter_AddRefs(contentFile));
184 if (!contentFile) {
185 // if we don't have a profile yet, that's OK!
186 return;
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);
199 void
200 nsLayoutStylesheetCache::LoadSheetFile(nsIFile* aFile, nsCOMPtr<nsICSSStyleSheet> &aSheet)
202 PRBool exists = PR_FALSE;
203 aFile->Exists(&exists);
205 if (!exists) return;
207 nsCOMPtr<nsIURI> uri;
208 NS_NewFileURI(getter_AddRefs(uri), aFile);
210 LoadSheet(uri, aSheet, PR_FALSE);
213 void
214 nsLayoutStylesheetCache::LoadSheet(nsIURI* aURI, nsCOMPtr<nsICSSStyleSheet> &aSheet,
215 PRBool aEnableUnsafeRules)
217 if (!aURI) {
218 NS_ERROR("Null URI. Out of memory?");
219 return;
222 if (!gCSSLoader)
223 NS_NewCSSLoader(&gCSSLoader);
225 if (gCSSLoader) {
226 gCSSLoader->LoadSheetSync(aURI, aEnableUnsafeRules, getter_AddRefs(aSheet));
230 nsLayoutStylesheetCache*
231 nsLayoutStylesheetCache::gStyleCache = nsnull;
233 nsICSSLoader*
234 nsLayoutStylesheetCache::gCSSLoader = nsnull;