diff options
Diffstat (limited to 'embedding/components/appstartup')
4 files changed, 194 insertions, 0 deletions
diff --git a/embedding/components/appstartup/moz.build b/embedding/components/appstartup/moz.build new file mode 100644 index 000000000..237591a50 --- /dev/null +++ b/embedding/components/appstartup/moz.build @@ -0,0 +1,15 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +EXPORTS += [ + 'nsIAppStartupNotifier.h', +] + +SOURCES += [ + 'nsAppStartupNotifier.cpp', +] + +FINAL_LIBRARY = 'xul' diff --git a/embedding/components/appstartup/nsAppStartupNotifier.cpp b/embedding/components/appstartup/nsAppStartupNotifier.cpp new file mode 100644 index 000000000..4ef38fb8d --- /dev/null +++ b/embedding/components/appstartup/nsAppStartupNotifier.cpp @@ -0,0 +1,90 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsCOMPtr.h" +#include "nsString.h" +#include "nsXPIDLString.h" +#include "nsIServiceManager.h" +#include "nsICategoryManager.h" +#include "nsXPCOM.h" +#include "nsISupportsPrimitives.h" +#include "nsAppStartupNotifier.h" +#include "nsISimpleEnumerator.h" + +NS_IMPL_ISUPPORTS(nsAppStartupNotifier, nsIObserver) + +nsAppStartupNotifier::nsAppStartupNotifier() +{ +} + +nsAppStartupNotifier::~nsAppStartupNotifier() +{ +} + +NS_IMETHODIMP nsAppStartupNotifier::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *someData) +{ + 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; + rv = categoryManager->EnumerateCategory(aTopic, + getter_AddRefs(enumerator)); + if (NS_FAILED(rv)) return rv; + + nsCOMPtr<nsISupports> entry; + while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) { + nsCOMPtr<nsISupportsCString> category = do_QueryInterface(entry, &rv); + + if (NS_SUCCEEDED(rv)) { + nsAutoCString categoryEntry; + rv = category->GetData(categoryEntry); + + nsXPIDLCString contractId; + categoryManager->GetCategoryEntry(aTopic, + categoryEntry.get(), + getter_Copies(contractId)); + + 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 + + nsCOMPtr<nsISupports> startupInstance; + if (Substring(contractId, 0, 8).EqualsLiteral("service,")) + startupInstance = do_GetService(contractId.get() + 8, &rv); + else + startupInstance = do_CreateInstance(contractId, &rv); + + if (NS_SUCCEEDED(rv)) { + // Try to QI to nsIObserver + nsCOMPtr<nsIObserver> startupObserver = + do_QueryInterface(startupInstance, &rv); + if (NS_SUCCEEDED(rv)) { + rv = startupObserver->Observe(nullptr, aTopic, nullptr); + + // mainly for debugging if you want to know if your observer worked. + NS_ASSERTION(NS_SUCCEEDED(rv), "Startup Observer failed!\n"); + } + } + else { + #ifdef DEBUG + nsAutoCString warnStr("Cannot create startup observer : "); + warnStr += contractId.get(); + NS_WARNING(warnStr.get()); + #endif + } + + } + } + } + + return NS_OK; +} diff --git a/embedding/components/appstartup/nsAppStartupNotifier.h b/embedding/components/appstartup/nsAppStartupNotifier.h new file mode 100644 index 000000000..31077ed38 --- /dev/null +++ b/embedding/components/appstartup/nsAppStartupNotifier.h @@ -0,0 +1,30 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef nsAppStartupNotifier_h___ +#define nsAppStartupNotifier_h___ + +#include "nsIAppStartupNotifier.h" + +// {1F59B001-02C9-11d5-AE76-CC92F7DB9E03} +#define NS_APPSTARTUPNOTIFIER_CID \ + { 0x1f59b001, 0x2c9, 0x11d5, { 0xae, 0x76, 0xcc, 0x92, 0xf7, 0xdb, 0x9e, 0x3 } } + +class nsAppStartupNotifier : public nsIObserver +{ +public: + NS_DEFINE_STATIC_CID_ACCESSOR( NS_APPSTARTUPNOTIFIER_CID ) + + NS_DECL_ISUPPORTS + NS_DECL_NSIOBSERVER + + nsAppStartupNotifier(); + +protected: + virtual ~nsAppStartupNotifier(); +}; + +#endif /* nsAppStartupNotifier_h___ */ + diff --git a/embedding/components/appstartup/nsIAppStartupNotifier.h b/embedding/components/appstartup/nsIAppStartupNotifier.h new file mode 100644 index 000000000..113bfa171 --- /dev/null +++ b/embedding/components/appstartup/nsIAppStartupNotifier.h @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef nsIAppStartupNotifier_h___ +#define nsIAppStartupNotifier_h___ + +#include "nsIObserver.h" + +/* + Some components need to be run at the startup of mozilla or embedding - to + start new services etc. + + This interface provides a generic way to start up arbitrary components + without requiring them to hack into main1() (or into NS_InitEmbedding) as + it's currently being done for services such as wallet, command line handlers + etc. + + We will have a category called "app-startup" which components register + themselves in using the CategoryManager. + + Components can also (optionally) add the word "service," as a prefix + to the "value" they pass in during a call to AddCategoryEntry() as + shown below: + + categoryManager->AddCategoryEntry(APPSTARTUP_CATEGORY, "testcomp", + "service," NS_WALLETSERVICE_CONTRACTID + true, true, + getter_Copies(previous)); + + Presence of the "service" keyword indicates the components desire to + be started as a service. When the "service" keyword is not present + we just do a do_CreateInstance. + + When mozilla starts (and when NS_InitEmbedding()) is invoked + we create an instance of the AppStartupNotifier component (which + implements nsIObserver) and invoke its Observe() method. + + Observe() will enumerate the components registered into the + APPSTARTUP_CATEGORY and notify them that startup has begun + and release them. +*/ + +#define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1" + +#define APPSTARTUP_CATEGORY "app-startup" +#define APPSTARTUP_TOPIC "app-startup" + + +/* + Please note that there's not a new interface in this file. + We're just leveraging nsIObserver instead of creating a + new one + + This file exists solely to provide the defines above +*/ + +#endif /* nsIAppStartupNotifier_h___ */ |