Line data Source code
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 : ///////////////////////////////////////////////////////////////////////////////
26 : // Implementation
27 : ///////////////////////////////////////////////////////////////////////////////
28 :
29 3 : struct ShutdownObserver : public nsIObserver
30 : {
31 : NS_DECL_ISUPPORTS
32 :
33 0 : NS_IMETHOD Observe(nsISupports*, const char* aTopic, const char16_t*) override
34 : {
35 0 : if (strcmp(aTopic, "xpcom-will-shutdown") != 0) {
36 0 : return NS_OK;
37 : }
38 :
39 0 : nsCOMPtr<nsIObserverService> os = services::GetObserverService();
40 0 : if (os) {
41 0 : os->RemoveObserver(this, "xpcom-will-shutdown");
42 : }
43 :
44 0 : sShutdownHasStarted = true;
45 0 : return NS_OK;
46 : }
47 :
48 : private:
49 0 : virtual ~ShutdownObserver() { }
50 : };
51 :
52 3 : NS_IMPL_ISUPPORTS(ShutdownObserver, nsIObserver)
53 :
54 :
55 : ///////////////////////////////////////////////////////////////////////////////
56 : // Public API
57 : ///////////////////////////////////////////////////////////////////////////////
58 :
59 : /* static */ void
60 3 : ShutdownTracker::Initialize()
61 : {
62 6 : nsCOMPtr<nsIObserverService> os = services::GetObserverService();
63 3 : if (os) {
64 6 : os->AddObserver(new ShutdownObserver, "xpcom-will-shutdown", false);
65 : }
66 3 : }
67 :
68 : /* static */ bool
69 4 : ShutdownTracker::ShutdownHasStarted()
70 : {
71 4 : return sShutdownHasStarted;
72 : }
73 :
74 : } // namespace image
75 : } // namespace mozilla
|