michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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: /* implementation of interface for managing user and user-agent style sheets */ michael@0: michael@0: #include "nsStyleSheetService.h" michael@0: #include "nsIStyleSheet.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "mozilla/unused.h" michael@0: #include "mozilla/css/Loader.h" michael@0: #include "mozilla/dom/ContentParent.h" michael@0: #include "mozilla/ipc/URIUtils.h" michael@0: #include "nsCSSStyleSheet.h" michael@0: #include "nsIURI.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsICategoryManager.h" michael@0: #include "nsISupportsPrimitives.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIObserverService.h" michael@0: #include "nsLayoutStatics.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: nsStyleSheetService *nsStyleSheetService::gInstance = nullptr; michael@0: michael@0: nsStyleSheetService::nsStyleSheetService() michael@0: { michael@0: PR_STATIC_ASSERT(0 == AGENT_SHEET && 1 == USER_SHEET && 2 == AUTHOR_SHEET); michael@0: NS_ASSERTION(!gInstance, "Someone is using CreateInstance instead of GetService"); michael@0: gInstance = this; michael@0: nsLayoutStatics::AddRef(); michael@0: } michael@0: michael@0: nsStyleSheetService::~nsStyleSheetService() michael@0: { michael@0: UnregisterWeakMemoryReporter(this); michael@0: michael@0: gInstance = nullptr; michael@0: nsLayoutStatics::Release(); michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS( michael@0: nsStyleSheetService, nsIStyleSheetService, nsIMemoryReporter) michael@0: michael@0: void michael@0: nsStyleSheetService::RegisterFromEnumerator(nsICategoryManager *aManager, michael@0: const char *aCategory, michael@0: nsISimpleEnumerator *aEnumerator, michael@0: uint32_t aSheetType) michael@0: { michael@0: if (!aEnumerator) michael@0: return; michael@0: michael@0: bool hasMore; michael@0: while (NS_SUCCEEDED(aEnumerator->HasMoreElements(&hasMore)) && hasMore) { michael@0: nsCOMPtr element; michael@0: if (NS_FAILED(aEnumerator->GetNext(getter_AddRefs(element)))) michael@0: break; michael@0: michael@0: nsCOMPtr icStr = do_QueryInterface(element); michael@0: NS_ASSERTION(icStr, michael@0: "category manager entries must be nsISupportsCStrings"); michael@0: michael@0: nsAutoCString name; michael@0: icStr->GetData(name); michael@0: michael@0: nsXPIDLCString spec; michael@0: aManager->GetCategoryEntry(aCategory, name.get(), getter_Copies(spec)); michael@0: michael@0: nsCOMPtr uri; michael@0: NS_NewURI(getter_AddRefs(uri), spec); michael@0: if (uri) michael@0: LoadAndRegisterSheetInternal(uri, aSheetType); michael@0: } michael@0: } michael@0: michael@0: int32_t michael@0: nsStyleSheetService::FindSheetByURI(const nsCOMArray &sheets, michael@0: nsIURI *sheetURI) michael@0: { michael@0: for (int32_t i = sheets.Count() - 1; i >= 0; i-- ) { michael@0: bool bEqual; michael@0: nsIURI* uri = sheets[i]->GetSheetURI(); michael@0: if (uri michael@0: && NS_SUCCEEDED(uri->Equals(sheetURI, &bEqual)) michael@0: && bEqual) { michael@0: return i; michael@0: } michael@0: } michael@0: michael@0: return -1; michael@0: } michael@0: michael@0: nsresult michael@0: nsStyleSheetService::Init() michael@0: { michael@0: // Child processes get their style sheets from the ContentParent. michael@0: if (XRE_GetProcessType() == GeckoProcessType_Content) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Enumerate all of the style sheet URIs registered in the category michael@0: // manager and load them. michael@0: michael@0: nsCOMPtr catMan = michael@0: do_GetService(NS_CATEGORYMANAGER_CONTRACTID); michael@0: michael@0: NS_ENSURE_TRUE(catMan, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: nsCOMPtr sheets; michael@0: catMan->EnumerateCategory("agent-style-sheets", getter_AddRefs(sheets)); michael@0: RegisterFromEnumerator(catMan, "agent-style-sheets", sheets, AGENT_SHEET); michael@0: michael@0: catMan->EnumerateCategory("user-style-sheets", getter_AddRefs(sheets)); michael@0: RegisterFromEnumerator(catMan, "user-style-sheets", sheets, USER_SHEET); michael@0: michael@0: catMan->EnumerateCategory("author-style-sheets", getter_AddRefs(sheets)); michael@0: RegisterFromEnumerator(catMan, "author-style-sheets", sheets, AUTHOR_SHEET); michael@0: michael@0: RegisterWeakMemoryReporter(this); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsStyleSheetService::LoadAndRegisterSheet(nsIURI *aSheetURI, michael@0: uint32_t aSheetType) michael@0: { michael@0: nsresult rv = LoadAndRegisterSheetInternal(aSheetURI, aSheetType); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: const char* message; michael@0: switch (aSheetType) { michael@0: case AGENT_SHEET: michael@0: message = "agent-sheet-added"; michael@0: break; michael@0: case USER_SHEET: michael@0: message = "user-sheet-added"; michael@0: break; michael@0: case AUTHOR_SHEET: michael@0: message = "author-sheet-added"; michael@0: break; michael@0: default: michael@0: return NS_ERROR_INVALID_ARG; michael@0: } michael@0: nsCOMPtr serv = services::GetObserverService(); michael@0: if (serv) { michael@0: // We're guaranteed that the new sheet is the last sheet in michael@0: // mSheets[aSheetType] michael@0: const nsCOMArray & sheets = mSheets[aSheetType]; michael@0: serv->NotifyObservers(sheets[sheets.Count() - 1], message, nullptr); michael@0: } michael@0: michael@0: if (XRE_GetProcessType() == GeckoProcessType_Default) { michael@0: nsTArray children; michael@0: dom::ContentParent::GetAll(children); michael@0: michael@0: if (children.IsEmpty()) { michael@0: return rv; michael@0: } michael@0: michael@0: mozilla::ipc::URIParams uri; michael@0: SerializeURI(aSheetURI, uri); michael@0: michael@0: for (uint32_t i = 0; i < children.Length(); i++) { michael@0: unused << children[i]->SendLoadAndRegisterSheet(uri, aSheetType); michael@0: } michael@0: } michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: nsresult michael@0: nsStyleSheetService::LoadAndRegisterSheetInternal(nsIURI *aSheetURI, michael@0: uint32_t aSheetType) michael@0: { michael@0: NS_ENSURE_ARG(aSheetType == AGENT_SHEET || michael@0: aSheetType == USER_SHEET || michael@0: aSheetType == AUTHOR_SHEET); michael@0: NS_ENSURE_ARG_POINTER(aSheetURI); michael@0: michael@0: nsRefPtr loader = new css::Loader(); michael@0: michael@0: nsRefPtr sheet; michael@0: // Allow UA sheets, but not user sheets, to use unsafe rules michael@0: nsresult rv = loader->LoadSheetSync(aSheetURI, aSheetType == AGENT_SHEET, michael@0: true, getter_AddRefs(sheet)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (!mSheets[aSheetType].AppendObject(sheet)) { michael@0: rv = NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsStyleSheetService::SheetRegistered(nsIURI *sheetURI, michael@0: uint32_t aSheetType, bool *_retval) michael@0: { michael@0: NS_ENSURE_ARG(aSheetType == AGENT_SHEET || michael@0: aSheetType == USER_SHEET || michael@0: aSheetType == AUTHOR_SHEET); michael@0: NS_ENSURE_ARG_POINTER(sheetURI); michael@0: NS_PRECONDITION(_retval, "Null out param"); michael@0: michael@0: *_retval = (FindSheetByURI(mSheets[aSheetType], sheetURI) >= 0); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsStyleSheetService::UnregisterSheet(nsIURI *aSheetURI, uint32_t aSheetType) michael@0: { michael@0: NS_ENSURE_ARG(aSheetType == AGENT_SHEET || michael@0: aSheetType == USER_SHEET || michael@0: aSheetType == AUTHOR_SHEET); michael@0: NS_ENSURE_ARG_POINTER(aSheetURI); michael@0: michael@0: int32_t foundIndex = FindSheetByURI(mSheets[aSheetType], aSheetURI); michael@0: NS_ENSURE_TRUE(foundIndex >= 0, NS_ERROR_INVALID_ARG); michael@0: nsCOMPtr sheet = mSheets[aSheetType][foundIndex]; michael@0: mSheets[aSheetType].RemoveObjectAt(foundIndex); michael@0: michael@0: const char* message; michael@0: switch (aSheetType) { michael@0: case AGENT_SHEET: michael@0: message = "agent-sheet-removed"; michael@0: break; michael@0: case USER_SHEET: michael@0: message = "user-sheet-removed"; michael@0: break; michael@0: case AUTHOR_SHEET: michael@0: message = "author-sheet-removed"; michael@0: break; michael@0: } michael@0: michael@0: nsCOMPtr serv = services::GetObserverService(); michael@0: if (serv) michael@0: serv->NotifyObservers(sheet, message, nullptr); michael@0: michael@0: if (XRE_GetProcessType() == GeckoProcessType_Default) { michael@0: nsTArray children; michael@0: dom::ContentParent::GetAll(children); michael@0: michael@0: if (children.IsEmpty()) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: mozilla::ipc::URIParams uri; michael@0: SerializeURI(aSheetURI, uri); michael@0: michael@0: for (uint32_t i = 0; i < children.Length(); i++) { michael@0: unused << children[i]->SendUnregisterSheet(uri, aSheetType); michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: //static michael@0: nsStyleSheetService * michael@0: nsStyleSheetService::GetInstance() michael@0: { michael@0: static bool first = true; michael@0: if (first) { michael@0: // make sure at first call that it's inited michael@0: nsCOMPtr dummy = michael@0: do_GetService(NS_STYLESHEETSERVICE_CONTRACTID); michael@0: first = false; michael@0: } michael@0: michael@0: return gInstance; michael@0: } michael@0: michael@0: static size_t michael@0: SizeOfElementIncludingThis(nsIStyleSheet* aElement, michael@0: MallocSizeOf aMallocSizeOf, void *aData) michael@0: { michael@0: return aElement->SizeOfIncludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: MOZ_DEFINE_MALLOC_SIZE_OF(StyleSheetServiceMallocSizeOf) michael@0: michael@0: NS_IMETHODIMP michael@0: nsStyleSheetService::CollectReports(nsIHandleReportCallback* aHandleReport, michael@0: nsISupports* aData) michael@0: { michael@0: return MOZ_COLLECT_REPORT( michael@0: "explicit/layout/style-sheet-service", KIND_HEAP, UNITS_BYTES, michael@0: SizeOfIncludingThis(StyleSheetServiceMallocSizeOf), michael@0: "Memory used for style sheets held by the style sheet service."); michael@0: } michael@0: michael@0: size_t michael@0: nsStyleSheetService::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: size_t n = aMallocSizeOf(this); michael@0: n += mSheets[AGENT_SHEET].SizeOfExcludingThis(SizeOfElementIncludingThis, michael@0: aMallocSizeOf); michael@0: n += mSheets[USER_SHEET].SizeOfExcludingThis(SizeOfElementIncludingThis, michael@0: aMallocSizeOf); michael@0: n += mSheets[AUTHOR_SHEET].SizeOfExcludingThis(SizeOfElementIncludingThis, michael@0: aMallocSizeOf); michael@0: return n; michael@0: } michael@0: michael@0: