ShutdownTracker.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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 "ShutdownTracker.h"
  6. #include "mozilla/Services.h"
  7. #include "nsIObserver.h"
  8. #include "nsIObserverService.h"
  9. namespace mozilla {
  10. namespace image {
  11. class ShutdownTrackerImpl;
  12. ///////////////////////////////////////////////////////////////////////////////
  13. // Static Data
  14. ///////////////////////////////////////////////////////////////////////////////
  15. // Whether we've observed shutdown starting yet.
  16. static bool sShutdownHasStarted = false;
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // Implementation
  19. ///////////////////////////////////////////////////////////////////////////////
  20. struct ShutdownObserver : public nsIObserver
  21. {
  22. NS_DECL_ISUPPORTS
  23. NS_IMETHOD Observe(nsISupports*, const char* aTopic, const char16_t*) override
  24. {
  25. if (strcmp(aTopic, "xpcom-will-shutdown") != 0) {
  26. return NS_OK;
  27. }
  28. nsCOMPtr<nsIObserverService> os = services::GetObserverService();
  29. if (os) {
  30. os->RemoveObserver(this, "xpcom-will-shutdown");
  31. }
  32. sShutdownHasStarted = true;
  33. return NS_OK;
  34. }
  35. private:
  36. virtual ~ShutdownObserver() { }
  37. };
  38. NS_IMPL_ISUPPORTS(ShutdownObserver, nsIObserver)
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // Public API
  41. ///////////////////////////////////////////////////////////////////////////////
  42. /* static */ void
  43. ShutdownTracker::Initialize()
  44. {
  45. nsCOMPtr<nsIObserverService> os = services::GetObserverService();
  46. if (os) {
  47. os->AddObserver(new ShutdownObserver, "xpcom-will-shutdown", false);
  48. }
  49. }
  50. /* static */ bool
  51. ShutdownTracker::ShutdownHasStarted()
  52. {
  53. return sShutdownHasStarted;
  54. }
  55. } // namespace image
  56. } // namespace mozilla