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