Files
palemoon27/layout/base/nsStyleSheetService.cpp
T
roytam1 672938ff15 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1253678 - Rename mozilla::Function to mozilla::function. r=froydnj (888553522d)
- Bug 1250788 - Part 2: Expose IsCSSSheetType method. r=bholley (7eb73ec696)
- Bug 1254943 - Set JS execution reason when invoking Promise callbacks. r=bz (212c135941)
- Bug 1175881 - about:sync-log can't read files on OS X with e10s on and content process sandbox enabled. r=areinald (52cf449634)
- Bug 1250788 - Part 3.1: Factor out CSSStyleSheetInner members so they can be used by ServoStyleSheet. r=bholley (4c50adcf26)
- Bug 1250788 - Part 3.2: Factor out CSSStyleSheet members so they can be used by ServoStyleSheet. r=bholley (7f09f8b56e)
- Bug 1250788 - Part 3.3: Implement enough of ServoStyleSheet for Loader to be able to create and parse one. r=bholley (7bb9e8bd47)
- Bug 1251496 - Hoist existing RefPtrTraits into a ServoBindingHelpers.h. r=heycam (c62ad77b46)
- Bug 1242690 - Ensure that mouse events have the callback transform applied. r=botond (296db0746b)
- Bug 1250788 - Part 1: Add interface to create and parse Servo style sheets. r=bholley (c50e5ada02)
- Bug 1250788 - Part 4: Implement enough of ServoStyleSet to handle the UA style sheets being added to it. r=bholley (d0ac9aa262)
- Bug 1250788 - Part 5: Create and parse ServoStyleSheets in css::Loader. r=bholley (25f6b1f8b9)
- Bug 1250788 - Part 6: Create ServoStyleSheets for the pref style sheet. r=bholley (275eb39243)
- Bug 1250788 - Unified build fix. (e52a27ef8d)
- Bug 1250379 - Create css::Loaders for specific style backend types. r=dholbert (a85f56226a)
- Bug 1256500 - Root style contexts before calling ApplyStyleFixups. r=heycam (1b47d3401c)
- Bug 1251496 - Introduce a servo-side data structure to represent the style set. r=heycam (956bd63b56)
- Bug 1252611 - Hoist mDocument into StyleSheet. r=dholbert (993664de79)
- Bug 1252611 - Add a method to get the StyleSheetInfo for a given StyleSheet. r=dholbert (59578e1bfc)
- Bug 1252611 - Hoist IsComplete/SetComplete into StyleSheet. r=dholbert (7e88b78d11)
- Preemptive unified build fix; no bug (100762c5c9)
- Bug 1239945 part 0 - Fix unified build (425e1109bf)
- Bug 1239945 part 1 - Drop a number of animation manager/collection-related methods that are unused (and in some cases undefined); r=dholbert (4d52514fe5)
- Bug 1239945 part 2 - Drop CommonAnimationManager::ContentOrAncestorHasAnimation; r=dholbert (ba0a919dcb)
- Bug 1239945 part 3 - Move AnimationCollection to a separate file; r=dholbert (b6af8d9ff3)
- Bug 1239945 part 4 - Remove the pointer from an AnimationCollection to its manager since it is no longer used; r=dholbert (875581afd5)
- Bug 1239945 part 5 - Use MayHaveAnimations() to return early in CommonAnimationManager::GetAnimationCollection; r=dholbert (aa4e1988e1)
- Bug 1242872 - Part 1: Introduce CSSAnimationBuilder to factor a bunch of stuff in BuildAnimations and CheckAnimationRule out. r=dbaron (25c1002b42)
- Bug 1242872 - Part 2: Set timeline in CSSAnimationBuilder::Build. r=dbaron (fd7461c6cb)
- Bug 1242872 - Part 3: Factor finding old animations process out. r=dbaron (901e5dadae)
- Bug 1242872 - Part 4: Factor updating animation properties process out. r=dbaron (deba9bbc34)
- Bug 1242872 - Part 5: Change CopyPropertiesFrom to UpdateProperties. r=birtles (26cfdf190b)
- Bug 1242872 - Part 6: Trigger a layer update explicitly when copying animation properties. r=birtles (8150212286)
- Bug 1242872 - Part 7: Eliminate creation of temporary animations. r=dbaron (24e0a5b79d)
- Bug 1242872 - Part 8: ElementPropertyTransition::ToValue(). r=birtles (2f21ea5887)
- Bug 1242872 - Part 1.1: Move AnimationManager::BuildAnimations. r=dbaron (6c51d5409d)
- Bug 1242872 - Part 9: Should not assume any order of animations in MurationObserver. r=pbrosset (910c5c0519)
- Bug 1239945 part 6 - Templatize AnimationCollection based on the concrete type of Animation stored; r=dholbert (bb37958019)
- Bug 1239945 part 7 - Move GetAnimationCollection to AnimationCollection; r=dholbert (e254079b58)
- Bug 1239945 part 8 - Add GetOrCreateAnimationCollection; r=dholbert (4e59c24fc1)
- Bug 1244641 - Part 1: Let AnimationEffectTiming have an effect. r=hiro (f2c4e43e80)
- Bug 1244641 - Part 2: Add duration implementation in dom/animation/AnimationEffectTiming.cpp. r=hiro, r=smaug (05819e6b8f)
- Bug 1244641 - Part 3: Notify animation mutation observers from AnimationEffectTiming::SetDuration. r=hiro (12ba42404a)
- Bug 1244080 - Part 0: Remove unnecessary argument for remove(). r=bbirtles (4753fd443d)
- Bug 1244080 - Part 1: Test that setting duration in the middle of the animation causes only once a restyling. r=bbirtles (64d568f0b2)
- Bug 1241692 - Part 1: Ensure that element which is added in each test is removed before subsequent test is processed. r=dholbert (890cce3bb3)
- Bug 1241692 - Part 2: We should observe stylings only for animations. r=dholbert (24e9497037)
- Bug 1218646 - Skip to check whether property can run on compositor or not if mWinsInCascade is false. r=birtles (f59232736b)
- Bug 1244641 - Part 4: Add duration tests in dom/animation/test/chrome. r=hiro (53fd5bdaf3)
- Bug 1244641 - Part 5: Add duration tests in layout/style/test. r=hiro (a5fd00fe51)
- Bug 1244641 - Part 6: Add duration tests in testing/web-platform/tests/web-animations. r=hiro (9ba57998ce)
- Bug 1257422 part 1. Add a way to do IDBObjectStore::OpenCursor without a JSContext if we are not passing a keyrange. r=khuey (a281fffd31)
- Bug 1257422 part 2. Use the overload of IDBRequest::GetResult that doesn't ask for a JSContext in FirstRevisionIdCallback::HandleEvent, since there is no reason not to. r=khuey (1d62d3da69)
2024-02-02 11:08:44 +08:00

