sbServiceManager.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 //------------------------------------------------------------------------------
28 //------------------------------------------------------------------------------
29 //
30 // Songbird service manager.
31 //
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 
40 //------------------------------------------------------------------------------
41 //
42 // Songbird service manager imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Self imports.
47 #include "sbServiceManager.h"
48 
49 // Songbird imports.
51 
52 // Mozilla imports.
53 #include <nsServiceManagerUtils.h>
54 
55 
56 //------------------------------------------------------------------------------
57 //
58 // Songbird service manager logging services.
59 //
60 //------------------------------------------------------------------------------
61 
69 #include "prlog.h"
70 #ifdef PR_LOGGING
71 static PRLogModuleInfo* gServiceManagerLog = nsnull;
72 #define TRACE(args) PR_LOG(gServiceManagerLog, PR_LOG_DEBUG, args)
73 #define LOG(args) PR_LOG(gServiceManagerLog, PR_LOG_WARN, args)
74 #ifdef __GNUC__
75 #define __FUNCTION__ __PRETTY_FUNCTION__
76 #endif /* __GNUC__ */
77 #else
78 #define TRACE(args) /* nothing */
79 #define LOG(args) /* nothing */
80 #endif /* PR_LOGGING */
81 
82 
83 //------------------------------------------------------------------------------
84 //
85 // Songbird service manager nsISupports implementation.
86 //
87 //------------------------------------------------------------------------------
88 
90 
91 
92 //------------------------------------------------------------------------------
93 //
94 // Songbird service manager sbIServiceManager implementation.
95 //
96 //------------------------------------------------------------------------------
97 
98 //-------------------------------------
99 //
100 // IsServiceReady
101 //
102 
103 NS_IMETHODIMP
104 sbServiceManager::IsServiceReady(const char* aServiceContractID,
105  PRBool* retval)
106 {
107  // Validate state and arguments.
108  NS_ENSURE_STATE(mInitialized);
109  NS_ENSURE_ARG_POINTER(retval);
110 
111  // Service is ready if it's in the ready service table.
112  *retval = mReadyServiceTable.Get(NS_ConvertUTF8toUTF16(aServiceContractID),
113  nsnull);
114  TRACE(("%s[%.8x] %s %d", __FUNCTION__, this, aServiceContractID, *retval));
115 
116  return NS_OK;
117 }
118 
119 
120 //-------------------------------------
121 //
122 // SetServiceReady
123 //
124 
125 NS_IMETHODIMP
126 sbServiceManager::SetServiceReady(const char* aServiceContractID,
127  PRBool aServiceReady)
128 {
129  TRACE(("%s[%.8x] %s %d",
130  __FUNCTION__, this, aServiceContractID, aServiceReady));
131 
132  // Validate state.
133  NS_ENSURE_STATE(mInitialized);
134 
135  // Function variables.
136  PRBool serviceReady = !!aServiceReady;
137  PRBool success;
138  nsresult rv;
139 
140  // Get the current service ready state.
141  PRBool currentServiceReady;
142  rv = IsServiceReady(aServiceContractID, &currentServiceReady);
143  NS_ENSURE_SUCCESS(rv, rv);
144 
145  // Just return if state is not changing.
146  if (currentServiceReady == serviceReady)
147  return NS_OK;
148 
149  // Update the ready service table.
150  nsAutoString serviceContractID = NS_ConvertUTF8toUTF16(aServiceContractID);
151  if (aServiceReady) {
152  // Add the service to the ready service table.
153  success = mReadyServiceTable.Put(serviceContractID, PR_TRUE);
154  NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
155 
156  // Send notification that the service is ready.
157  rv = mObserverService->NotifyObservers(this,
158  "service-ready",
159  serviceContractID.get());
160  NS_ENSURE_SUCCESS(rv, rv);
161  }
162  else {
163  // Send notification that the service is about to be shut down.
164  rv = mObserverService->NotifyObservers(this,
165  "before-service-shutdown",
166  serviceContractID.get());
167  NS_ENSURE_SUCCESS(rv, rv);
168 
169  // Remove the service from the ready service table.
170  mReadyServiceTable.Remove(serviceContractID);
171  }
172 
173  return NS_OK;
174 }
175 
176 
177 //------------------------------------------------------------------------------
178 //
179 // Songbird service manager public services.
180 //
181 //------------------------------------------------------------------------------
182 
183 //-------------------------------------
184 //
185 // sbServiceManager
186 //
187 
189  mInitialized(PR_FALSE)
190 {
191  // Initialize logging.
192 #ifdef PR_LOGGING
193  if (!gServiceManagerLog) {
194  gServiceManagerLog = PR_NewLogModule("sbServiceManager");
195  }
196 #endif
197 
198  TRACE(("%s[%.8x]", __FUNCTION__, this));
199 }
200 
201 
202 //-------------------------------------
203 //
204 // ~sbServiceManager
205 //
206 
208 {
209  TRACE(("%s[%.8x]", __FUNCTION__, this));
210 
211  // Do nothing if not initialized.
212  if (!mInitialized)
213  return;
214 
215  // Clear table of ready services.
216  mReadyServiceTable.Clear();
217 }
218 
219 
220 //-------------------------------------
221 //
222 // Initialize
223 //
224 
225 nsresult
227 {
228  TRACE(("%s[%.8x]", __FUNCTION__, this));
229 
230  nsresult rv;
231 
232  // Do nothing if already initialized.
233  if (mInitialized)
234  return NS_OK;
235 
236  // Initialize the table of ready services.
237  PRBool success = mReadyServiceTable.Init();
238  NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
239 
240  // Get a proxied observer service.
241  mObserverService = do_ProxiedGetService("@mozilla.org/observer-service;1",
242  &rv);
243  NS_ENSURE_SUCCESS(rv, rv);
244 
245  // The service manager is now initialized.
246  mInitialized = PR_TRUE;
247 
248  return NS_OK;
249 }
250 
return NS_OK
Songbird Service Manager Definitions.
NS_DECL_ISUPPORTS NS_DECL_SBISERVICEMANAGER sbServiceManager()
const sbCreateProxiedComponent do_ProxiedGetService(const nsCID &aCID, nsresult *error=0)
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
#define TRACE(args)