embedding/components/appstartup/src/nsAppStartupNotifier.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/embedding/components/appstartup/src/nsAppStartupNotifier.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsCOMPtr.h"
    1.10 +#include "nsString.h"
    1.11 +#include "nsXPIDLString.h"
    1.12 +#include "nsIServiceManager.h"
    1.13 +#include "nsICategoryManager.h"
    1.14 +#include "nsXPCOM.h"
    1.15 +#include "nsISupportsPrimitives.h"
    1.16 +#include "nsAppStartupNotifier.h"
    1.17 +#include "nsISimpleEnumerator.h"
    1.18 +
    1.19 +NS_IMPL_ISUPPORTS(nsAppStartupNotifier, nsIObserver)
    1.20 +
    1.21 +nsAppStartupNotifier::nsAppStartupNotifier()
    1.22 +{
    1.23 +}
    1.24 +
    1.25 +nsAppStartupNotifier::~nsAppStartupNotifier()
    1.26 +{
    1.27 +}
    1.28 +
    1.29 +NS_IMETHODIMP nsAppStartupNotifier::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *someData)
    1.30 +{
    1.31 +    NS_ENSURE_ARG(aTopic);
    1.32 +    nsresult rv;
    1.33 +
    1.34 +    // now initialize all startup listeners
    1.35 +    nsCOMPtr<nsICategoryManager> categoryManager =
    1.36 +                    do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
    1.37 +    NS_ENSURE_SUCCESS(rv, rv);
    1.38 +
    1.39 +    nsCOMPtr<nsISimpleEnumerator> enumerator;
    1.40 +    rv = categoryManager->EnumerateCategory(aTopic,
    1.41 +                               getter_AddRefs(enumerator));
    1.42 +    if (NS_FAILED(rv)) return rv;
    1.43 +
    1.44 +    nsCOMPtr<nsISupports> entry;
    1.45 +    while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) {
    1.46 +        nsCOMPtr<nsISupportsCString> category = do_QueryInterface(entry, &rv);
    1.47 +
    1.48 +        if (NS_SUCCEEDED(rv)) {
    1.49 +            nsAutoCString categoryEntry;
    1.50 +            rv = category->GetData(categoryEntry);
    1.51 +
    1.52 +            nsXPIDLCString contractId;
    1.53 +            categoryManager->GetCategoryEntry(aTopic, 
    1.54 +                                              categoryEntry.get(),
    1.55 +                                              getter_Copies(contractId));
    1.56 +
    1.57 +            if (NS_SUCCEEDED(rv)) {
    1.58 +
    1.59 +                // If we see the word "service," in the beginning
    1.60 +                // of the contractId then we create it as a service
    1.61 +                // if not we do a createInstance
    1.62 +
    1.63 +                nsCOMPtr<nsISupports> startupInstance;
    1.64 +                if (Substring(contractId, 0, 8).EqualsLiteral("service,"))
    1.65 +                    startupInstance = do_GetService(contractId.get() + 8, &rv);
    1.66 +                else
    1.67 +                    startupInstance = do_CreateInstance(contractId, &rv);
    1.68 +
    1.69 +                if (NS_SUCCEEDED(rv)) {
    1.70 +                    // Try to QI to nsIObserver
    1.71 +                    nsCOMPtr<nsIObserver> startupObserver =
    1.72 +                        do_QueryInterface(startupInstance, &rv);
    1.73 +                    if (NS_SUCCEEDED(rv)) {
    1.74 +                        rv = startupObserver->Observe(nullptr, aTopic, nullptr);
    1.75 +     
    1.76 +                        // mainly for debugging if you want to know if your observer worked.
    1.77 +                        NS_ASSERTION(NS_SUCCEEDED(rv), "Startup Observer failed!\n");
    1.78 +                    }
    1.79 +                }
    1.80 +                else {
    1.81 +                  #ifdef DEBUG
    1.82 +                    nsAutoCString warnStr("Cannot create startup observer : ");
    1.83 +                    warnStr += contractId.get();
    1.84 +                    NS_WARNING(warnStr.get());
    1.85 +                  #endif
    1.86 +                }
    1.87 +
    1.88 +            }
    1.89 +        }
    1.90 +    }
    1.91 +
    1.92 +    return NS_OK;
    1.93 +}

mercurial