|
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/. */ |
|
5 |
|
6 #include "nsCOMPtr.h" |
|
7 #include "nsString.h" |
|
8 #include "nsXPIDLString.h" |
|
9 #include "nsIServiceManager.h" |
|
10 #include "nsICategoryManager.h" |
|
11 #include "nsXPCOM.h" |
|
12 #include "nsISupportsPrimitives.h" |
|
13 #include "nsAppStartupNotifier.h" |
|
14 #include "nsISimpleEnumerator.h" |
|
15 |
|
16 NS_IMPL_ISUPPORTS(nsAppStartupNotifier, nsIObserver) |
|
17 |
|
18 nsAppStartupNotifier::nsAppStartupNotifier() |
|
19 { |
|
20 } |
|
21 |
|
22 nsAppStartupNotifier::~nsAppStartupNotifier() |
|
23 { |
|
24 } |
|
25 |
|
26 NS_IMETHODIMP nsAppStartupNotifier::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *someData) |
|
27 { |
|
28 NS_ENSURE_ARG(aTopic); |
|
29 nsresult rv; |
|
30 |
|
31 // now initialize all startup listeners |
|
32 nsCOMPtr<nsICategoryManager> categoryManager = |
|
33 do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv); |
|
34 NS_ENSURE_SUCCESS(rv, rv); |
|
35 |
|
36 nsCOMPtr<nsISimpleEnumerator> enumerator; |
|
37 rv = categoryManager->EnumerateCategory(aTopic, |
|
38 getter_AddRefs(enumerator)); |
|
39 if (NS_FAILED(rv)) return rv; |
|
40 |
|
41 nsCOMPtr<nsISupports> entry; |
|
42 while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) { |
|
43 nsCOMPtr<nsISupportsCString> category = do_QueryInterface(entry, &rv); |
|
44 |
|
45 if (NS_SUCCEEDED(rv)) { |
|
46 nsAutoCString categoryEntry; |
|
47 rv = category->GetData(categoryEntry); |
|
48 |
|
49 nsXPIDLCString contractId; |
|
50 categoryManager->GetCategoryEntry(aTopic, |
|
51 categoryEntry.get(), |
|
52 getter_Copies(contractId)); |
|
53 |
|
54 if (NS_SUCCEEDED(rv)) { |
|
55 |
|
56 // If we see the word "service," in the beginning |
|
57 // of the contractId then we create it as a service |
|
58 // if not we do a createInstance |
|
59 |
|
60 nsCOMPtr<nsISupports> startupInstance; |
|
61 if (Substring(contractId, 0, 8).EqualsLiteral("service,")) |
|
62 startupInstance = do_GetService(contractId.get() + 8, &rv); |
|
63 else |
|
64 startupInstance = do_CreateInstance(contractId, &rv); |
|
65 |
|
66 if (NS_SUCCEEDED(rv)) { |
|
67 // Try to QI to nsIObserver |
|
68 nsCOMPtr<nsIObserver> startupObserver = |
|
69 do_QueryInterface(startupInstance, &rv); |
|
70 if (NS_SUCCEEDED(rv)) { |
|
71 rv = startupObserver->Observe(nullptr, aTopic, nullptr); |
|
72 |
|
73 // mainly for debugging if you want to know if your observer worked. |
|
74 NS_ASSERTION(NS_SUCCEEDED(rv), "Startup Observer failed!\n"); |
|
75 } |
|
76 } |
|
77 else { |
|
78 #ifdef DEBUG |
|
79 nsAutoCString warnStr("Cannot create startup observer : "); |
|
80 warnStr += contractId.get(); |
|
81 NS_WARNING(warnStr.get()); |
|
82 #endif |
|
83 } |
|
84 |
|
85 } |
|
86 } |
|
87 } |
|
88 |
|
89 return NS_OK; |
|
90 } |