1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/FOGIPC.h"
9 #include "mozilla/RDDProcessManager.h"
10 #include "mozilla/dom/ContentParent.h"
11 #include "mozilla/dom/MemoryReportRequest.h"
12 #include "mozilla/gfx/GPUProcessManager.h"
13 #include "mozilla/gfx/gfxVars.h"
14 #include "mozilla/ipc/CrashReporterHost.h"
15 #include "mozilla/ipc/Endpoint.h"
17 #if defined(XP_LINUX) && defined(MOZ_SANDBOX)
18 # include "mozilla/SandboxBroker.h"
19 # include "mozilla/SandboxBrokerPolicyFactory.h"
22 #include "mozilla/Telemetry.h"
23 #include "mozilla/TelemetryIPC.h"
26 # include "mozilla/WinDllServices.h"
29 #include "ProfilerParent.h"
30 #include "RDDProcessHost.h"
34 using namespace layers
;
37 RDDChild::RDDChild(RDDProcessHost
* aHost
) : mHost(aHost
) {}
39 RDDChild::~RDDChild() = default;
41 bool RDDChild::Init() {
42 Maybe
<FileDescriptor
> brokerFd
;
44 #if defined(XP_LINUX) && defined(MOZ_SANDBOX)
45 auto policy
= SandboxBrokerPolicyFactory::GetRDDPolicy(OtherPid());
46 if (policy
!= nullptr) {
47 brokerFd
= Some(FileDescriptor());
49 SandboxBroker::Create(std::move(policy
), OtherPid(), brokerFd
.ref());
50 // This is unlikely to fail and probably indicates OS resource
51 // exhaustion, but we can at least try to recover.
52 if (NS_WARN_IF(mSandboxBroker
== nullptr)) {
55 MOZ_ASSERT(brokerFd
.ref().IsValid());
57 #endif // XP_LINUX && MOZ_SANDBOX
59 nsTArray
<GfxVarUpdate
> updates
= gfxVars::FetchNonDefaultVars();
61 bool isReadyForBackgroundProcessing
= false;
63 RefPtr
<DllServices
> dllSvc(DllServices::Get());
64 isReadyForBackgroundProcessing
= dllSvc
->IsReadyForBackgroundProcessing();
67 SendInit(updates
, brokerFd
, Telemetry::CanRecordReleaseData(),
68 isReadyForBackgroundProcessing
);
70 Unused
<< SendInitProfiler(ProfilerParent::CreateForProcess(OtherPid()));
72 gfxVars::AddReceiver(this);
73 auto* gpm
= gfx::GPUProcessManager::Get();
75 gpm
->AddListener(this);
81 bool RDDChild::SendRequestMemoryReport(const uint32_t& aGeneration
,
82 const bool& aAnonymize
,
83 const bool& aMinimizeMemoryUsage
,
84 const Maybe
<FileDescriptor
>& aDMDFile
) {
85 mMemoryReportRequest
= MakeUnique
<MemoryReportRequestHost
>(aGeneration
);
87 PRDDChild::SendRequestMemoryReport(
88 aGeneration
, aAnonymize
, aMinimizeMemoryUsage
, aDMDFile
,
89 [&](const uint32_t& aGeneration2
) {
90 if (RDDProcessManager
* rddpm
= RDDProcessManager::Get()) {
91 if (RDDChild
* child
= rddpm
->GetRDDChild()) {
92 if (child
->mMemoryReportRequest
) {
93 child
->mMemoryReportRequest
->Finish(aGeneration2
);
94 child
->mMemoryReportRequest
= nullptr;
99 [&](mozilla::ipc::ResponseRejectReason
) {
100 if (RDDProcessManager
* rddpm
= RDDProcessManager::Get()) {
101 if (RDDChild
* child
= rddpm
->GetRDDChild()) {
102 child
->mMemoryReportRequest
= nullptr;
110 void RDDChild::OnCompositorUnexpectedShutdown() {
111 auto* rddm
= RDDProcessManager::Get();
113 rddm
->CreateVideoBridge();
117 void RDDChild::OnVarChanged(const GfxVarUpdate
& aVar
) { SendUpdateVar(aVar
); }
119 mozilla::ipc::IPCResult
RDDChild::RecvAddMemoryReport(
120 const MemoryReport
& aReport
) {
121 if (mMemoryReportRequest
) {
122 mMemoryReportRequest
->RecvReport(aReport
);
128 mozilla::ipc::IPCResult
RDDChild::RecvGetModulesTrust(
129 ModulePaths
&& aModPaths
, bool aRunAtNormalPriority
,
130 GetModulesTrustResolver
&& aResolver
) {
131 RefPtr
<DllServices
> dllSvc(DllServices::Get());
132 dllSvc
->GetModulesTrust(std::move(aModPaths
), aRunAtNormalPriority
)
134 GetMainThreadSerialEventTarget(), __func__
,
135 [aResolver
](ModulesMapResult
&& aResult
) {
136 aResolver(Some(ModulesMapResult(std::move(aResult
))));
138 [aResolver
](nsresult aRv
) { aResolver(Nothing()); });
141 #endif // defined(XP_WIN)
143 mozilla::ipc::IPCResult
RDDChild::RecvUpdateMediaCodecsSupported(
144 const media::MediaCodecsSupported
& aSupported
) {
145 dom::ContentParent::BroadcastMediaCodecsSupportedUpdate(
146 RemoteDecodeIn::RddProcess
, aSupported
);
150 mozilla::ipc::IPCResult
RDDChild::RecvAccumulateChildHistograms(
151 nsTArray
<HistogramAccumulation
>&& aAccumulations
) {
152 TelemetryIPC::AccumulateChildHistograms(Telemetry::ProcessID::Rdd
,
157 mozilla::ipc::IPCResult
RDDChild::RecvAccumulateChildKeyedHistograms(
158 nsTArray
<KeyedHistogramAccumulation
>&& aAccumulations
) {
159 TelemetryIPC::AccumulateChildKeyedHistograms(Telemetry::ProcessID::Rdd
,
164 mozilla::ipc::IPCResult
RDDChild::RecvUpdateChildScalars(
165 nsTArray
<ScalarAction
>&& aScalarActions
) {
166 TelemetryIPC::UpdateChildScalars(Telemetry::ProcessID::Rdd
, aScalarActions
);
170 mozilla::ipc::IPCResult
RDDChild::RecvUpdateChildKeyedScalars(
171 nsTArray
<KeyedScalarAction
>&& aScalarActions
) {
172 TelemetryIPC::UpdateChildKeyedScalars(Telemetry::ProcessID::Rdd
,
177 mozilla::ipc::IPCResult
RDDChild::RecvRecordChildEvents(
178 nsTArray
<mozilla::Telemetry::ChildEventData
>&& aEvents
) {
179 TelemetryIPC::RecordChildEvents(Telemetry::ProcessID::Rdd
, aEvents
);
183 mozilla::ipc::IPCResult
RDDChild::RecvRecordDiscardedData(
184 const mozilla::Telemetry::DiscardedData
& aDiscardedData
) {
185 TelemetryIPC::RecordDiscardedData(Telemetry::ProcessID::Rdd
, aDiscardedData
);
189 mozilla::ipc::IPCResult
RDDChild::RecvFOGData(ByteBuf
&& aBuf
) {
190 glean::FOGData(std::move(aBuf
));
194 void RDDChild::ActorDestroy(ActorDestroyReason aWhy
) {
195 if (aWhy
== AbnormalShutdown
) {
196 GenerateCrashReport(OtherPid());
199 auto* gpm
= gfx::GPUProcessManager::Get();
201 // Note: the manager could have shutdown already.
202 gpm
->RemoveListener(this);
205 gfxVars::RemoveReceiver(this);
206 mHost
->OnChannelClosed();
209 class DeferredDeleteRDDChild
: public Runnable
{
211 explicit DeferredDeleteRDDChild(RefPtr
<RDDChild
>&& aChild
)
212 : Runnable("gfx::DeferredDeleteRDDChild"), mChild(std::move(aChild
)) {}
214 NS_IMETHODIMP
Run() override
{ return NS_OK
; }
217 RefPtr
<RDDChild
> mChild
;
221 void RDDChild::Destroy(RefPtr
<RDDChild
>&& aChild
) {
222 NS_DispatchToMainThread(new DeferredDeleteRDDChild(std::move(aChild
)));
225 } // namespace mozilla