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/StaticPrefs_image.h"
31 void ImageFactory::Initialize() {}
33 static uint32_t ComputeImageFlags(nsIURI
* uri
, const nsCString
& aMimeType
,
35 // We default to the static globals.
36 bool isDiscardable
= StaticPrefs::image_mem_discardable();
37 bool doDecodeImmediately
= StaticPrefs::image_decode_immediately_enabled();
39 // We want UI to be as snappy as possible and not to flicker. Disable
40 // discarding for chrome URLS.
41 if (uri
->SchemeIs("chrome")) {
42 isDiscardable
= false;
45 // We don't want resources like the "loading" icon to be discardable either.
46 if (uri
->SchemeIs("resource")) {
47 isDiscardable
= false;
50 // For multipart/x-mixed-replace, we basically want a direct channel to the
51 // decoder. Disable everything for this case.
53 isDiscardable
= false;
56 // We have all the information we need.
57 uint32_t imageFlags
= Image::INIT_FLAG_NONE
;
59 imageFlags
|= Image::INIT_FLAG_DISCARDABLE
;
61 if (doDecodeImmediately
) {
62 imageFlags
|= Image::INIT_FLAG_DECODE_IMMEDIATELY
;
65 imageFlags
|= Image::INIT_FLAG_TRANSIENT
;
68 // Synchronously decode metadata (including size) if we have a data URI since
69 // the data is immediately available.
70 if (uri
->SchemeIs("data")) {
71 imageFlags
|= Image::INIT_FLAG_SYNC_LOAD
;
78 static void NotifyImageLoading(nsIURI
* aURI
) {
79 if (!NS_IsMainThread()) {
80 nsCOMPtr
<nsIURI
> uri(aURI
);
81 nsCOMPtr
<nsIRunnable
> ev
= NS_NewRunnableFunction(
82 "NotifyImageLoading", [uri
]() -> void { NotifyImageLoading(uri
); });
83 SystemGroup::Dispatch(TaskCategory::Other
, ev
.forget());
87 nsCOMPtr
<nsIObserverService
> obs
= services::GetObserverService();
88 NS_WARNING_ASSERTION(obs
, "Can't get an observer service handle");
92 obs
->NotifyObservers(nullptr, "image-loading",
93 NS_ConvertUTF8toUTF16(spec
).get());
99 already_AddRefed
<Image
> ImageFactory::CreateImage(
100 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
101 const nsCString
& aMimeType
, nsIURI
* aURI
, bool aIsMultiPart
,
102 uint32_t aInnerWindowId
) {
103 // Compute the image's initialization flags.
104 uint32_t imageFlags
= ComputeImageFlags(aURI
, aMimeType
, aIsMultiPart
);
107 // Record the image load for startup performance testing.
108 if (aURI
->SchemeIs("resource") || aURI
->SchemeIs("chrome")) {
109 NotifyImageLoading(aURI
);
113 // Select the type of image to create based on MIME type.
114 if (aMimeType
.EqualsLiteral(IMAGE_SVG_XML
)) {
115 return CreateVectorImage(aRequest
, aProgressTracker
, aMimeType
, aURI
,
116 imageFlags
, aInnerWindowId
);
118 return CreateRasterImage(aRequest
, aProgressTracker
, aMimeType
, aURI
,
119 imageFlags
, aInnerWindowId
);
123 // Marks an image as having an error before returning it.
124 template <typename T
>
125 static already_AddRefed
<Image
> BadImage(const char* aMessage
,
127 aImage
->SetHasError();
128 return aImage
.forget();
132 already_AddRefed
<Image
> ImageFactory::CreateAnonymousImage(
133 const nsCString
& aMimeType
, uint32_t aSizeHint
/* = 0 */) {
136 RefPtr
<RasterImage
> newImage
= new RasterImage();
138 RefPtr
<ProgressTracker
> newTracker
= new ProgressTracker();
139 newTracker
->SetImage(newImage
);
140 newImage
->SetProgressTracker(newTracker
);
142 rv
= newImage
->Init(aMimeType
.get(), Image::INIT_FLAG_SYNC_LOAD
);
144 return BadImage("RasterImage::Init failed", newImage
);
147 rv
= newImage
->SetSourceSizeHint(aSizeHint
);
149 return BadImage("RasterImage::SetSourceSizeHint failed", newImage
);
152 return newImage
.forget();
156 already_AddRefed
<MultipartImage
> ImageFactory::CreateMultipartImage(
157 Image
* aFirstPart
, ProgressTracker
* aProgressTracker
) {
158 MOZ_ASSERT(aFirstPart
);
159 MOZ_ASSERT(aProgressTracker
);
161 RefPtr
<MultipartImage
> newImage
= new MultipartImage(aFirstPart
);
162 aProgressTracker
->SetImage(newImage
);
163 newImage
->SetProgressTracker(aProgressTracker
);
167 return newImage
.forget();
170 int32_t SaturateToInt32(int64_t val
) {
178 return static_cast<int32_t>(val
);
181 uint32_t GetContentSize(nsIRequest
* aRequest
) {
182 nsCOMPtr
<nsIChannel
> channel(do_QueryInterface(aRequest
));
185 nsresult rv
= channel
->GetContentLength(&size
);
186 if (NS_SUCCEEDED(rv
)) {
187 return std::max(SaturateToInt32(size
), 0);
191 // Use the file size as a size hint for file channels.
192 nsCOMPtr
<nsIFileChannel
> fileChannel(do_QueryInterface(aRequest
));
194 nsCOMPtr
<nsIFile
> file
;
195 nsresult rv
= fileChannel
->GetFile(getter_AddRefs(file
));
196 if (NS_SUCCEEDED(rv
)) {
198 rv
= file
->GetFileSize(&filesize
);
199 if (NS_SUCCEEDED(rv
)) {
200 return std::max(SaturateToInt32(filesize
), 0);
205 // Fallback - neither http nor file. We'll use dynamic allocation.
210 already_AddRefed
<Image
> ImageFactory::CreateRasterImage(
211 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
212 const nsCString
& aMimeType
, nsIURI
* aURI
, uint32_t aImageFlags
,
213 uint32_t aInnerWindowId
) {
214 MOZ_ASSERT(aProgressTracker
);
218 RefPtr
<RasterImage
> newImage
= new RasterImage(aURI
);
219 aProgressTracker
->SetImage(newImage
);
220 newImage
->SetProgressTracker(aProgressTracker
);
222 rv
= newImage
->Init(aMimeType
.get(), aImageFlags
);
224 return BadImage("RasterImage::Init failed", newImage
);
227 newImage
->SetInnerWindowID(aInnerWindowId
);
229 rv
= newImage
->SetSourceSizeHint(GetContentSize(aRequest
));
231 return BadImage("RasterImage::SetSourceSizeHint failed", newImage
);
234 return newImage
.forget();
238 already_AddRefed
<Image
> ImageFactory::CreateVectorImage(
239 nsIRequest
* aRequest
, ProgressTracker
* aProgressTracker
,
240 const nsCString
& aMimeType
, nsIURI
* aURI
, uint32_t aImageFlags
,
241 uint32_t aInnerWindowId
) {
242 MOZ_ASSERT(aProgressTracker
);
246 RefPtr
<VectorImage
> newImage
= new VectorImage(aURI
);
247 aProgressTracker
->SetImage(newImage
);
248 newImage
->SetProgressTracker(aProgressTracker
);
250 rv
= newImage
->Init(aMimeType
.get(), aImageFlags
);
252 return BadImage("VectorImage::Init failed", newImage
);
255 newImage
->SetInnerWindowID(aInnerWindowId
);
257 rv
= newImage
->OnStartRequest(aRequest
);
259 return BadImage("VectorImage::OnStartRequest failed", newImage
);
262 return newImage
.forget();
266 } // namespace mozilla