Bug 1646700 [wpt PR 24235] - Update picture-in-picture idlharness test, a=testonly
[gecko.git] / image / ImageFactory.cpp
blob87bb3deac72544ae9e68351755700d0882089943
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 "nsIFileChannel.h"
14 #include "nsIFile.h"
15 #include "nsMimeTypes.h"
16 #include "nsIRequest.h"
18 #include "MultipartImage.h"
19 #include "RasterImage.h"
20 #include "VectorImage.h"
21 #include "Image.h"
22 #include "nsMediaFragmentURIParser.h"
23 #include "nsContentUtils.h"
25 #include "mozilla/SchedulerGroup.h"
26 #include "mozilla/StaticPrefs_image.h"
28 namespace mozilla {
29 namespace image {
31 /*static*/
32 void ImageFactory::Initialize() {}
34 static uint32_t ComputeImageFlags(nsIURI* uri, const nsCString& aMimeType,
35 bool isMultiPart) {
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.
53 if (isMultiPart) {
54 isDiscardable = false;
57 // We have all the information we need.
58 uint32_t imageFlags = Image::INIT_FLAG_NONE;
59 if (isDiscardable) {
60 imageFlags |= Image::INIT_FLAG_DISCARDABLE;
62 if (doDecodeImmediately) {
63 imageFlags |= Image::INIT_FLAG_DECODE_IMMEDIATELY;
65 if (isMultiPart) {
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;
75 return imageFlags;
78 #ifdef DEBUG
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());
85 return;
88 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
89 NS_WARNING_ASSERTION(obs, "Can't get an observer service handle");
90 if (obs) {
91 nsAutoCString spec;
92 aURI->GetSpec(spec);
93 obs->NotifyObservers(nullptr, "image-loading",
94 NS_ConvertUTF8toUTF16(spec).get());
97 #endif
99 /* static */
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);
107 #ifdef DEBUG
108 // Record the image load for startup performance testing.
109 if (aURI->SchemeIs("resource") || aURI->SchemeIs("chrome")) {
110 NotifyImageLoading(aURI);
112 #endif
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);
118 } else {
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,
127 RefPtr<T>& aImage) {
128 aImage->SetHasError();
129 return aImage.forget();
132 /* static */
133 already_AddRefed<Image> ImageFactory::CreateAnonymousImage(
134 const nsCString& aMimeType, uint32_t aSizeHint /* = 0 */) {
135 nsresult rv;
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);
144 if (NS_FAILED(rv)) {
145 return BadImage("RasterImage::Init failed", newImage);
148 rv = newImage->SetSourceSizeHint(aSizeHint);
149 if (NS_FAILED(rv)) {
150 return BadImage("RasterImage::SetSourceSizeHint failed", newImage);
153 return newImage.forget();
156 /* static */
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);
166 newImage->Init();
168 return newImage.forget();
171 int32_t SaturateToInt32(int64_t val) {
172 if (val > INT_MAX) {
173 return INT_MAX;
175 if (val < INT_MIN) {
176 return INT_MIN;
179 return static_cast<int32_t>(val);
182 uint32_t GetContentSize(nsIRequest* aRequest) {
183 nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
184 if (channel) {
185 int64_t size;
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));
194 if (fileChannel) {
195 nsCOMPtr<nsIFile> file;
196 nsresult rv = fileChannel->GetFile(getter_AddRefs(file));
197 if (NS_SUCCEEDED(rv)) {
198 int64_t filesize;
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.
207 return 0;
210 /* static */
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);
217 nsresult rv;
219 RefPtr<RasterImage> newImage = new RasterImage(aURI);
220 aProgressTracker->SetImage(newImage);
221 newImage->SetProgressTracker(aProgressTracker);
223 rv = newImage->Init(aMimeType.get(), aImageFlags);
224 if (NS_FAILED(rv)) {
225 return BadImage("RasterImage::Init failed", newImage);
228 newImage->SetInnerWindowID(aInnerWindowId);
230 rv = newImage->SetSourceSizeHint(GetContentSize(aRequest));
231 if (NS_FAILED(rv)) {
232 return BadImage("RasterImage::SetSourceSizeHint failed", newImage);
235 return newImage.forget();
238 /* static */
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);
245 nsresult rv;
247 RefPtr<VectorImage> newImage = new VectorImage(aURI);
248 aProgressTracker->SetImage(newImage);
249 newImage->SetProgressTracker(aProgressTracker);
251 rv = newImage->Init(aMimeType.get(), aImageFlags);
252 if (NS_FAILED(rv)) {
253 return BadImage("VectorImage::Init failed", newImage);
256 newImage->SetInnerWindowID(aInnerWindowId);
258 rv = newImage->OnStartRequest(aRequest);
259 if (NS_FAILED(rv)) {
260 return BadImage("VectorImage::OnStartRequest failed", newImage);
263 return newImage.forget();
266 } // namespace image
267 } // namespace mozilla