1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2001
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Stuart Parmenter <pavlov@netscape.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : /* class to notify frames of background image loads */
41 :
42 : #include "nsImageLoader.h"
43 :
44 : #include "imgILoader.h"
45 :
46 : #include "nsIURI.h"
47 : #include "nsILoadGroup.h"
48 : #include "nsNetUtil.h"
49 :
50 : #include "nsPresContext.h"
51 : #include "nsIPresShell.h"
52 : #include "nsIFrame.h"
53 : #include "nsIContent.h"
54 : #include "nsIDocument.h"
55 :
56 : #include "imgIContainer.h"
57 :
58 : #include "nsStyleContext.h"
59 : #include "nsGkAtoms.h"
60 : #include "nsLayoutUtils.h"
61 :
62 : // Paint forcing
63 : #include "prenv.h"
64 :
65 0 : NS_IMPL_ISUPPORTS2(nsImageLoader, imgIDecoderObserver, imgIContainerObserver)
66 :
67 0 : nsImageLoader::nsImageLoader(nsIFrame *aFrame, PRUint32 aActions,
68 : nsImageLoader *aNextLoader)
69 : : mFrame(aFrame),
70 : mActions(aActions),
71 : mNextLoader(aNextLoader),
72 0 : mRequestRegistered(false)
73 : {
74 0 : }
75 :
76 0 : nsImageLoader::~nsImageLoader()
77 : {
78 0 : Destroy();
79 0 : }
80 :
81 : /* static */ already_AddRefed<nsImageLoader>
82 0 : nsImageLoader::Create(nsIFrame *aFrame, imgIRequest *aRequest,
83 : PRUint32 aActions, nsImageLoader *aNextLoader)
84 : {
85 : nsRefPtr<nsImageLoader> loader =
86 0 : new nsImageLoader(aFrame, aActions, aNextLoader);
87 :
88 0 : loader->Load(aRequest);
89 :
90 0 : return loader.forget();
91 : }
92 :
93 : void
94 0 : nsImageLoader::Destroy()
95 : {
96 : // Destroy the chain with only one level of recursion.
97 0 : nsRefPtr<nsImageLoader> list = mNextLoader;
98 0 : mNextLoader = nsnull;
99 0 : while (list) {
100 0 : nsRefPtr<nsImageLoader> todestroy = list;
101 0 : list = todestroy->mNextLoader;
102 0 : todestroy->mNextLoader = nsnull;
103 0 : todestroy->Destroy();
104 : }
105 :
106 0 : if (mRequest && mFrame) {
107 : nsLayoutUtils::DeregisterImageRequest(mFrame->PresContext(), mRequest,
108 0 : &mRequestRegistered);
109 : }
110 :
111 0 : mFrame = nsnull;
112 :
113 0 : if (mRequest) {
114 0 : mRequest->CancelAndForgetObserver(NS_ERROR_FAILURE);
115 : }
116 :
117 0 : mRequest = nsnull;
118 0 : }
119 :
120 : nsresult
121 0 : nsImageLoader::Load(imgIRequest *aImage)
122 : {
123 0 : NS_ASSERTION(!mRequest, "can't reuse image loaders");
124 0 : NS_ASSERTION(mFrame, "not initialized");
125 0 : NS_ASSERTION(aImage, "must have non-null image");
126 :
127 0 : if (!mFrame)
128 0 : return NS_ERROR_NOT_INITIALIZED;
129 :
130 0 : if (!aImage)
131 0 : return NS_ERROR_FAILURE;
132 :
133 : // Deregister mRequest from the refresh driver, since it is no longer
134 : // going to be managed by this nsImageLoader.
135 0 : nsPresContext* presContext = mFrame->PresContext();
136 :
137 : nsLayoutUtils::DeregisterImageRequest(presContext, mRequest,
138 0 : &mRequestRegistered);
139 :
140 : // Make sure to clone into a temporary, then set mRequest, since
141 : // cloning may notify and we don't want to trigger paints from this
142 : // code.
143 0 : nsCOMPtr<imgIRequest> newRequest;
144 0 : nsresult rv = aImage->Clone(this, getter_AddRefs(newRequest));
145 0 : mRequest.swap(newRequest);
146 :
147 0 : if (mRequest) {
148 : nsLayoutUtils::RegisterImageRequestIfAnimated(presContext, mRequest,
149 0 : &mRequestRegistered);
150 : }
151 :
152 0 : return rv;
153 : }
154 :
155 0 : NS_IMETHODIMP nsImageLoader::OnStartContainer(imgIRequest *aRequest,
156 : imgIContainer *aImage)
157 : {
158 0 : NS_ABORT_IF_FALSE(aImage, "Who's calling us then?");
159 :
160 : /* Get requested animation policy from the pres context:
161 : * normal = 0
162 : * one frame = 1
163 : * one loop = 2
164 : */
165 0 : aImage->SetAnimationMode(mFrame->PresContext()->ImageAnimationMode());
166 :
167 0 : return NS_OK;
168 : }
169 :
170 0 : NS_IMETHODIMP nsImageLoader::OnStopFrame(imgIRequest *aRequest,
171 : PRUint32 aFrame)
172 : {
173 0 : if (!mFrame)
174 0 : return NS_ERROR_FAILURE;
175 :
176 0 : if (!mRequest) {
177 : // We're in the middle of a paint anyway
178 0 : return NS_OK;
179 : }
180 :
181 : // Take requested actions
182 0 : if (mActions & ACTION_REFLOW_ON_DECODE) {
183 0 : DoReflow();
184 : }
185 0 : if (mActions & ACTION_REDRAW_ON_DECODE) {
186 0 : DoRedraw(nsnull);
187 : }
188 0 : return NS_OK;
189 : }
190 :
191 0 : NS_IMETHODIMP nsImageLoader::OnImageIsAnimated(imgIRequest *aRequest)
192 : {
193 : // Register with the refresh driver now that we are aware that
194 : // we are animated.
195 : nsLayoutUtils::RegisterImageRequest(mFrame->PresContext(),
196 0 : aRequest, &mRequestRegistered);
197 0 : return NS_OK;
198 : }
199 :
200 0 : NS_IMETHODIMP nsImageLoader::OnStopRequest(imgIRequest *aRequest,
201 : bool aLastPart)
202 : {
203 0 : if (!mFrame)
204 0 : return NS_ERROR_FAILURE;
205 :
206 0 : if (!mRequest) {
207 : // We're in the middle of a paint anyway
208 0 : return NS_OK;
209 : }
210 :
211 : // Take requested actions
212 0 : if (mActions & ACTION_REFLOW_ON_LOAD) {
213 0 : DoReflow();
214 : }
215 0 : if (mActions & ACTION_REDRAW_ON_LOAD) {
216 0 : DoRedraw(nsnull);
217 : }
218 0 : return NS_OK;
219 : }
220 :
221 0 : NS_IMETHODIMP nsImageLoader::FrameChanged(imgIRequest *aRequest,
222 : imgIContainer *aContainer,
223 : const nsIntRect *aDirtyRect)
224 : {
225 0 : if (!mFrame)
226 0 : return NS_ERROR_FAILURE;
227 :
228 0 : if (!mRequest) {
229 : // We're in the middle of a paint anyway
230 0 : return NS_OK;
231 : }
232 :
233 0 : NS_ASSERTION(aRequest == mRequest, "This is a neat trick.");
234 :
235 0 : nsRect r = aDirtyRect->IsEqualInterior(nsIntRect::GetMaxSizedIntRect()) ?
236 0 : nsRect(nsPoint(0, 0), mFrame->GetSize()) :
237 0 : aDirtyRect->ToAppUnits(nsPresContext::AppUnitsPerCSSPixel());
238 :
239 0 : DoRedraw(&r);
240 :
241 0 : return NS_OK;
242 : }
243 :
244 : void
245 0 : nsImageLoader::DoReflow()
246 : {
247 0 : nsIPresShell *shell = mFrame->PresContext()->GetPresShell();
248 0 : shell->FrameNeedsReflow(mFrame, nsIPresShell::eStyleChange, NS_FRAME_IS_DIRTY);
249 0 : }
250 :
251 : void
252 0 : nsImageLoader::DoRedraw(const nsRect* aDamageRect)
253 : {
254 : // NOTE: It is not sufficient to invalidate only the size of the image:
255 : // the image may be tiled!
256 : // The best option is to call into the frame, however lacking this
257 : // we have to at least invalidate the frame's bounds, hence
258 : // as long as we have a frame we'll use its size.
259 : //
260 :
261 : // Invalidate the entire frame
262 : // XXX We really only need to invalidate the client area of the frame...
263 :
264 0 : nsRect bounds(nsPoint(0, 0), mFrame->GetSize());
265 :
266 0 : if (mFrame->GetType() == nsGkAtoms::canvasFrame) {
267 : // The canvas's background covers the whole viewport.
268 0 : bounds = mFrame->GetVisualOverflowRect();
269 : }
270 :
271 : // XXX this should be ok, but there is some crappy ass bug causing it not to work
272 : // XXX seems related to the "body fixup rule" dealing with the canvas and body frames...
273 : #if 0
274 : // Invalidate the entire frame only if the frame has a tiled background
275 : // image, otherwise just invalidate the intersection of the frame's bounds
276 : // with the damaged rect.
277 : nsStyleContext* styleContext;
278 : mFrame->GetStyleContext(&styleContext);
279 : const nsStyleBackground* bg = styleContext->GetStyleBackground();
280 :
281 : if ((bg->mBackgroundFlags & NS_STYLE_BG_IMAGE_NONE) ||
282 : (bg->mBackgroundRepeat == NS_STYLE_BG_REPEAT_OFF)) {
283 : // The frame does not have a background image so we are free
284 : // to invalidate only the intersection of the damage rect and
285 : // the frame's bounds.
286 :
287 : if (aDamageRect) {
288 : bounds.IntersectRect(*aDamageRect, bounds);
289 : }
290 : }
291 :
292 : #endif
293 :
294 0 : if (mFrame->GetStyleVisibility()->IsVisible()) {
295 0 : mFrame->Invalidate(bounds);
296 : }
297 0 : }
|