Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / embedding / components / appstartup / src / nsAppStartupNotifier.cpp
blob8fd85901d126656e137f6fc600df0b60a9a481e2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Ben Turner <mozilla@songbirdnest.com
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsCOMPtr.h"
40 #include "nsString.h"
41 #include "nsXPIDLString.h"
42 #include "nsIServiceManager.h"
43 #include "nsICategoryManager.h"
44 #include "nsXPCOM.h"
45 #include "nsISupportsPrimitives.h"
46 #include "nsAppStartupNotifier.h"
48 NS_IMPL_ISUPPORTS1(nsAppStartupNotifier, nsIObserver)
50 nsAppStartupNotifier::nsAppStartupNotifier()
54 nsAppStartupNotifier::~nsAppStartupNotifier()
58 NS_IMETHODIMP nsAppStartupNotifier::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
60 NS_ENSURE_ARG(aTopic);
61 nsresult rv;
63 // now initialize all startup listeners
64 nsCOMPtr<nsICategoryManager> categoryManager =
65 do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
66 NS_ENSURE_SUCCESS(rv, rv);
68 nsCOMPtr<nsISimpleEnumerator> enumerator;
69 rv = categoryManager->EnumerateCategory(aTopic,
70 getter_AddRefs(enumerator));
71 if (NS_FAILED(rv)) return rv;
73 nsCOMPtr<nsISupports> entry;
74 while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) {
75 nsCOMPtr<nsISupportsCString> category = do_QueryInterface(entry, &rv);
77 if (NS_SUCCEEDED(rv)) {
78 nsCAutoString categoryEntry;
79 rv = category->GetData(categoryEntry);
81 nsXPIDLCString contractId;
82 categoryManager->GetCategoryEntry(aTopic,
83 categoryEntry.get(),
84 getter_Copies(contractId));
86 if (NS_SUCCEEDED(rv)) {
88 // If we see the word "service," in the beginning
89 // of the contractId then we create it as a service
90 // if not we do a createInstance
92 nsCOMPtr<nsISupports> startupInstance;
93 if (Substring(contractId, 0, 8).EqualsLiteral("service,"))
94 startupInstance = do_GetService(contractId.get() + 8, &rv);
95 else
96 startupInstance = do_CreateInstance(contractId, &rv);
98 if (NS_SUCCEEDED(rv)) {
99 // Try to QI to nsIObserver
100 nsCOMPtr<nsIObserver> startupObserver =
101 do_QueryInterface(startupInstance, &rv);
102 if (NS_SUCCEEDED(rv)) {
103 rv = startupObserver->Observe(nsnull, aTopic, nsnull);
105 // mainly for debugging if you want to know if your observer worked.
106 NS_ASSERTION(NS_SUCCEEDED(rv), "Startup Observer failed!\n");
109 else {
110 #ifdef NS_DEBUG
111 nsCAutoString warnStr("Cannot create startup observer : ");
112 warnStr += contractId.get();
113 NS_WARNING(warnStr.get());
114 #endif
120 return NS_OK;