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