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 "nsIChannel.h"
14 #include "nsIFileChannel.h"
15 #include "nsIObserverService.h"
17 #include "nsMimeTypes.h"
18 #include "nsIRequest.h"
20 #include "MultipartImage.h"
21 #include "RasterImage.h"
22 #include "VectorImage.h"
24 #include "nsMediaFragmentURIParser.h"
25 #include "nsContentUtils.h"
27 #include "mozilla/SchedulerGroup.h"
28 #include "mozilla/StaticPrefs_image.h"
34 void ImageFactory::Initialize() {}
36 static uint32_t ComputeImageFlags(nsIURI
* uri
, const nsCString
& aMimeType
,
38 // We default to the static globals.
39 bool isDiscardable
= StaticPrefs::image_mem_discardable();
40 bool doDecodeImmediately
= StaticPrefs::image_decode_immediately_enabled();
42 // We want UI to be as snappy as possible and not to flicker. Disable
43 // discarding for chrome URLS.
44 if (uri
->SchemeIs("chrome")) {
45 isDiscardable
= false;
48 // We don't want resources like the "loading" icon to be discardable either.
49 if (uri
->SchemeIs("resource")) {
50 isDiscardable
= false;
53 // For multipart/x-mixed-replace, we basically want a direct channel to the
54 // decoder. Disable everything for this case.
56 isDiscardable
= false;
59 // We have all the information we need.
60 uint32_t imageFlags
= Image::INIT_FLAG_NONE
;
62 imageFlags
|= Image::INIT_FLAG_DISCARDABLE
;
64 if (doDecodeImmediately
) {
65 imageFlags
|= Image::INIT_FLAG_DECODE_IMMEDIATELY
;
68 imageFlags
|= Image::INIT_FLAG_TRANSIENT
;
71 // Synchronously decode metadata (including size) if we have a data URI since
72 // the data is immediately available.
73 if (uri
->SchemeIs("data")) {
74 imageFlags
|= Image::INIT_FLAG_SYNC_LOAD
;
81 static void NotifyImageLoading(nsIURI
* aURI
) {
82 if (!NS_IsMainThread()) {
83 nsCOMPtr
<nsIURI
> uri(aURI
);
84 nsCOMPtr
<nsIRunnable
> ev
= NS_NewRunnableFunction(
85 "NotifyImageLoading", [uri
]() -> void { NotifyImageLoading(uri
); });
86 SchedulerGroup::Dispatch(TaskCategory::Other
, ev
.forget());
90 nsCOMPtr
<nsIObserverService
> obs
= services::GetObserverService();
91 NS_WARNING_ASSERTION(obs
, "Can't get an observer service handle");
95 obs
->NotifyObservers(nullptr, "image-loading",
96 NS_ConvertUTF8toUTF16(spec
).get());
102 already_AddRefed
<Image
> ImageFactory::CreateImage(
103 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
104 const nsCString
& aMimeType
, nsIURI
* aURI
, bool aIsMultiPart
,
105 uint32_t aInnerWindowId
) {
106 // Compute the image's initialization flags.
107 uint32_t imageFlags
= ComputeImageFlags(aURI
, aMimeType
, aIsMultiPart
);
110 // Record the image load for startup performance testing.
111 if (aURI
->SchemeIs("resource") || aURI
->SchemeIs("chrome")) {
112 NotifyImageLoading(aURI
);
116 // Select the type of image to create based on MIME type.
117 if (aMimeType
.EqualsLiteral(IMAGE_SVG_XML
)) {
118 return CreateVectorImage(aRequest
, aProgressTracker
, aMimeType
, aURI
,
119 imageFlags
, aInnerWindowId
);
121 return CreateRasterImage(aRequest
, aProgressTracker
, aMimeType
, aURI
,
122 imageFlags
, aInnerWindowId
);
126 // Marks an image as having an error before returning it.
127 template <typename T
>
128 static already_AddRefed
<Image
> BadImage(const char* aMessage
,
130 aImage
->SetHasError();
131 return aImage
.forget();
135 already_AddRefed
<Image
> ImageFactory::CreateAnonymousImage(
136 const nsCString
& aMimeType
, uint32_t aSizeHint
/* = 0 */) {
139 RefPtr
<RasterImage
> newImage
= new RasterImage();
141 RefPtr
<ProgressTracker
> newTracker
= new ProgressTracker();
142 newTracker
->SetImage(newImage
);
143 newImage
->SetProgressTracker(newTracker
);
145 rv
= newImage
->Init(aMimeType
.get(), Image::INIT_FLAG_SYNC_LOAD
);
147 return BadImage("RasterImage::Init failed", newImage
);
150 rv
= newImage
->SetSourceSizeHint(aSizeHint
);
152 return BadImage("RasterImage::SetSourceSizeHint failed", newImage
);
155 return newImage
.forget();
159 already_AddRefed
<MultipartImage
> ImageFactory::CreateMultipartImage(
160 Image
* aFirstPart
, ProgressTracker
* aProgressTracker
) {
161 MOZ_ASSERT(aFirstPart
);
162 MOZ_ASSERT(aProgressTracker
);
164 RefPtr
<MultipartImage
> newImage
= new MultipartImage(aFirstPart
);
165 aProgressTracker
->SetImage(newImage
);
166 newImage
->SetProgressTracker(aProgressTracker
);
170 return newImage
.forget();
173 int32_t SaturateToInt32(int64_t val
) {
181 return static_cast<int32_t>(val
);
184 uint32_t GetContentSize(nsIRequest
* aRequest
) {
185 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
188 nsresult rv
= channel
->GetContentLength(&size
);
189 if (NS_SUCCEEDED(rv
)) {
190 return std::max(SaturateToInt32(size
), 0);
194 // Use the file size as a size hint for file channels.
195 nsCOMPtr
<nsIFileChannel
> fileChannel(do_QueryInterface(aRequest
));
197 nsCOMPtr
<nsIFile
> file
;
198 nsresult rv
= fileChannel
->GetFile(getter_AddRefs(file
));
199 if (NS_SUCCEEDED(rv
)) {
201 rv
= file
->GetFileSize(&filesize
);
202 if (NS_SUCCEEDED(rv
)) {
203 return std::max(SaturateToInt32(filesize
), 0);
208 // Fallback - neither http nor file. We'll use dynamic allocation.
213 already_AddRefed
<Image
> ImageFactory::CreateRasterImage(
214 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
215 const nsCString
& aMimeType
, nsIURI
* aURI
, uint32_t aImageFlags
,
216 uint32_t aInnerWindowId
) {
217 MOZ_ASSERT(aProgressTracker
);
221 RefPtr
<RasterImage
> newImage
= new RasterImage(aURI
);
222 aProgressTracker
->SetImage(newImage
);
223 newImage
->SetProgressTracker(aProgressTracker
);
225 rv
= newImage
->Init(aMimeType
.get(), aImageFlags
);
227 return BadImage("RasterImage::Init failed", newImage
);
230 newImage
->SetInnerWindowID(aInnerWindowId
);
232 rv
= newImage
->SetSourceSizeHint(GetContentSize(aRequest
));
234 return BadImage("RasterImage::SetSourceSizeHint failed", newImage
);
237 return newImage
.forget();
241 already_AddRefed
<Image
> ImageFactory::CreateVectorImage(
242 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
243 const nsCString
& aMimeType
, nsIURI
* aURI
, uint32_t aImageFlags
,
244 uint32_t aInnerWindowId
) {
245 MOZ_ASSERT(aProgressTracker
);
249 RefPtr
<VectorImage
> newImage
= new VectorImage(aURI
);
250 aProgressTracker
->SetImage(newImage
);
251 newImage
->SetProgressTracker(aProgressTracker
);
253 rv
= newImage
->Init(aMimeType
.get(), aImageFlags
);
255 return BadImage("VectorImage::Init failed", newImage
);
258 newImage
->SetInnerWindowID(aInnerWindowId
);
260 rv
= newImage
->OnStartRequest(aRequest
);
262 return BadImage("VectorImage::OnStartRequest failed", newImage
);
265 return newImage
.forget();
269 } // namespace mozilla