Backed out 2 changesets (bug 1900622) for causing Bug 1908553 and ktlint failure...
[gecko.git] / netwerk / base / RedirectChannelRegistrar.cpp
blob9aa687ed88dc84d97f2ac12f92019f640aa06154
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"
7 #include "mozilla/ClearOnShutdown.h"
8 #include "mozilla/StaticPtr.h"
9 #include "nsThreadUtils.h"
11 namespace mozilla {
12 namespace net {
14 StaticRefPtr<RedirectChannelRegistrar> RedirectChannelRegistrar::gSingleton;
16 NS_IMPL_ISUPPORTS(RedirectChannelRegistrar, nsIRedirectChannelRegistrar)
18 RedirectChannelRegistrar::RedirectChannelRegistrar()
19 : mRealChannels(32),
20 mParentChannels(32),
21 mLock("RedirectChannelRegistrar") {
22 MOZ_ASSERT(!gSingleton);
25 // static
26 already_AddRefed<nsIRedirectChannelRegistrar>
27 RedirectChannelRegistrar::GetOrCreate() {
28 MOZ_ASSERT(NS_IsMainThread());
29 if (!gSingleton) {
30 gSingleton = new RedirectChannelRegistrar();
31 ClearOnShutdown(&gSingleton);
33 return do_AddRef(gSingleton);
36 NS_IMETHODIMP
37 RedirectChannelRegistrar::RegisterChannel(nsIChannel* channel, uint64_t id) {
38 MutexAutoLock lock(mLock);
40 mRealChannels.InsertOrUpdate(id, channel);
42 return NS_OK;
45 NS_IMETHODIMP
46 RedirectChannelRegistrar::GetRegisteredChannel(uint64_t id,
47 nsIChannel** _retval) {
48 MutexAutoLock lock(mLock);
50 if (!mRealChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
52 return NS_OK;
55 NS_IMETHODIMP
56 RedirectChannelRegistrar::LinkChannels(uint64_t id, nsIParentChannel* channel,
57 nsIChannel** _retval) {
58 MutexAutoLock lock(mLock);
60 if (!mRealChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
62 mParentChannels.InsertOrUpdate(id, channel);
63 return NS_OK;
66 NS_IMETHODIMP
67 RedirectChannelRegistrar::GetParentChannel(uint64_t id,
68 nsIParentChannel** _retval) {
69 MutexAutoLock lock(mLock);
71 if (!mParentChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
73 return NS_OK;
76 NS_IMETHODIMP
77 RedirectChannelRegistrar::DeregisterChannels(uint64_t id) {
78 MutexAutoLock lock(mLock);
80 mRealChannels.Remove(id);
81 mParentChannels.Remove(id);
82 return NS_OK;
85 } // namespace net
86 } // namespace mozilla