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 "nsIFileChannel.h"
15 #include "nsMimeTypes.h"
16 #include "nsIRequest.h"
18 #include "MultipartImage.h"
19 #include "RasterImage.h"
20 #include "VectorImage.h"
22 #include "nsMediaFragmentURIParser.h"
23 #include "nsContentUtils.h"
25 #include "mozilla/SchedulerGroup.h"
26 #include "mozilla/StaticPrefs_image.h"
32 void ImageFactory::Initialize() {}
34 static uint32_t ComputeImageFlags(nsIURI
* uri
, const nsCString
& aMimeType
,
36 // We default to the static globals.
37 bool isDiscardable
= StaticPrefs::image_mem_discardable();
38 bool doDecodeImmediately
= StaticPrefs::image_decode_immediately_enabled();
40 // We want UI to be as snappy as possible and not to flicker. Disable
41 // discarding for chrome URLS.
42 if (uri
->SchemeIs("chrome")) {
43 isDiscardable
= false;
46 // We don't want resources like the "loading" icon to be discardable either.
47 if (uri
->SchemeIs("resource")) {
48 isDiscardable
= false;
51 // For multipart/x-mixed-replace, we basically want a direct channel to the
52 // decoder. Disable everything for this case.
54 isDiscardable
= false;
57 // We have all the information we need.
58 uint32_t imageFlags
= Image::INIT_FLAG_NONE
;
60 imageFlags
|= Image::INIT_FLAG_DISCARDABLE
;
62 if (doDecodeImmediately
) {
63 imageFlags
|= Image::INIT_FLAG_DECODE_IMMEDIATELY
;
66 imageFlags
|= Image::INIT_FLAG_TRANSIENT
;
69 // Synchronously decode metadata (including size) if we have a data URI since
70 // the data is immediately available.
71 if (uri
->SchemeIs("data")) {
72 imageFlags
|= Image::INIT_FLAG_SYNC_LOAD
;
79 static void NotifyImageLoading(nsIURI
* aURI
) {
80 if (!NS_IsMainThread()) {
81 nsCOMPtr
<nsIURI
> uri(aURI
);
82 nsCOMPtr
<nsIRunnable
> ev
= NS_NewRunnableFunction(
83 "NotifyImageLoading", [uri
]() -> void { NotifyImageLoading(uri
); });
84 SchedulerGroup::Dispatch(TaskCategory::Other
, ev
.forget());
88 nsCOMPtr
<nsIObserverService
> obs
= services::GetObserverService();
89 NS_WARNING_ASSERTION(obs
, "Can't get an observer service handle");
93 obs
->NotifyObservers(nullptr, "image-loading",
94 NS_ConvertUTF8toUTF16(spec
).get());
100 already_AddRefed
<Image
> ImageFactory::CreateImage(
101 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
102 const nsCString
& aMimeType
, nsIURI
* aURI
, bool aIsMultiPart
,
103 uint32_t aInnerWindowId
) {
104 // Compute the image's initialization flags.
105 uint32_t imageFlags
= ComputeImageFlags(aURI
, aMimeType
, aIsMultiPart
);
108 // Record the image load for startup performance testing.
109 if (aURI
->SchemeIs("resource") || aURI
->SchemeIs("chrome")) {
110 NotifyImageLoading(aURI
);
114 // Select the type of image to create based on MIME type.
115 if (aMimeType
.EqualsLiteral(IMAGE_SVG_XML
)) {
116 return CreateVectorImage(aRequest
, aProgressTracker
, aMimeType
, aURI
,
117 imageFlags
, aInnerWindowId
);
119 return CreateRasterImage(aRequest
, aProgressTracker
, aMimeType
, aURI
,
120 imageFlags
, aInnerWindowId
);
124 // Marks an image as having an error before returning it.
125 template <typename T
>
126 static already_AddRefed
<Image
> BadImage(const char* aMessage
,
128 aImage
->SetHasError();
129 return aImage
.forget();
133 already_AddRefed
<Image
> ImageFactory::CreateAnonymousImage(
134 const nsCString
& aMimeType
, uint32_t aSizeHint
/* = 0 */) {
137 RefPtr
<RasterImage
> newImage
= new RasterImage();
139 RefPtr
<ProgressTracker
> newTracker
= new ProgressTracker();
140 newTracker
->SetImage(newImage
);
141 newImage
->SetProgressTracker(newTracker
);
143 rv
= newImage
->Init(aMimeType
.get(), Image::INIT_FLAG_SYNC_LOAD
);
145 return BadImage("RasterImage::Init failed", newImage
);
148 rv
= newImage
->SetSourceSizeHint(aSizeHint
);
150 return BadImage("RasterImage::SetSourceSizeHint failed", newImage
);
153 return newImage
.forget();
157 already_AddRefed
<MultipartImage
> ImageFactory::CreateMultipartImage(
158 Image
* aFirstPart
, ProgressTracker
* aProgressTracker
) {
159 MOZ_ASSERT(aFirstPart
);
160 MOZ_ASSERT(aProgressTracker
);
162 RefPtr
<MultipartImage
> newImage
= new MultipartImage(aFirstPart
);
163 aProgressTracker
->SetImage(newImage
);
164 newImage
->SetProgressTracker(aProgressTracker
);
168 return newImage
.forget();
171 int32_t SaturateToInt32(int64_t val
) {
179 return static_cast<int32_t>(val
);
182 uint32_t GetContentSize(nsIRequest
* aRequest
) {
183 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
186 nsresult rv
= channel
->GetContentLength(&size
);
187 if (NS_SUCCEEDED(rv
)) {
188 return std::max(SaturateToInt32(size
), 0);
192 // Use the file size as a size hint for file channels.
193 nsCOMPtr
<nsIFileChannel
> fileChannel(do_QueryInterface(aRequest
));
195 nsCOMPtr
<nsIFile
> file
;
196 nsresult rv
= fileChannel
->GetFile(getter_AddRefs(file
));
197 if (NS_SUCCEEDED(rv
)) {
199 rv
= file
->GetFileSize(&filesize
);
200 if (NS_SUCCEEDED(rv
)) {
201 return std::max(SaturateToInt32(filesize
), 0);
206 // Fallback - neither http nor file. We'll use dynamic allocation.
211 already_AddRefed
<Image
> ImageFactory::CreateRasterImage(
212 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
213 const nsCString
& aMimeType
, nsIURI
* aURI
, uint32_t aImageFlags
,
214 uint32_t aInnerWindowId
) {
215 MOZ_ASSERT(aProgressTracker
);
219 RefPtr
<RasterImage
> newImage
= new RasterImage(aURI
);
220 aProgressTracker
->SetImage(newImage
);
221 newImage
->SetProgressTracker(aProgressTracker
);
223 rv
= newImage
->Init(aMimeType
.get(), aImageFlags
);
225 return BadImage("RasterImage::Init failed", newImage
);
228 newImage
->SetInnerWindowID(aInnerWindowId
);
230 rv
= newImage
->SetSourceSizeHint(GetContentSize(aRequest
));
232 return BadImage("RasterImage::SetSourceSizeHint failed", newImage
);
235 return newImage
.forget();
239 already_AddRefed
<Image
> ImageFactory::CreateVectorImage(
240 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
241 const nsCString
& aMimeType
, nsIURI
* aURI
, uint32_t aImageFlags
,
242 uint32_t aInnerWindowId
) {
243 MOZ_ASSERT(aProgressTracker
);
247 RefPtr
<VectorImage
> newImage
= new VectorImage(aURI
);
248 aProgressTracker
->SetImage(newImage
);
249 newImage
->SetProgressTracker(aProgressTracker
);
251 rv
= newImage
->Init(aMimeType
.get(), aImageFlags
);
253 return BadImage("VectorImage::Init failed", newImage
);
256 newImage
->SetInnerWindowID(aInnerWindowId
);
258 rv
= newImage
->OnStartRequest(aRequest
);
260 return BadImage("VectorImage::OnStartRequest failed", newImage
);
263 return newImage
.forget();
267 } // namespace mozilla