Bug 1890277: part 1) Add CSP parser tests for `require-trusted-types-for`. r=tschuster
[gecko.git] / image / build / nsImageModule.cpp
blobb3c70f9bd016d58d0fb28040fd3fed17a3b0a0e7
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 "nsImageModule.h"
9 #include "mozilla/ModuleUtils.h"
10 #include "mozilla/Preferences.h"
11 #include "mozilla/StaticPrefs_image.h"
13 #include "DecodePool.h"
14 #include "ImageFactory.h"
15 #include "nsICategoryManager.h"
16 #include "nsServiceManagerUtils.h"
17 #include "ShutdownTracker.h"
18 #include "SurfaceCache.h"
19 #include "imgLoader.h"
21 using namespace mozilla::image;
23 struct ImageEnablementCookie {
24 bool (*mIsEnabled)();
25 const nsLiteralCString mMimeType;
28 static void UpdateDocumentViewerRegistration(const char* aPref, void* aData) {
29 auto* cookie = static_cast<ImageEnablementCookie*>(aData);
31 nsCOMPtr<nsICategoryManager> catMan =
32 do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
33 if (!catMan) {
34 return;
37 static nsLiteralCString kCategory = "Gecko-Content-Viewers"_ns;
38 static nsLiteralCString kContractId =
39 "@mozilla.org/content/plugin/document-loader-factory;1"_ns;
41 if (cookie->mIsEnabled()) {
42 catMan->AddCategoryEntry(kCategory, cookie->mMimeType, kContractId,
43 false /* aPersist */, true /* aReplace */);
44 } else {
45 catMan->DeleteCategoryEntry(
46 kCategory, cookie->mMimeType, false /* aPersist */
51 static bool sInitialized = false;
52 nsresult mozilla::image::EnsureModuleInitialized() {
53 MOZ_ASSERT(NS_IsMainThread());
55 if (sInitialized) {
56 return NS_OK;
59 static ImageEnablementCookie kAVIFCookie = {
60 mozilla::StaticPrefs::image_avif_enabled, "image/avif"_ns};
61 static ImageEnablementCookie kJXLCookie = {
62 mozilla::StaticPrefs::image_jxl_enabled, "image/jxl"_ns};
63 Preferences::RegisterCallbackAndCall(UpdateDocumentViewerRegistration,
64 "image.avif.enabled", &kAVIFCookie);
65 Preferences::RegisterCallbackAndCall(UpdateDocumentViewerRegistration,
66 "image.jxl.enabled", &kJXLCookie);
68 mozilla::image::ShutdownTracker::Initialize();
69 mozilla::image::ImageFactory::Initialize();
70 mozilla::image::DecodePool::Initialize();
71 mozilla::image::SurfaceCache::Initialize();
72 imgLoader::GlobalInit();
73 sInitialized = true;
74 return NS_OK;
77 void mozilla::image::ShutdownModule() {
78 if (!sInitialized) {
79 return;
81 imgLoader::Shutdown();
82 mozilla::image::SurfaceCache::Shutdown();
83 sInitialized = false;