Bug 1526591 - Remove devtools.inspector.shapesHighlighter.enabled pref. r=rcaliman
[gecko.git] / netwerk / base / RedirectChannelRegistrar.cpp
blob64f45bb64a81dd69e9ab57afebf0c5ae1373ba3d
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "RedirectChannelRegistrar.h"
6 #include "mozilla/StaticPtr.h"
7 #include "nsThreadUtils.h"
9 namespace mozilla {
10 namespace net {
12 StaticRefPtr<RedirectChannelRegistrar> RedirectChannelRegistrar::gSingleton;
14 NS_IMPL_ISUPPORTS(RedirectChannelRegistrar, nsIRedirectChannelRegistrar)
16 RedirectChannelRegistrar::RedirectChannelRegistrar()
17 : mRealChannels(32),
18 mParentChannels(32),
19 mId(1),
20 mLock("RedirectChannelRegistrar") {
21 MOZ_ASSERT(!gSingleton);
24 // static
25 already_AddRefed<nsIRedirectChannelRegistrar>
26 RedirectChannelRegistrar::GetOrCreate() {
27 MOZ_ASSERT(NS_IsMainThread());
28 if (!gSingleton) {
29 gSingleton = new RedirectChannelRegistrar();
31 return do_AddRef(gSingleton);
34 // static
35 void RedirectChannelRegistrar::Shutdown() {
36 MOZ_ASSERT(NS_IsMainThread());
37 gSingleton = nullptr;
40 NS_IMETHODIMP
41 RedirectChannelRegistrar::RegisterChannel(nsIChannel *channel,
42 uint32_t *_retval) {
43 MutexAutoLock lock(mLock);
45 mRealChannels.Put(mId, channel);
46 *_retval = mId;
48 ++mId;
50 // Ensure we always provide positive ids
51 if (!mId) mId = 1;
53 return NS_OK;
56 NS_IMETHODIMP
57 RedirectChannelRegistrar::GetRegisteredChannel(uint32_t id,
58 nsIChannel **_retval) {
59 MutexAutoLock lock(mLock);
61 if (!mRealChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
63 return NS_OK;
66 NS_IMETHODIMP
67 RedirectChannelRegistrar::LinkChannels(uint32_t id, nsIParentChannel *channel,
68 nsIChannel **_retval) {
69 MutexAutoLock lock(mLock);
71 if (!mRealChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
73 mParentChannels.Put(id, channel);
74 return NS_OK;
77 NS_IMETHODIMP
78 RedirectChannelRegistrar::GetParentChannel(uint32_t id,
79 nsIParentChannel **_retval) {
80 MutexAutoLock lock(mLock);
82 if (!mParentChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
84 return NS_OK;
87 NS_IMETHODIMP
88 RedirectChannelRegistrar::DeregisterChannels(uint32_t id) {
89 MutexAutoLock lock(mLock);
91 mRealChannels.Remove(id);
92 mParentChannels.Remove(id);
93 return NS_OK;
96 } // namespace net
97 } // namespace mozilla