summaryrefslogtreecommitdiffstats
path: root/security/sandbox/common/test/SandboxTestingParent.cpp
blob: dff2d03896d26ee909c7aa1cc9accff67d927a37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* -*- 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 */
already_AddRefed<SandboxTestingParent> SandboxTestingParent::Create(
    Endpoint<PSandboxTestingParent>&& aParentEnd) {
  SandboxTestingThread* thread = SandboxTestingThread::Create();
  if (!thread) {
    return nullptr;
  }
  RefPtr<SandboxTestingParent> instance = new SandboxTestingParent(thread);
  thread->Dispatch(NewRunnableMethod<Endpoint<PSandboxTestingParent>&&>(
      "SandboxTestingParent::Bind", instance, &SandboxTestingParent::Bind,
      std::move(aParentEnd)));
  return instance.forget();
}

SandboxTestingParent::SandboxTestingParent(SandboxTestingThread* aThread)
    : mThread(aThread),
      mMonitor("SandboxTestingParent Lock"),
      mShutdownDone(false) {}

SandboxTestingParent::~SandboxTestingParent() = default;

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(
    already_AddRefed<SandboxTestingParent> aInstance) {
  MOZ_ASSERT(NS_IsMainThread());
  RefPtr<SandboxTestingParent> instance(aInstance);
  if (!instance) {
    return;
  }

  {
    // Hold the lock while we destroy the actor on the test thread.
    MonitorAutoLock lock(instance->mMonitor);
    instance->mThread->Dispatch(NewRunnableMethod(
        "SandboxTestingParent::ShutdownSandboxTestThread", instance,
        &SandboxTestingParent::ShutdownSandboxTestThread));

    // Wait for test thread to complete destruction.
    while (!instance->mShutdownDone) {
      instance->mMonitor.Wait();
    }
  }
}

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(GetMainThreadSerialEventTarget(), runnable,
                                 /*aForceDispatch*/ true);
  return IPC_OK();
}

}  // namespace mozilla