nsAHttpConnection.cpp (1579B)
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 "nsAHttpConnection.h" 6 7 #include "mozilla/Components.h" 8 #include "nsSocketTransportService2.h" 9 #include "nsThreadUtils.h" 10 11 namespace mozilla::net { 12 13 NS_IMPL_ADDREF(nsAHttpConnection) 14 NS_IMETHODIMP_(MozExternalRefCountType) 15 nsAHttpConnection::Release() { 16 nsrefcnt count; 17 MOZ_ASSERT(0 != mRefCnt, "dup release"); 18 count = --mRefCnt; 19 NS_LOG_RELEASE(this, count, "nsAHttpConnection"); 20 if (0 == count) { 21 mRefCnt = 1; /* stablize */ 22 // it is essential that the connection be destroyed on the socket thread. 23 DeleteSelfOnSocketThread(); 24 return 0; 25 } 26 return count; 27 } 28 29 NS_IMPL_QUERY_INTERFACE0(nsAHttpConnection) 30 31 nsAHttpConnection::~nsAHttpConnection() = default; 32 33 class DeleteAHttpConnection : public Runnable { 34 public: 35 explicit DeleteAHttpConnection(nsAHttpConnection* aConn) 36 : Runnable("net::DeleteAHttpConnection"), mConn(aConn) {} 37 38 NS_IMETHOD Run() override { 39 delete mConn; 40 return NS_OK; 41 } 42 43 private: 44 nsAHttpConnection* mConn; 45 }; 46 47 void nsAHttpConnection::DeleteSelfOnSocketThread() { 48 if (OnSocketThread()) { 49 delete this; 50 return; 51 } 52 53 nsCOMPtr<nsIEventTarget> sts = 54 mozilla::components::SocketTransport::Service(); 55 nsCOMPtr<nsIRunnable> event = new DeleteAHttpConnection(this); 56 (void)NS_WARN_IF( 57 NS_FAILED(sts->Dispatch(event.forget(), NS_DISPATCH_NORMAL))); 58 } 59 60 } // namespace mozilla::net