Commit cb51073c authored by Kent Tamura's avatar Kent Tamura Committed by Commit Bot

Blob: Change the type of |expected_modification_time| arguments of BlobData

from double to base::Optional<base::Time>.

This CL also changes a double argument of File::CaptureSnapshot() to
base::Optional<base::Time>.

This CL has no behavior changes.

Bug: 988343
Change-Id: Ida6d0d415cc8f72d2c965bfd242b584080570791
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1933668
Commit-Queue: Kent Tamura <tkent@chromium.org>
Reviewed-by: default avatarJoshua Bell <jsbell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#718857}
parent c3b33fdb
......@@ -24,6 +24,7 @@ class WebBlobInfo {
const WebString& type,
uint64_t size,
mojo::ScopedMessagePipeHandle);
// |last_modified| - Seconds from Unix epoch.
BLINK_EXPORT WebBlobInfo(const WebString& uuid,
const WebString& file_path,
const WebString& file_name,
......
......@@ -94,6 +94,7 @@ class WebHTTPBody {
BLINK_PLATFORM_EXPORT void AppendData(const WebData&);
BLINK_PLATFORM_EXPORT void AppendFile(const WebString&);
// Passing -1 to |file_length| means to the end of the file.
// |modification_time| - Seconds from Unix epoch.
BLINK_PLATFORM_EXPORT void AppendFileRange(const WebString&,
int64_t file_start,
int64_t file_length,
......
......@@ -625,10 +625,13 @@ bool V8ScriptValueSerializer::WriteFile(File* file,
size_t index = blob_info_array_->size();
DCHECK_LE(index, std::numeric_limits<uint32_t>::max());
uint64_t size;
double last_modified_ms = InvalidFileTime();
file->CaptureSnapshot(size, last_modified_ms);
// FIXME: transition WebBlobInfo.lastModified to be milliseconds-based also.
double last_modified = last_modified_ms / kMsPerSecond;
base::Optional<base::Time> last_modified_time;
file->CaptureSnapshot(size, last_modified_time);
// TODO(crbug.com/988343): transition WebBlobInfo.lastModified to be
// base::Time also.
double last_modified = last_modified_time
? last_modified_time->ToDoubleT()
: std::numeric_limits<double>::quiet_NaN();
blob_info_array_->emplace_back(file->GetBlobDataHandle(), file->GetPath(),
file->name(), file->type(), last_modified,
size);
......@@ -644,11 +647,12 @@ bool V8ScriptValueSerializer::WriteFile(File* file,
if (file->HasValidSnapshotMetadata()) {
WriteUint32(1);
uint64_t size;
double last_modified_ms;
file->CaptureSnapshot(size, last_modified_ms);
base::Optional<base::Time> last_modified;
file->CaptureSnapshot(size, last_modified);
DCHECK_NE(size, std::numeric_limits<uint64_t>::max());
WriteUint64(size);
WriteDouble(last_modified_ms);
WriteDouble(last_modified ? last_modified->ToJsTimeIgnoringNull()
: std::numeric_limits<double>::quiet_NaN());
} else {
WriteUint32(0);
}
......
......@@ -48,7 +48,7 @@ WebBlob WebBlob::CreateFromUUID(const WebString& uuid,
WebBlob WebBlob::CreateFromFile(const WebString& path, uint64_t size) {
auto blob_data = std::make_unique<BlobData>();
blob_data->AppendFile(path, 0, size, InvalidFileTime());
blob_data->AppendFile(path, 0, size, base::nullopt);
return Blob::Create(BlobDataHandle::Create(std::move(blob_data), size));
}
......
......@@ -415,9 +415,9 @@ class ComplexFormDataBytesConsumer final : public BytesConsumer {
return;
}
}
blob_data->AppendFile(element.filename_, element.file_start_,
file_length,
element.expected_file_modification_time_);
blob_data->AppendFile(
element.filename_, element.file_start_, file_length,
DoubleTToOptionalTime(element.expected_file_modification_time_));
break;
}
case FormDataElement::kEncodedBlob:
......
......@@ -95,13 +95,11 @@ static std::unique_ptr<BlobData> CreateBlobDataForFileWithMetadata(
std::unique_ptr<BlobData> blob_data;
if (metadata.length == BlobData::kToEndOfFile) {
blob_data = BlobData::CreateForFileWithUnknownSize(
metadata.platform_path,
ToJsTimeOrNaN(metadata.modification_time) / kMsPerSecond);
metadata.platform_path, metadata.modification_time);
} else {
blob_data = std::make_unique<BlobData>();
blob_data->AppendFile(
metadata.platform_path, 0, metadata.length,
ToJsTimeOrNaN(metadata.modification_time) / kMsPerSecond);
blob_data->AppendFile(metadata.platform_path, 0, metadata.length,
metadata.modification_time);
}
blob_data->SetContentType(GetContentTypeFromFileName(
file_system_name, File::kWellKnownContentTypes));
......@@ -114,13 +112,11 @@ static std::unique_ptr<BlobData> CreateBlobDataForFileSystemURL(
std::unique_ptr<BlobData> blob_data;
if (metadata.length == BlobData::kToEndOfFile) {
blob_data = BlobData::CreateForFileSystemURLWithUnknownSize(
file_system_url,
ToJsTimeOrNaN(metadata.modification_time) / kMsPerSecond);
file_system_url, metadata.modification_time);
} else {
blob_data = std::make_unique<BlobData>();
blob_data->AppendFileSystemURL(
file_system_url, 0, metadata.length,
ToJsTimeOrNaN(metadata.modification_time) / kMsPerSecond);
blob_data->AppendFileSystemURL(file_system_url, 0, metadata.length,
metadata.modification_time);
}
blob_data->SetContentType(GetContentTypeFromFileName(
file_system_url.GetPath(), File::kWellKnownContentTypes));
......@@ -364,24 +360,25 @@ Blob* File::slice(int64_t start,
// FIXME: This involves synchronous file operation. We need to figure out how
// to make it asynchronous.
uint64_t size;
double modification_time_ms;
CaptureSnapshot(size, modification_time_ms);
base::Optional<base::Time> modification_time;
CaptureSnapshot(size, modification_time);
ClampSliceOffsets(size, start, end);
uint64_t length = end - start;
auto blob_data = std::make_unique<BlobData>();
blob_data->SetContentType(NormalizeType(content_type));
DCHECK(!path_.IsEmpty());
blob_data->AppendFile(path_, start, length,
modification_time_ms / kMsPerSecond);
blob_data->AppendFile(path_, start, length, modification_time);
return Blob::Create(BlobDataHandle::Create(std::move(blob_data), length));
}
void File::CaptureSnapshot(uint64_t& snapshot_size,
double& snapshot_modification_time_ms) const {
void File::CaptureSnapshot(
uint64_t& snapshot_size,
base::Optional<base::Time>& snapshot_modification_time) const {
if (HasValidSnapshotMetadata()) {
snapshot_size = *snapshot_size_;
snapshot_modification_time_ms = snapshot_modification_time_ms_;
snapshot_modification_time =
JsTimeToOptionalTime(snapshot_modification_time_ms_);
return;
}
......@@ -392,12 +389,12 @@ void File::CaptureSnapshot(uint64_t& snapshot_size,
FileMetadata metadata;
if (!HasBackingFile() || !GetFileMetadata(path_, metadata)) {
snapshot_size = 0;
snapshot_modification_time_ms = InvalidFileTime();
snapshot_modification_time = base::nullopt;
return;
}
snapshot_size = static_cast<uint64_t>(metadata.length);
snapshot_modification_time_ms = ToJsTimeOrNaN(metadata.modification_time);
snapshot_modification_time = metadata.modification_time;
}
void File::AppendTo(BlobData& blob_data) const {
......@@ -409,10 +406,10 @@ void File::AppendTo(BlobData& blob_data) const {
// FIXME: This involves synchronous file operation. We need to figure out how
// to make it asynchronous.
uint64_t size;
double modification_time_ms;
CaptureSnapshot(size, modification_time_ms);
base::Optional<base::Time> modification_time;
CaptureSnapshot(size, modification_time);
DCHECK(!path_.IsEmpty());
blob_data.AppendFile(path_, 0, size, modification_time_ms / kMsPerSecond);
blob_data.AppendFile(path_, 0, size, modification_time);
}
bool File::HasSameSource(const File& other) const {
......
......@@ -219,8 +219,9 @@ class CORE_EXPORT File final : public Blob {
// Note that this involves synchronous file operation. Think twice before
// calling this function.
void CaptureSnapshot(uint64_t& snapshot_size,
double& snapshot_modification_time_ms) const;
void CaptureSnapshot(
uint64_t& snapshot_size,
base::Optional<base::Time>& snapshot_modification_time) const;
// Returns true if this has a valid snapshot metadata
// (i.e. m_snapshotSize >= 0).
......
......@@ -575,7 +575,7 @@ void FileSystemDispatcher::DidCreateSnapshotFile(
auto blob_data = std::make_unique<BlobData>();
blob_data->AppendFile(file_metadata.platform_path, 0, file_metadata.length,
InvalidFileTime());
base::nullopt);
scoped_refptr<BlobDataHandle> snapshot_blob =
BlobDataHandle::Create(std::move(blob_data), file_metadata.length);
......
......@@ -120,24 +120,23 @@ std::unique_ptr<BlobData> BlobData::CreateForFileWithUnknownSize(
std::unique_ptr<BlobData> BlobData::CreateForFileWithUnknownSize(
const String& path,
double expected_modification_time) {
const base::Optional<base::Time>& expected_modification_time) {
std::unique_ptr<BlobData> data = base::WrapUnique(
new BlobData(FileCompositionStatus::SINGLE_UNKNOWN_SIZE_FILE));
data->elements_.push_back(DataElement::NewFile(DataElementFile::New(
WebStringToFilePath(path), 0, BlobData::kToEndOfFile,
base::Time::FromDoubleT(expected_modification_time))));
data->elements_.push_back(DataElement::NewFile(
DataElementFile::New(WebStringToFilePath(path), 0, BlobData::kToEndOfFile,
expected_modification_time)));
return data;
}
std::unique_ptr<BlobData> BlobData::CreateForFileSystemURLWithUnknownSize(
const KURL& file_system_url,
double expected_modification_time) {
const base::Optional<base::Time>& expected_modification_time) {
std::unique_ptr<BlobData> data = base::WrapUnique(
new BlobData(FileCompositionStatus::SINGLE_UNKNOWN_SIZE_FILE));
data->elements_.push_back(
DataElement::NewFileFilesystem(DataElementFilesystemURL::New(
file_system_url, 0, BlobData::kToEndOfFile,
base::Time::FromDoubleT(expected_modification_time))));
data->elements_.push_back(DataElement::NewFileFilesystem(
DataElementFilesystemURL::New(file_system_url, 0, BlobData::kToEndOfFile,
expected_modification_time)));
return data;
}
......@@ -162,10 +161,11 @@ void BlobData::AppendData(scoped_refptr<RawData> data) {
AppendDataInternal(base::make_span(data->data(), data->length()), data);
}
void BlobData::AppendFile(const String& path,
void BlobData::AppendFile(
const String& path,
int64_t offset,
int64_t length,
double expected_modification_time) {
const base::Optional<base::Time>& expected_modification_time) {
DCHECK_EQ(file_composition_, FileCompositionStatus::NO_UNKNOWN_SIZE_FILES)
<< "Blobs with a unknown-size file cannot have other items.";
DCHECK_NE(length, BlobData::kToEndOfFile)
......@@ -177,8 +177,7 @@ void BlobData::AppendFile(const String& path,
if (length == 0)
return;
elements_.push_back(DataElement::NewFile(DataElementFile::New(
WebStringToFilePath(path), offset, length,
base::Time::FromDoubleT(expected_modification_time))));
WebStringToFilePath(path), offset, length, expected_modification_time)));
}
void BlobData::AppendBlob(scoped_refptr<BlobDataHandle> data_handle,
......@@ -195,10 +194,11 @@ void BlobData::AppendBlob(scoped_refptr<BlobDataHandle> data_handle,
DataElementBlob::New(data_handle->CloneBlobRemote(), offset, length)));
}
void BlobData::AppendFileSystemURL(const KURL& url,
void BlobData::AppendFileSystemURL(
const KURL& url,
int64_t offset,
int64_t length,
double expected_modification_time) {
const base::Optional<base::Time>& expected_modification_time) {
DCHECK_EQ(file_composition_, FileCompositionStatus::NO_UNKNOWN_SIZE_FILES)
<< "Blobs with a unknown-size file cannot have other items.";
// Skip zero-byte items, as they don't matter for the contents of the blob.
......@@ -206,8 +206,7 @@ void BlobData::AppendFileSystemURL(const KURL& url,
return;
elements_.push_back(
DataElement::NewFileFilesystem(DataElementFilesystemURL::New(
url, offset, length,
base::Time::FromDoubleT(expected_modification_time))));
url, offset, length, expected_modification_time)));
}
void BlobData::AppendText(const String& text,
......
......@@ -113,10 +113,10 @@ class PLATFORM_EXPORT BlobData {
const String& path);
static std::unique_ptr<BlobData> CreateForFileWithUnknownSize(
const String& path,
double expected_modification_time);
const base::Optional<base::Time>& expected_modification_time);
static std::unique_ptr<BlobData> CreateForFileSystemURLWithUnknownSize(
const KURL& file_system_url,
double expected_modification_time);
const base::Optional<base::Time>& expected_modification_time);
// Detaches from current thread so that it can be passed to another thread.
void DetachFromCurrentThread();
......@@ -134,17 +134,18 @@ class PLATFORM_EXPORT BlobData {
void AppendFile(const String& path,
int64_t offset,
int64_t length,
double expected_modification_time);
const base::Optional<base::Time>& expected_modification_time);
// The given blob must not be a file with unknown size. Please use the
// File::appendTo instead.
void AppendBlob(scoped_refptr<BlobDataHandle>,
int64_t offset,
int64_t length);
void AppendFileSystemURL(const KURL&,
void AppendFileSystemURL(
const KURL&,
int64_t offset,
int64_t length,
double expected_modification_time);
const base::Optional<base::Time>& expected_modification_time);
void AppendText(const String&, bool normalize_line_endings_to_native);
// The value of the size property for a Blob who has this data.
......
......@@ -284,8 +284,8 @@ TEST_F(BlobDataHandleTest, CreateFromEmptyElements) {
auto data = std::make_unique<BlobData>();
data->AppendBytes(small_test_data_.data(), 0);
data->AppendBlob(empty_blob_, 0, 0);
data->AppendFile("path", 0, 0, 0.0);
data->AppendFileSystemURL(NullURL(), 0, 0, 0.0);
data->AppendFile("path", 0, 0, base::Time::UnixEpoch());
data->AppendFileSystemURL(NullURL(), 0, 0, base::Time::UnixEpoch());
TestCreateBlob(std::move(data), {});
}
......@@ -359,18 +359,17 @@ TEST_F(BlobDataHandleTest, CreateFromMergedSmallAndLargeBytes) {
}
TEST_F(BlobDataHandleTest, CreateFromFileAndFileSystemURL) {
double timestamp1 = base::Time::Now().ToDoubleT();
double timestamp2 = timestamp1 + 1;
base::Time timestamp1 = base::Time::Now();
base::Time timestamp2 = timestamp1 + base::TimeDelta::FromSeconds(1);
KURL url(NullURL(), "http://example.com/");
auto data = std::make_unique<BlobData>();
data->AppendFile("path", 4, 32, timestamp1);
data->AppendFileSystemURL(url, 15, 876, timestamp2);
Vector<ExpectedElement> expected_elements;
expected_elements.push_back(ExpectedElement::File(
"path", 4, 32, base::Time::FromDoubleT(timestamp1)));
expected_elements.push_back(ExpectedElement::FileFilesystem(
url, 15, 876, base::Time::FromDoubleT(timestamp2)));
expected_elements.push_back(ExpectedElement::File("path", 4, 32, timestamp1));
expected_elements.push_back(
ExpectedElement::FileFilesystem(url, 15, 876, timestamp2));
TestCreateBlob(std::move(data), std::move(expected_elements));
}
......@@ -385,11 +384,11 @@ TEST_F(BlobDataHandleTest, CreateFromFileWithUnknownSize) {
}
TEST_F(BlobDataHandleTest, CreateFromFilesystemFileWithUnknownSize) {
double timestamp = base::Time::Now().ToDoubleT();
base::Time timestamp = base::Time::Now();
KURL url(NullURL(), "http://example.com/");
Vector<ExpectedElement> expected_elements;
expected_elements.push_back(ExpectedElement::FileFilesystem(
url, 0, uint64_t(-1), base::Time::FromDoubleT(timestamp)));
expected_elements.push_back(
ExpectedElement::FileFilesystem(url, 0, uint64_t(-1), timestamp));
TestCreateBlob(
BlobData::CreateForFileSystemURLWithUnknownSize(url, timestamp),
......
......@@ -85,6 +85,22 @@ inline double ToJsTimeOrNaN(base::Optional<base::Time> time) {
return time ? time->ToJsTimeIgnoringNull() : InvalidFileTime();
}
// TODO(crbug.com/988343): Temporary conversion function. This should be
// removed.
inline base::Optional<base::Time> JsTimeToOptionalTime(double ms) {
if (!IsValidFileTime(ms))
return base::nullopt;
return base::Time::FromJsTime(ms);
}
// TODO(crbug.com/988343): Temporary conversion function. This should be
// removed.
inline base::Optional<base::Time> DoubleTToOptionalTime(double ms) {
if (!IsValidFileTime(ms))
return base::nullopt;
return base::Time::FromDoubleT(ms);
}
inline base::Optional<base::Time> NullableTimeToOptionalTime(base::Time time) {
if (time.is_null())
return base::nullopt;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment