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"
14 StaticRefPtr
<RedirectChannelRegistrar
> RedirectChannelRegistrar::gSingleton
;
16 NS_IMPL_ISUPPORTS(RedirectChannelRegistrar
, nsIRedirectChannelRegistrar
)
18 RedirectChannelRegistrar::RedirectChannelRegistrar()
21 mLock("RedirectChannelRegistrar") {
22 MOZ_ASSERT(!gSingleton
);
26 already_AddRefed
<nsIRedirectChannelRegistrar
>
27 RedirectChannelRegistrar::GetOrCreate() {
28 MOZ_ASSERT(NS_IsMainThread());
30 gSingleton
= new RedirectChannelRegistrar();
31 ClearOnShutdown(&gSingleton
);
33 return do_AddRef(gSingleton
);
37 RedirectChannelRegistrar::RegisterChannel(nsIChannel
* channel
, uint64_t id
) {
38 MutexAutoLock
lock(mLock
);
40 mRealChannels
.InsertOrUpdate(id
, channel
);
46 RedirectChannelRegistrar::GetRegisteredChannel(uint64_t id
,
47 nsIChannel
** _retval
) {
48 MutexAutoLock
lock(mLock
);
50 if (!mRealChannels
.Get(id
, _retval
)) return NS_ERROR_NOT_AVAILABLE
;
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
);
67 RedirectChannelRegistrar::GetParentChannel(uint64_t id
,
68 nsIParentChannel
** _retval
) {
69 MutexAutoLock
lock(mLock
);
71 if (!mParentChannels
.Get(id
, _retval
)) return NS_ERROR_NOT_AVAILABLE
;
77 RedirectChannelRegistrar::DeregisterChannels(uint64_t id
) {
78 MutexAutoLock
lock(mLock
);
80 mRealChannels
.Remove(id
);
81 mParentChannels
.Remove(id
);
86 } // namespace mozilla