2001-09-25 22:58:40 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 22:01:16 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2001-03-12 22:52:28 +00:00
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2001-03-12 22:52:28 +00:00
|
|
|
*
|
2001-09-25 22:58:40 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2001-03-12 22:52:28 +00:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-25 22:58:40 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2001-03-12 22:52:28 +00:00
|
|
|
*
|
2001-09-25 22:58:40 +00:00
|
|
|
* Contributor(s):
|
2007-05-16 20:06:01 +00:00
|
|
|
* Ben Turner <mozilla@songbirdnest.com
|
2001-09-25 22:58:40 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 22:01:16 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-25 22:58:40 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 22:01:16 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-25 22:58:40 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-03-12 22:52:28 +00:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsICategoryManager.h"
|
2002-09-03 23:36:13 +00:00
|
|
|
#include "nsXPCOM.h"
|
2001-03-12 22:52:28 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsAppStartupNotifier.h"
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsAppStartupNotifier, nsIObserver)
|
|
|
|
|
|
|
|
nsAppStartupNotifier::nsAppStartupNotifier()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAppStartupNotifier::~nsAppStartupNotifier()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
NS_IMETHODIMP nsAppStartupNotifier::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
|
2001-03-12 22:52:28 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aTopic);
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// now initialize all startup listeners
|
|
|
|
nsCOMPtr<nsICategoryManager> categoryManager =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
2001-10-19 20:52:59 +00:00
|
|
|
rv = categoryManager->EnumerateCategory(aTopic,
|
2001-03-12 22:52:28 +00:00
|
|
|
getter_AddRefs(enumerator));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> entry;
|
|
|
|
while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) {
|
2002-08-06 00:53:19 +00:00
|
|
|
nsCOMPtr<nsISupportsCString> category = do_QueryInterface(entry, &rv);
|
2001-03-12 22:52:28 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2002-08-26 21:20:34 +00:00
|
|
|
nsCAutoString categoryEntry;
|
|
|
|
rv = category->GetData(categoryEntry);
|
2001-03-12 22:52:28 +00:00
|
|
|
|
|
|
|
nsXPIDLCString contractId;
|
2001-10-19 20:52:59 +00:00
|
|
|
categoryManager->GetCategoryEntry(aTopic,
|
2002-08-26 21:20:34 +00:00
|
|
|
categoryEntry.get(),
|
2001-10-19 20:52:59 +00:00
|
|
|
getter_Copies(contractId));
|
2001-03-12 22:52:28 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
|
|
|
|
// If we see the word "service," in the beginning
|
|
|
|
// of the contractId then we create it as a service
|
|
|
|
// if not we do a createInstance
|
|
|
|
|
2007-05-16 20:06:01 +00:00
|
|
|
nsCOMPtr<nsISupports> startupInstance;
|
|
|
|
if (Substring(contractId, 0, 8).EqualsLiteral("service,"))
|
|
|
|
startupInstance = do_GetService(contractId.get() + 8, &rv);
|
2001-03-12 22:52:28 +00:00
|
|
|
else
|
2007-05-16 20:06:01 +00:00
|
|
|
startupInstance = do_CreateInstance(contractId, &rv);
|
2001-03-12 22:52:28 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2007-05-16 20:06:01 +00:00
|
|
|
// Try to QI to nsIObserver
|
|
|
|
nsCOMPtr<nsIObserver> startupObserver =
|
|
|
|
do_QueryInterface(startupInstance, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = startupObserver->Observe(nsnull, aTopic, nsnull);
|
|
|
|
|
|
|
|
// mainly for debugging if you want to know if your observer worked.
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Startup Observer failed!\n");
|
|
|
|
}
|
2001-03-12 22:52:28 +00:00
|
|
|
}
|
2001-06-28 02:32:12 +00:00
|
|
|
else {
|
|
|
|
#ifdef NS_DEBUG
|
|
|
|
nsCAutoString warnStr("Cannot create startup observer : ");
|
|
|
|
warnStr += contractId.get();
|
|
|
|
NS_WARNING(warnStr.get());
|
|
|
|
#endif
|
|
|
|
}
|
2001-03-12 22:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|