Bug 1225722 - Modularize RTCStatsIdGenerator::RewriteIds somewhat. r=bwc

Differential Revision: https://phabricator.services.mozilla.com/D135864
This commit is contained in:
Andreas Pehrson 2022-02-01 23:12:27 +00:00
Родитель 2b179f4152
Коммит 362e85af4b
6 изменённых файлов: 77 добавлений и 57 удалений

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

@ -2979,9 +2979,8 @@ RefPtr<dom::RTCStatsReportPromise> PeerConnectionImpl::GetStats(
->Then(
mThread, __func__,
[report = std::move(report), idGen = mIdGenerator](
const nsTArray<UniquePtr<dom::RTCStatsCollection>>&
aStats) mutable {
idGen->RewriteIds(aStats, report.get());
nsTArray<UniquePtr<dom::RTCStatsCollection>> aStats) mutable {
idGen->RewriteIds(std::move(aStats), report.get());
return dom::RTCStatsReportPromise::CreateAndResolve(
std::move(report), __func__);
},

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

@ -166,9 +166,9 @@ already_AddRefed<Promise> RTCRtpReceiver::GetStats() {
->Then(
mMainThread, __func__,
[promise, window = mWindow, idGen = mIdGenerator](
const nsTArray<UniquePtr<RTCStatsCollection>>& aStats) {
nsTArray<UniquePtr<RTCStatsCollection>> aStats) {
RTCStatsCollection opaqueStats;
idGen->RewriteIds(aStats, &opaqueStats);
idGen->RewriteIds(std::move(aStats), &opaqueStats);
RefPtr<RTCStatsReport> report(new RTCStatsReport(window));
report->Incorporate(opaqueStats);

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

@ -8,8 +8,8 @@
#include <iostream>
#include "mozilla/dom/RTCStatsReportBinding.h"
#include "mozilla/RandomNum.h"
#include "RTCStatsReport.h"
namespace mozilla {
@ -17,7 +17,7 @@ RTCStatsIdGenerator::RTCStatsIdGenerator()
: mSalt(RandomUint64().valueOr(0xa5a5a5a5)), mCounter(0) {}
void RTCStatsIdGenerator::RewriteIds(
const nsTArray<UniquePtr<dom::RTCStatsCollection>>& aFromStats,
nsTArray<UniquePtr<dom::RTCStatsCollection>> aFromStats,
dom::RTCStatsCollection* aIntoReport) {
// Rewrite an Optional id
auto rewriteId = [&](dom::Optional<nsString>& id) {
@ -26,13 +26,10 @@ void RTCStatsIdGenerator::RewriteIds(
}
};
auto assignWithOpaqueIds = [&](auto& aSource, auto& aDest) {
for (auto& stat : aSource) {
auto rewriteIds = [&](auto& aList) {
for (auto& stat : aList) {
rewriteId(stat.mId);
}
if (!aDest.AppendElements(aSource, fallible)) {
mozalloc_handle_oom(0);
}
};
auto rewriteRemoteIds = [&](auto& aList) {
@ -49,54 +46,37 @@ void RTCStatsIdGenerator::RewriteIds(
// 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);
// Create a temporary to avoid double-rewriting any stats already in
// aIntoReport.
auto stats = MakeUnique<dom::RTCStatsCollection>();
dom::FlattenStats(std::move(aFromStats), stats.get());
rewriteRemoteIds(stats->mInboundRtpStreamStats);
assignWithOpaqueIds(stats->mInboundRtpStreamStats,
aIntoReport->mInboundRtpStreamStats);
for (auto& stat : stats->mIceCandidatePairStats) {
rewriteId(stat.mLocalCandidateId);
rewriteId(stat.mRemoteCandidateId);
};
rewriteIds(stats->mIceCandidatePairStats);
rewriteRemoteIds(stats->mOutboundRtpStreamStats);
assignWithOpaqueIds(stats->mOutboundRtpStreamStats,
aIntoReport->mOutboundRtpStreamStats);
rewriteIds(stats->mIceCandidateStats);
rewriteLocalIds(stats->mRemoteInboundRtpStreamStats);
assignWithOpaqueIds(stats->mRemoteInboundRtpStreamStats,
aIntoReport->mRemoteInboundRtpStreamStats);
rewriteRemoteIds(stats->mInboundRtpStreamStats);
rewriteIds(stats->mInboundRtpStreamStats);
rewriteLocalIds(stats->mRemoteOutboundRtpStreamStats);
assignWithOpaqueIds(stats->mRemoteOutboundRtpStreamStats,
aIntoReport->mRemoteOutboundRtpStreamStats);
rewriteRemoteIds(stats->mOutboundRtpStreamStats);
rewriteIds(stats->mOutboundRtpStreamStats);
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);
}
}
rewriteLocalIds(stats->mRemoteInboundRtpStreamStats);
rewriteIds(stats->mRemoteInboundRtpStreamStats);
rewriteLocalIds(stats->mRemoteOutboundRtpStreamStats);
rewriteIds(stats->mRemoteOutboundRtpStreamStats);
rewriteIds(stats->mRtpContributingSourceStats);
rewriteIds(stats->mTrickledIceCandidateStats);
rewriteIds(stats->mDataChannelStats);
dom::MergeStats(std::move(stats), aIntoReport);
}
nsString RTCStatsIdGenerator::Id(const nsString& aKey) {

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

@ -25,9 +25,8 @@ class RTCStatsIdGenerator {
RTCStatsIdGenerator();
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RTCStatsIdGenerator);
void RewriteIds(
const nsTArray<UniquePtr<dom::RTCStatsCollection>>& aFromStats,
dom::RTCStatsCollection* aIntoReport);
void RewriteIds(nsTArray<UniquePtr<dom::RTCStatsCollection>> aFromStats,
dom::RTCStatsCollection* aIntoReport);
private:
virtual ~RTCStatsIdGenerator(){};

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

@ -130,4 +130,40 @@ void RTCStatsReport::Set(const nsAString& aKey, JS::Handle<JSObject*> aValue,
RTCStatsReport_Binding::MaplikeHelpers::Set(this, aKey, aValue, aRv);
}
void MergeStats(UniquePtr<dom::RTCStatsCollection> aFromStats,
dom::RTCStatsCollection* aIntoStats) {
auto move = [&](auto& aSource, auto& aDest) {
if (!aDest.AppendElements(std::move(aSource), fallible)) {
mozalloc_handle_oom(0);
}
};
move(aFromStats->mIceCandidatePairStats, aIntoStats->mIceCandidatePairStats);
move(aFromStats->mIceCandidateStats, aIntoStats->mIceCandidateStats);
move(aFromStats->mInboundRtpStreamStats, aIntoStats->mInboundRtpStreamStats);
move(aFromStats->mOutboundRtpStreamStats,
aIntoStats->mOutboundRtpStreamStats);
move(aFromStats->mRemoteInboundRtpStreamStats,
aIntoStats->mRemoteInboundRtpStreamStats);
move(aFromStats->mRemoteOutboundRtpStreamStats,
aIntoStats->mRemoteOutboundRtpStreamStats);
move(aFromStats->mCodecStats, aIntoStats->mCodecStats);
move(aFromStats->mRtpContributingSourceStats,
aIntoStats->mRtpContributingSourceStats);
move(aFromStats->mTrickledIceCandidateStats,
aIntoStats->mTrickledIceCandidateStats);
move(aFromStats->mDataChannelStats, aIntoStats->mDataChannelStats);
move(aFromStats->mRawLocalCandidates, aIntoStats->mRawLocalCandidates);
move(aFromStats->mRawRemoteCandidates, aIntoStats->mRawRemoteCandidates);
move(aFromStats->mVideoFrameHistories, aIntoStats->mVideoFrameHistories);
move(aFromStats->mBandwidthEstimations, aIntoStats->mBandwidthEstimations);
}
void FlattenStats(nsTArray<UniquePtr<dom::RTCStatsCollection>> aFromStats,
dom::RTCStatsCollection* aIntoStats) {
for (auto& stats : aFromStats) {
MergeStats(std::move(stats), aIntoStats);
}
}
} // namespace mozilla::dom

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

@ -143,6 +143,12 @@ class RTCStatsReport final : public nsWrapperCache {
nsCOMPtr<nsPIDOMWindowInner> mParent;
};
void MergeStats(UniquePtr<dom::RTCStatsCollection> aFromStats,
dom::RTCStatsCollection* aIntoStats);
void FlattenStats(nsTArray<UniquePtr<dom::RTCStatsCollection>> aFromStats,
dom::RTCStatsCollection* aIntoStats);
} // namespace dom
} // namespace mozilla