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"
12 StaticRefPtr
<RedirectChannelRegistrar
> RedirectChannelRegistrar::gSingleton
;
14 NS_IMPL_ISUPPORTS(RedirectChannelRegistrar
, nsIRedirectChannelRegistrar
)
16 RedirectChannelRegistrar::RedirectChannelRegistrar()
20 mLock("RedirectChannelRegistrar") {
21 MOZ_ASSERT(!gSingleton
);
25 already_AddRefed
<nsIRedirectChannelRegistrar
>
26 RedirectChannelRegistrar::GetOrCreate() {
27 MOZ_ASSERT(NS_IsMainThread());
29 gSingleton
= new RedirectChannelRegistrar();
31 return do_AddRef(gSingleton
);
35 void RedirectChannelRegistrar::Shutdown() {
36 MOZ_ASSERT(NS_IsMainThread());
41 RedirectChannelRegistrar::RegisterChannel(nsIChannel
*channel
,
43 MutexAutoLock
lock(mLock
);
45 mRealChannels
.Put(mId
, channel
);
50 // Ensure we always provide positive ids
57 RedirectChannelRegistrar::GetRegisteredChannel(uint32_t id
,
58 nsIChannel
**_retval
) {
59 MutexAutoLock
lock(mLock
);
61 if (!mRealChannels
.Get(id
, _retval
)) return NS_ERROR_NOT_AVAILABLE
;
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
);
78 RedirectChannelRegistrar::GetParentChannel(uint32_t id
,
79 nsIParentChannel
**_retval
) {
80 MutexAutoLock
lock(mLock
);
82 if (!mParentChannels
.Get(id
, _retval
)) return NS_ERROR_NOT_AVAILABLE
;
88 RedirectChannelRegistrar::DeregisterChannels(uint32_t id
) {
89 MutexAutoLock
lock(mLock
);
91 mRealChannels
.Remove(id
);
92 mParentChannels
.Remove(id
);
97 } // namespace mozilla