nsImageModule.cpp (2328B)
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 28 static void UpdateDocumentViewerRegistration(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; 52 nsresult mozilla::image::EnsureModuleInitialized() { 53 MOZ_ASSERT(NS_IsMainThread()); 54 55 if (sInitialized) { 56 return NS_OK; 57 } 58 59 static ImageEnablementCookie kJXLCookie = { 60 mozilla::StaticPrefs::image_jxl_enabled, "image/jxl"_ns}; 61 Preferences::RegisterCallbackAndCall(UpdateDocumentViewerRegistration, 62 "image.jxl.enabled", &kJXLCookie); 63 64 mozilla::image::ShutdownTracker::Initialize(); 65 mozilla::image::ImageFactory::Initialize(); 66 mozilla::image::DecodePool::Initialize(); 67 mozilla::image::SurfaceCache::Initialize(); 68 imgLoader::GlobalInit(); 69 sInitialized = true; 70 return NS_OK; 71 } 72 73 void mozilla::image::ShutdownModule() { 74 if (!sInitialized) { 75 return; 76 } 77 imgLoader::Shutdown(); 78 mozilla::image::SurfaceCache::Shutdown(); 79 sInitialized = false; 80 }