Skip to content

Commit

Permalink
Issue #1820 - Improve the Blob implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Shing Lyu authored and shinglyu committed Dec 9, 2014
1 parent bdb3a25 commit f2885b8
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 22 deletions.
101 changes: 92 additions & 9 deletions components/script/dom/blob.rs
Expand Up @@ -3,11 +3,15 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::InheritTypes::FileDerived;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Temporary;
use dom::bindings::global::{GlobalRef, GlobalField};
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector, reflect_dom_object};
use dom::bindings::error::Fallible;
use dom::bindings::codegen::Bindings::BlobBinding;
use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods;

use servo_util::str::DOMString;
use std::cmp::{min, max};

#[jstraceable]
pub enum BlobType {
Expand All @@ -18,28 +22,107 @@ pub enum BlobType {
#[dom_struct]
pub struct Blob {
reflector_: Reflector,
type_: BlobType
type_: BlobType,
bytes: Option<Vec<u8>>,
typeString: DOMString,
global: GlobalField
// isClosed_: bool
}

impl Blob {
pub fn new_inherited() -> Blob {
pub fn new_inherited(global: &GlobalRef, bytes: Option<Vec<u8>>) -> Blob {
Blob {
reflector_: Reflector::new(),
type_: BlobTypeId
type_: BlobTypeId,
bytes: bytes,
typeString: "".to_string(),
global: GlobalField::from_rooted(global)
//isClosed_: false
}
}

pub fn new(global: GlobalRef) -> Temporary<Blob> {
reflect_dom_object(box Blob::new_inherited(),
global,
pub fn new(global: &GlobalRef, bytes: Option<Vec<u8>>) -> Temporary<Blob> {
reflect_dom_object(box Blob::new_inherited(global, bytes),
*global,
BlobBinding::Wrap)
}

pub fn Constructor(global: &GlobalRef) -> Fallible<Temporary<Blob>> {
Ok(Blob::new(*global))
Ok(Blob::new(global, None))
}

pub fn Constructor_(global: &GlobalRef, blobParts: DOMString) -> Fallible<Temporary<Blob>> {
//TODO: accept other blobParts types - ArrayBuffer or ArrayBufferView or Blob
//TODO: accept options parameter
let bytes: Option<Vec<u8>> = Some(blobParts.into_bytes());
Ok(Blob::new(global, bytes))
}
}

impl<'a> BlobMethods for JSRef<'a, Blob> {
fn Size(self) -> u64{
match self.bytes {
None => 0,
Some(ref bytes) => bytes.len() as u64
}
}

fn Type(self) -> DOMString {
self.typeString.clone()
}

fn Slice(self, start: Option<i64>, end: Option<i64>,
_contentType: Option<DOMString>) -> Temporary<Blob> {
let size: i64 = self.Size().to_i64().unwrap();
let relativeStart: i64 = match start {
None => 0,
Some(start) => {
if start < 0 {
max(size.to_i64().unwrap() + start, 0)
} else {
min(start, size)
}
}
};
let relativeEnd: i64 = match end {
None => size,
Some(end) => {
if end < 0 {
max(size + end, 0)
} else {
min(end, size)
}
}
};
/*
let relativeContentType = match contentType {
None => "".to_string(),
Some(str) => str
};
*/
//TODO: actually use relativeContentType in constructor
let span: i64 = max(relativeEnd - relativeStart, 0);
let global = self.global.root();
match self.bytes {
None => Blob::new(&global.root_ref(), None),
Some(ref vec) => {
let start = relativeStart.to_uint().unwrap();
let end = (relativeStart + span).to_uint().unwrap();
let mut bytes: Vec<u8> = Vec::new();
bytes.push_all(vec.slice(start, end));
Blob::new(&global.root_ref(), Some(bytes))
}
}
}

//fn IsClosed(self) -> bool {
// self.isClosed_.clone()
//}

//fn Close(self) {
// TODO
//}
}
impl Reflectable for Blob {
fn reflector<'a>(&'a self) -> &'a Reflector {
&self.reflector_
Expand Down
10 changes: 5 additions & 5 deletions components/script/dom/file.rs
Expand Up @@ -18,19 +18,19 @@ pub struct File {
}

impl File {
fn new_inherited(_file_bits: JSRef<Blob>, name: DOMString) -> File {
fn new_inherited(global: &GlobalRef, _file_bits: JSRef<Blob>, name: DOMString) -> File {
File {
blob: Blob::new_inherited(),
blob: Blob::new_inherited(global, None),
name: name,
type_: FileTypeId
}
// XXXManishearth Once Blob is able to store data
// the relevant subfields of file_bits should be copied over
}

pub fn new(global: GlobalRef, file_bits: JSRef<Blob>, name: DOMString) -> Temporary<File> {
reflect_dom_object(box File::new_inherited(file_bits, name),
global,
pub fn new(global: &GlobalRef, file_bits: JSRef<Blob>, name: DOMString) -> Temporary<File> {
reflect_dom_object(box File::new_inherited(global, file_bits, name),
*global,
FileBinding::Wrap)
}

Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/formdata.rs
Expand Up @@ -121,6 +121,6 @@ impl PrivateFormDataHelpers for FormData {
let global = self.global.root();
let f: Option<JSRef<File>> = FileCast::to_ref(value);
let name = filename.unwrap_or(f.map(|inner| inner.name().clone()).unwrap_or("blob".to_string()));
File::new(global.root_ref(), value, name)
File::new(&global.root_ref(), value, name)
}
}
8 changes: 4 additions & 4 deletions components/script/dom/testbinding.rs
Expand Up @@ -57,7 +57,7 @@ impl<'a> TestBindingMethods for JSRef<'a, TestBinding> {
fn SetEnumAttribute(self, _: TestEnum) {}
fn InterfaceAttribute(self) -> Temporary<Blob> {
let global = self.global.root();
Blob::new(global.root_ref())
Blob::new(&global.root_ref(), None)
}
fn SetInterfaceAttribute(self, _: JSRef<Blob>) {}
fn UnionAttribute(self) -> HTMLElementOrLong { eLong(0) }
Expand Down Expand Up @@ -96,7 +96,7 @@ impl<'a> TestBindingMethods for JSRef<'a, TestBinding> {
fn GetEnumAttributeNullable(self) -> Option<TestEnum> { Some(_empty) }
fn GetInterfaceAttributeNullable(self) -> Option<Temporary<Blob>> {
let global = self.global.root();
Some(Blob::new(global.root_ref()))
Some(Blob::new(&global.root_ref(), None))
}
fn SetInterfaceAttributeNullable(self, _: Option<JSRef<Blob>>) {}
fn GetUnionAttributeNullable(self) -> Option<HTMLElementOrLong> { Some(eLong(0)) }
Expand All @@ -120,7 +120,7 @@ impl<'a> TestBindingMethods for JSRef<'a, TestBinding> {
fn ReceiveEnum(self) -> TestEnum { _empty }
fn ReceiveInterface(self) -> Temporary<Blob> {
let global = self.global.root();
Blob::new(global.root_ref())
Blob::new(&global.root_ref(), None)
}
fn ReceiveAny(self, _: *mut JSContext) -> JSVal { NullValue() }
fn ReceiveUnion(self) -> HTMLElementOrLong { eLong(0) }
Expand All @@ -142,7 +142,7 @@ impl<'a> TestBindingMethods for JSRef<'a, TestBinding> {
fn ReceiveNullableEnum(self) -> Option<TestEnum> { Some(_empty) }
fn ReceiveNullableInterface(self) -> Option<Temporary<Blob>> {
let global = self.global.root();
Some(Blob::new(global.root_ref()))
Some(Blob::new(&global.root_ref(), None))
}
fn ReceiveNullableUnion(self) -> Option<HTMLElementOrLong> { Some(eLong(0)) }
fn ReceiveNullableUnion2(self) -> Option<EventOrString> { Some(eString("".to_string())) }
Expand Down
11 changes: 8 additions & 3 deletions components/script/dom/webidls/Blob.webidl
Expand Up @@ -6,18 +6,23 @@
// http://dev.w3.org/2006/webapi/FileAPI/#dfn-Blob
//[Exposed=Window,Worker][Constructor,
// Constructor(sequence<(ArrayBuffer or ArrayBufferView or Blob or DOMString)> blobParts, optional BlobPropertyBag options)]
[Constructor]
[Constructor,
Constructor(DOMString blobParts)]
interface Blob {

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

//slice Blob into byte-ranged chunks

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

};
Expand Down
15 changes: 15 additions & 0 deletions tests/content/test_blob.html
@@ -0,0 +1,15 @@
<html>
<head>
<script src="harness.js"></script>
<script>
var testData = ['<a id="a"><b id="b">hey!</b></a>'];
var b = new Blob(testData); // the blob
is(b.size, 32);
is(b.type, "");

var bs = b.slice(0, 5);
is(bs.size, 5);
is(b.type, "");
</script>
</head>
</html>

5 comments on commit f2885b8

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from jdm
at shinglyu@f2885b8

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging shinglyu/servo/blob = f2885b8 into auto

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shinglyu/servo/blob = f2885b8 merged ok, testing candidate = d2a67ab

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors-servo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d2a67ab

Please sign in to comment.