1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/. */
8 #include "nsIServiceManager.h"
9 #include "nsICategoryManager.h"
11 #include "nsISupportsPrimitives.h"
12 #include "nsAppStartupNotifier.h"
13 #include "nsISimpleEnumerator.h"
16 nsAppStartupNotifier::NotifyObservers(const char* aTopic
)
18 NS_ENSURE_ARG(aTopic
);
21 // now initialize all startup listeners
22 nsCOMPtr
<nsICategoryManager
> categoryManager
=
23 do_GetService(NS_CATEGORYMANAGER_CONTRACTID
, &rv
);
24 NS_ENSURE_SUCCESS(rv
, rv
);
26 nsDependentCString
topic(aTopic
);
28 nsCOMPtr
<nsISimpleEnumerator
> enumerator
;
29 rv
= categoryManager
->EnumerateCategory(topic
,
30 getter_AddRefs(enumerator
));
31 if (NS_FAILED(rv
)) return rv
;
33 nsCOMPtr
<nsISupports
> entry
;
34 while (NS_SUCCEEDED(enumerator
->GetNext(getter_AddRefs(entry
)))) {
35 nsCOMPtr
<nsISupportsCString
> category
= do_QueryInterface(entry
, &rv
);
37 if (NS_SUCCEEDED(rv
)) {
38 nsAutoCString categoryEntry
;
39 rv
= category
->GetData(categoryEntry
);
42 categoryManager
->GetCategoryEntry(topic
, categoryEntry
,
45 if (NS_SUCCEEDED(rv
)) {
47 // If we see the word "service," in the beginning
48 // of the contractId then we create it as a service
49 // if not we do a createInstance
51 nsCOMPtr
<nsISupports
> startupInstance
;
52 if (Substring(contractId
, 0, 8).EqualsLiteral("service,"))
53 startupInstance
= do_GetService(contractId
.get() + 8, &rv
);
55 startupInstance
= do_CreateInstance(contractId
.get(), &rv
);
57 if (NS_SUCCEEDED(rv
)) {
58 // Try to QI to nsIObserver
59 nsCOMPtr
<nsIObserver
> startupObserver
=
60 do_QueryInterface(startupInstance
, &rv
);
61 if (NS_SUCCEEDED(rv
)) {
62 rv
= startupObserver
->Observe(nullptr, aTopic
, nullptr);
64 // mainly for debugging if you want to know if your observer worked.
65 NS_ASSERTION(NS_SUCCEEDED(rv
), "Startup Observer failed!\n");
70 nsAutoCString
warnStr("Cannot create startup observer : ");
71 warnStr
+= contractId
.get();
72 NS_WARNING(warnStr
.get());