Coverage Report

Created: 2018-09-25 14:53

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