Bug 1586761 - P4 - Use new methods in dom/file and dom/events; r=baku

For the usecases in dom/file, I'm not so sure how to get the global at
callsites, so I let aIsSystemPrincipal and aCrossOriginIsolated to be false for
now.

Differential Revision: https://phabricator.services.mozilla.com/D63905

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Tom Tung 2020-03-25 14:14:37 +00:00
parent b05759de17
commit 3d9adc5e80
9 changed files with 97 additions and 52 deletions

View File

@ -723,20 +723,22 @@ double Event::TimeStamp() {
double ret =
perf->GetDOMTiming()->TimeStampToDOMHighRes(mEvent->mTimeStamp);
MOZ_ASSERT(mOwner->PrincipalOrNull());
if (mOwner->PrincipalOrNull()->IsSystemPrincipal()) return ret;
return nsRFPService::ReduceTimePrecisionAsMSecs(
ret, perf->GetRandomTimelineSeed());
ret, perf->GetRandomTimelineSeed(),
mOwner->PrincipalOrNull()->IsSystemPrincipal(),
mOwner->CrossOriginIsolated());
}
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
double ret = workerPrivate->TimeStampToDOMHighRes(mEvent->mTimeStamp);
if (workerPrivate->UsesSystemPrincipal()) return ret;
return nsRFPService::ReduceTimePrecisionAsMSecs(
ret, workerPrivate->GetRandomTimelineSeed());
ret, workerPrivate->GetRandomTimelineSeed(),
workerPrivate->UsesSystemPrincipal(),
workerPrivate->CrossOriginIsolated());
}
void Event::Serialize(IPC::Message* aMsg, bool aSerializeInterfaceType) {

View File

@ -59,5 +59,20 @@ uint64_t BaseBlobImpl::NextSerialNumber() {
return nextSerialNumber++;
}
void BaseBlobImpl::SetLastModificationDatePrecisely(int64_t aDate) {
MOZ_ASSERT(mIsFile, "Should only be called on files");
mLastModificationDate = aDate;
}
void BaseBlobImpl::SetLastModificationDate(bool aCrossOriginIsolated,
int64_t aDate) {
return SetLastModificationDatePrecisely(
nsRFPService::ReduceTimePrecisionAsUSecs(aDate, 0,
/* aIsSystemPrincipal */ false,
aCrossOriginIsolated));
// mLastModificationDate is an absolute timestamp so we supply a zero
// context mix-in
}
} // namespace dom
} // namespace mozilla

View File

