Bug 1745707 - Put id-rewriting into the id generator. r=ng

As the id generator is already a shared resource, this seems like a natural fit.

Differential Revision: https://phabricator.services.mozilla.com/D133636
This commit is contained in:
Andreas Pehrson 2021-12-14 12:52:25 +00:00
Родитель 0742b38c97
Коммит a7e4636aee
3 изменённых файлов: 103 добавлений и 94 удалений

Просмотреть файл

@ -2869,37 +2869,6 @@ void PeerConnectionImpl::CollectConduitTelemetryData() {
}
}
template <class T>
void AssignWithOpaqueIds(dom::Sequence<T>& aSource, dom::Sequence<T>& aDest,
RefPtr<RTCStatsIdGenerator>& aGenerator) {
for (auto& stat : aSource) {
stat.mId.Value() = aGenerator->Id(stat.mId.Value());
}
if (!aDest.AppendElements(aSource, fallible)) {
mozalloc_handle_oom(0);
}
}
template <class T>
void RewriteRemoteIds(dom::Sequence<T>& aList,
RefPtr<RTCStatsIdGenerator>& aGenerator) {
for (auto& stat : aList) {
if (stat.mRemoteId.WasPassed()) {
stat.mRemoteId.Value() = aGenerator->Id(stat.mRemoteId.Value());
}
}
}
template <class T>
void RewriteLocalIds(dom::Sequence<T>& aList,
RefPtr<RTCStatsIdGenerator>& aGenerator) {
for (auto& stat : aList) {
if (stat.mLocalId.WasPassed()) {
stat.mLocalId.Value() = aGenerator->Id(stat.mLocalId.Value());
}
}
}
RefPtr<dom::RTCStatsReportPromise> PeerConnectionImpl::GetStats(
dom::MediaStreamTrack* aSelector, bool aInternalStats) {
MOZ_ASSERT(NS_IsMainThread());
@ -3006,63 +2975,7 @@ RefPtr<dom::RTCStatsReportPromise> PeerConnectionImpl::GetStats(
[report = std::move(report), idGen = mIdGenerator](
const nsTArray<UniquePtr<dom::RTCStatsCollection>>&
aStats) mutable {
// Rewrite an Optional id
auto rewriteId = [&idGen](Optional<nsString>& id) {
if (id.WasPassed()) {
id.Value() = idGen->Id(id.Value());
}
};
// Involves a lot of copying, since webidl dictionaries don't have
// move semantics. Oh well.
for (const auto& stats : aStats) {
for (auto& stat : stats->mIceCandidatePairStats) {
rewriteId(stat.mLocalCandidateId);
rewriteId(stat.mRemoteCandidateId);
};
AssignWithOpaqueIds(stats->mIceCandidatePairStats,
report->mIceCandidatePairStats, idGen);
AssignWithOpaqueIds(stats->mIceCandidateStats,
report->mIceCandidateStats, idGen);
RewriteRemoteIds(stats->mInboundRtpStreamStats, idGen);
AssignWithOpaqueIds(stats->mInboundRtpStreamStats,
report->mInboundRtpStreamStats, idGen);
RewriteRemoteIds(stats->mOutboundRtpStreamStats, idGen);
AssignWithOpaqueIds(stats->mOutboundRtpStreamStats,
report->mOutboundRtpStreamStats, idGen);
RewriteLocalIds(stats->mRemoteInboundRtpStreamStats, idGen);
AssignWithOpaqueIds(stats->mRemoteInboundRtpStreamStats,
report->mRemoteInboundRtpStreamStats, idGen);
RewriteLocalIds(stats->mRemoteOutboundRtpStreamStats, idGen);
AssignWithOpaqueIds(stats->mRemoteOutboundRtpStreamStats,
report->mRemoteOutboundRtpStreamStats, idGen);
AssignWithOpaqueIds(stats->mRtpContributingSourceStats,
report->mRtpContributingSourceStats, idGen);
AssignWithOpaqueIds(stats->mTrickledIceCandidateStats,
report->mTrickledIceCandidateStats, idGen);
AssignWithOpaqueIds(stats->mDataChannelStats,
report->mDataChannelStats, idGen);
if (!report->mRawLocalCandidates.AppendElements(
stats->mRawLocalCandidates, fallible) ||
!report->mRawRemoteCandidates.AppendElements(
stats->mRawRemoteCandidates, fallible) ||
!report->mVideoFrameHistories.AppendElements(
stats->mVideoFrameHistories, fallible) ||
!report->mBandwidthEstimations.AppendElements(
stats->mBandwidthEstimations, fallible)) {
// XXX(Bug 1632090) Instead of extending the array 1-by-1
// (which might involve multiple reallocations) and
// potentially crashing here, SetCapacity could be called
// outside the loop once.
mozalloc_handle_oom(0);
}
}
idGen->RewriteIds(aStats, report.get());
return dom::RTCStatsReportPromise::CreateAndResolve(
std::move(report), __func__);
},

Просмотреть файл

@ -5,13 +5,100 @@
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
#include "RTCStatsIdGenerator.h"
#include "mozilla/RandomNum.h"
#include <iostream>
#include "mozilla/dom/RTCStatsReportBinding.h"
#include "mozilla/RandomNum.h"
namespace mozilla {
RTCStatsIdGenerator::RTCStatsIdGenerator()
: mSalt(RandomUint64().valueOr(0xa5a5a5a5)), mCounter(0) {}
void RTCStatsIdGenerator::RewriteIds(
const nsTArray<UniquePtr<dom::RTCStatsCollection>>& aFromStats,
dom::RTCStatsCollection* aIntoReport) {
// Rewrite an Optional id
auto rewriteId = [&](dom::Optional<nsString>& id) {
if (id.WasPassed()) {
id.Value() = Id(id.Value());
}
};
auto assignWithOpaqueIds = [&](auto& aSource, auto& aDest) {
for (auto& stat : aSource) {
rewriteId(stat.mId);
}
if (!aDest.AppendElements(aSource, fallible)) {
mozalloc_handle_oom(0);
}
};
auto rewriteRemoteIds = [&](auto& aList) {
for (auto& stat : aList) {
rewriteId(stat.mRemoteId);
}
};
auto rewriteLocalIds = [&](auto& aList) {
for (auto& stat : aList) {
rewriteId(stat.mLocalId);
}
};
// Involves a lot of copying, since webidl dictionaries don't have
// move semantics. Oh well.
for (const auto& stats : aFromStats) {
for (auto& stat : stats->mIceCandidatePairStats) {
rewriteId(stat.mLocalCandidateId);
rewriteId(stat.mRemoteCandidateId);
};
assignWithOpaqueIds(stats->mIceCandidatePairStats,
aIntoReport->mIceCandidatePairStats);
assignWithOpaqueIds(stats->mIceCandidateStats,
aIntoReport->mIceCandidateStats);
rewriteRemoteIds(stats->mInboundRtpStreamStats);
assignWithOpaqueIds(stats->mInboundRtpStreamStats,
aIntoReport->mInboundRtpStreamStats);
rewriteRemoteIds(stats->mOutboundRtpStreamStats);
assignWithOpaqueIds(stats->mOutboundRtpStreamStats,
aIntoReport->mOutboundRtpStreamStats);
rewriteLocalIds(stats->mRemoteInboundRtpStreamStats);
assignWithOpaqueIds(stats->mRemoteInboundRtpStreamStats,
aIntoReport->mRemoteInboundRtpStreamStats);
rewriteLocalIds(stats->mRemoteOutboundRtpStreamStats);
assignWithOpaqueIds(stats->mRemoteOutboundRtpStreamStats,
aIntoReport->mRemoteOutboundRtpStreamStats);
assignWithOpaqueIds(stats->mRtpContributingSourceStats,
aIntoReport->mRtpContributingSourceStats);
assignWithOpaqueIds(stats->mTrickledIceCandidateStats,
aIntoReport->mTrickledIceCandidateStats);
assignWithOpaqueIds(stats->mDataChannelStats,
aIntoReport->mDataChannelStats);
if (!aIntoReport->mRawLocalCandidates.AppendElements(
stats->mRawLocalCandidates, fallible) ||
!aIntoReport->mRawRemoteCandidates.AppendElements(
stats->mRawRemoteCandidates, fallible) ||
!aIntoReport->mVideoFrameHistories.AppendElements(
stats->mVideoFrameHistories, fallible) ||
!aIntoReport->mBandwidthEstimations.AppendElements(
stats->mBandwidthEstimations, fallible)) {
// XXX(Bug 1632090) Instead of extending the array 1-by-1
// (which might involve multiple reallocations) and
// potentially crashing here, SetCapacity could be called
// outside the loop once.
mozalloc_handle_oom(0);
}
}
}
nsString RTCStatsIdGenerator::Id(const nsString& aKey) {
if (!aKey.Length()) {
MOZ_ASSERT(aKey.Length(), "Stats IDs should never be empty.");

Просмотреть файл

@ -7,22 +7,31 @@
#ifndef _RTCSTATSIDGENERATOR_H_
#define _RTCSTATSIDGENERATOR_H_
#include "mozilla/Atomics.h"
#include "nsISupportsImpl.h"
#include "nsString.h"
#include <map>
#include "mozilla/Atomics.h"
#include "mozilla/UniquePtr.h"
#include "nsISupportsImpl.h"
#include "nsString.h"
#include "nsTArray.h"
namespace mozilla {
namespace dom {
struct RTCStatsCollection;
} // namespace dom
class RTCStatsIdGenerator {
public:
RTCStatsIdGenerator();
nsString Id(const nsString& aKey);
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RTCStatsIdGenerator);
void RewriteIds(
const nsTArray<UniquePtr<dom::RTCStatsCollection>>& aFromStats,
dom::RTCStatsCollection* aIntoReport);
private:
virtual ~RTCStatsIdGenerator(){};
nsString Id(const nsString& aKey);
nsString Generate();
const uint64_t mSalt;