diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /security/sandbox/common/test/SandboxTestingParent.cpp | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'security/sandbox/common/test/SandboxTestingParent.cpp')
-rw-r--r-- | security/sandbox/common/test/SandboxTestingParent.cpp | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/security/sandbox/common/test/SandboxTestingParent.cpp b/security/sandbox/common/test/SandboxTestingParent.cpp new file mode 100644 index 0000000000..d4939fc5b5 --- /dev/null +++ b/security/sandbox/common/test/SandboxTestingParent.cpp @@ -0,0 +1,126 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#include "SandboxTestingParent.h" +#include "SandboxTestingThread.h" +#include "nsIObserverService.h" +#include "mozilla/ipc/Endpoint.h" +#include "mozilla/Services.h" +#include "mozilla/SyncRunnable.h" +#include "nsDirectoryServiceUtils.h" + +namespace mozilla { + +/* static */ +SandboxTestingParent* SandboxTestingParent::Create( + Endpoint<PSandboxTestingParent>&& aParentEnd) { + SandboxTestingThread* thread = SandboxTestingThread::Create(); + if (!thread) { + return nullptr; + } + return new SandboxTestingParent(thread, std::move(aParentEnd)); +} + +SandboxTestingParent::SandboxTestingParent( + SandboxTestingThread* aThread, Endpoint<PSandboxTestingParent>&& aParentEnd) + : mThread(aThread), + mMonitor("SandboxTestingParent Lock"), + mShutdownDone(false) { + MOZ_ASSERT(mThread); + mThread->Dispatch( + NewNonOwningRunnableMethod<Endpoint<PSandboxTestingParent>&&>( + "SandboxTestingParent::Bind", this, &SandboxTestingParent::Bind, + std::move(aParentEnd))); +} + +void SandboxTestingParent::Bind(Endpoint<PSandboxTestingParent>&& aEnd) { + MOZ_RELEASE_ASSERT(mThread->IsOnThread()); + DebugOnly<bool> ok = aEnd.Bind(this); + MOZ_ASSERT(ok); +} + +void SandboxTestingParent::ShutdownSandboxTestThread() { + MOZ_ASSERT(mThread->IsOnThread()); + Close(); + // Notify waiting thread that we are done. + MonitorAutoLock lock(mMonitor); + mShutdownDone = true; + mMonitor.Notify(); +} + +void SandboxTestingParent::Destroy(SandboxTestingParent* aInstance) { + MOZ_ASSERT(NS_IsMainThread()); + if (!aInstance) { + return; + } + + { + // Hold the lock while we destroy the actor on the test thread. + MonitorAutoLock lock(aInstance->mMonitor); + aInstance->mThread->Dispatch(NewNonOwningRunnableMethod( + "SandboxTestingParent::ShutdownSandboxTestThread", aInstance, + &SandboxTestingParent::ShutdownSandboxTestThread)); + + // Wait for test thread to complete destruction. + while (!aInstance->mShutdownDone) { + aInstance->mMonitor.Wait(); + } + } + + delete aInstance; +} + +void SandboxTestingParent::ActorDestroy(ActorDestroyReason aWhy) { + MOZ_RELEASE_ASSERT(mThread->IsOnThread()); +} + +mozilla::ipc::IPCResult SandboxTestingParent::RecvReportTestResults( + const nsCString& testName, bool passed, const nsCString& resultMessage) { + NS_DispatchToMainThread( + NS_NewRunnableFunction("SandboxReportTestResults", [=]() { + nsCOMPtr<nsIObserverService> observerService = + mozilla::services::GetObserverService(); + MOZ_RELEASE_ASSERT(observerService); + nsCString passedStr(passed ? "true"_ns : "false"_ns); + nsString json; + json += u"{ \"testid\" : \""_ns + NS_ConvertUTF8toUTF16(testName) + + u"\", \"passed\" : "_ns + NS_ConvertUTF8toUTF16(passedStr) + + u", \"message\" : \""_ns + + NS_ConvertUTF8toUTF16(resultMessage) + u"\" }"_ns; + observerService->NotifyObservers(nullptr, "sandbox-test-result", + json.BeginReading()); + })); + return IPC_OK(); +} + +mozilla::ipc::IPCResult SandboxTestingParent::RecvTestCompleted() { + Unused << SendShutDown(); + NS_DispatchToMainThread( + NS_NewRunnableFunction("SandboxReportTestResults", []() { + nsCOMPtr<nsIObserverService> observerService = + mozilla::services::GetObserverService(); + MOZ_RELEASE_ASSERT(observerService); + observerService->NotifyObservers(nullptr, "sandbox-test-done", 0); + })); + return IPC_OK(); +} + +mozilla::ipc::IPCResult SandboxTestingParent::RecvGetSpecialDirectory( + const nsCString& aSpecialDirName, nsString* aDirPath) { + RefPtr<Runnable> runnable = NS_NewRunnableFunction( + "SandboxTestingParent::RecvGetSpecialDirectory", [&]() { + nsCOMPtr<nsIFile> dir; + NS_GetSpecialDirectory(aSpecialDirName.get(), getter_AddRefs(dir)); + if (dir) { + dir->GetPath(*aDirPath); + } + }); + SyncRunnable::DispatchToThread(GetMainThreadEventTarget(), runnable, + /*aForceDispatch*/ true); + return IPC_OK(); +} + +} // namespace mozilla |