382 lines
11 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* implementation of interface for managing user and user-agent style sheets */
#include "nsStyleSheetService.h"
#include "mozilla/CSSStyleSheet.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/StyleSheetHandle.h"
#include "mozilla/StyleSheetHandleInlines.h"
#include "mozilla/unused.h"
#include "mozilla/css/Loader.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/ipc/URIUtils.h"
#include "nsIURI.h"
#include "nsCOMPtr.h"
#include "nsICategoryManager.h"
#include "nsISupportsPrimitives.h"
#include "nsISimpleEnumerator.h"
#include "nsNetUtil.h"
#include "nsIObserverService.h"
#include "nsLayoutStatics.h"
using namespace mozilla;
nsStyleSheetService *nsStyleSheetService::gInstance = nullptr;
nsStyleSheetService::nsStyleSheetService()
{
PR_STATIC_ASSERT(0 == AGENT_SHEET && 1 == USER_SHEET && 2 == AUTHOR_SHEET);
NS_ASSERTION(!gInstance, "Someone is using CreateInstance instead of GetService");
gInstance = this;
nsLayoutStatics::AddRef();
}
nsStyleSheetService::~nsStyleSheetService()
{
UnregisterWeakMemoryReporter(this);
gInstance = nullptr;
nsLayoutStatics::Release();
}
NS_IMPL_ISUPPORTS(
nsStyleSheetService, nsIStyleSheetService, nsIMemoryReporter)
void
nsStyleSheetService::RegisterFromEnumerator(nsICategoryManager *aManager,
const char *aCategory,
nsISimpleEnumerator *aEnumerator,
uint32_t aSheetType)
{
if (!aEnumerator)
return;
bool hasMore;
while (NS_SUCCEEDED(aEnumerator->HasMoreElements(&hasMore)) && hasMore) {
nsCOMPtr<nsISupports> element;
if (NS_FAILED(aEnumerator->GetNext(getter_AddRefs(element))))
break;
nsCOMPtr<nsISupportsCString> icStr = do_QueryInterface(element);
NS_ASSERTION(icStr,
"category manager entries must be nsISupportsCStrings");
nsAutoCString name;
icStr->GetData(name);
nsXPIDLCString spec;
aManager->GetCategoryEntry(aCategory, name.get(), getter_Copies(spec));
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), spec);
if (uri)
LoadAndRegisterSheetInternal(uri, aSheetType);
}
}
int32_t
nsStyleSheetService::FindSheetByURI(const nsTArray<StyleSheetHandle::RefPtr>& aSheets,
nsIURI* aSheetURI)
{
for (int32_t i = aSheets.Length() - 1; i >= 0; i-- ) {
bool bEqual;
nsIURI* uri = aSheets[i]->GetSheetURI();
if (uri
&& NS_SUCCEEDED(uri->Equals(aSheetURI, &bEqual))
&& bEqual) {
return i;
}
}
return -1;
}
nsresult
nsStyleSheetService::Init()
{
// If you make changes here, consider whether
// SVGDocument::EnsureNonSVGUserAgentStyleSheetsLoaded should be updated too.
// Child processes get their style sheets from the ContentParent.
if (XRE_IsContentProcess()) {
return NS_OK;
}
// Enumerate all of the style sheet URIs registered in the category
// manager and load them.
nsCOMPtr<nsICategoryManager> catMan =
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
NS_ENSURE_TRUE(catMan, NS_ERROR_OUT_OF_MEMORY);
nsCOMPtr<nsISimpleEnumerator> sheets;
catMan->EnumerateCategory("agent-style-sheets", getter_AddRefs(sheets));
RegisterFromEnumerator(catMan, "agent-style-sheets", sheets, AGENT_SHEET);
catMan->EnumerateCategory("user-style-sheets", getter_AddRefs(sheets));
RegisterFromEnumerator(catMan, "user-style-sheets", sheets, USER_SHEET);
catMan->EnumerateCategory("author-style-sheets", getter_AddRefs(sheets));
RegisterFromEnumerator(catMan, "author-style-sheets", sheets, AUTHOR_SHEET);
RegisterWeakMemoryReporter(this);
return NS_OK;
}
NS_IMETHODIMP
nsStyleSheetService::LoadAndRegisterSheet(nsIURI *aSheetURI,
uint32_t aSheetType)
{
nsresult rv = LoadAndRegisterSheetInternal(aSheetURI, aSheetType);
if (NS_SUCCEEDED(rv)) {
const char* message;
switch (aSheetType) {
case AGENT_SHEET:
message = "agent-sheet-added";
break;
case USER_SHEET:
message = "user-sheet-added";
break;
case AUTHOR_SHEET:
message = "author-sheet-added";
break;
default:
return NS_ERROR_INVALID_ARG;
}
nsCOMPtr<nsIObserverService> serv = services::GetObserverService();
if (serv) {
// We're guaranteed that the new sheet is the last sheet in
// mSheets[aSheetType]
// XXXheycam Once the nsStyleSheetService can hold ServoStyleSheets too,
// we'll need to include them in the notification.
StyleSheetHandle sheet = mSheets[aSheetType].LastElement();
if (sheet->IsGecko()) {
CSSStyleSheet* cssSheet = sheet->AsGecko();
serv->NotifyObservers(NS_ISUPPORTS_CAST(nsIDOMCSSStyleSheet*, cssSheet),
message, nullptr);
} else {
NS_ERROR("stylo: can't notify observers of ServoStyleSheets");
}
}
if (XRE_IsParentProcess()) {
nsTArray<dom::ContentParent*> children;
dom::ContentParent::GetAll(children);
if (children.IsEmpty()) {
return rv;
}
mozilla::ipc::URIParams uri;
SerializeURI(aSheetURI, uri);
for (uint32_t i = 0; i < children.Length(); i++) {
Unused << children[i]->SendLoadAndRegisterSheet(uri, aSheetType);
}
}
}
return rv;
}
nsresult
nsStyleSheetService::LoadAndRegisterSheetInternal(nsIURI *aSheetURI,
uint32_t aSheetType)
{
NS_ENSURE_ARG_POINTER(aSheetURI);
css::SheetParsingMode parsingMode;
switch (aSheetType) {
case AGENT_SHEET:
parsingMode = css::eAgentSheetFeatures;
break;
case USER_SHEET:
parsingMode = css::eUserSheetFeatures;
break;
case AUTHOR_SHEET:
parsingMode = css::eAuthorSheetFeatures;
break;
default:
NS_WARNING("invalid sheet type argument");
return NS_ERROR_INVALID_ARG;
}
// XXXheycam We'll need to load and register both a Gecko- and Servo-backed
// style sheet.
RefPtr<css::Loader> loader = new css::Loader(StyleBackendType::Gecko);
StyleSheetHandle::RefPtr sheet;
nsresult rv = loader->LoadSheetSync(aSheetURI, parsingMode, true, &sheet);
NS_ENSURE_SUCCESS(rv, rv);
mSheets[aSheetType].AppendElement(sheet);
return rv;
}
NS_IMETHODIMP
nsStyleSheetService::SheetRegistered(nsIURI *sheetURI,
uint32_t aSheetType, bool *_retval)
{
NS_ENSURE_ARG(aSheetType == AGENT_SHEET ||
aSheetType == USER_SHEET ||
aSheetType == AUTHOR_SHEET);
NS_ENSURE_ARG_POINTER(sheetURI);
NS_PRECONDITION(_retval, "Null out param");
*_retval = (FindSheetByURI(mSheets[aSheetType], sheetURI) >= 0);
return NS_OK;
}
NS_IMETHODIMP
nsStyleSheetService::PreloadSheet(nsIURI *aSheetURI, uint32_t aSheetType,
nsIDOMStyleSheet **aSheet)
{
NS_PRECONDITION(aSheet, "Null out param");
NS_ENSURE_ARG_POINTER(aSheetURI);
css::SheetParsingMode parsingMode;
switch (aSheetType) {
case AGENT_SHEET:
parsingMode = css::eAgentSheetFeatures;
break;
case USER_SHEET:
parsingMode = css::eUserSheetFeatures;
break;
case AUTHOR_SHEET:
parsingMode = css::eAuthorSheetFeatures;
break;
default:
NS_WARNING("invalid sheet type argument");
return NS_ERROR_INVALID_ARG;
}
// XXXheycam PreloadSheet can't support ServoStyleSheets until they implement
// nsIDOMStyleSheet.
RefPtr<css::Loader> loader = new css::Loader(StyleBackendType::Gecko);
StyleSheetHandle::RefPtr sheet;
nsresult rv = loader->LoadSheetSync(aSheetURI, parsingMode, true, &sheet);
NS_ENSURE_SUCCESS(rv, rv);
MOZ_ASSERT(sheet->IsGecko(),
"stylo: didn't expect Loader to create a ServoStyleSheet");
RefPtr<CSSStyleSheet> cssSheet = sheet->AsGecko();
cssSheet.forget(aSheet);
return NS_OK;
}
NS_IMETHODIMP
nsStyleSheetService::UnregisterSheet(nsIURI *aSheetURI, uint32_t aSheetType)
{
NS_ENSURE_ARG(aSheetType == AGENT_SHEET ||
aSheetType == USER_SHEET ||
aSheetType == AUTHOR_SHEET);
NS_ENSURE_ARG_POINTER(aSheetURI);
int32_t foundIndex = FindSheetByURI(mSheets[aSheetType], aSheetURI);
NS_ENSURE_TRUE(foundIndex >= 0, NS_ERROR_INVALID_ARG);
StyleSheetHandle::RefPtr sheet = mSheets[aSheetType][foundIndex];
mSheets[aSheetType].RemoveElementAt(foundIndex);
const char* message;
switch (aSheetType) {
case AGENT_SHEET:
message = "agent-sheet-removed";
break;
case USER_SHEET:
message = "user-sheet-removed";
break;
case AUTHOR_SHEET:
message = "author-sheet-removed";
break;
}
nsCOMPtr<nsIObserverService> serv = services::GetObserverService();
if (serv) {
// XXXheycam Once the nsStyleSheetService can hold ServoStyleSheets too,
// we'll need to include them in the notification.
if (sheet->IsGecko()) {
CSSStyleSheet* cssSheet = sheet->AsGecko();
serv->NotifyObservers(NS_ISUPPORTS_CAST(nsIDOMCSSStyleSheet*, cssSheet),
message, nullptr);
} else {
NS_ERROR("stylo: can't notify observers of ServoStyleSheets");
}
}
if (XRE_IsParentProcess()) {
nsTArray<dom::ContentParent*> children;
dom::ContentParent::GetAll(children);
if (children.IsEmpty()) {
return NS_OK;
}
mozilla::ipc::URIParams uri;
SerializeURI(aSheetURI, uri);
for (uint32_t i = 0; i < children.Length(); i++) {
Unused << children[i]->SendUnregisterSheet(uri, aSheetType);
}
}
return NS_OK;
}
//static
nsStyleSheetService *
nsStyleSheetService::GetInstance()
{
static bool first = true;
if (first) {
// make sure at first call that it's inited
nsCOMPtr<nsIStyleSheetService> dummy =
do_GetService(NS_STYLESHEETSERVICE_CONTRACTID);
first = false;
}
return gInstance;
}
MOZ_DEFINE_MALLOC_SIZE_OF(StyleSheetServiceMallocSizeOf)
NS_IMETHODIMP
nsStyleSheetService::CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData, bool aAnonymize)
{
return MOZ_COLLECT_REPORT(
"explicit/layout/style-sheet-service", KIND_HEAP, UNITS_BYTES,
SizeOfIncludingThis(StyleSheetServiceMallocSizeOf),
"Memory used for style sheets held by the style sheet service.");
}
size_t
nsStyleSheetService::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
{
size_t n = aMallocSizeOf(this);
for (auto& sheetArray : mSheets) {
n += sheetArray.ShallowSizeOfExcludingThis(aMallocSizeOf);
for (StyleSheetHandle sheet : sheetArray) {
n += sheet->SizeOfIncludingThis(aMallocSizeOf);
}
}
return n;
}