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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : * Ben Turner <mozilla@songbirdnest.com
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
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"
47 :
48 : #include "mozilla/FunctionTimer.h"
49 :
50 0 : NS_IMPL_ISUPPORTS1(nsAppStartupNotifier, nsIObserver)
51 :
52 0 : nsAppStartupNotifier::nsAppStartupNotifier()
53 : {
54 0 : }
55 :
56 0 : nsAppStartupNotifier::~nsAppStartupNotifier()
57 : {
58 0 : }
59 :
60 0 : NS_IMETHODIMP nsAppStartupNotifier::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
61 : {
62 : NS_TIME_FUNCTION;
63 :
64 0 : NS_ENSURE_ARG(aTopic);
65 : nsresult rv;
66 :
67 : // now initialize all startup listeners
68 : nsCOMPtr<nsICategoryManager> categoryManager =
69 0 : do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
70 0 : NS_ENSURE_SUCCESS(rv, rv);
71 :
72 0 : nsCOMPtr<nsISimpleEnumerator> enumerator;
73 0 : rv = categoryManager->EnumerateCategory(aTopic,
74 0 : getter_AddRefs(enumerator));
75 0 : if (NS_FAILED(rv)) return rv;
76 :
77 : NS_TIME_FUNCTION_MARK("EnumerateCategory");
78 :
79 0 : nsCOMPtr<nsISupports> entry;
80 0 : while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) {
81 0 : nsCOMPtr<nsISupportsCString> category = do_QueryInterface(entry, &rv);
82 :
83 0 : if (NS_SUCCEEDED(rv)) {
84 0 : nsCAutoString categoryEntry;
85 0 : rv = category->GetData(categoryEntry);
86 :
87 0 : nsXPIDLCString contractId;
88 0 : categoryManager->GetCategoryEntry(aTopic,
89 : categoryEntry.get(),
90 0 : getter_Copies(contractId));
91 :
92 0 : if (NS_SUCCEEDED(rv)) {
93 :
94 : // If we see the word "service," in the beginning
95 : // of the contractId then we create it as a service
96 : // if not we do a createInstance
97 :
98 0 : nsCOMPtr<nsISupports> startupInstance;
99 0 : if (Substring(contractId, 0, 8).EqualsLiteral("service,"))
100 0 : startupInstance = do_GetService(contractId.get() + 8, &rv);
101 : else
102 0 : startupInstance = do_CreateInstance(contractId, &rv);
103 :
104 0 : if (NS_SUCCEEDED(rv)) {
105 : // Try to QI to nsIObserver
106 : nsCOMPtr<nsIObserver> startupObserver =
107 0 : do_QueryInterface(startupInstance, &rv);
108 0 : if (NS_SUCCEEDED(rv)) {
109 0 : rv = startupObserver->Observe(nsnull, aTopic, nsnull);
110 :
111 : // mainly for debugging if you want to know if your observer worked.
112 0 : NS_ASSERTION(NS_SUCCEEDED(rv), "Startup Observer failed!\n");
113 : }
114 : }
115 : else {
116 : #ifdef NS_DEBUG
117 0 : nsCAutoString warnStr("Cannot create startup observer : ");
118 0 : warnStr += contractId.get();
119 0 : NS_WARNING(warnStr.get());
120 : #endif
121 : }
122 :
123 : NS_TIME_FUNCTION_MARK("observer: category: %s cid: %s", categoryEntry.get(), nsPromiseFlatCString(contractId).get());
124 :
125 : }
126 : }
127 : }
128 :
129 0 : return NS_OK;
130 : }
|