layout/base/nsStyleSheetService.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /* implementation of interface for managing user and user-agent style sheets */
michael@0 8
michael@0 9 #include "nsStyleSheetService.h"
michael@0 10 #include "nsIStyleSheet.h"
michael@0 11 #include "mozilla/MemoryReporting.h"
michael@0 12 #include "mozilla/unused.h"
michael@0 13 #include "mozilla/css/Loader.h"
michael@0 14 #include "mozilla/dom/ContentParent.h"
michael@0 15 #include "mozilla/ipc/URIUtils.h"
michael@0 16 #include "nsCSSStyleSheet.h"
michael@0 17 #include "nsIURI.h"
michael@0 18 #include "nsCOMPtr.h"
michael@0 19 #include "nsICategoryManager.h"
michael@0 20 #include "nsISupportsPrimitives.h"
michael@0 21 #include "nsNetUtil.h"
michael@0 22 #include "nsIObserverService.h"
michael@0 23 #include "nsLayoutStatics.h"
michael@0 24
michael@0 25 using namespace mozilla;
michael@0 26
michael@0 27 nsStyleSheetService *nsStyleSheetService::gInstance = nullptr;
michael@0 28
michael@0 29 nsStyleSheetService::nsStyleSheetService()
michael@0 30 {
michael@0 31 PR_STATIC_ASSERT(0 == AGENT_SHEET && 1 == USER_SHEET && 2 == AUTHOR_SHEET);
michael@0 32 NS_ASSERTION(!gInstance, "Someone is using CreateInstance instead of GetService");
michael@0 33 gInstance = this;
michael@0 34 nsLayoutStatics::AddRef();
michael@0 35 }
michael@0 36
michael@0 37 nsStyleSheetService::~nsStyleSheetService()
michael@0 38 {
michael@0 39 UnregisterWeakMemoryReporter(this);
michael@0 40
michael@0 41 gInstance = nullptr;
michael@0 42 nsLayoutStatics::Release();
michael@0 43 }
michael@0 44
michael@0 45 NS_IMPL_ISUPPORTS(
michael@0 46 nsStyleSheetService, nsIStyleSheetService, nsIMemoryReporter)
michael@0 47
michael@0 48 void
michael@0 49 nsStyleSheetService::RegisterFromEnumerator(nsICategoryManager *aManager,
michael@0 50 const char *aCategory,
michael@0 51 nsISimpleEnumerator *aEnumerator,
michael@0 52 uint32_t aSheetType)
michael@0 53 {
michael@0 54 if (!aEnumerator)
michael@0 55 return;
michael@0 56
michael@0 57 bool hasMore;
michael@0 58 while (NS_SUCCEEDED(aEnumerator->HasMoreElements(&hasMore)) && hasMore) {
michael@0 59 nsCOMPtr<nsISupports> element;
michael@0 60 if (NS_FAILED(aEnumerator->GetNext(getter_AddRefs(element))))
michael@0 61 break;
michael@0 62
michael@0 63 nsCOMPtr<nsISupportsCString> icStr = do_QueryInterface(element);
michael@0 64 NS_ASSERTION(icStr,
michael@0 65 "category manager entries must be nsISupportsCStrings");
michael@0 66
michael@0 67 nsAutoCString name;
michael@0 68 icStr->GetData(name);
michael@0 69
michael@0 70 nsXPIDLCString spec;
michael@0 71 aManager->GetCategoryEntry(aCategory, name.get(), getter_Copies(spec));
michael@0 72
michael@0 73 nsCOMPtr<nsIURI> uri;
michael@0 74 NS_NewURI(getter_AddRefs(uri), spec);
michael@0 75 if (uri)
michael@0 76 LoadAndRegisterSheetInternal(uri, aSheetType);
michael@0 77 }
michael@0 78 }
michael@0 79
michael@0 80 int32_t
michael@0 81 nsStyleSheetService::FindSheetByURI(const nsCOMArray<nsIStyleSheet> &sheets,
michael@0 82 nsIURI *sheetURI)
michael@0 83 {
michael@0 84 for (int32_t i = sheets.Count() - 1; i >= 0; i-- ) {
michael@0 85 bool bEqual;
michael@0 86 nsIURI* uri = sheets[i]->GetSheetURI();
michael@0 87 if (uri
michael@0 88 && NS_SUCCEEDED(uri->Equals(sheetURI, &bEqual))
michael@0 89 && bEqual) {
michael@0 90 return i;
michael@0 91 }
michael@0 92 }
michael@0 93
michael@0 94 return -1;
michael@0 95 }
michael@0 96
michael@0 97 nsresult
michael@0 98 nsStyleSheetService::Init()
michael@0 99 {
michael@0 100 // Child processes get their style sheets from the ContentParent.
michael@0 101 if (XRE_GetProcessType() == GeckoProcessType_Content) {
michael@0 102 return NS_OK;
michael@0 103 }
michael@0 104
michael@0 105 // Enumerate all of the style sheet URIs registered in the category
michael@0 106 // manager and load them.
michael@0 107
michael@0 108 nsCOMPtr<nsICategoryManager> catMan =
michael@0 109 do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
michael@0 110
michael@0 111 NS_ENSURE_TRUE(catMan, NS_ERROR_OUT_OF_MEMORY);
michael@0 112
michael@0 113 nsCOMPtr<nsISimpleEnumerator> sheets;
michael@0 114 catMan->EnumerateCategory("agent-style-sheets", getter_AddRefs(sheets));
michael@0 115 RegisterFromEnumerator(catMan, "agent-style-sheets", sheets, AGENT_SHEET);
michael@0 116
michael@0 117 catMan->EnumerateCategory("user-style-sheets", getter_AddRefs(sheets));
michael@0 118 RegisterFromEnumerator(catMan, "user-style-sheets", sheets, USER_SHEET);
michael@0 119
michael@0 120 catMan->EnumerateCategory("author-style-sheets", getter_AddRefs(sheets));
michael@0 121 RegisterFromEnumerator(catMan, "author-style-sheets", sheets, AUTHOR_SHEET);
michael@0 122
michael@0 123 RegisterWeakMemoryReporter(this);
michael@0 124
michael@0 125 return NS_OK;
michael@0 126 }
michael@0 127
michael@0 128 NS_IMETHODIMP
michael@0 129 nsStyleSheetService::LoadAndRegisterSheet(nsIURI *aSheetURI,
michael@0 130 uint32_t aSheetType)
michael@0 131 {
michael@0 132 nsresult rv = LoadAndRegisterSheetInternal(aSheetURI, aSheetType);
michael@0 133 if (NS_SUCCEEDED(rv)) {
michael@0 134 const char* message;
michael@0 135 switch (aSheetType) {
michael@0 136 case AGENT_SHEET:
michael@0 137 message = "agent-sheet-added";
michael@0 138 break;
michael@0 139 case USER_SHEET:
michael@0 140 message = "user-sheet-added";
michael@0 141 break;
michael@0 142 case AUTHOR_SHEET:
michael@0 143 message = "author-sheet-added";
michael@0 144 break;
michael@0 145 default:
michael@0 146 return NS_ERROR_INVALID_ARG;
michael@0 147 }
michael@0 148 nsCOMPtr<nsIObserverService> serv = services::GetObserverService();
michael@0 149 if (serv) {
michael@0 150 // We're guaranteed that the new sheet is the last sheet in
michael@0 151 // mSheets[aSheetType]
michael@0 152 const nsCOMArray<nsIStyleSheet> & sheets = mSheets[aSheetType];
michael@0 153 serv->NotifyObservers(sheets[sheets.Count() - 1], message, nullptr);
michael@0 154 }
michael@0 155
michael@0 156 if (XRE_GetProcessType() == GeckoProcessType_Default) {
michael@0 157 nsTArray<dom::ContentParent*> children;
michael@0 158 dom::ContentParent::GetAll(children);
michael@0 159
michael@0 160 if (children.IsEmpty()) {
michael@0 161 return rv;
michael@0 162 }
michael@0 163
michael@0 164 mozilla::ipc::URIParams uri;
michael@0 165 SerializeURI(aSheetURI, uri);
michael@0 166
michael@0 167 for (uint32_t i = 0; i < children.Length(); i++) {
michael@0 168 unused << children[i]->SendLoadAndRegisterSheet(uri, aSheetType);
michael@0 169 }
michael@0 170 }
michael@0 171 }
michael@0 172 return rv;
michael@0 173 }
michael@0 174
michael@0 175 nsresult
michael@0 176 nsStyleSheetService::LoadAndRegisterSheetInternal(nsIURI *aSheetURI,
michael@0 177 uint32_t aSheetType)
michael@0 178 {
michael@0 179 NS_ENSURE_ARG(aSheetType == AGENT_SHEET ||
michael@0 180 aSheetType == USER_SHEET ||
michael@0 181 aSheetType == AUTHOR_SHEET);
michael@0 182 NS_ENSURE_ARG_POINTER(aSheetURI);
michael@0 183
michael@0 184 nsRefPtr<css::Loader> loader = new css::Loader();
michael@0 185
michael@0 186 nsRefPtr<nsCSSStyleSheet> sheet;
michael@0 187 // Allow UA sheets, but not user sheets, to use unsafe rules
michael@0 188 nsresult rv = loader->LoadSheetSync(aSheetURI, aSheetType == AGENT_SHEET,
michael@0 189 true, getter_AddRefs(sheet));
michael@0 190 NS_ENSURE_SUCCESS(rv, rv);
michael@0 191
michael@0 192 if (!mSheets[aSheetType].AppendObject(sheet)) {
michael@0 193 rv = NS_ERROR_OUT_OF_MEMORY;
michael@0 194 }
michael@0 195
michael@0 196 return rv;
michael@0 197 }
michael@0 198
michael@0 199 NS_IMETHODIMP
michael@0 200 nsStyleSheetService::SheetRegistered(nsIURI *sheetURI,
michael@0 201 uint32_t aSheetType, bool *_retval)
michael@0 202 {
michael@0 203 NS_ENSURE_ARG(aSheetType == AGENT_SHEET ||
michael@0 204 aSheetType == USER_SHEET ||
michael@0 205 aSheetType == AUTHOR_SHEET);
michael@0 206 NS_ENSURE_ARG_POINTER(sheetURI);
michael@0 207 NS_PRECONDITION(_retval, "Null out param");
michael@0 208
michael@0 209 *_retval = (FindSheetByURI(mSheets[aSheetType], sheetURI) >= 0);
michael@0 210
michael@0 211 return NS_OK;
michael@0 212 }
michael@0 213
michael@0 214 NS_IMETHODIMP
michael@0 215 nsStyleSheetService::UnregisterSheet(nsIURI *aSheetURI, uint32_t aSheetType)
michael@0 216 {
michael@0 217 NS_ENSURE_ARG(aSheetType == AGENT_SHEET ||
michael@0 218 aSheetType == USER_SHEET ||
michael@0 219 aSheetType == AUTHOR_SHEET);
michael@0 220 NS_ENSURE_ARG_POINTER(aSheetURI);
michael@0 221
michael@0 222 int32_t foundIndex = FindSheetByURI(mSheets[aSheetType], aSheetURI);
michael@0 223 NS_ENSURE_TRUE(foundIndex >= 0, NS_ERROR_INVALID_ARG);
michael@0 224 nsCOMPtr<nsIStyleSheet> sheet = mSheets[aSheetType][foundIndex];
michael@0 225 mSheets[aSheetType].RemoveObjectAt(foundIndex);
michael@0 226
michael@0 227 const char* message;
michael@0 228 switch (aSheetType) {
michael@0 229 case AGENT_SHEET:
michael@0 230 message = "agent-sheet-removed";
michael@0 231 break;
michael@0 232 case USER_SHEET:
michael@0 233 message = "user-sheet-removed";
michael@0 234 break;
michael@0 235 case AUTHOR_SHEET:
michael@0 236 message = "author-sheet-removed";
michael@0 237 break;
michael@0 238 }
michael@0 239
michael@0 240 nsCOMPtr<nsIObserverService> serv = services::GetObserverService();
michael@0 241 if (serv)
michael@0 242 serv->NotifyObservers(sheet, message, nullptr);
michael@0 243
michael@0 244 if (XRE_GetProcessType() == GeckoProcessType_Default) {
michael@0 245 nsTArray<dom::ContentParent*> children;
michael@0 246 dom::ContentParent::GetAll(children);
michael@0 247
michael@0 248 if (children.IsEmpty()) {
michael@0 249 return NS_OK;
michael@0 250 }
michael@0 251
michael@0 252 mozilla::ipc::URIParams uri;
michael@0 253 SerializeURI(aSheetURI, uri);
michael@0 254
michael@0 255 for (uint32_t i = 0; i < children.Length(); i++) {
michael@0 256 unused << children[i]->SendUnregisterSheet(uri, aSheetType);
michael@0 257 }
michael@0 258 }
michael@0 259
michael@0 260 return NS_OK;
michael@0 261 }
michael@0 262
michael@0 263 //static
michael@0 264 nsStyleSheetService *
michael@0 265 nsStyleSheetService::GetInstance()
michael@0 266 {
michael@0 267 static bool first = true;
michael@0 268 if (first) {
michael@0 269 // make sure at first call that it's inited
michael@0 270 nsCOMPtr<nsIStyleSheetService> dummy =
michael@0 271 do_GetService(NS_STYLESHEETSERVICE_CONTRACTID);
michael@0 272 first = false;
michael@0 273 }
michael@0 274
michael@0 275 return gInstance;
michael@0 276 }
michael@0 277
michael@0 278 static size_t
michael@0 279 SizeOfElementIncludingThis(nsIStyleSheet* aElement,
michael@0 280 MallocSizeOf aMallocSizeOf, void *aData)
michael@0 281 {
michael@0 282 return aElement->SizeOfIncludingThis(aMallocSizeOf);
michael@0 283 }
michael@0 284
michael@0 285 MOZ_DEFINE_MALLOC_SIZE_OF(StyleSheetServiceMallocSizeOf)
michael@0 286
michael@0 287 NS_IMETHODIMP
michael@0 288 nsStyleSheetService::CollectReports(nsIHandleReportCallback* aHandleReport,
michael@0 289 nsISupports* aData)
michael@0 290 {
michael@0 291 return MOZ_COLLECT_REPORT(
michael@0 292 "explicit/layout/style-sheet-service", KIND_HEAP, UNITS_BYTES,
michael@0 293 SizeOfIncludingThis(StyleSheetServiceMallocSizeOf),
michael@0 294 "Memory used for style sheets held by the style sheet service.");
michael@0 295 }
michael@0 296
michael@0 297 size_t
michael@0 298 nsStyleSheetService::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
michael@0 299 {
michael@0 300 size_t n = aMallocSizeOf(this);
michael@0 301 n += mSheets[AGENT_SHEET].SizeOfExcludingThis(SizeOfElementIncludingThis,
michael@0 302 aMallocSizeOf);
michael@0 303 n += mSheets[USER_SHEET].SizeOfExcludingThis(SizeOfElementIncludingThis,
michael@0 304 aMallocSizeOf);
michael@0 305 n += mSheets[AUTHOR_SHEET].SizeOfExcludingThis(SizeOfElementIncludingThis,
michael@0 306 aMallocSizeOf);
michael@0 307 return n;
michael@0 308 }
michael@0 309
michael@0 310

mercurial