Skip to content

Commit

Permalink
Auto merge of #6803 - farodin91:blob, r=Ms2ger
Browse files Browse the repository at this point in the history
Adding for support Blob.{close,isClose} #6723



<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/6803)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Jul 29, 2015
2 parents 2ec4c49 + 20f99e9 commit 96b0f96
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 61 deletions.
38 changes: 22 additions & 16 deletions components/script/dom/blob.rs
Expand Up @@ -18,6 +18,7 @@ use num::ToPrimitive;
use std::ascii::AsciiExt;
use std::borrow::ToOwned;
use std::cmp::{min, max};
use std::cell::{Cell};

#[derive(JSTraceable)]
pub enum BlobTypeId {
Expand All @@ -32,8 +33,8 @@ pub struct Blob {
type_: BlobTypeId,
bytes: Option<Vec<u8>>,
typeString: DOMString,
global: GlobalField
// isClosed_: bool
global: GlobalField,
isClosed_: Cell<bool>
}

fn is_ascii_printable(string: &DOMString) -> bool{
Expand All @@ -50,8 +51,8 @@ impl Blob {
type_: type_,
bytes: bytes,
typeString: typeString.to_owned(),
global: GlobalField::from_rooted(&global)
//isClosed_: false
global: GlobalField::from_rooted(&global),
isClosed_: Cell::new(false)
}
}

Expand Down Expand Up @@ -83,7 +84,6 @@ impl Blob {

pub trait BlobHelpers {
fn read_out_buffer(self) -> Receiver<Vec<u8>>;
fn read_out_type(self) -> DOMString;
}

impl<'a> BlobHelpers for &'a Blob {
Expand All @@ -92,9 +92,6 @@ impl<'a> BlobHelpers for &'a Blob {
send.send(self.bytes.clone().unwrap_or(vec![])).unwrap();
recv
}
fn read_out_type(self) -> DOMString {
self.typeString.clone()
}
}

impl<'a> BlobMethods for &'a Blob {
Expand Down Expand Up @@ -159,15 +156,24 @@ impl<'a> BlobMethods for &'a Blob {
}
}

// http://dev.w3.org/2006/webapi/FileAPI/#dfn-isClosed
//fn IsClosed(self) -> bool {
// self.isClosed_.clone()
//}
// https://dev.w3.org/2006/webapi/FileAPI/#dfn-isClosed
fn IsClosed(self) -> bool {
self.isClosed_.get()
}

// https://dev.w3.org/2006/webapi/FileAPI/#dfn-close
fn Close(self) {
// Step 1
if self.isClosed_.get() {
return;
}

// Step 2
self.isClosed_.set(true);

// http://dev.w3.org/2006/webapi/FileAPI/#dfn-close
//fn Close(self) {
// TODO
//}
// TODO Step 3 if Blob URL Store is implemented

}
}

impl FileDerived for Blob {
Expand Down
28 changes: 23 additions & 5 deletions components/script/dom/filereader.rs
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods;
use dom::bindings::codegen::Bindings::FileReaderBinding::{self, FileReaderConstants, FileReaderMethods};
use dom::bindings::codegen::InheritTypes::{EventCast, EventTargetCast};
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
Expand Down Expand Up @@ -268,7 +269,6 @@ impl FileReader {

Ok(Some(output))
}

}

impl<'a> FileReaderMethods for &'a FileReader {
Expand All @@ -287,13 +287,22 @@ impl<'a> FileReaderMethods for &'a FileReader {
if self.ready_state.get() == FileReaderReadyState::Loading {
return Err(InvalidState);
}
//TODO STEP 2 if isClosed implemented in Blob

// Step 2
if blob.IsClosed() {
let global = self.global.root();
let exception = DOMException::new(global.r(), DOMErrorName::InvalidStateError);
self.error.set(Some(JS::from_rooted(&exception)));

self.dispatch_progress_event("error".to_owned(), 0, None);
return Ok(());
}

// Step 3
self.change_ready_state(FileReaderReadyState::Loading);

let bytes = blob.read_out_buffer();
let type_ = blob.read_out_type();
let type_ = blob.Type();

let load_data = ReadData::new(bytes, type_, None, FileReaderFunction::ReadAsDataUrl);

Expand All @@ -307,13 +316,22 @@ impl<'a> FileReaderMethods for &'a FileReader {
if self.ready_state.get() == FileReaderReadyState::Loading {
return Err(InvalidState);
}
//TODO STEP 2 if isClosed implemented in Blob

// Step 2
if blob.IsClosed() {
let global = self.global.root();
let exception = DOMException::new(global.r(), DOMErrorName::InvalidStateError);
self.error.set(Some(JS::from_rooted(&exception)));

self.dispatch_progress_event("error".to_owned(), 0, None);
return Ok(());
}

// Step 3
self.change_ready_state(FileReaderReadyState::Loading);

let bytes = blob.read_out_buffer();
let type_ = blob.read_out_type();
let type_ = blob.Type();

let load_data = ReadData::new(bytes, type_, label, FileReaderFunction::ReadAsText);

Expand Down
7 changes: 4 additions & 3 deletions components/script/dom/webidls/Blob.webidl
Expand Up @@ -8,19 +8,20 @@
// Constructor(sequence<(ArrayBuffer or ArrayBufferView or Blob or DOMString)> blobParts,
// optional BlobPropertyBag options)]
[Constructor,
Constructor(DOMString blobParts, optional BlobPropertyBag options)]
Constructor(DOMString blobParts, optional BlobPropertyBag options),
Exposed=Window/*,Worker*/]
interface Blob {

readonly attribute unsigned long long size;
readonly attribute DOMString type;
//readonly attribute boolean isClosed;
readonly attribute boolean isClosed;

//slice Blob into byte-ranged chunks

Blob slice([Clamp] optional long long start,
[Clamp] optional long long end,
optional DOMString contentType);
//void close();
void close();

};

Expand Down
5 changes: 0 additions & 5 deletions tests/wpt/metadata/FileAPI/blob/Blob-close.html.ini

This file was deleted.

12 changes: 0 additions & 12 deletions tests/wpt/metadata/FileAPI/idlharness.html.ini
Expand Up @@ -9,18 +9,6 @@
[URL interface: operation revokeObjectURL(DOMString)]
expected: FAIL

[Blob interface: attribute isClosed]
expected: FAIL

[Blob interface: operation close()]
expected: FAIL

[Blob interface: new Blob(["TEST"\]) must inherit property "isClosed" with the proper type (2)]
expected: FAIL

[Blob interface: new Blob(["TEST"\]) must inherit property "close" with the proper type (4)]
expected: FAIL

[File interface: existence and properties of interface object]
expected: FAIL

Expand Down
12 changes: 0 additions & 12 deletions tests/wpt/metadata/FileAPI/idlharness.worker.js.ini
Expand Up @@ -9,18 +9,6 @@
[URL interface: operation revokeObjectURL(DOMString)]
expected: FAIL

[Blob interface: attribute isClosed]
expected: FAIL

[Blob interface: operation close()]
expected: FAIL

[Blob interface: new Blob(["TEST"\]) must inherit property "isClosed" with the proper type (2)]
expected: FAIL

[Blob interface: new Blob(["TEST"\]) must inherit property "close" with the proper type (4)]
expected: FAIL

[File interface: existence and properties of interface object]
expected: FAIL

Expand Down
26 changes: 18 additions & 8 deletions tests/wpt/web-platform-tests/FileAPI/blob/Blob-close.html
Expand Up @@ -11,17 +11,27 @@
var blob = new Blob(["TEST"]);
var sliced = blob.slice();
blob.close();
test_blob(function() {
return blob;
}, {
expected: "",
type: "",
desc: "Blob should be empty."
});

async_test(function(t) {
var reader = new FileReader();

reader.onload = t.step_func(function(evt) {
assert_unreached("Should not dispatch the load event");
});

reader.onerror = t.step_func(function(e) {
assert_equals(reader.result, null);
assert_equals(reader.error.code, DOMException.INVALID_STATE_ERR);
t.done();
});

reader.readAsText(blob, "UTF-8");
}, "Closed Blob");

test_blob(function() {
return sliced;
}, {
expected: "PASS",
expected: "TEST",
type: "",
desc: "Slice should still have the data."
});
Expand Down

0 comments on commit 96b0f96

Please sign in to comment.