/src/mozilla-central/xpcom/components/nsCategoryCache.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "nsIObserverService.h" |
8 | | #include "mozilla/Services.h" |
9 | | #include "mozilla/SimpleEnumerator.h" |
10 | | #include "nsISupportsPrimitives.h" |
11 | | #include "nsIStringEnumerator.h" |
12 | | |
13 | | #include "nsXPCOMCID.h" |
14 | | |
15 | | #include "nsCategoryCache.h" |
16 | | |
17 | | using mozilla::SimpleEnumerator; |
18 | | |
19 | | nsCategoryObserver::nsCategoryObserver(const nsACString& aCategory) |
20 | | : mCategory(aCategory) |
21 | | , mCallback(nullptr) |
22 | | , mClosure(nullptr) |
23 | | , mObserversRemoved(false) |
24 | 0 | { |
25 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
26 | 0 | // First, enumerate the currently existing entries |
27 | 0 | nsCOMPtr<nsICategoryManager> catMan = |
28 | 0 | do_GetService(NS_CATEGORYMANAGER_CONTRACTID); |
29 | 0 | if (!catMan) { |
30 | 0 | return; |
31 | 0 | } |
32 | 0 | |
33 | 0 | nsCOMPtr<nsISimpleEnumerator> enumerator; |
34 | 0 | nsresult rv = catMan->EnumerateCategory(aCategory, |
35 | 0 | getter_AddRefs(enumerator)); |
36 | 0 | if (NS_FAILED(rv)) { |
37 | 0 | return; |
38 | 0 | } |
39 | 0 | |
40 | 0 | for (auto& categoryEntry : SimpleEnumerator<nsICategoryEntry>(enumerator)) { |
41 | 0 | nsAutoCString entryValue; |
42 | 0 | categoryEntry->GetValue(entryValue); |
43 | 0 |
|
44 | 0 | if (nsCOMPtr<nsISupports> service = do_GetService(entryValue.get())) { |
45 | 0 | nsAutoCString entryName; |
46 | 0 | categoryEntry->GetEntry(entryName); |
47 | 0 |
|
48 | 0 | mHash.Put(entryName, service); |
49 | 0 | } |
50 | 0 | } |
51 | 0 |
|
52 | 0 | // Now, listen for changes |
53 | 0 | nsCOMPtr<nsIObserverService> serv = |
54 | 0 | mozilla::services::GetObserverService(); |
55 | 0 | if (serv) { |
56 | 0 | serv->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false); |
57 | 0 | serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID, false); |
58 | 0 | serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID, false); |
59 | 0 | serv->AddObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID, false); |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | 0 | nsCategoryObserver::~nsCategoryObserver() = default; |
64 | | |
65 | | NS_IMPL_ISUPPORTS(nsCategoryObserver, nsIObserver) |
66 | | |
67 | | void |
68 | | nsCategoryObserver::ListenerDied() |
69 | 0 | { |
70 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
71 | 0 | RemoveObservers(); |
72 | 0 | mCallback = nullptr; |
73 | 0 | mClosure = nullptr; |
74 | 0 | } |
75 | | |
76 | | void |
77 | | nsCategoryObserver::SetListener(void(aCallback)(void*), void* aClosure) |
78 | 0 | { |
79 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
80 | 0 | mCallback = aCallback; |
81 | 0 | mClosure = aClosure; |
82 | 0 | } |
83 | | |
84 | | void |
85 | | nsCategoryObserver::RemoveObservers() |
86 | 0 | { |
87 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
88 | 0 |
|
89 | 0 | if (mObserversRemoved) { |
90 | 0 | return; |
91 | 0 | } |
92 | 0 | |
93 | 0 | if (mCallback) { |
94 | 0 | mCallback(mClosure); |
95 | 0 | } |
96 | 0 |
|
97 | 0 | mObserversRemoved = true; |
98 | 0 | nsCOMPtr<nsIObserverService> obsSvc = |
99 | 0 | mozilla::services::GetObserverService(); |
100 | 0 | if (obsSvc) { |
101 | 0 | obsSvc->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID); |
102 | 0 | obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID); |
103 | 0 | obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID); |
104 | 0 | obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID); |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | NS_IMETHODIMP |
109 | | nsCategoryObserver::Observe(nsISupports* aSubject, const char* aTopic, |
110 | | const char16_t* aData) |
111 | 0 | { |
112 | 0 | MOZ_ASSERT(NS_IsMainThread()); |
113 | 0 |
|
114 | 0 | if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) { |
115 | 0 | mHash.Clear(); |
116 | 0 | RemoveObservers(); |
117 | 0 |
|
118 | 0 | return NS_OK; |
119 | 0 | } |
120 | 0 | |
121 | 0 | if (!aData || |
122 | 0 | !nsDependentString(aData).Equals(NS_ConvertASCIItoUTF16(mCategory))) { |
123 | 0 | return NS_OK; |
124 | 0 | } |
125 | 0 | |
126 | 0 | nsAutoCString str; |
127 | 0 | nsCOMPtr<nsISupportsCString> strWrapper(do_QueryInterface(aSubject)); |
128 | 0 | if (strWrapper) { |
129 | 0 | strWrapper->GetData(str); |
130 | 0 | } |
131 | 0 |
|
132 | 0 | if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID) == 0) { |
133 | 0 | // We may get an add notification even when we already have an entry. This |
134 | 0 | // is due to the notification happening asynchronously, so if the entry gets |
135 | 0 | // added and an nsCategoryObserver gets instantiated before events get |
136 | 0 | // processed, we'd get the notification for an existing entry. |
137 | 0 | // Do nothing in that case. |
138 | 0 | if (mHash.GetWeak(str)) { |
139 | 0 | return NS_OK; |
140 | 0 | } |
141 | 0 | |
142 | 0 | nsCOMPtr<nsICategoryManager> catMan = |
143 | 0 | do_GetService(NS_CATEGORYMANAGER_CONTRACTID); |
144 | 0 | if (!catMan) { |
145 | 0 | return NS_OK; |
146 | 0 | } |
147 | 0 | |
148 | 0 | nsCString entryValue; |
149 | 0 | catMan->GetCategoryEntry(mCategory, str, entryValue); |
150 | 0 |
|
151 | 0 | nsCOMPtr<nsISupports> service = do_GetService(entryValue.get()); |
152 | 0 |
|
153 | 0 | if (service) { |
154 | 0 | mHash.Put(str, service); |
155 | 0 | } |
156 | 0 | if (mCallback) { |
157 | 0 | mCallback(mClosure); |
158 | 0 | } |
159 | 0 | } else if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID) == 0) { |
160 | 0 | mHash.Remove(str); |
161 | 0 | if (mCallback) { |
162 | 0 | mCallback(mClosure); |
163 | 0 | } |
164 | 0 | } else if (strcmp(aTopic, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID) == 0) { |
165 | 0 | mHash.Clear(); |
166 | 0 | if (mCallback) { |
167 | 0 | mCallback(mClosure); |
168 | 0 | } |
169 | 0 | } |
170 | 0 | return NS_OK; |
171 | 0 | } |