nsAppStartupNotifier.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsCOMPtr.h"
  6. #include "nsString.h"
  7. #include "nsXPIDLString.h"
  8. #include "nsIServiceManager.h"
  9. #include "nsICategoryManager.h"
  10. #include "nsXPCOM.h"
  11. #include "nsISupportsPrimitives.h"
  12. #include "nsAppStartupNotifier.h"
  13. #include "nsISimpleEnumerator.h"
  14. NS_IMPL_ISUPPORTS(nsAppStartupNotifier, nsIObserver)
  15. nsAppStartupNotifier::nsAppStartupNotifier()
  16. {
  17. }
  18. nsAppStartupNotifier::~nsAppStartupNotifier()
  19. {
  20. }
  21. NS_IMETHODIMP nsAppStartupNotifier::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *someData)
  22. {
  23. NS_ENSURE_ARG(aTopic);
  24. nsresult rv;
  25. // now initialize all startup listeners
  26. nsCOMPtr<nsICategoryManager> categoryManager =
  27. do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
  28. NS_ENSURE_SUCCESS(rv, rv);
  29. nsCOMPtr<nsISimpleEnumerator> enumerator;
  30. rv = categoryManager->EnumerateCategory(aTopic,
  31. getter_AddRefs(enumerator));
  32. if (NS_FAILED(rv)) return rv;
  33. nsCOMPtr<nsISupports> entry;
  34. while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) {
  35. nsCOMPtr<nsISupportsCString> category = do_QueryInterface(entry, &rv);
  36. if (NS_SUCCEEDED(rv)) {
  37. nsAutoCString categoryEntry;
  38. rv = category->GetData(categoryEntry);
  39. nsXPIDLCString contractId;
  40. categoryManager->GetCategoryEntry(aTopic,
  41. categoryEntry.get(),
  42. getter_Copies(contractId));
  43. if (NS_SUCCEEDED(rv)) {
  44. // If we see the word "service," in the beginning
  45. // of the contractId then we create it as a service
  46. // if not we do a createInstance
  47. nsCOMPtr<nsISupports> startupInstance;
  48. if (Substring(contractId, 0, 8).EqualsLiteral("service,"))
  49. startupInstance = do_GetService(contractId.get() + 8, &rv);
  50. else
  51. startupInstance = do_CreateInstance(contractId, &rv);
  52. if (NS_SUCCEEDED(rv)) {
  53. // Try to QI to nsIObserver
  54. nsCOMPtr<nsIObserver> startupObserver =
  55. do_QueryInterface(startupInstance, &rv);
  56. if (NS_SUCCEEDED(rv)) {
  57. rv = startupObserver->Observe(nullptr, aTopic, nullptr);
  58. // mainly for debugging if you want to know if your observer worked.
  59. NS_ASSERTION(NS_SUCCEEDED(rv), "Startup Observer failed!\n");
  60. }
  61. }
  62. else {
  63. #ifdef DEBUG
  64. nsAutoCString warnStr("Cannot create startup observer : ");
  65. warnStr += contractId.get();
  66. NS_WARNING(warnStr.get());
  67. #endif
  68. }
  69. }
  70. }
  71. }
  72. return NS_OK;
  73. }