1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
6 
7 #include "nsImageModule.h"
8 
9 #include "mozilla/ModuleUtils.h"
10 #include "mozilla/Preferences.h"
11 #include "mozilla/StaticPrefs_image.h"
12 
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"
20 
21 using namespace mozilla::image;
22 
23 struct ImageEnablementCookie {
24   bool (*mIsEnabled)();
25   const nsLiteralCString mMimeType;
26 };
27 
UpdateContentViewerRegistration(const char * aPref,void * aData)28 static void UpdateContentViewerRegistration(const char* aPref, void* aData) {
29   auto* cookie = static_cast<ImageEnablementCookie*>(aData);
30 
31   nsCOMPtr<nsICategoryManager> catMan =
32       do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
33   if (!catMan) {
34     return;
35   }
36 
37   static nsLiteralCString kCategory = "Gecko-Content-Viewers"_ns;
38   static nsLiteralCString kContractId =
39       "@mozilla.org/content/plugin/document-loader-factory;1"_ns;
40 
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 */
47     );
48   }
49 }
50 
51 static bool sInitialized = false;
EnsureModuleInitialized()52 nsresult mozilla::image::EnsureModuleInitialized() {
53   MOZ_ASSERT(NS_IsMainThread());
54 
55   if (sInitialized) {
56     return NS_OK;
57   }
58 
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   static ImageEnablementCookie kWebPCookie = {
64       mozilla::StaticPrefs::image_webp_enabled, "image/webp"_ns};
65   Preferences::RegisterCallbackAndCall(UpdateContentViewerRegistration,
66                                        "image.avif.enabled", &kAVIFCookie);
67   Preferences::RegisterCallbackAndCall(UpdateContentViewerRegistration,
68                                        "image.jxl.enabled", &kJXLCookie);
69   Preferences::RegisterCallbackAndCall(UpdateContentViewerRegistration,
70                                        "image.webp.enabled", &kWebPCookie);
71 
72   mozilla::image::ShutdownTracker::Initialize();
73   mozilla::image::ImageFactory::Initialize();
74   mozilla::image::DecodePool::Initialize();
75   mozilla::image::SurfaceCache::Initialize();
76   imgLoader::GlobalInit();
77   sInitialized = true;
78   return NS_OK;
79 }
80 
ShutdownModule()81 void mozilla::image::ShutdownModule() {
82   if (!sInitialized) {
83     return;
84   }
85   imgLoader::Shutdown();
86   mozilla::image::SurfaceCache::Shutdown();
87   sInitialized = false;
88 }
89