michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 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: #include "nsCOMPtr.h" michael@0: #include "nsString.h" michael@0: #include "nsXPIDLString.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsICategoryManager.h" michael@0: #include "nsXPCOM.h" michael@0: #include "nsISupportsPrimitives.h" michael@0: #include "nsAppStartupNotifier.h" michael@0: #include "nsISimpleEnumerator.h" michael@0: michael@0: NS_IMPL_ISUPPORTS(nsAppStartupNotifier, nsIObserver) michael@0: michael@0: nsAppStartupNotifier::nsAppStartupNotifier() michael@0: { michael@0: } michael@0: michael@0: nsAppStartupNotifier::~nsAppStartupNotifier() michael@0: { michael@0: } michael@0: michael@0: NS_IMETHODIMP nsAppStartupNotifier::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *someData) michael@0: { michael@0: NS_ENSURE_ARG(aTopic); michael@0: nsresult rv; michael@0: michael@0: // now initialize all startup listeners michael@0: nsCOMPtr categoryManager = michael@0: do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr enumerator; michael@0: rv = categoryManager->EnumerateCategory(aTopic, michael@0: getter_AddRefs(enumerator)); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsCOMPtr entry; michael@0: while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) { michael@0: nsCOMPtr category = do_QueryInterface(entry, &rv); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: nsAutoCString categoryEntry; michael@0: rv = category->GetData(categoryEntry); michael@0: michael@0: nsXPIDLCString contractId; michael@0: categoryManager->GetCategoryEntry(aTopic, michael@0: categoryEntry.get(), michael@0: getter_Copies(contractId)); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: michael@0: // If we see the word "service," in the beginning michael@0: // of the contractId then we create it as a service michael@0: // if not we do a createInstance michael@0: michael@0: nsCOMPtr startupInstance; michael@0: if (Substring(contractId, 0, 8).EqualsLiteral("service,")) michael@0: startupInstance = do_GetService(contractId.get() + 8, &rv); michael@0: else michael@0: startupInstance = do_CreateInstance(contractId, &rv); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // Try to QI to nsIObserver michael@0: nsCOMPtr startupObserver = michael@0: do_QueryInterface(startupInstance, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: rv = startupObserver->Observe(nullptr, aTopic, nullptr); michael@0: michael@0: // mainly for debugging if you want to know if your observer worked. michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "Startup Observer failed!\n"); michael@0: } michael@0: } michael@0: else { michael@0: #ifdef DEBUG michael@0: nsAutoCString warnStr("Cannot create startup observer : "); michael@0: warnStr += contractId.get(); michael@0: NS_WARNING(warnStr.get()); michael@0: #endif michael@0: } michael@0: michael@0: } michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: }