Skip to content

Commit

Permalink
Bug 1809755 - Dispatch TCPSocket.ActivateTLS() to the socket thread. …
Browse files Browse the repository at this point in the history
…r=keeler

Differential Revision: https://phabricator.services.mozilla.com/D168125
  • Loading branch information
kaie committed Feb 8, 2023
1 parent a8f8b6f commit 4ca8cc8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
28 changes: 28 additions & 0 deletions dom/network/TCPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "TCPSocketParent.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/SyncRunnable.h"
#include "mozilla/dom/RootedDictionary.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/TCPSocketBinding.h"
Expand Down Expand Up @@ -455,6 +456,33 @@ void TCPSocket::NotifyCopyComplete(nsresult aStatus) {
}

void TCPSocket::ActivateTLS() {
nsresult rv;
nsCOMPtr<nsIEventTarget> socketThread =
do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
return;
}

bool alreadyOnSTST = false;
if (NS_FAILED(socketThread->IsOnCurrentThread(&alreadyOnSTST))) {
return;
}

if (alreadyOnSTST) {
ActivateTLSHelper();
return;
}

auto CallActivateTLS = [sock = RefPtr{this}]() mutable {
sock->ActivateTLSHelper();
};
mozilla::SyncRunnable::DispatchToThread(
socketThread,
NS_NewRunnableFunction("TCPSocket::UpgradeToSecure->ActivateTLSHelper",
CallActivateTLS));
}

void TCPSocket::ActivateTLSHelper() {
nsCOMPtr<nsITLSSocketControl> tlsSocketControl;
mTransport->GetTlsSocketControl(getter_AddRefs(tlsSocketControl));
if (tlsSocketControl) {
Expand Down
4 changes: 3 additions & 1 deletion dom/network/TCPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ class TCPSocket final : public DOMEventTargetHelper,
nsresult EnsureCopying();
// Re-calculate buffered amount.
void CalculateBufferedAmount();
// Enable TLS on this socket.
// Helper function, should be called by ActivateTLS(), only.
void ActivateTLSHelper();
// Enable TLS on this socket, dispatch to STSThread if necessary.
void ActivateTLS();
// Dispatch an error event if necessary, then dispatch a "close" event.
nsresult MaybeReportErrorAndCloseIfOpen(nsresult status);
Expand Down

0 comments on commit 4ca8cc8

Please sign in to comment.