1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Original Author: David W. Hyatt (hyatt@netscape.com)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsIStyleRuleProcessor.h"
40 : #include "nsIDocument.h"
41 : #include "nsIContent.h"
42 : #include "nsIXBLService.h"
43 : #include "nsIServiceManager.h"
44 : #include "nsXBLResourceLoader.h"
45 : #include "nsXBLPrototypeResources.h"
46 : #include "nsXBLPrototypeBinding.h"
47 : #include "nsIDocumentObserver.h"
48 : #include "mozilla/css/Loader.h"
49 : #include "nsIURI.h"
50 : #include "nsLayoutCID.h"
51 : #include "nsCSSRuleProcessor.h"
52 : #include "nsStyleSet.h"
53 :
54 0 : nsXBLPrototypeResources::nsXBLPrototypeResources(nsXBLPrototypeBinding* aBinding)
55 : {
56 0 : MOZ_COUNT_CTOR(nsXBLPrototypeResources);
57 :
58 0 : mLoader = new nsXBLResourceLoader(aBinding, this);
59 0 : NS_IF_ADDREF(mLoader);
60 0 : }
61 :
62 0 : nsXBLPrototypeResources::~nsXBLPrototypeResources()
63 : {
64 0 : MOZ_COUNT_DTOR(nsXBLPrototypeResources);
65 0 : if (mLoader) {
66 0 : mLoader->mResources = nsnull;
67 0 : NS_RELEASE(mLoader);
68 : }
69 0 : }
70 :
71 : void
72 0 : nsXBLPrototypeResources::AddResource(nsIAtom* aResourceType, const nsAString& aSrc)
73 : {
74 0 : if (mLoader)
75 0 : mLoader->AddResource(aResourceType, aSrc);
76 0 : }
77 :
78 : void
79 0 : nsXBLPrototypeResources::LoadResources(bool* aResult)
80 : {
81 0 : if (mLoader)
82 0 : mLoader->LoadResources(aResult);
83 : else
84 0 : *aResult = true; // All resources loaded.
85 0 : }
86 :
87 : void
88 0 : nsXBLPrototypeResources::AddResourceListener(nsIContent* aBoundElement)
89 : {
90 0 : if (mLoader)
91 0 : mLoader->AddResourceListener(aBoundElement);
92 0 : }
93 :
94 0 : static bool IsChromeURI(nsIURI* aURI)
95 : {
96 0 : bool isChrome=false;
97 0 : if (NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)) && isChrome)
98 0 : return true;
99 0 : return false;
100 : }
101 :
102 : nsresult
103 0 : nsXBLPrototypeResources::FlushSkinSheets()
104 : {
105 0 : if (mStyleSheetList.Length() == 0)
106 0 : return NS_OK;
107 :
108 : nsCOMPtr<nsIDocument> doc =
109 0 : mLoader->mBinding->XBLDocumentInfo()->GetDocument();
110 0 : mozilla::css::Loader* cssLoader = doc->CSSLoader();
111 :
112 : // We have scoped stylesheets. Reload any chrome stylesheets we
113 : // encounter. (If they aren't skin sheets, it doesn't matter, since
114 : // they'll still be in the chrome cache.
115 0 : mRuleProcessor = nsnull;
116 :
117 0 : sheet_array_type oldSheets(mStyleSheetList);
118 0 : mStyleSheetList.Clear();
119 :
120 0 : for (sheet_array_type::size_type i = 0, count = oldSheets.Length();
121 : i < count; ++i) {
122 0 : nsCSSStyleSheet* oldSheet = oldSheets[i];
123 :
124 0 : nsIURI* uri = oldSheet->GetSheetURI();
125 :
126 0 : nsRefPtr<nsCSSStyleSheet> newSheet;
127 0 : if (IsChromeURI(uri)) {
128 0 : if (NS_FAILED(cssLoader->LoadSheetSync(uri, getter_AddRefs(newSheet))))
129 0 : continue;
130 : }
131 : else {
132 0 : newSheet = oldSheet;
133 : }
134 :
135 0 : mStyleSheetList.AppendElement(newSheet);
136 : }
137 : mRuleProcessor = new nsCSSRuleProcessor(mStyleSheetList,
138 0 : nsStyleSet::eDocSheet);
139 :
140 0 : return NS_OK;
141 : }
142 :
143 : nsresult
144 0 : nsXBLPrototypeResources::Write(nsIObjectOutputStream* aStream)
145 : {
146 0 : if (mLoader)
147 0 : return mLoader->Write(aStream);
148 0 : return NS_OK;
149 : }
|