Commit fdf2d30a authored by Victor Costan's avatar Victor Costan Committed by Commit Bot

File API: Match Firefox on readAsDataURL default MIME type.

In Firefox, File.readAsDataURL's default MIME type is to
application/octet-stream. Chrome currently leaves the MIME type out when
it is unknown. While this meets all relevant specifications, matching
Firefox's behavior makes the platform easier to reason about.

Bug: 48368
Change-Id: If480df5cc3a1177a58c7c3dc68c57f3d6408b9eb
Reviewed-on: https://chromium-review.googlesource.com/1104183
Commit-Queue: Victor Costan <pwnall@chromium.org>
Reviewed-by: default avatarMarijn Kruisselbrink <mek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568292}
parent aefa2144
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>FileAPI Test: filereader_readAsDataURL</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="help" href="http://dev.w3.org/2006/webapi/FileAPI/#readAsDataURL">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<div id="log"></div>
<script>
async_test(function() {
<!doctype html>
<meta charset="utf-8">
<title>FileAPI Test: FileReader.readAsDataURL</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="help" href="https://w3c.github.io/FileAPI/#readAsDataURL">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
async_test(function(testCase) {
var blob = new Blob(["TEST"]);
var reader = new FileReader();
reader.onload = this.step_func(function(evt) {
assert_equals(typeof reader.result, "string", "The result is string");
assert_equals(reader.result.indexOf("data:"), 0, "The result attribute starts with 'data'");
assert_true(reader.result.indexOf("base64") > 0, "The result attribute contains 'base64'");
assert_equals(reader.readyState, reader.DONE);
this.done();
testCase.done();
});
reader.onloadstart = this.step_func(function(evt) {
assert_equals(reader.readyState, reader.LOADING);
});
reader.onprogress = this.step_func(function(evt) {
assert_equals(reader.readyState, reader.LOADING);
});
reader.readAsDataURL(blob);
}, 'FileReader readyState during readAsDataURL');
async_test(function(testCase) {
var blob = new Blob(["TEST"], { type: 'text/plain' });
var reader = new FileReader();
reader.onload = this.step_func(function() {
assert_equals(reader.result, "data:text/plain;base64,VEVTVA==");
testCase.done();
});
reader.readAsDataURL(blob);
}, 'readAsDataURL result for Blob with specified MIME type');
async_test(function(testCase) {
var blob = new Blob(["TEST"]);
var reader = new FileReader();
reader.onload = this.step_func(function() {
assert_equals(reader.result,
"data:application/octet-stream;base64,VEVTVA==");
testCase.done();
});
</script>
</body>
</html>
reader.readAsDataURL(blob);
}, 'readAsDataURL result for Blob with unspecified MIME type');
</script>
\ No newline at end of file
......@@ -68,8 +68,8 @@ Received loadstart event
readyState: 1
Received load event
readyState: 2
result size: 21
result: data:;base64,Rmlyc3Q=
result size: 45
result: data:application/octet-stream;base64,Rmlyc3Q=
Received loadend event
Test reading a blob containing single text as data URL (optional content type provided)
readyState: 0
......
......@@ -69,8 +69,8 @@ Received loadstart event
readyState: 1
Received load event
readyState: 2
result size: 21
result: data:;base64,Rmlyc3Q=
result size: 45
result: data:application/octet-stream;base64,Rmlyc3Q=
Received loadend event
Test reading a blob containing single text as data URL (optional content type provided)
readyState: 0
......
......@@ -29,8 +29,8 @@ result size: 5
result: First
Received exception, name: TypeError, message: Failed to execute 'readAsBinaryString' on 'FileReaderSync': parameter 1 is not of type 'Blob'.
Test reading a blob containing single text as data URL
result size: 21
result: data:;base64,Rmlyc3Q=
result size: 45
result: data:application/octet-stream;base64,Rmlyc3Q=
Received exception, name: TypeError, message: Failed to execute 'readAsDataURL' on 'FileReaderSync': parameter 1 is not of type 'Blob'.
Test reading a blob containing single text as data URL (optional content type provided)
result size: 29
......
......@@ -423,7 +423,13 @@ String FileReaderLoader::ConvertToDataURL() {
if (!bytes_loaded_)
return builder.ToString();
if (data_type_.IsEmpty()) {
// Match Firefox in defaulting to application/octet-stream when the MIME
// type is unknown. See https://crbug.com/48368.
builder.Append("application/octet-stream");
} else {
builder.Append(data_type_);
}
builder.Append(";base64,");
Vector<char> out;
......
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