michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TCPServerSocketParent.h" michael@0: #include "nsJSUtils.h" michael@0: #include "TCPSocketParent.h" michael@0: #include "mozilla/unused.h" michael@0: #include "mozilla/AppProcessChecker.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: static void michael@0: FireInteralError(mozilla::net::PTCPServerSocketParent* aActor, michael@0: uint32_t aLineNo) michael@0: { michael@0: mozilla::unused << michael@0: aActor->SendCallbackError(NS_LITERAL_STRING("Internal error"), michael@0: NS_LITERAL_STRING(__FILE__), aLineNo, 0); michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION(TCPServerSocketParent, mServerSocket, mIntermediary) michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPServerSocketParent) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPServerSocketParent) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPServerSocketParent) michael@0: NS_INTERFACE_MAP_ENTRY(nsITCPServerSocketParent) michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: void michael@0: TCPServerSocketParent::ReleaseIPDLReference() michael@0: { michael@0: MOZ_ASSERT(mIPCOpen); michael@0: mIPCOpen = false; michael@0: this->Release(); michael@0: } michael@0: michael@0: void michael@0: TCPServerSocketParent::AddIPDLReference() michael@0: { michael@0: MOZ_ASSERT(!mIPCOpen); michael@0: mIPCOpen = true; michael@0: this->AddRef(); michael@0: } michael@0: michael@0: bool michael@0: TCPServerSocketParent::Init(PNeckoParent* neckoParent, const uint16_t& aLocalPort, michael@0: const uint16_t& aBacklog, const nsString& aBinaryType) michael@0: { michael@0: mNeckoParent = neckoParent; michael@0: michael@0: nsresult rv; michael@0: mIntermediary = do_CreateInstance("@mozilla.org/tcp-socket-intermediary;1", &rv); michael@0: if (NS_FAILED(rv)) { michael@0: FireInteralError(this, __LINE__); michael@0: return true; michael@0: } michael@0: michael@0: rv = mIntermediary->Listen(this, aLocalPort, aBacklog, aBinaryType, getter_AddRefs(mServerSocket)); michael@0: if (NS_FAILED(rv) || !mServerSocket) { michael@0: FireInteralError(this, __LINE__); michael@0: return true; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: TCPServerSocketParent::SendCallbackAccept(nsITCPSocketParent *socket) michael@0: { michael@0: TCPSocketParent* _socket = static_cast(socket); michael@0: PTCPSocketParent* _psocket = static_cast(_socket); michael@0: michael@0: _socket->AddIPDLReference(); michael@0: michael@0: if (mNeckoParent) { michael@0: if (mNeckoParent->SendPTCPSocketConstructor(_psocket)) { michael@0: mozilla::unused << PTCPServerSocketParent::SendCallbackAccept(_psocket); michael@0: } michael@0: else { michael@0: NS_ERROR("Sending data from PTCPSocketParent was failed."); michael@0: }; michael@0: } michael@0: else { michael@0: NS_ERROR("The member value for NeckoParent is wrong."); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: TCPServerSocketParent::SendCallbackError(const nsAString& message, michael@0: const nsAString& filename, michael@0: uint32_t lineNumber, michael@0: uint32_t columnNumber) michael@0: { michael@0: mozilla::unused << michael@0: PTCPServerSocketParent::SendCallbackError(nsString(message), nsString(filename), michael@0: lineNumber, columnNumber); michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool michael@0: TCPServerSocketParent::RecvClose() michael@0: { michael@0: NS_ENSURE_TRUE(mServerSocket, true); michael@0: mServerSocket->Close(); michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: TCPServerSocketParent::ActorDestroy(ActorDestroyReason why) michael@0: { michael@0: if (mServerSocket) { michael@0: mServerSocket->Close(); michael@0: mServerSocket = nullptr; michael@0: } michael@0: mNeckoParent = nullptr; michael@0: mIntermediary = nullptr; michael@0: } michael@0: michael@0: bool michael@0: TCPServerSocketParent::RecvRequestDelete() michael@0: { michael@0: mozilla::unused << Send__delete__(this); michael@0: return true; michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla