Bug 846687 - Set the transport as non-seekable if the server sends Accept-Ranges...
[gecko.git] / xpcom / components / nsCategoryManager.cpp
blob62e873c3828a56fcc4da6d33882af64a6328d67e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #define PL_ARENA_CONST_ALIGN_MASK 7
8 #include "nsICategoryManager.h"
9 #include "nsCategoryManager.h"
11 #include "plarena.h"
12 #include "prio.h"
13 #include "prprf.h"
14 #include "prlock.h"
15 #include "nsCOMPtr.h"
16 #include "nsTHashtable.h"
17 #include "nsClassHashtable.h"
18 #include "nsIFactory.h"
19 #include "nsIStringEnumerator.h"
20 #include "nsIMemoryReporter.h"
21 #include "nsSupportsPrimitives.h"
22 #include "nsComponentManagerUtils.h"
23 #include "nsServiceManagerUtils.h"
24 #include "nsIObserver.h"
25 #include "nsIObserverService.h"
26 #include "nsReadableUtils.h"
27 #include "nsCRT.h"
28 #include "nsQuickSort.h"
29 #include "nsEnumeratorUtils.h"
30 #include "nsThreadUtils.h"
31 #include "mozilla/Services.h"
33 #include "ManifestParser.h"
35 using namespace mozilla;
36 class nsIComponentLoaderManager;
39 CategoryDatabase
40 contains 0 or more 1-1 mappings of string to Category
41 each Category contains 0 or more 1-1 mappings of string keys to string values
43 In other words, the CategoryDatabase is a tree, whose root is a hashtable.
44 Internal nodes (or Categories) are hashtables. Leaf nodes are strings.
46 The leaf strings are allocated in an arena, because we assume they're not
47 going to change much ;)
50 #define NS_CATEGORYMANAGER_ARENA_SIZE (1024 * 8)
52 // pulled in from nsComponentManager.cpp
53 char* ArenaStrdup(const char* s, PLArenaPool* aArena);
56 // BaseStringEnumerator is subclassed by EntryEnumerator and
57 // CategoryEnumerator
59 class BaseStringEnumerator
60 : public nsISimpleEnumerator,
61 private nsIUTF8StringEnumerator
63 public:
64 NS_DECL_ISUPPORTS
65 NS_DECL_NSISIMPLEENUMERATOR
66 NS_DECL_NSIUTF8STRINGENUMERATOR
68 protected:
69 // Callback function for NS_QuickSort to sort mArray
70 static int SortCallback(const void *, const void *, void *);
72 BaseStringEnumerator()
73 : mArray(nullptr),
74 mCount(0),
75 mSimpleCurItem(0),
76 mStringCurItem(0) { }
78 // A virtual destructor is needed here because subclasses of
79 // BaseStringEnumerator do not implement their own Release() method.
81 virtual ~BaseStringEnumerator()
83 if (mArray)
84 delete[] mArray;
87 void Sort();
89 const char** mArray;
90 uint32_t mCount;
91 uint32_t mSimpleCurItem;
92 uint32_t mStringCurItem;
95 NS_IMPL_ISUPPORTS2(BaseStringEnumerator, nsISimpleEnumerator, nsIUTF8StringEnumerator)
97 NS_IMETHODIMP
98 BaseStringEnumerator::HasMoreElements(bool *_retval)
100 *_retval = (mSimpleCurItem < mCount);
102 return NS_OK;
105 NS_IMETHODIMP
106 BaseStringEnumerator::GetNext(nsISupports **_retval)
108 if (mSimpleCurItem >= mCount)
109 return NS_ERROR_FAILURE;
111 nsSupportsDependentCString* str =
112 new nsSupportsDependentCString(mArray[mSimpleCurItem++]);
113 if (!str)
114 return NS_ERROR_OUT_OF_MEMORY;
116 *_retval = str;
117 NS_ADDREF(*_retval);
118 return NS_OK;
121 NS_IMETHODIMP
122 BaseStringEnumerator::HasMore(bool *_retval)
124 *_retval = (mStringCurItem < mCount);
126 return NS_OK;
129 NS_IMETHODIMP
130 BaseStringEnumerator::GetNext(nsACString& _retval)
132 if (mStringCurItem >= mCount)
133 return NS_ERROR_FAILURE;
135 _retval = nsDependentCString(mArray[mStringCurItem++]);
136 return NS_OK;
140 BaseStringEnumerator::SortCallback(const void *e1, const void *e2,
141 void * /*unused*/)
143 char const *const *s1 = reinterpret_cast<char const *const *>(e1);
144 char const *const *s2 = reinterpret_cast<char const *const *>(e2);
146 return strcmp(*s1, *s2);
149 void
150 BaseStringEnumerator::Sort()
152 NS_QuickSort(mArray, mCount, sizeof(mArray[0]), SortCallback, nullptr);
156 // EntryEnumerator is the wrapper that allows nsICategoryManager::EnumerateCategory
158 class EntryEnumerator
159 : public BaseStringEnumerator
161 public:
162 static EntryEnumerator* Create(nsTHashtable<CategoryLeaf>& aTable);
164 private:
165 static PLDHashOperator
166 enumfunc_createenumerator(CategoryLeaf* aLeaf, void* userArg);
170 PLDHashOperator
171 EntryEnumerator::enumfunc_createenumerator(CategoryLeaf* aLeaf, void* userArg)
173 EntryEnumerator* mythis = static_cast<EntryEnumerator*>(userArg);
174 if (aLeaf->value)
175 mythis->mArray[mythis->mCount++] = aLeaf->GetKey();
177 return PL_DHASH_NEXT;
180 EntryEnumerator*
181 EntryEnumerator::Create(nsTHashtable<CategoryLeaf>& aTable)
183 EntryEnumerator* enumObj = new EntryEnumerator();
184 if (!enumObj)
185 return nullptr;
187 enumObj->mArray = new char const* [aTable.Count()];
188 if (!enumObj->mArray) {
189 delete enumObj;
190 return nullptr;
193 aTable.EnumerateEntries(enumfunc_createenumerator, enumObj);
195 enumObj->Sort();
197 return enumObj;
202 // CategoryNode implementations
205 CategoryNode*
206 CategoryNode::Create(PLArenaPool* aArena)
208 CategoryNode* node = new(aArena) CategoryNode();
209 if (!node)
210 return nullptr;
212 node->mTable.Init();
213 return node;
216 CategoryNode::~CategoryNode()
220 void*
221 CategoryNode::operator new(size_t aSize, PLArenaPool* aArena)
223 void* p;
224 PL_ARENA_ALLOCATE(p, aArena, aSize);
225 return p;
228 NS_METHOD
229 CategoryNode::GetLeaf(const char* aEntryName,
230 char** _retval)
232 MutexAutoLock lock(mLock);
233 nsresult rv = NS_ERROR_NOT_AVAILABLE;
234 CategoryLeaf* ent =
235 mTable.GetEntry(aEntryName);
237 if (ent && ent->value) {
238 *_retval = NS_strdup(ent->value);
239 if (*_retval)
240 rv = NS_OK;
243 return rv;
246 NS_METHOD
247 CategoryNode::AddLeaf(const char* aEntryName,
248 const char* aValue,
249 bool aReplace,
250 char** _retval,
251 PLArenaPool* aArena)
253 if (_retval)
254 *_retval = NULL;
256 MutexAutoLock lock(mLock);
257 CategoryLeaf* leaf =
258 mTable.GetEntry(aEntryName);
260 if (!leaf) {
261 const char* arenaEntryName = ArenaStrdup(aEntryName, aArena);
262 if (!arenaEntryName)
263 return NS_ERROR_OUT_OF_MEMORY;
265 leaf = mTable.PutEntry(arenaEntryName);
266 if (!leaf)
267 return NS_ERROR_OUT_OF_MEMORY;
270 if (leaf->value && !aReplace)
271 return NS_ERROR_INVALID_ARG;
273 const char* arenaValue = ArenaStrdup(aValue, aArena);
274 if (!arenaValue)
275 return NS_ERROR_OUT_OF_MEMORY;
277 if (_retval && leaf->value) {
278 *_retval = ToNewCString(nsDependentCString(leaf->value));
279 if (!*_retval)
280 return NS_ERROR_OUT_OF_MEMORY;
283 leaf->value = arenaValue;
284 return NS_OK;
287 void
288 CategoryNode::DeleteLeaf(const char* aEntryName)
290 // we don't throw any errors, because it normally doesn't matter
291 // and it makes JS a lot cleaner
292 MutexAutoLock lock(mLock);
294 // we can just remove the entire hash entry without introspection
295 mTable.RemoveEntry(aEntryName);
298 NS_METHOD
299 CategoryNode::Enumerate(nsISimpleEnumerator **_retval)
301 NS_ENSURE_ARG_POINTER(_retval);
303 MutexAutoLock lock(mLock);
304 EntryEnumerator* enumObj = EntryEnumerator::Create(mTable);
306 if (!enumObj)
307 return NS_ERROR_OUT_OF_MEMORY;
309 *_retval = enumObj;
310 NS_ADDREF(*_retval);
311 return NS_OK;
314 size_t
315 CategoryNode::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf)
317 // We don't measure the strings pointed to by the entries because the
318 // pointers are non-owning.
319 return mTable.SizeOfExcludingThis(nullptr, aMallocSizeOf);
322 struct persistent_userstruct {
323 PRFileDesc* fd;
324 const char* categoryName;
325 bool success;
328 PLDHashOperator
329 enumfunc_pentries(CategoryLeaf* aLeaf, void* userArg)
331 persistent_userstruct* args =
332 static_cast<persistent_userstruct*>(userArg);
334 PLDHashOperator status = PL_DHASH_NEXT;
336 if (aLeaf->value) {
337 if (PR_fprintf(args->fd,
338 "%s,%s,%s\n",
339 args->categoryName,
340 aLeaf->GetKey(),
341 aLeaf->value) == (uint32_t) -1) {
342 args->success = false;
343 status = PL_DHASH_STOP;
347 return status;
351 // CategoryEnumerator class
354 class CategoryEnumerator
355 : public BaseStringEnumerator
357 public:
358 static CategoryEnumerator* Create(nsClassHashtable<nsDepCharHashKey, CategoryNode>& aTable);
360 private:
361 static PLDHashOperator
362 enumfunc_createenumerator(const char* aStr,
363 CategoryNode* aNode,
364 void* userArg);
367 CategoryEnumerator*
368 CategoryEnumerator::Create(nsClassHashtable<nsDepCharHashKey, CategoryNode>& aTable)
370 CategoryEnumerator* enumObj = new CategoryEnumerator();
371 if (!enumObj)
372 return nullptr;
374 enumObj->mArray = new const char* [aTable.Count()];
375 if (!enumObj->mArray) {
376 delete enumObj;
377 return nullptr;
380 aTable.EnumerateRead(enumfunc_createenumerator, enumObj);
382 return enumObj;
385 PLDHashOperator
386 CategoryEnumerator::enumfunc_createenumerator(const char* aStr, CategoryNode* aNode, void* userArg)
388 CategoryEnumerator* mythis = static_cast<CategoryEnumerator*>(userArg);
390 // if a category has no entries, we pretend it doesn't exist
391 if (aNode->Count())
392 mythis->mArray[mythis->mCount++] = aStr;
394 return PL_DHASH_NEXT;
399 // nsCategoryManager implementations
402 NS_IMPL_QUERY_INTERFACE1(nsCategoryManager, nsICategoryManager)
404 NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(CategoryManagerMallocSizeOf)
406 NS_MEMORY_REPORTER_IMPLEMENT(CategoryManager,
407 "explicit/xpcom/category-manager",
408 KIND_HEAP,
409 nsIMemoryReporter::UNITS_BYTES,
410 nsCategoryManager::GetCategoryManagerSize,
411 "Memory used for the XPCOM category manager.")
413 NS_IMETHODIMP_(nsrefcnt)
414 nsCategoryManager::AddRef()
416 return 2;
419 NS_IMETHODIMP_(nsrefcnt)
420 nsCategoryManager::Release()
422 return 1;
425 nsCategoryManager* nsCategoryManager::gCategoryManager;
427 /* static */ nsCategoryManager*
428 nsCategoryManager::GetSingleton()
430 if (!gCategoryManager)
431 gCategoryManager = new nsCategoryManager();
432 return gCategoryManager;
435 /* static */ void
436 nsCategoryManager::Destroy()
438 delete gCategoryManager;
439 gCategoryManager = nullptr;
442 nsresult
443 nsCategoryManager::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
445 if (aOuter)
446 return NS_ERROR_NO_AGGREGATION;
448 return GetSingleton()->QueryInterface(aIID, aResult);
451 nsCategoryManager::nsCategoryManager()
452 : mLock("nsCategoryManager")
453 , mSuppressNotifications(false)
454 , mReporter(nullptr)
456 PL_INIT_ARENA_POOL(&mArena, "CategoryManagerArena",
457 NS_CATEGORYMANAGER_ARENA_SIZE);
459 mTable.Init();
462 void
463 nsCategoryManager::InitMemoryReporter()
465 mReporter = new NS_MEMORY_REPORTER_NAME(CategoryManager);
466 NS_RegisterMemoryReporter(mReporter);
469 nsCategoryManager::~nsCategoryManager()
471 (void)::NS_UnregisterMemoryReporter(mReporter);
472 mReporter = nullptr;
474 // the hashtable contains entries that must be deleted before the arena is
475 // destroyed, or else you will have PRLocks undestroyed and other Really
476 // Bad Stuff (TM)
477 mTable.Clear();
479 PL_FinishArenaPool(&mArena);
482 inline CategoryNode*
483 nsCategoryManager::get_category(const char* aName) {
484 CategoryNode* node;
485 if (!mTable.Get(aName, &node)) {
486 return nullptr;
488 return node;
491 /* static */ int64_t
492 nsCategoryManager::GetCategoryManagerSize()
494 MOZ_ASSERT(nsCategoryManager::gCategoryManager);
495 return nsCategoryManager::gCategoryManager->SizeOfIncludingThis(
496 CategoryManagerMallocSizeOf);
499 static size_t
500 SizeOfCategoryManagerTableEntryExcludingThis(nsDepCharHashKey::KeyType aKey,
501 const nsAutoPtr<CategoryNode> &aData,
502 nsMallocSizeOfFun aMallocSizeOf,
503 void* aUserArg)
505 // We don't measure the string pointed to by aKey because it's a non-owning
506 // pointer.
507 return aData.get()->SizeOfExcludingThis(aMallocSizeOf);
510 size_t
511 nsCategoryManager::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf)
513 size_t n = aMallocSizeOf(this);
515 n += PL_SizeOfArenaPoolExcludingPool(&mArena, aMallocSizeOf);
517 n += mTable.SizeOfExcludingThis(SizeOfCategoryManagerTableEntryExcludingThis,
518 aMallocSizeOf);
520 return n;
523 namespace {
525 class CategoryNotificationRunnable : public nsRunnable
527 public:
528 CategoryNotificationRunnable(nsISupports* aSubject,
529 const char* aTopic,
530 const char* aData)
531 : mSubject(aSubject)
532 , mTopic(aTopic)
533 , mData(aData)
536 NS_DECL_NSIRUNNABLE
538 private:
539 nsCOMPtr<nsISupports> mSubject;
540 const char* mTopic;
541 NS_ConvertUTF8toUTF16 mData;
544 NS_IMETHODIMP
545 CategoryNotificationRunnable::Run()
547 nsCOMPtr<nsIObserverService> observerService =
548 mozilla::services::GetObserverService();
549 if (observerService)
550 observerService->NotifyObservers(mSubject, mTopic, mData.get());
552 return NS_OK;
555 } // anonymous namespace
558 void
559 nsCategoryManager::NotifyObservers( const char *aTopic,
560 const char *aCategoryName,
561 const char *aEntryName )
563 if (mSuppressNotifications)
564 return;
566 nsRefPtr<CategoryNotificationRunnable> r;
568 if (aEntryName) {
569 nsCOMPtr<nsISupportsCString> entry
570 (do_CreateInstance (NS_SUPPORTS_CSTRING_CONTRACTID));
571 if (!entry)
572 return;
574 nsresult rv = entry->SetData(nsDependentCString(aEntryName));
575 if (NS_FAILED(rv))
576 return;
578 r = new CategoryNotificationRunnable(entry, aTopic, aCategoryName);
579 } else {
580 r = new CategoryNotificationRunnable(this, aTopic, aCategoryName);
583 NS_DispatchToMainThread(r);
586 NS_IMETHODIMP
587 nsCategoryManager::GetCategoryEntry( const char *aCategoryName,
588 const char *aEntryName,
589 char **_retval )
591 NS_ENSURE_ARG_POINTER(aCategoryName);
592 NS_ENSURE_ARG_POINTER(aEntryName);
593 NS_ENSURE_ARG_POINTER(_retval);
595 nsresult status = NS_ERROR_NOT_AVAILABLE;
597 CategoryNode* category;
599 MutexAutoLock lock(mLock);
600 category = get_category(aCategoryName);
603 if (category) {
604 status = category->GetLeaf(aEntryName, _retval);
607 return status;
610 NS_IMETHODIMP
611 nsCategoryManager::AddCategoryEntry( const char *aCategoryName,
612 const char *aEntryName,
613 const char *aValue,
614 bool aPersist,
615 bool aReplace,
616 char **_retval )
618 if (aPersist) {
619 NS_ERROR("Category manager doesn't support persistence.");
620 return NS_ERROR_INVALID_ARG;
623 AddCategoryEntry(aCategoryName, aEntryName, aValue, aReplace, _retval);
624 return NS_OK;
627 void
628 nsCategoryManager::AddCategoryEntry(const char *aCategoryName,
629 const char *aEntryName,
630 const char *aValue,
631 bool aReplace,
632 char** aOldValue)
634 if (aOldValue)
635 *aOldValue = NULL;
637 // Before we can insert a new entry, we'll need to
638 // find the |CategoryNode| to put it in...
639 CategoryNode* category;
641 MutexAutoLock lock(mLock);
642 category = get_category(aCategoryName);
644 if (!category) {
645 // That category doesn't exist yet; let's make it.
646 category = CategoryNode::Create(&mArena);
648 char* categoryName = ArenaStrdup(aCategoryName, &mArena);
649 mTable.Put(categoryName, category);
653 if (!category)
654 return;
656 // We will need the return value of AddLeaf even if the called doesn't want it
657 char *oldEntry = nullptr;
659 nsresult rv = category->AddLeaf(aEntryName,
660 aValue,
661 aReplace,
662 &oldEntry,
663 &mArena);
665 if (NS_SUCCEEDED(rv)) {
666 if (oldEntry) {
667 NotifyObservers(NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID,
668 aCategoryName, oldEntry);
670 NotifyObservers(NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID,
671 aCategoryName, aEntryName);
673 if (aOldValue)
674 *aOldValue = oldEntry;
675 else
676 NS_Free(oldEntry);
680 NS_IMETHODIMP
681 nsCategoryManager::DeleteCategoryEntry( const char *aCategoryName,
682 const char *aEntryName,
683 bool aDontPersist)
685 NS_ENSURE_ARG_POINTER(aCategoryName);
686 NS_ENSURE_ARG_POINTER(aEntryName);
689 Note: no errors are reported since failure to delete
690 probably won't hurt you, and returning errors seriously
691 inconveniences JS clients
694 CategoryNode* category;
696 MutexAutoLock lock(mLock);
697 category = get_category(aCategoryName);
700 if (category) {
701 category->DeleteLeaf(aEntryName);
703 NotifyObservers(NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID,
704 aCategoryName, aEntryName);
707 return NS_OK;
710 NS_IMETHODIMP
711 nsCategoryManager::DeleteCategory( const char *aCategoryName )
713 NS_ENSURE_ARG_POINTER(aCategoryName);
715 // the categories are arena-allocated, so we don't
716 // actually delete them. We just remove all of the
717 // leaf nodes.
719 CategoryNode* category;
721 MutexAutoLock lock(mLock);
722 category = get_category(aCategoryName);
725 if (category) {
726 category->Clear();
727 NotifyObservers(NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID,
728 aCategoryName, nullptr);
731 return NS_OK;
734 NS_IMETHODIMP
735 nsCategoryManager::EnumerateCategory( const char *aCategoryName,
736 nsISimpleEnumerator **_retval )
738 NS_ENSURE_ARG_POINTER(aCategoryName);
739 NS_ENSURE_ARG_POINTER(_retval);
741 CategoryNode* category;
743 MutexAutoLock lock(mLock);
744 category = get_category(aCategoryName);
747 if (!category) {
748 return NS_NewEmptyEnumerator(_retval);
751 return category->Enumerate(_retval);
754 NS_IMETHODIMP
755 nsCategoryManager::EnumerateCategories(nsISimpleEnumerator **_retval)
757 NS_ENSURE_ARG_POINTER(_retval);
759 MutexAutoLock lock(mLock);
760 CategoryEnumerator* enumObj = CategoryEnumerator::Create(mTable);
762 if (!enumObj)
763 return NS_ERROR_OUT_OF_MEMORY;
765 *_retval = enumObj;
766 NS_ADDREF(*_retval);
767 return NS_OK;
770 struct writecat_struct {
771 PRFileDesc* fd;
772 bool success;
775 NS_METHOD
776 nsCategoryManager::SuppressNotifications(bool aSuppress)
778 mSuppressNotifications = aSuppress;
779 return NS_OK;
783 * CreateServicesFromCategory()
785 * Given a category, this convenience functions enumerates the category and
786 * creates a service of every CID or ContractID registered under the category.
787 * If observerTopic is non null and the service implements nsIObserver,
788 * this will attempt to notify the observer with the origin, observerTopic string
789 * as parameter.
791 void
792 NS_CreateServicesFromCategory(const char *category,
793 nsISupports *origin,
794 const char *observerTopic)
796 nsresult rv;
798 nsCOMPtr<nsICategoryManager> categoryManager =
799 do_GetService("@mozilla.org/categorymanager;1");
800 if (!categoryManager)
801 return;
803 nsCOMPtr<nsISimpleEnumerator> enumerator;
804 rv = categoryManager->EnumerateCategory(category,
805 getter_AddRefs(enumerator));
806 if (NS_FAILED(rv))
807 return;
809 nsCOMPtr<nsIUTF8StringEnumerator> senumerator =
810 do_QueryInterface(enumerator);
811 if (!senumerator) {
812 NS_WARNING("Category enumerator doesn't support nsIUTF8StringEnumerator.");
813 return;
816 bool hasMore;
817 while (NS_SUCCEEDED(senumerator->HasMore(&hasMore)) && hasMore) {
818 // From here on just skip any error we get.
819 nsAutoCString entryString;
820 if (NS_FAILED(senumerator->GetNext(entryString)))
821 continue;
823 nsXPIDLCString contractID;
824 rv = categoryManager->GetCategoryEntry(category,entryString.get(),
825 getter_Copies(contractID));
826 if (NS_FAILED(rv))
827 continue;
829 nsCOMPtr<nsISupports> instance = do_GetService(contractID);
830 if (!instance) {
831 LogMessage("While creating services from category '%s', could not create service for entry '%s', contract ID '%s'",
832 category, entryString.get(), contractID.get());
833 continue;
836 if (observerTopic) {
837 // try an observer, if it implements it.
838 nsCOMPtr<nsIObserver> observer = do_QueryInterface(instance);
839 if (observer)
840 observer->Observe(origin, observerTopic, EmptyString().get());
841 else
842 LogMessage("While creating services from category '%s', service for entry '%s', contract ID '%s' does not implement nsIObserver.",
843 category, entryString.get(), contractID.get());