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/. */
4
5 #include "RedirectChannelRegistrar.h"
6 #include "mozilla/StaticPtr.h"
7 #include "nsThreadUtils.h"
8
9 namespace mozilla {
10 namespace net {
11
12 StaticRefPtr<RedirectChannelRegistrar> RedirectChannelRegistrar::gSingleton;
13
NS_IMPL_ISUPPORTS(RedirectChannelRegistrar,nsIRedirectChannelRegistrar)14 NS_IMPL_ISUPPORTS(RedirectChannelRegistrar, nsIRedirectChannelRegistrar)
15
16 RedirectChannelRegistrar::RedirectChannelRegistrar()
17 : mRealChannels(32),
18 mParentChannels(32),
19 mLock("RedirectChannelRegistrar") {
20 MOZ_ASSERT(!gSingleton);
21 }
22
23 // static
24 already_AddRefed<nsIRedirectChannelRegistrar>
GetOrCreate()25 RedirectChannelRegistrar::GetOrCreate() {
26 MOZ_ASSERT(NS_IsMainThread());
27 if (!gSingleton) {
28 gSingleton = new RedirectChannelRegistrar();
29 }
30 return do_AddRef(gSingleton);
31 }
32
33 // static
Shutdown()34 void RedirectChannelRegistrar::Shutdown() {
35 MOZ_ASSERT(NS_IsMainThread());
36 gSingleton = nullptr;
37 }
38
39 NS_IMETHODIMP
RegisterChannel(nsIChannel * channel,uint64_t id)40 RedirectChannelRegistrar::RegisterChannel(nsIChannel* channel, uint64_t id) {
41 MutexAutoLock lock(mLock);
42
43 mRealChannels.InsertOrUpdate(id, channel);
44
45 return NS_OK;
46 }
47
48 NS_IMETHODIMP
GetRegisteredChannel(uint64_t id,nsIChannel ** _retval)49 RedirectChannelRegistrar::GetRegisteredChannel(uint64_t id,
50 nsIChannel** _retval) {
51 MutexAutoLock lock(mLock);
52
53 if (!mRealChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
54
55 return NS_OK;
56 }
57
58 NS_IMETHODIMP
LinkChannels(uint64_t id,nsIParentChannel * channel,nsIChannel ** _retval)59 RedirectChannelRegistrar::LinkChannels(uint64_t id, nsIParentChannel* channel,
60 nsIChannel** _retval) {
61 MutexAutoLock lock(mLock);
62
63 if (!mRealChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
64
65 mParentChannels.InsertOrUpdate(id, channel);
66 return NS_OK;
67 }
68
69 NS_IMETHODIMP
GetParentChannel(uint64_t id,nsIParentChannel ** _retval)70 RedirectChannelRegistrar::GetParentChannel(uint64_t id,
71 nsIParentChannel** _retval) {
72 MutexAutoLock lock(mLock);
73
74 if (!mParentChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
75
76 return NS_OK;
77 }
78
79 NS_IMETHODIMP
DeregisterChannels(uint64_t id)80 RedirectChannelRegistrar::DeregisterChannels(uint64_t id) {
81 MutexAutoLock lock(mLock);
82
83 mRealChannels.Remove(id);
84 mParentChannels.Remove(id);
85 return NS_OK;
86 }
87
88 } // namespace net
89 } // namespace mozilla
90