michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #include "mozilla/Preferences.h" michael@0: #include "mozilla/Likely.h" michael@0: michael@0: #include "nsIHttpChannel.h" michael@0: #include "nsIFileChannel.h" michael@0: #include "nsIFile.h" michael@0: #include "nsMimeTypes.h" michael@0: #include "nsIRequest.h" michael@0: michael@0: #include "RasterImage.h" michael@0: #include "VectorImage.h" michael@0: #include "Image.h" michael@0: #include "nsMediaFragmentURIParser.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsIScriptSecurityManager.h" michael@0: michael@0: #include "ImageFactory.h" michael@0: #include "gfxPrefs.h" michael@0: michael@0: namespace mozilla { michael@0: namespace image { michael@0: michael@0: // Global preferences related to image containers. michael@0: static bool gInitializedPrefCaches = false; michael@0: static bool gDecodeOnDraw = false; michael@0: static bool gDiscardable = false; michael@0: static bool gEnableMozSampleSize = false; michael@0: michael@0: /*static*/ void michael@0: ImageFactory::Initialize() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: if (!gInitializedPrefCaches) { michael@0: // Initialize the graphics preferences michael@0: gfxPrefs::GetSingleton(); michael@0: Preferences::AddBoolVarCache(&gDiscardable, "image.mem.discardable"); michael@0: Preferences::AddBoolVarCache(&gDecodeOnDraw, "image.mem.decodeondraw"); michael@0: Preferences::AddBoolVarCache(&gEnableMozSampleSize, "image.mozsamplesize.enabled"); michael@0: gInitializedPrefCaches = true; michael@0: } michael@0: } michael@0: michael@0: static uint32_t michael@0: ComputeImageFlags(ImageURL* uri, bool isMultiPart) michael@0: { michael@0: nsresult rv; michael@0: michael@0: // We default to the static globals. michael@0: bool isDiscardable = gDiscardable; michael@0: bool doDecodeOnDraw = gDecodeOnDraw; michael@0: michael@0: // We want UI to be as snappy as possible and not to flicker. Disable discarding michael@0: // and decode-on-draw for chrome URLS. michael@0: bool isChrome = false; michael@0: rv = uri->SchemeIs("chrome", &isChrome); michael@0: if (NS_SUCCEEDED(rv) && isChrome) michael@0: isDiscardable = doDecodeOnDraw = false; michael@0: michael@0: // We don't want resources like the "loading" icon to be discardable or michael@0: // decode-on-draw either. michael@0: bool isResource = false; michael@0: rv = uri->SchemeIs("resource", &isResource); michael@0: if (NS_SUCCEEDED(rv) && isResource) michael@0: isDiscardable = doDecodeOnDraw = false; michael@0: michael@0: // For multipart/x-mixed-replace, we basically want a direct channel to the michael@0: // decoder. Disable both for this case as well. michael@0: if (isMultiPart) michael@0: isDiscardable = doDecodeOnDraw = false; michael@0: michael@0: // We have all the information we need. michael@0: uint32_t imageFlags = Image::INIT_FLAG_NONE; michael@0: if (isDiscardable) michael@0: imageFlags |= Image::INIT_FLAG_DISCARDABLE; michael@0: if (doDecodeOnDraw) michael@0: imageFlags |= Image::INIT_FLAG_DECODE_ON_DRAW; michael@0: if (isMultiPart) michael@0: imageFlags |= Image::INIT_FLAG_MULTIPART; michael@0: michael@0: return imageFlags; michael@0: } michael@0: michael@0: /* static */ bool michael@0: ImageFactory::CanRetargetOnDataAvailable(ImageURL* aURI, bool aIsMultiPart) michael@0: { michael@0: // We can't retarget OnDataAvailable safely in cases where we aren't storing michael@0: // source data (and thus need to sync decode in ODA) because allocating frames michael@0: // off-main-thread is currently not possible and we don't have a workaround in michael@0: // place yet. (See bug 967985.) For now, we detect those cases and refuse to michael@0: // retarget. When the problem is fixed, this function can be removed. michael@0: michael@0: if (aIsMultiPart) { michael@0: return false; michael@0: } michael@0: michael@0: uint32_t imageFlags = ComputeImageFlags(aURI, aIsMultiPart); michael@0: if (!(imageFlags & Image::INIT_FLAG_DISCARDABLE) && michael@0: !(imageFlags & Image::INIT_FLAG_DECODE_ON_DRAW)) { michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /* static */ already_AddRefed michael@0: ImageFactory::CreateImage(nsIRequest* aRequest, michael@0: imgStatusTracker* aStatusTracker, michael@0: const nsCString& aMimeType, michael@0: ImageURL* aURI, michael@0: bool aIsMultiPart, michael@0: uint32_t aInnerWindowId) michael@0: { michael@0: MOZ_ASSERT(gInitializedPrefCaches, michael@0: "Pref observers should have been initialized already"); michael@0: michael@0: // Compute the image's initialization flags. michael@0: uint32_t imageFlags = ComputeImageFlags(aURI, aIsMultiPart); michael@0: michael@0: // Select the type of image to create based on MIME type. michael@0: if (aMimeType.EqualsLiteral(IMAGE_SVG_XML)) { michael@0: return CreateVectorImage(aRequest, aStatusTracker, aMimeType, michael@0: aURI, imageFlags, aInnerWindowId); michael@0: } else { michael@0: return CreateRasterImage(aRequest, aStatusTracker, aMimeType, michael@0: aURI, imageFlags, aInnerWindowId); michael@0: } michael@0: } michael@0: michael@0: // Marks an image as having an error before returning it. Used with macros like michael@0: // NS_ENSURE_SUCCESS, since we guarantee to always return an image even if an michael@0: // error occurs, but callers need to be able to tell that this happened. michael@0: template michael@0: static already_AddRefed michael@0: BadImage(nsRefPtr& image) michael@0: { michael@0: image->SetHasError(); michael@0: return image.forget(); michael@0: } michael@0: michael@0: /* static */ already_AddRefed michael@0: ImageFactory::CreateAnonymousImage(const nsCString& aMimeType) michael@0: { michael@0: nsresult rv; michael@0: michael@0: nsRefPtr newImage = new RasterImage(); michael@0: michael@0: rv = newImage->Init(aMimeType.get(), Image::INIT_FLAG_NONE); michael@0: NS_ENSURE_SUCCESS(rv, BadImage(newImage)); michael@0: michael@0: return newImage.forget(); michael@0: } michael@0: michael@0: int32_t michael@0: SaturateToInt32(int64_t val) michael@0: { michael@0: if (val > INT_MAX) michael@0: return INT_MAX; michael@0: if (val < INT_MIN) michael@0: return INT_MIN; michael@0: michael@0: return static_cast(val); michael@0: } michael@0: michael@0: uint32_t michael@0: GetContentSize(nsIRequest* aRequest) michael@0: { michael@0: nsCOMPtr channel(do_QueryInterface(aRequest)); michael@0: if (channel) { michael@0: int64_t size; michael@0: nsresult rv = channel->GetContentLength(&size); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: return std::max(SaturateToInt32(size), 0); michael@0: } michael@0: } michael@0: michael@0: // Use the file size as a size hint for file channels. michael@0: nsCOMPtr fileChannel(do_QueryInterface(aRequest)); michael@0: if (fileChannel) { michael@0: nsCOMPtr file; michael@0: nsresult rv = fileChannel->GetFile(getter_AddRefs(file)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: int64_t filesize; michael@0: rv = file->GetFileSize(&filesize); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: return std::max(SaturateToInt32(filesize), 0); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Fallback - neither http nor file. We'll use dynamic allocation. michael@0: return 0; michael@0: } michael@0: michael@0: /* static */ already_AddRefed michael@0: ImageFactory::CreateRasterImage(nsIRequest* aRequest, michael@0: imgStatusTracker* aStatusTracker, michael@0: const nsCString& aMimeType, michael@0: ImageURL* aURI, michael@0: uint32_t aImageFlags, michael@0: uint32_t aInnerWindowId) michael@0: { michael@0: nsresult rv; michael@0: michael@0: nsRefPtr newImage = new RasterImage(aStatusTracker, aURI); michael@0: michael@0: rv = newImage->Init(aMimeType.get(), aImageFlags); michael@0: NS_ENSURE_SUCCESS(rv, BadImage(newImage)); michael@0: michael@0: newImage->SetInnerWindowID(aInnerWindowId); michael@0: michael@0: uint32_t len = GetContentSize(aRequest); michael@0: michael@0: // Pass anything usable on so that the RasterImage can preallocate michael@0: // its source buffer. michael@0: if (len > 0) { michael@0: uint32_t sizeHint = std::min(len, 20000000); // Bound by something reasonable michael@0: rv = newImage->SetSourceSizeHint(sizeHint); michael@0: if (NS_FAILED(rv)) { michael@0: // Flush memory, try to get some back, and try again. michael@0: rv = nsMemory::HeapMinimize(true); michael@0: nsresult rv2 = newImage->SetSourceSizeHint(sizeHint); michael@0: // If we've still failed at this point, things are going downhill. michael@0: if (NS_FAILED(rv) || NS_FAILED(rv2)) { michael@0: NS_WARNING("About to hit OOM in imagelib!"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: nsAutoCString ref; michael@0: aURI->GetRef(ref); michael@0: mozilla::net::nsMediaFragmentURIParser parser(ref); michael@0: if (parser.HasResolution()) { michael@0: newImage->SetRequestedResolution(parser.GetResolution()); michael@0: } michael@0: michael@0: if (parser.HasSampleSize()) { michael@0: /* Get our principal */ michael@0: nsCOMPtr chan(do_QueryInterface(aRequest)); michael@0: nsCOMPtr principal; michael@0: if (chan) { michael@0: nsContentUtils::GetSecurityManager()->GetChannelPrincipal(chan, michael@0: getter_AddRefs(principal)); michael@0: } michael@0: michael@0: if ((principal && michael@0: principal->GetAppStatus() == nsIPrincipal::APP_STATUS_CERTIFIED) || michael@0: gEnableMozSampleSize) { michael@0: newImage->SetRequestedSampleSize(parser.GetSampleSize()); michael@0: } michael@0: } michael@0: michael@0: return newImage.forget(); michael@0: } michael@0: michael@0: /* static */ already_AddRefed michael@0: ImageFactory::CreateVectorImage(nsIRequest* aRequest, michael@0: imgStatusTracker* aStatusTracker, michael@0: const nsCString& aMimeType, michael@0: ImageURL* aURI, michael@0: uint32_t aImageFlags, michael@0: uint32_t aInnerWindowId) michael@0: { michael@0: nsresult rv; michael@0: michael@0: nsRefPtr newImage = new VectorImage(aStatusTracker, aURI); michael@0: michael@0: rv = newImage->Init(aMimeType.get(), aImageFlags); michael@0: NS_ENSURE_SUCCESS(rv, BadImage(newImage)); michael@0: michael@0: newImage->SetInnerWindowID(aInnerWindowId); michael@0: michael@0: rv = newImage->OnStartRequest(aRequest, nullptr); michael@0: NS_ENSURE_SUCCESS(rv, BadImage(newImage)); michael@0: michael@0: return newImage.forget(); michael@0: } michael@0: michael@0: } // namespace image michael@0: } // namespace mozilla