1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsIStyleRuleProcessor.h"
7 #include "nsIDocument.h"
8 #include "nsIContent.h"
9 #include "nsIServiceManager.h"
10 #include "nsXBLResourceLoader.h"
11 #include "nsXBLPrototypeResources.h"
12 #include "nsXBLPrototypeBinding.h"
13 #include "nsIDocumentObserver.h"
14 #include "mozilla/css/Loader.h"
16 #include "nsLayoutCID.h"
17 #include "nsCSSRuleProcessor.h"
18 #include "nsStyleSet.h"
19 #include "mozilla/dom/URL.h"
20 #include "mozilla/DebugOnly.h"
22 using namespace mozilla
;
23 using mozilla::dom::IsChromeURI
;
25 nsXBLPrototypeResources::nsXBLPrototypeResources(nsXBLPrototypeBinding
* aBinding
)
27 MOZ_COUNT_CTOR(nsXBLPrototypeResources
);
29 mLoader
= new nsXBLResourceLoader(aBinding
, this);
32 nsXBLPrototypeResources::~nsXBLPrototypeResources()
34 MOZ_COUNT_DTOR(nsXBLPrototypeResources
);
36 mLoader
->mResources
= nullptr;
41 nsXBLPrototypeResources::AddResource(nsIAtom
* aResourceType
, const nsAString
& aSrc
)
44 mLoader
->AddResource(aResourceType
, aSrc
);
48 nsXBLPrototypeResources::LoadResources(bool* aResult
)
51 mLoader
->LoadResources(aResult
);
53 *aResult
= true; // All resources loaded.
57 nsXBLPrototypeResources::AddResourceListener(nsIContent
* aBoundElement
)
60 mLoader
->AddResourceListener(aBoundElement
);
64 nsXBLPrototypeResources::FlushSkinSheets()
66 if (mStyleSheetList
.Length() == 0)
69 nsCOMPtr
<nsIDocument
> doc
=
70 mLoader
->mBinding
->XBLDocumentInfo()->GetDocument();
72 // If doc is null, we're in the process of tearing things down, so just
73 // return without rebuilding anything.
78 // We have scoped stylesheets. Reload any chrome stylesheets we
79 // encounter. (If they aren't skin sheets, it doesn't matter, since
80 // they'll still be in the chrome cache.
81 mRuleProcessor
= nullptr;
83 nsTArray
<nsRefPtr
<CSSStyleSheet
>> oldSheets
;
85 oldSheets
.SwapElements(mStyleSheetList
);
87 mozilla::css::Loader
* cssLoader
= doc
->CSSLoader();
89 for (size_t i
= 0, count
= oldSheets
.Length(); i
< count
; ++i
) {
90 CSSStyleSheet
* oldSheet
= oldSheets
[i
];
92 nsIURI
* uri
= oldSheet
->GetSheetURI();
94 nsRefPtr
<CSSStyleSheet
> newSheet
;
95 if (IsChromeURI(uri
)) {
96 if (NS_FAILED(cssLoader
->LoadSheetSync(uri
, getter_AddRefs(newSheet
))))
103 mStyleSheetList
.AppendElement(newSheet
);
106 GatherRuleProcessor();
112 nsXBLPrototypeResources::Write(nsIObjectOutputStream
* aStream
)
115 return mLoader
->Write(aStream
);
120 nsXBLPrototypeResources::Traverse(nsCycleCollectionTraversalCallback
&cb
)
122 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb
, "proto mResources mLoader");
123 cb
.NoteXPCOMChild(mLoader
);
125 CycleCollectionNoteChild(cb
, mRuleProcessor
.get(), "mRuleProcessor");
126 ImplCycleCollectionTraverse(cb
, mStyleSheetList
, "mStyleSheetList");
130 nsXBLPrototypeResources::Unlink()
132 mStyleSheetList
.Clear();
133 mRuleProcessor
= nullptr;
137 nsXBLPrototypeResources::ClearLoader()
143 nsXBLPrototypeResources::GatherRuleProcessor()
145 mRuleProcessor
= new nsCSSRuleProcessor(mStyleSheetList
,
146 nsStyleSet::eDocSheet
,
151 nsXBLPrototypeResources::AppendStyleSheet(CSSStyleSheet
* aSheet
)
153 mStyleSheetList
.AppendElement(aSheet
);
157 nsXBLPrototypeResources::RemoveStyleSheet(CSSStyleSheet
* aSheet
)
159 mStyleSheetList
.RemoveElement(aSheet
);
163 nsXBLPrototypeResources::InsertStyleSheetAt(size_t aIndex
, CSSStyleSheet
* aSheet
)
165 mStyleSheetList
.InsertElementAt(aIndex
, aSheet
);
169 nsXBLPrototypeResources::StyleSheetAt(size_t aIndex
) const
171 return mStyleSheetList
[aIndex
];
175 nsXBLPrototypeResources::SheetCount() const
177 return mStyleSheetList
.Length();
181 nsXBLPrototypeResources::HasStyleSheets() const
183 return !mStyleSheetList
.IsEmpty();
187 nsXBLPrototypeResources::AppendStyleSheetsTo(
188 nsTArray
<CSSStyleSheet
*>& aResult
) const
190 aResult
.AppendElements(mStyleSheetList
);