Source code

Revision control

Other Tools

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
14
NS_IMPL_ISUPPORTS(RedirectChannelRegistrar, nsIRedirectChannelRegistrar)
15
16
RedirectChannelRegistrar::RedirectChannelRegistrar()
17
: mRealChannels(32),
18
mParentChannels(32),
19
mId(1),
20
mLock("RedirectChannelRegistrar") {
21
MOZ_ASSERT(!gSingleton);
22
}
23
24
// static
25
already_AddRefed<nsIRedirectChannelRegistrar>
26
RedirectChannelRegistrar::GetOrCreate() {
27
MOZ_ASSERT(NS_IsMainThread());
28
if (!gSingleton) {
29
gSingleton = new RedirectChannelRegistrar();
30
}
31
return do_AddRef(gSingleton);
32
}
33
34
// static
35
void RedirectChannelRegistrar::Shutdown() {
36
MOZ_ASSERT(NS_IsMainThread());
37
gSingleton = nullptr;
38
}
39
40
NS_IMETHODIMP
41
RedirectChannelRegistrar::RegisterChannel(nsIChannel* channel,
42
uint32_t* _retval) {
43
MutexAutoLock lock(mLock);
44
45
mRealChannels.Put(mId, channel);
46
*_retval = mId;
47
48
++mId;
49
50
// Ensure we always provide positive ids
51
if (!mId) mId = 1;
52
53
return NS_OK;
54
}
55
56
NS_IMETHODIMP
57
RedirectChannelRegistrar::GetRegisteredChannel(uint32_t id,
58
nsIChannel** _retval) {
59
MutexAutoLock lock(mLock);
60
61
if (!mRealChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
62
63
return NS_OK;
64
}
65
66
NS_IMETHODIMP
67
RedirectChannelRegistrar::LinkChannels(uint32_t id, nsIParentChannel* channel,
68
nsIChannel** _retval) {
69
MutexAutoLock lock(mLock);
70
71
if (!mRealChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
72
73
mParentChannels.Put(id, channel);
74
return NS_OK;
75
}
76
77
NS_IMETHODIMP
78
RedirectChannelRegistrar::GetParentChannel(uint32_t id,
79
nsIParentChannel** _retval) {
80
MutexAutoLock lock(mLock);
81
82
if (!mParentChannels.Get(id, _retval)) return NS_ERROR_NOT_AVAILABLE;
83
84
return NS_OK;
85
}
86
87
NS_IMETHODIMP
88
RedirectChannelRegistrar::DeregisterChannels(uint32_t id) {
89
MutexAutoLock lock(mLock);
90
91
mRealChannels.Remove(id);
92
mParentChannels.Remove(id);
93
return NS_OK;
94
}
95
96
} // namespace net
97
} // namespace mozilla