From b876426809ba07212a318d0b9c16a283d5a2a7a7 Mon Sep 17 00:00:00 2001 From: Chris H-C Date: Tue, 23 Jun 2020 21:20:12 +0000 Subject: [PATCH] Bug 1635255 - Flesh out FOG IPC one additional layer r=janerik Introducing FOGIPC, the central clearinghouse for the C++ layer between PContent and FOG's Rust impl. Gotta add tests. Differential Revision: https://phabricator.services.mozilla.com/D79742 --- dom/ipc/ContentChild.cpp | 2 + dom/ipc/ContentParent.cpp | 6 +- toolkit/components/glean/ipc/FOGIPC.cpp | 85 +++++++++++++++++++++++++ toolkit/components/glean/ipc/FOGIPC.h | 56 ++++++++++++++++ toolkit/components/glean/moz.build | 13 ++++ 5 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 toolkit/components/glean/ipc/FOGIPC.cpp create mode 100644 toolkit/components/glean/ipc/FOGIPC.h diff --git a/dom/ipc/ContentChild.cpp b/dom/ipc/ContentChild.cpp index 902cb5b88b32..e5fcbeee9d8b 100644 --- a/dom/ipc/ContentChild.cpp +++ b/dom/ipc/ContentChild.cpp @@ -19,6 +19,7 @@ #include "mozilla/BackgroundHangMonitor.h" #include "mozilla/BenchmarkStorageChild.h" #include "mozilla/ContentBlocking.h" +#include "mozilla/FOGIPC.h" #include "mozilla/LookAndFeel.h" #include "mozilla/MemoryTelemetry.h" #include "mozilla/NullPrincipal.h" @@ -4236,6 +4237,7 @@ already_AddRefed ContentChild::GetActor( } IPCResult ContentChild::RecvFlushFOGData(FlushFOGDataResolver&& aResolver) { + glean::FlushFOGData(std::move(aResolver)); return IPC_OK(); } diff --git a/dom/ipc/ContentParent.cpp b/dom/ipc/ContentParent.cpp index 9bf645357b70..c3ab4b0766ce 100644 --- a/dom/ipc/ContentParent.cpp +++ b/dom/ipc/ContentParent.cpp @@ -52,6 +52,7 @@ #include "mozilla/ClearOnShutdown.h" #include "mozilla/Components.h" #include "mozilla/DataStorage.h" +#include "mozilla/FOGIPC.h" #include "mozilla/GlobalStyleSheetCache.h" #include "mozilla/HangDetails.h" #include "mozilla/LoginReputationIPC.h" @@ -6902,7 +6903,10 @@ NS_IMETHODIMP ContentParent::GetActor(const nsACString& aName, return NS_OK; } -IPCResult ContentParent::RecvFOGData(ByteBuf&& buf) { return IPC_OK(); } +IPCResult ContentParent::RecvFOGData(ByteBuf&& buf) { + glean::FOGData(std::move(buf)); + return IPC_OK(); +} } // namespace dom } // namespace mozilla diff --git a/toolkit/components/glean/ipc/FOGIPC.cpp b/toolkit/components/glean/ipc/FOGIPC.cpp new file mode 100644 index 000000000000..bfc4f45c784b --- /dev/null +++ b/toolkit/components/glean/ipc/FOGIPC.cpp @@ -0,0 +1,85 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ +/* 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 http://mozilla.org/MPL/2.0/. */ + +#include "FOGIPC.h" + +#include "mozilla/dom/ContentChild.h" +#include "mozilla/dom/ContentParent.h" +#include "mozilla/MozPromise.h" +#include "nsTArray.h" +#include "nsThreadUtils.h" + +using mozilla::dom::ContentParent; +using FlushFOGDataPromise = mozilla::dom::ContentParent::FlushFOGDataPromise; + +namespace mozilla { +namespace glean { + +/** + * The parent process is asking you to flush your data ASAP. + * + * @param aResolver - The function you need to call with the bincoded, + * serialized payload that the Rust impl hands you. + */ +void FlushFOGData(std::function&& aResolver) {} + +/** + * Called by FOG on the parent process when it wants to flush all its + * children's data. + * @param aResolver - The function that'll be called with the results. + */ +void FlushAllChildData( + std::function&&)>&& aResolver) { + nsTArray parents; + ContentParent::GetAll(parents); + if (parents.Length() == 0) { + nsTArray results; + aResolver(std::move(results)); + return; + } + + nsTArray> promises; + for (auto parent : parents) { + promises.EmplaceBack(parent->SendFlushFOGData()); + } + // TODO: Don't throw away resolved data if some of the promises reject. + // (not sure how, but it'll mean not using ::All... maybe a custom copy of + // AllPromiseHolder? Might be impossible outside MozPromise.h) + FlushFOGDataPromise::All(GetCurrentThreadSerialEventTarget(), promises) + ->Then(GetCurrentThreadSerialEventTarget(), __func__, + [&aResolver]( + FlushFOGDataPromise::AllPromiseType::ResolveOrRejectValue&& + aValue) { + if (aValue.IsResolve()) { + aResolver(std::move(aValue.ResolveValue())); + } else { + nsTArray results; + aResolver(std::move(results)); + } + }); +} + +/** + * A child process has sent you this buf as a treat. + * @param buf - a bincoded serialized payload that the Rust impl understands. + */ +void FOGData(ipc::ByteBuf&& buf) {} + +/** + * Called by FOG on a child process when it wants to send a buf to the parent. + * @param buf - a bincoded serialized payload that the Rust impl understands. + */ +void SendFOGData(ipc::ByteBuf&& buf) { + switch (XRE_GetProcessType()) { + case GeckoProcessType_Content: + mozilla::dom::ContentChild::GetSingleton()->SendFOGData(std::move(buf)); + break; + default: + MOZ_ASSERT_UNREACHABLE("Unsuppored process type"); + } +} + +} // namespace glean +} // namespace mozilla diff --git a/toolkit/components/glean/ipc/FOGIPC.h b/toolkit/components/glean/ipc/FOGIPC.h new file mode 100644 index 000000000000..1b19f355e77b --- /dev/null +++ b/toolkit/components/glean/ipc/FOGIPC.h @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ +/* 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 http://mozilla.org/MPL/2.0/. */ + +#ifndef FOGIPC_h__ +#define FOGIPC_h__ + +#include + +#include "mozilla/Maybe.h" +#include "nsTArrayForwardDeclare.h" + +namespace mozilla { +namespace ipc { +class ByteBuf; +} // namespace ipc +} // namespace mozilla + +// This module provides the interface for FOG to communicate between processes. + +namespace mozilla { +namespace glean { + +/** + * The parent process is asking you to flush your data ASAP. + * + * @param aResolver - The function you need to call with the bincoded, + * serialized payload that the Rust impl hands you. + */ +void FlushFOGData(std::function&& aResolver); + +/** + * Called by FOG on the parent process when it wants to flush all its + * children's data. + * @param aResolver - The function that'll be called with the results. + */ +void FlushAllChildData( + std::function&&)>&& aResolver); + +/** + * A child process has sent you this buf as a treat. + * @param buf - a bincoded serialized payload that the Rust impl understands. + */ +void FOGData(ipc::ByteBuf&& buf); + +/** + * Called by FOG on a child process when it wants to send a buf to the parent. + * @param buf - a bincoded serialized payload that the Rust impl understands. + */ +void SendFOGData(ipc::ByteBuf&& buf); + +} // namespace glean +} // namespace mozilla + +#endif // FOGIPC_h__ diff --git a/toolkit/components/glean/moz.build b/toolkit/components/glean/moz.build index bd09f6ab37c6..59c275e7a80f 100644 --- a/toolkit/components/glean/moz.build +++ b/toolkit/components/glean/moz.build @@ -6,6 +6,19 @@ SPHINX_TREES['/toolkit/components/glean'] = 'docs' +# Needed so that we can include IPC things. +include('/ipc/chromium/chromium-config.mozbuild') + +FINAL_LIBRARY = 'xul' + +EXPORTS.mozilla += [ + 'ipc/FOGIPC.h', +] + +UNIFIED_SOURCES += [ + 'ipc/FOGIPC.cpp', +] + TEST_DIRS += [ 'gtest', ]