@ -26,8 +26,8 @@ class BaseBlobImpl : public BlobImpl {
mName(aName),
mStart(0),
mLength(aLength),
mLastModificationDate(aLastModifiedDate),
mSerialNumber(NextSerialNumber()) {
mSerialNumber(NextSerialNumber()),
mLastModificationDate(aLastModifiedDate) {
// Ensure non-null mContentType by default
mContentType.SetIsVoid(false);
}
@ -38,8 +38,8 @@ class BaseBlobImpl : public BlobImpl {
mContentType(aContentType),
mStart(0),
mLength(aLength),
mLastModificationDate(0),
mSerialNumber(NextSerialNumber()) {
mSerialNumber(NextSerialNumber()),
mLastModificationDate(0) {
// Ensure non-null mContentType by default
mContentType.SetIsVoid(false);
}
@ -50,8 +50,8 @@ class BaseBlobImpl : public BlobImpl {
mContentType(aContentType),
mStart(aStart),
mLength(aLength),
mLastModificationDate(0),
mSerialNumber(NextSerialNumber()) {
mSerialNumber(NextSerialNumber()),
mLastModificationDate(0) {
// Ensure non-null mContentType by default
mContentType.SetIsVoid(false);
}
@ -108,7 +108,7 @@ class BaseBlobImpl : public BlobImpl {
mName = aName;
mContentType = aContentType;
mLength = aLength;
mLastModificationDate = aLastModifiedDate;
SetLastModificationDatePrecisely(aLastModifiedDate);
mIsFile = !aName.IsVoid();
}
@ -130,6 +130,17 @@ class BaseBlobImpl : public BlobImpl {
*/
static uint64_t NextSerialNumber();
void SetLastModificationDate(bool aCrossOriginIsolated, int64_t aDate);
void SetLastModificationDatePrecisely(int64_t aDate);
#ifdef DEBUG
bool IsLastModificationDateUnset() const {
return mLastModificationDate == INT64_MAX;
}
#endif
const nsString mBlobImplType;
bool mIsFile;
nsString mContentType;
@ -139,9 +150,10 @@ class BaseBlobImpl : public BlobImpl {
uint64_t mStart;
uint64_t mLength;
int64_t mLastModificationDate;
const uint64_t mSerialNumber;
private:
int64_t mLastModificationDate;
};
} // namespace dom

View File

@ -147,7 +147,8 @@ already_AddRefed<File> Blob::ToFile(const nsAString& aName,
mImpl->GetType(contentType);
RefPtr<MultipartBlobImpl> impl =
MultipartBlobImpl::Create(std::move(blobImpls), aName, contentType, aRv);
MultipartBlobImpl::Create(std::move(blobImpls), aName, contentType,
mGlobal->CrossOriginIsolated(), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
@ -216,13 +217,16 @@ already_AddRefed<Blob> Blob::Constructor(
const BlobPropertyBag& aBag, ErrorResult& aRv) {
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl();
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
MOZ_ASSERT(global);
if (aData.WasPassed()) {
nsAutoString type(aBag.mType);
MakeValidBlobType(type);
impl->InitializeBlob(aData.Value(), type,
aBag.mEndings == EndingType::Native, aRv);
aBag.mEndings == EndingType::Native,
global->CrossOriginIsolated(), aRv);
} else {
impl->InitializeBlob(aRv);
impl->InitializeBlob(global->CrossOriginIsolated(), aRv);
}
if (NS_WARN_IF(aRv.Failed())) {
@ -231,9 +235,6 @@ already_AddRefed<Blob> Blob::Constructor(
MOZ_ASSERT(!impl->IsFile());
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
MOZ_ASSERT(global);
RefPtr<Blob> blob = Blob::Create(global, impl);
return blob.forget();
}

View File

@ -55,8 +55,11 @@ already_AddRefed<File> File::CreateMemoryFileWithCustomLastModified(
already_AddRefed<File> File::CreateMemoryFileWithLastModifiedNow(
nsIGlobalObject* aGlobal, void* aMemoryBuffer, uint64_t aLength,
const nsAString& aName, const nsAString& aContentType) {
MOZ_ASSERT(aGlobal);
RefPtr<MemoryBlobImpl> blobImpl = MemoryBlobImpl::CreateWithLastModifiedNow(
aMemoryBuffer, aLength, aName, aContentType);
aMemoryBuffer, aLength, aName, aContentType,
aGlobal->CrossOriginIsolated());
MOZ_ASSERT(blobImpl);
RefPtr<File> file = File::Create(aGlobal, blobImpl);
@ -138,9 +141,13 @@ already_AddRefed<File> File::Constructor(const GlobalObject& aGlobal,
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(name);
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
MOZ_ASSERT(global);
nsAutoString type(aBag.mType);
MakeValidBlobType(type);
impl->InitializeBlob(aData, type, aBag.mEndings == EndingType::Native, aRv);
impl->InitializeBlob(aData, type, aBag.mEndings == EndingType::Native,
global->CrossOriginIsolated(), aRv);
if (aRv.Failed()) {
return nullptr;
}
@ -150,9 +157,6 @@ already_AddRefed<File> File::Constructor(const GlobalObject& aGlobal,
impl->SetLastModified(aBag.mLastModified.Value());
}
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
MOZ_ASSERT(global);
RefPtr<File> file = new File(global, impl);
return file.forget();
}

View File

@ -37,9 +37,10 @@ already_AddRefed<MemoryBlobImpl> MemoryBlobImpl::CreateWithCustomLastModified(
// static
already_AddRefed<MemoryBlobImpl> MemoryBlobImpl::CreateWithLastModifiedNow(
void* aMemoryBuffer, uint64_t aLength, const nsAString& aName,
const nsAString& aContentType) {
int64_t lastModificationDate =
nsRFPService::ReduceTimePrecisionAsUSecs(PR_Now(), 0);
const nsAString& aContentType, bool aCrossOriginIsolated) {
int64_t lastModificationDate = nsRFPService::ReduceTimePrecisionAsUSecs(
PR_Now(), 0,
/* aIsSystemPrincipal */ false, aCrossOriginIsolated);
return CreateWithCustomLastModified(aMemoryBuffer, aLength, aName,
aContentType, lastModificationDate);
}

View File

@ -26,7 +26,7 @@ class MemoryBlobImpl final : public BaseBlobImpl {
// File constructor.
static already_AddRefed<MemoryBlobImpl> CreateWithLastModifiedNow(
void* aMemoryBuffer, uint64_t aLength, const nsAString& aName,
const nsAString& aContentType);
const nsAString& aContentType, bool aCrossOriginIsolated);
// File constructor with custom lastModified attribue value. You should
// probably use CreateWithLastModifiedNow() instead of this one.

View File

@ -23,10 +23,11 @@ using namespace mozilla::dom;
/* static */
already_AddRefed<MultipartBlobImpl> MultipartBlobImpl::Create(
nsTArray<RefPtr<BlobImpl>>&& aBlobImpls, const nsAString& aName,
const nsAString& aContentType, ErrorResult& aRv) {
const nsAString& aContentType, bool aCrossOriginIsolated,
ErrorResult& aRv) {
RefPtr<MultipartBlobImpl> blobImpl =
new MultipartBlobImpl(std::move(aBlobImpls), aName, aContentType);
blobImpl->SetLengthAndModifiedDate(aRv);
blobImpl->SetLengthAndModifiedDate(Some(aCrossOriginIsolated), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
@ -40,7 +41,7 @@ already_AddRefed<MultipartBlobImpl> MultipartBlobImpl::Create(
ErrorResult& aRv) {
RefPtr<MultipartBlobImpl> blobImpl =
new MultipartBlobImpl(std::move(aBlobImpls), aContentType);
blobImpl->SetLengthAndModifiedDate(aRv);
blobImpl->SetLengthAndModifiedDate(/* aCrossOriginIsolated */ Nothing(), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
@ -174,14 +175,17 @@ already_AddRefed<BlobImpl> MultipartBlobImpl::CreateSlice(
return impl.forget();
}
void MultipartBlobImpl::InitializeBlob(ErrorResult& aRv) {
SetLengthAndModifiedDate(aRv);
void MultipartBlobImpl::InitializeBlob(bool aCrossOriginIsolated,
ErrorResult& aRv) {
SetLengthAndModifiedDate(Some(aCrossOriginIsolated), aRv);
NS_WARNING_ASSERTION(!aRv.Failed(), "SetLengthAndModifiedDate failed");
}
void MultipartBlobImpl::InitializeBlob(const Sequence<Blob::BlobPart>& aData,
const nsAString& aContentType,
bool aNativeEOL, ErrorResult& aRv) {
bool aNativeEOL,
bool aCrossOriginIsolated,
ErrorResult& aRv) {
mContentType = aContentType;
BlobSet blobSet;
@ -227,14 +231,14 @@ void MultipartBlobImpl::InitializeBlob(const Sequence<Blob::BlobPart>& aData,
}
mBlobImpls = blobSet.GetBlobImpls();
SetLengthAndModifiedDate(aRv);
SetLengthAndModifiedDate(Some(aCrossOriginIsolated), aRv);
NS_WARNING_ASSERTION(!aRv.Failed(), "SetLengthAndModifiedDate failed");
}
void MultipartBlobImpl::SetLengthAndModifiedDate(ErrorResult& aRv) {
void MultipartBlobImpl::SetLengthAndModifiedDate(
const Maybe<bool>& aCrossOriginIsolated, ErrorResult& aRv) {
MOZ_ASSERT(mLength == MULTIPARTBLOBIMPL_UNKNOWN_LENGTH);
MOZ_ASSERT_IF(mIsFile, mLastModificationDate ==
MULTIPARTBLOBIMPL_UNKNOWN_LAST_MODIFIED);
MOZ_ASSERT_IF(mIsFile, IsLastModificationDateUnset());
uint64_t totalLength = 0;
int64_t lastModified = 0;
@ -259,7 +263,7 @@ void MultipartBlobImpl::SetLengthAndModifiedDate(ErrorResult& aRv) {
}
if (lastModified < partLastModified) {
lastModified = partLastModified;
lastModified = partLastModified * PR_USEC_PER_MSEC;
lastModifiedSet = true;
}
}
@ -268,14 +272,17 @@ void MultipartBlobImpl::SetLengthAndModifiedDate(ErrorResult& aRv) {
mLength = totalLength;
if (mIsFile) {
// We cannot use PR_Now() because bug 493756 and, for this reason:
// var x = new Date(); var f = new File(...);
// x.getTime() < f.dateModified.getTime()
// could fail.
mLastModificationDate = nsRFPService::ReduceTimePrecisionAsUSecs(
lastModifiedSet ? lastModified * PR_USEC_PER_MSEC : JS_Now(), 0);
// mLastModificationDate is an absolute timestamp so we supply a zero
// context mix-in
if (lastModifiedSet) {
SetLastModificationDatePrecisely(lastModified);
} else {
MOZ_ASSERT(aCrossOriginIsolated.isSome());
// We cannot use PR_Now() because bug 493756 and, for this reason:
// var x = new Date(); var f = new File(...);
// x.getTime() < f.dateModified.getTime()
// could fail.
SetLastModificationDate(aCrossOriginIsolated.value(), JS_Now());
}
}
}
@ -327,5 +334,5 @@ void MultipartBlobImpl::GetBlobImplType(nsAString& aBlobImplType) const {
}
void MultipartBlobImpl::SetLastModified(int64_t aLastModified) {
mLastModificationDate = aLastModified * PR_USEC_PER_MSEC;
SetLastModificationDatePrecisely(aLastModified * PR_USEC_PER_MSEC);
}

View File

@ -11,6 +11,7 @@
#include "mozilla/Attributes.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/Maybe.h"
#include "mozilla/dom/BaseBlobImpl.h"
namespace mozilla {
@ -28,7 +29,8 @@ class MultipartBlobImpl final : public BaseBlobImpl {
// Create as a file
static already_AddRefed<MultipartBlobImpl> Create(
nsTArray<RefPtr<BlobImpl>>&& aBlobImpls, const nsAString& aName,
const nsAString& aContentType, ErrorResult& aRv);
const nsAString& aContentType, bool aCrossOriginIsolated,
ErrorResult& aRv);
// Create as a blob
static already_AddRefed<MultipartBlobImpl> Create(
@ -44,11 +46,11 @@ class MultipartBlobImpl final : public BaseBlobImpl {
MultipartBlobImpl()
: BaseBlobImpl(EmptyString(), MULTIPARTBLOBIMPL_UNKNOWN_LENGTH) {}
void InitializeBlob(ErrorResult& aRv);
void InitializeBlob(bool aCrossOriginIsolated, ErrorResult& aRv);
void InitializeBlob(const Sequence<Blob::BlobPart>& aData,
const nsAString& aContentType, bool aNativeEOL,
ErrorResult& aRv);
bool aCrossOriginIsolated, ErrorResult& aRv);
already_AddRefed<BlobImpl> CreateSlice(uint64_t aStart, uint64_t aLength,
const nsAString& aContentType,
@ -89,7 +91,8 @@ class MultipartBlobImpl final : public BaseBlobImpl {
~MultipartBlobImpl() = default;
void SetLengthAndModifiedDate(ErrorResult& aRv);
void SetLengthAndModifiedDate(const Maybe<bool>& aCrossOriginIsolated,
ErrorResult& aRv);
nsTArray<RefPtr<BlobImpl>> mBlobImpls;
};