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