Bug 1395017. P3 - always dispatch a task to run UpdatePrincipal().
When ChannelMediaResource::OnDataAvailable() runs off the main thread,
there is no guarantee that the principal will be updated before the new
data is observable to the consumer because the principal can only be
updated on the main thread while the consumer can access the data off
the main thread.
To make the code simpler, we always dispatch a task to run UpdatePrincipal()
even when OnDataAvailable() already runs on the main thread.
MozReview-Commit-ID: DQ0c9NXiR6u
--- a/dom/media/MediaResource.cpp
+++ b/dom/media/MediaResource.cpp
@@ -475,30 +475,21 @@ ChannelMediaResource::CopySegmentToCache
nsresult
ChannelMediaResource::OnDataAvailable(nsIRequest* aRequest,
nsIInputStream* aStream,
uint32_t aCount)
{
// This might happen off the main thread.
NS_ASSERTION(mChannel.get() == aRequest, "Wrong channel!");
- bool isMainThread = NS_IsMainThread();
- if (isMainThread) {
- // Update the principal now if we are already on the main thread. Otherwise
- // we will dispatch a runnable to do it.
- UpdatePrincipal();
- }
-
RefPtr<ChannelMediaResource> self = this;
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
- "ChannelMediaResource::OnDataAvailable", [self, aCount, isMainThread]() {
+ "ChannelMediaResource::OnDataAvailable", [self, aCount]() {
self->mChannelStatistics.AddBytes(aCount);
- if (!isMainThread) {
- self->UpdatePrincipal();
- }
+ self->UpdatePrincipal();
});
mCallback->AbstractMainThread()->Dispatch(r.forget());
uint32_t count = aCount;
while (count > 0) {
uint32_t read;
nsresult rv = aStream->ReadSegments(CopySegmentToCache, this, count, &read);
if (NS_FAILED(rv))