TCPServerSocketParent.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "TCPServerSocketParent.h"
  6. #include "nsIScriptSecurityManager.h"
  7. #include "TCPSocket.h"
  8. #include "TCPServerSocket.h"
  9. #include "nsJSUtils.h"
  10. #include "TCPSocketParent.h"
  11. #include "mozilla/Unused.h"
  12. #include "mozilla/AppProcessChecker.h"
  13. #include "mozilla/dom/ContentParent.h"
  14. #include "mozilla/dom/TabParent.h"
  15. #include "mozilla/dom/TCPServerSocketEvent.h"
  16. namespace mozilla {
  17. namespace dom {
  18. NS_IMPL_CYCLE_COLLECTION(TCPServerSocketParent, mServerSocket)
  19. NS_IMPL_CYCLE_COLLECTING_ADDREF(TCPServerSocketParent)
  20. NS_IMPL_CYCLE_COLLECTING_RELEASE(TCPServerSocketParent)
  21. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TCPServerSocketParent)
  22. NS_INTERFACE_MAP_ENTRY(nsISupports)
  23. NS_INTERFACE_MAP_END
  24. void
  25. TCPServerSocketParent::ReleaseIPDLReference()
  26. {
  27. MOZ_ASSERT(mIPCOpen);
  28. mIPCOpen = false;
  29. this->Release();
  30. }
  31. void
  32. TCPServerSocketParent::AddIPDLReference()
  33. {
  34. MOZ_ASSERT(!mIPCOpen);
  35. mIPCOpen = true;
  36. this->AddRef();
  37. }
  38. TCPServerSocketParent::TCPServerSocketParent(PNeckoParent* neckoParent,
  39. uint16_t aLocalPort,
  40. uint16_t aBacklog,
  41. bool aUseArrayBuffers)
  42. : mNeckoParent(neckoParent)
  43. , mIPCOpen(false)
  44. {
  45. mServerSocket = new TCPServerSocket(nullptr, aLocalPort, aUseArrayBuffers, aBacklog);
  46. mServerSocket->SetServerBridgeParent(this);
  47. }
  48. TCPServerSocketParent::~TCPServerSocketParent()
  49. {
  50. }
  51. void
  52. TCPServerSocketParent::Init()
  53. {
  54. NS_ENSURE_SUCCESS_VOID(mServerSocket->Init());
  55. }
  56. uint32_t
  57. TCPServerSocketParent::GetAppId()
  58. {
  59. const PContentParent *content = Manager()->Manager();
  60. if (PBrowserParent* browser = SingleManagedOrNull(content->ManagedPBrowserParent())) {
  61. TabParent *tab = TabParent::GetFrom(browser);
  62. return tab->OwnAppId();
  63. } else {
  64. return nsIScriptSecurityManager::UNKNOWN_APP_ID;
  65. }
  66. }
  67. bool
  68. TCPServerSocketParent::GetInIsolatedMozBrowser()
  69. {
  70. const PContentParent *content = Manager()->Manager();
  71. if (PBrowserParent* browser = SingleManagedOrNull(content->ManagedPBrowserParent())) {
  72. TabParent *tab = TabParent::GetFrom(browser);
  73. return tab->IsIsolatedMozBrowserElement();
  74. } else {
  75. return false;
  76. }
  77. }
  78. nsresult
  79. TCPServerSocketParent::SendCallbackAccept(TCPSocketParent *socket)
  80. {
  81. socket->AddIPDLReference();
  82. nsresult rv;
  83. nsString host;
  84. rv = socket->GetHost(host);
  85. if (NS_FAILED(rv)) {
  86. NS_ERROR("Failed to get host from nsITCPSocketParent");
  87. return NS_ERROR_FAILURE;
  88. }
  89. uint16_t port;
  90. rv = socket->GetPort(&port);
  91. if (NS_FAILED(rv)) {
  92. NS_ERROR("Failed to get port from nsITCPSocketParent");
  93. return NS_ERROR_FAILURE;
  94. }
  95. if (mNeckoParent) {
  96. if (mNeckoParent->SendPTCPSocketConstructor(socket, host, port)) {
  97. mozilla::Unused << PTCPServerSocketParent::SendCallbackAccept(socket);
  98. }
  99. else {
  100. NS_ERROR("Sending data from PTCPSocketParent was failed.");
  101. }
  102. }
  103. else {
  104. NS_ERROR("The member value for NeckoParent is wrong.");
  105. }
  106. return NS_OK;
  107. }
  108. bool
  109. TCPServerSocketParent::RecvClose()
  110. {
  111. NS_ENSURE_TRUE(mServerSocket, true);
  112. mServerSocket->Close();
  113. return true;
  114. }
  115. void
  116. TCPServerSocketParent::ActorDestroy(ActorDestroyReason why)
  117. {
  118. if (mServerSocket) {
  119. mServerSocket->Close();
  120. mServerSocket = nullptr;
  121. }
  122. mNeckoParent = nullptr;
  123. }
  124. bool
  125. TCPServerSocketParent::RecvRequestDelete()
  126. {
  127. mozilla::Unused << Send__delete__(this);
  128. return true;
  129. }
  130. void
  131. TCPServerSocketParent::OnConnect(TCPServerSocketEvent* event)
  132. {
  133. RefPtr<TCPSocket> socket = event->Socket();
  134. socket->SetAppIdAndBrowser(GetAppId(), GetInIsolatedMozBrowser());
  135. RefPtr<TCPSocketParent> socketParent = new TCPSocketParent();
  136. socketParent->SetSocket(socket);
  137. socket->SetSocketBridgeParent(socketParent);
  138. SendCallbackAccept(socketParent);
  139. }
  140. } // namespace dom
  141. } // namespace mozilla