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> <!doctype html>
<html> <meta charset="utf-8">
<head> <title>FileAPI Test: FileReader.readAsDataURL</title>
<meta charset="utf-8"> <link rel="author" title="Intel" href="http://www.intel.com">
<title>FileAPI Test: filereader_readAsDataURL</title> <link rel="help" href="https://w3c.github.io/FileAPI/#readAsDataURL">
<link rel="author" title="Intel" href="http://www.intel.com"> <script src="/resources/testharness.js"></script>
<link rel="help" href="http://dev.w3.org/2006/webapi/FileAPI/#readAsDataURL"> <script src="/resources/testharnessreport.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script> <script>
</head> async_test(function(testCase) {
<body> var blob = new Blob(["TEST"]);
<div id="log"></div> var reader = new FileReader();
<script> reader.onload = this.step_func(function(evt) {
async_test(function() { assert_equals(reader.readyState, reader.DONE);
var blob = new Blob(["TEST"]); testCase.done();
var reader = new FileReader(); });
reader.onloadstart = this.step_func(function(evt) {
reader.onload = this.step_func(function(evt) { assert_equals(reader.readyState, reader.LOADING);
assert_equals(typeof reader.result, "string", "The result is string"); });
assert_equals(reader.result.indexOf("data:"), 0, "The result attribute starts with 'data'"); reader.onprogress = this.step_func(function(evt) {
assert_true(reader.result.indexOf("base64") > 0, "The result attribute contains 'base64'"); assert_equals(reader.readyState, reader.LOADING);
assert_equals(reader.readyState, reader.DONE); });
this.done();
}); reader.readAsDataURL(blob);
}, 'FileReader readyState during readAsDataURL');
reader.onloadstart = this.step_func(function(evt) {
assert_equals(reader.readyState, reader.LOADING); async_test(function(testCase) {
}); var blob = new Blob(["TEST"], { type: 'text/plain' });
var reader = new FileReader();
reader.onprogress = this.step_func(function(evt) {
assert_equals(reader.readyState, reader.LOADING); reader.onload = this.step_func(function() {
}); assert_equals(reader.result, "data:text/plain;base64,VEVTVA==");
testCase.done();
reader.readAsDataURL(blob); });
}); reader.readAsDataURL(blob);
</script> }, 'readAsDataURL result for Blob with specified MIME type');
</body>
</html> 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();
});
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 ...@@ -68,8 +68,8 @@ Received loadstart event
readyState: 1 readyState: 1
Received load event Received load event
readyState: 2 readyState: 2
result size: 21 result size: 45
result: data:;base64,Rmlyc3Q= result: data:application/octet-stream;base64,Rmlyc3Q=
Received loadend event Received loadend event
Test reading a blob containing single text as data URL (optional content type provided) Test reading a blob containing single text as data URL (optional content type provided)
readyState: 0 readyState: 0
......
...@@ -69,8 +69,8 @@ Received loadstart event ...@@ -69,8 +69,8 @@ Received loadstart event
readyState: 1 readyState: 1
Received load event Received load event
readyState: 2 readyState: 2
result size: 21 result size: 45
result: data:;base64,Rmlyc3Q= result: data:application/octet-stream;base64,Rmlyc3Q=
Received loadend event Received loadend event
Test reading a blob containing single text as data URL (optional content type provided) Test reading a blob containing single text as data URL (optional content type provided)
readyState: 0 readyState: 0
......
...@@ -29,8 +29,8 @@ result size: 5 ...@@ -29,8 +29,8 @@ result size: 5
result: First result: First
Received exception, name: TypeError, message: Failed to execute 'readAsBinaryString' on 'FileReaderSync': parameter 1 is not of type 'Blob'. 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 Test reading a blob containing single text as data URL
result size: 21 result size: 45
result: data:;base64,Rmlyc3Q= 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'. 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) Test reading a blob containing single text as data URL (optional content type provided)
result size: 29 result size: 29
......
...@@ -423,7 +423,13 @@ String FileReaderLoader::ConvertToDataURL() { ...@@ -423,7 +423,13 @@ String FileReaderLoader::ConvertToDataURL() {
if (!bytes_loaded_) if (!bytes_loaded_)
return builder.ToString(); return builder.ToString();
builder.Append(data_type_); 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,"); builder.Append(";base64,");
Vector<char> out; 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