1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ImageFactory.h"
11 #include "mozilla/Likely.h"
13 #include "nsIHttpChannel.h"
14 #include "nsIFileChannel.h"
16 #include "nsMimeTypes.h"
17 #include "nsIRequest.h"
19 #include "MultipartImage.h"
20 #include "RasterImage.h"
21 #include "VectorImage.h"
23 #include "nsMediaFragmentURIParser.h"
24 #include "nsContentUtils.h"
25 #include "nsIScriptSecurityManager.h"
33 void ImageFactory::Initialize() {}
35 static uint32_t ComputeImageFlags(nsIURI
* uri
, const nsCString
& aMimeType
,
39 // We default to the static globals.
40 bool isDiscardable
= gfxPrefs::ImageMemDiscardable();
41 bool doDecodeImmediately
= gfxPrefs::ImageDecodeImmediatelyEnabled();
43 // We want UI to be as snappy as possible and not to flicker. Disable
44 // discarding for chrome URLS.
45 bool isChrome
= false;
46 rv
= uri
->SchemeIs("chrome", &isChrome
);
47 if (NS_SUCCEEDED(rv
) && isChrome
) {
48 isDiscardable
= false;
51 // We don't want resources like the "loading" icon to be discardable either.
52 bool isResource
= false;
53 rv
= uri
->SchemeIs("resource", &isResource
);
54 if (NS_SUCCEEDED(rv
) && isResource
) {
55 isDiscardable
= false;
58 // For multipart/x-mixed-replace, we basically want a direct channel to the
59 // decoder. Disable everything for this case.
61 isDiscardable
= false;
64 // We have all the information we need.
65 uint32_t imageFlags
= Image::INIT_FLAG_NONE
;
67 imageFlags
|= Image::INIT_FLAG_DISCARDABLE
;
69 if (doDecodeImmediately
) {
70 imageFlags
|= Image::INIT_FLAG_DECODE_IMMEDIATELY
;
73 imageFlags
|= Image::INIT_FLAG_TRANSIENT
;
76 // Synchronously decode metadata (including size) if we have a data URI since
77 // the data is immediately available.
78 bool isDataURI
= false;
79 rv
= uri
->SchemeIs("data", &isDataURI
);
80 if (NS_SUCCEEDED(rv
) && isDataURI
) {
81 imageFlags
|= Image::INIT_FLAG_SYNC_LOAD
;
88 static void NotifyImageLoading(nsIURI
* aURI
) {
89 if (!NS_IsMainThread()) {
90 nsCOMPtr
<nsIURI
> uri(aURI
);
91 nsCOMPtr
<nsIRunnable
> ev
= NS_NewRunnableFunction(
92 "NotifyImageLoading", [uri
]() -> void { NotifyImageLoading(uri
); });
93 SystemGroup::Dispatch(TaskCategory::Other
, ev
.forget());
97 nsCOMPtr
<nsIObserverService
> obs
= services::GetObserverService();
98 NS_WARNING_ASSERTION(obs
, "Can't get an observer service handle");
102 obs
->NotifyObservers(nullptr, "image-loading",
103 NS_ConvertUTF8toUTF16(spec
).get());
109 already_AddRefed
<Image
> ImageFactory::CreateImage(
110 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
111 const nsCString
& aMimeType
, nsIURI
* aURI
, bool aIsMultiPart
,
112 uint32_t aInnerWindowId
) {
113 MOZ_ASSERT(gfxPrefs::SingletonExists(),
114 "Pref observers should have been initialized already");
116 // Compute the image's initialization flags.
117 uint32_t imageFlags
= ComputeImageFlags(aURI
, aMimeType
, aIsMultiPart
);
120 // Record the image load for startup performance testing.
122 if ((NS_SUCCEEDED(aURI
->SchemeIs("resource", &match
)) && match
) ||
123 (NS_SUCCEEDED(aURI
->SchemeIs("chrome", &match
)) && match
)) {
124 NotifyImageLoading(aURI
);
128 // Select the type of image to create based on MIME type.
129 if (aMimeType
.EqualsLiteral(IMAGE_SVG_XML
)) {
130 return CreateVectorImage(aRequest
, aProgressTracker
, aMimeType
, aURI
,
131 imageFlags
, aInnerWindowId
);
133 return CreateRasterImage(aRequest
, aProgressTracker
, aMimeType
, aURI
,
134 imageFlags
, aInnerWindowId
);
138 // Marks an image as having an error before returning it.
139 template <typename T
>
140 static already_AddRefed
<Image
> BadImage(const char* aMessage
,
142 aImage
->SetHasError();
143 return aImage
.forget();
147 already_AddRefed
<Image
> ImageFactory::CreateAnonymousImage(
148 const nsCString
& aMimeType
, uint32_t aSizeHint
/* = 0 */) {
151 RefPtr
<RasterImage
> newImage
= new RasterImage();
153 RefPtr
<ProgressTracker
> newTracker
= new ProgressTracker();
154 newTracker
->SetImage(newImage
);
155 newImage
->SetProgressTracker(newTracker
);
157 rv
= newImage
->Init(aMimeType
.get(), Image::INIT_FLAG_SYNC_LOAD
);
159 return BadImage("RasterImage::Init failed", newImage
);
162 rv
= newImage
->SetSourceSizeHint(aSizeHint
);
164 return BadImage("RasterImage::SetSourceSizeHint failed", newImage
);
167 return newImage
.forget();
171 already_AddRefed
<MultipartImage
> ImageFactory::CreateMultipartImage(
172 Image
* aFirstPart
, ProgressTracker
* aProgressTracker
) {
173 MOZ_ASSERT(aFirstPart
);
174 MOZ_ASSERT(aProgressTracker
);
176 RefPtr
<MultipartImage
> newImage
= new MultipartImage(aFirstPart
);
177 aProgressTracker
->SetImage(newImage
);
178 newImage
->SetProgressTracker(aProgressTracker
);
182 return newImage
.forget();
185 int32_t SaturateToInt32(int64_t val
) {
193 return static_cast<int32_t>(val
);
196 uint32_t GetContentSize(nsIRequest
* aRequest
) {
197 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
200 nsresult rv
= channel
->GetContentLength(&size
);
201 if (NS_SUCCEEDED(rv
)) {
202 return std::max(SaturateToInt32(size
), 0);
206 // Use the file size as a size hint for file channels.
207 nsCOMPtr
<nsIFileChannel
> fileChannel(do_QueryInterface(aRequest
));
209 nsCOMPtr
<nsIFile
> file
;
210 nsresult rv
= fileChannel
->GetFile(getter_AddRefs(file
));
211 if (NS_SUCCEEDED(rv
)) {
213 rv
= file
->GetFileSize(&filesize
);
214 if (NS_SUCCEEDED(rv
)) {
215 return std::max(SaturateToInt32(filesize
), 0);
220 // Fallback - neither http nor file. We'll use dynamic allocation.
225 already_AddRefed
<Image
> ImageFactory::CreateRasterImage(
226 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
227 const nsCString
& aMimeType
, nsIURI
* aURI
, uint32_t aImageFlags
,
228 uint32_t aInnerWindowId
) {
229 MOZ_ASSERT(aProgressTracker
);
233 RefPtr
<RasterImage
> newImage
= new RasterImage(aURI
);
234 aProgressTracker
->SetImage(newImage
);
235 newImage
->SetProgressTracker(aProgressTracker
);
237 rv
= newImage
->Init(aMimeType
.get(), aImageFlags
);
239 return BadImage("RasterImage::Init failed", newImage
);
242 newImage
->SetInnerWindowID(aInnerWindowId
);
244 rv
= newImage
->SetSourceSizeHint(GetContentSize(aRequest
));
246 return BadImage("RasterImage::SetSourceSizeHint failed", newImage
);
249 return newImage
.forget();
253 already_AddRefed
<Image
> ImageFactory::CreateVectorImage(
254 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
255 const nsCString
& aMimeType
, nsIURI
* aURI
, uint32_t aImageFlags
,
256 uint32_t aInnerWindowId
) {
257 MOZ_ASSERT(aProgressTracker
);
261 RefPtr
<VectorImage
> newImage
= new VectorImage(aURI
);
262 aProgressTracker
->SetImage(newImage
);
263 newImage
->SetProgressTracker(aProgressTracker
);
265 rv
= newImage
->Init(aMimeType
.get(), aImageFlags
);
267 return BadImage("VectorImage::Init failed", newImage
);
270 newImage
->SetInnerWindowID(aInnerWindowId
);
272 rv
= newImage
->OnStartRequest(aRequest
);
274 return BadImage("VectorImage::OnStartRequest failed", newImage
);
277 return newImage
.forget();
281 } // namespace mozilla