Bug 1686538 [wpt PR 27147] - Update wpt metadata, a=testonly
[gecko.git] / image / ImageFactory.cpp
blob9ce405f2c8717c4365120f5bfaf761f6f46d28a5
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"
9 #include <algorithm>
11 #include "mozilla/Likely.h"
13 #include "nsIChannel.h"
14 #include "nsIFileChannel.h"
15 #include "nsIObserverService.h"
16 #include "nsIFile.h"
17 #include "nsMimeTypes.h"
18 #include "nsIRequest.h"
20 #include "MultipartImage.h"
21 #include "RasterImage.h"
22 #include "VectorImage.h"
23 #include "Image.h"
24 #include "nsMediaFragmentURIParser.h"
25 #include "nsContentUtils.h"
27 #include "mozilla/SchedulerGroup.h"
28 #include "mozilla/StaticPrefs_image.h"
30 namespace mozilla {
31 namespace image {
33 /*static*/
34 void ImageFactory::Initialize() {}
36 static uint32_t ComputeImageFlags(nsIURI* uri, const nsCString& aMimeType,
37 bool isMultiPart) {
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.
55 if (isMultiPart) {
56 isDiscardable = false;
59 // We have all the information we need.
60 uint32_t imageFlags = Image::INIT_FLAG_NONE;
61 if (isDiscardable) {
62 imageFlags |= Image::INIT_FLAG_DISCARDABLE;
64 if (doDecodeImmediately) {
65 imageFlags |= Image::INIT_FLAG_DECODE_IMMEDIATELY;
67 if (isMultiPart) {
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;
77 return imageFlags;
80 #ifdef DEBUG
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());
87 return;
90 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
91 NS_WARNING_ASSERTION(obs, "Can't get an observer service handle");
92 if (obs) {
93 nsAutoCString spec;
94 aURI->GetSpec(spec);
95 obs->NotifyObservers(nullptr, "image-loading",
96 NS_ConvertUTF8toUTF16(spec).get());
99 #endif
101 /* static */
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);
109 #ifdef DEBUG
110 // Record the image load for startup performance testing.
111 if (aURI->SchemeIs("resource") || aURI->SchemeIs("chrome")) {
112 NotifyImageLoading(aURI);
114 #endif
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);
120 } else {
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,
129 RefPtr<T>& aImage) {
130 aImage->SetHasError();
131 return aImage.forget();
134 /* static */
135 already_AddRefed<Image> ImageFactory::CreateAnonymousImage(
136 const nsCString& aMimeType, uint32_t aSizeHint /* = 0 */) {
137 nsresult rv;
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);
146 if (NS_FAILED(rv)) {
147 return BadImage("RasterImage::Init failed", newImage);
150 rv = newImage->SetSourceSizeHint(aSizeHint);
151 if (NS_FAILED(rv)) {
152 return BadImage("RasterImage::SetSourceSizeHint failed", newImage);
155 return newImage.forget();
158 /* static */
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);
168 newImage->Init();
170 return newImage.forget();
173 int32_t SaturateToInt32(int64_t val) {
174 if (val > INT_MAX) {
175 return INT_MAX;
177 if (val < INT_MIN) {
178 return INT_MIN;
181 return static_cast<int32_t>(val);
184 uint32_t GetContentSize(nsIRequest* aRequest) {
185 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
186 if (channel) {
187 int64_t size;
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));
196 if (fileChannel) {
197 nsCOMPtr<nsIFile> file;
198 nsresult rv = fileChannel->GetFile(getter_AddRefs(file));
199 if (NS_SUCCEEDED(rv)) {
200 int64_t filesize;
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.
209 return 0;
212 /* static */
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);
219 nsresult rv;
221 RefPtr<RasterImage> newImage = new RasterImage(aURI);
222 aProgressTracker->SetImage(newImage);
223 newImage->SetProgressTracker(aProgressTracker);
225 rv = newImage->Init(aMimeType.get(), aImageFlags);
226 if (NS_FAILED(rv)) {
227 return BadImage("RasterImage::Init failed", newImage);
230 newImage->SetInnerWindowID(aInnerWindowId);
232 rv = newImage->SetSourceSizeHint(GetContentSize(aRequest));
233 if (NS_FAILED(rv)) {
234 return BadImage("RasterImage::SetSourceSizeHint failed", newImage);
237 return newImage.forget();
240 /* static */
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);
247 nsresult rv;
249 RefPtr<VectorImage> newImage = new VectorImage(aURI);
250 aProgressTracker->SetImage(newImage);
251 newImage->SetProgressTracker(aProgressTracker);
253 rv = newImage->Init(aMimeType.get(), aImageFlags);
254 if (NS_FAILED(rv)) {
255 return BadImage("VectorImage::Init failed", newImage);
258 newImage->SetInnerWindowID(aInnerWindowId);
260 rv = newImage->OnStartRequest(aRequest);
261 if (NS_FAILED(rv)) {
262 return BadImage("VectorImage::OnStartRequest failed", newImage);
265 return newImage.forget();
268 } // namespace image
269 } // namespace mozilla