Skip to content

Commit

Permalink
Auto merge of #7214 - tomjakubowski:websocket-binaryType, r=Ms2ger
Browse files Browse the repository at this point in the history
Implement WebSocket#binaryType

Closes #7098

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7214)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Aug 16, 2015
2 parents 55e755e + e92f462 commit 7c63c7d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion components/script/dom/webidls/WebSocket.webidl
Expand Up @@ -27,7 +27,7 @@ interface WebSocket : EventTarget {

//messaging
attribute EventHandler onmessage;
//attribute BinaryType binaryType;
attribute BinaryType binaryType;
[Throws] void send(optional USVString data);
//void send(Blob data);
//void send(ArrayBuffer data);
Expand Down
36 changes: 33 additions & 3 deletions components/script/dom/websocket.rs
Expand Up @@ -4,7 +4,7 @@

use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::WebSocketBinding;
use dom::bindings::codegen::Bindings::WebSocketBinding::WebSocketMethods;
use dom::bindings::codegen::Bindings::WebSocketBinding::{BinaryType, WebSocketMethods};
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::InheritTypes::EventTargetCast;
use dom::bindings::codegen::InheritTypes::EventCast;
Expand All @@ -29,8 +29,10 @@ use util::str::DOMString;
use util::task::spawn_named;

use js::jsapi::{RootedValue, JSAutoRequest, JSAutoCompartment};
use js::jsapi::{JS_NewArrayBuffer, JS_GetArrayBufferData};
use js::jsval::UndefinedValue;
use hyper::header::Host;
use libc::{uint8_t, uint32_t};
use websocket::Message;
use websocket::ws::sender::Sender as Sender_Object;
use websocket::client::sender::Sender;
Expand All @@ -45,6 +47,7 @@ use websocket::ws::util::url::parse_url;

use std::borrow::ToOwned;
use std::cell::{Cell, RefCell};
use std::ptr;
use std::sync::{Arc, Mutex};

#[derive(JSTraceable, PartialEq, Copy, Clone, Debug, HeapSizeOf)]
Expand Down Expand Up @@ -78,6 +81,7 @@ pub struct WebSocket {
code: Cell<u16>, //Closing code
reason: DOMRefCell<DOMString>, //Closing reason
data: DOMRefCell<DOMString>, //Data from send - TODO: Remove after buffer is added.
binary_type: Cell<BinaryType>,
}

/// *Establish a WebSocket Connection* as defined in RFC 6455.
Expand Down Expand Up @@ -117,6 +121,7 @@ impl WebSocket {
code: Cell::new(0),
reason: DOMRefCell::new("".to_owned()),
data: DOMRefCell::new("".to_owned()),
binary_type: Cell::new(BinaryType::Blob),
}

}
Expand Down Expand Up @@ -238,6 +243,16 @@ impl<'a> WebSocketMethods for &'a WebSocket {
self.ready_state.get() as u16
}

// https://html.spec.whatwg.org/multipage/#dom-websocket-binarytype
fn BinaryType(self) -> BinaryType {
self.binary_type.get()
}

// https://html.spec.whatwg.org/multipage/#dom-websocket-binarytype
fn SetBinaryType(self, btype: BinaryType) {
self.binary_type.set(btype)
}

// https://html.spec.whatwg.org/multipage/#dom-websocket-send
fn Send(self, data: Option<USVString>) -> Fallible<()> {
match self.ready_state.get() {
Expand Down Expand Up @@ -396,6 +411,7 @@ struct MessageReceivedTask {
}

impl Runnable for MessageReceivedTask {
#[allow(unsafe_code)]
fn handler(self: Box<Self>) {
let ws = self.address.root();
debug!("MessageReceivedTask::handler({:p}): readyState={:?}", &*ws,
Expand All @@ -415,8 +431,22 @@ impl Runnable for MessageReceivedTask {
match self.message {
MessageData::Text(text) => text.to_jsval(cx, message.handle_mut()),
MessageData::Binary(data) => {
let blob = Blob::new(global.r(), Some(data), "");
blob.to_jsval(cx, message.handle_mut());
match ws.binary_type.get() {
BinaryType::Blob => {
let blob = Blob::new(global.r(), Some(data), "");
blob.to_jsval(cx, message.handle_mut());
}
BinaryType::Arraybuffer => {
unsafe {
let len = data.len() as uint32_t;
let buf = JS_NewArrayBuffer(cx, len);
let buf_data: *mut uint8_t = JS_GetArrayBufferData(buf, ptr::null());
ptr::copy_nonoverlapping(data.as_ptr(), buf_data, len as usize);
buf.to_jsval(cx, message.handle_mut());
}
}

}
},
}

Expand Down
4 changes: 0 additions & 4 deletions tests/wpt/metadata/html/dom/interfaces.html.ini
Expand Up @@ -8298,9 +8298,6 @@
[WebSocket interface: attribute protocol]
expected: FAIL

[WebSocket interface: attribute binaryType]
expected: FAIL

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

Expand Down Expand Up @@ -8981,4 +8978,3 @@
[HTMLOptionElement interface: new Option() must inherit property "index" with the proper type (7)]
expected: FAIL

This file was deleted.

9 changes: 0 additions & 9 deletions tests/wpt/metadata/websockets/binaryType-wrong-value.htm.ini

This file was deleted.

7 changes: 0 additions & 7 deletions tests/wpt/metadata/websockets/interfaces.html.ini
Expand Up @@ -15,9 +15,6 @@
[WebSocket interface: attribute protocol]
expected: FAIL

[WebSocket interface: attribute binaryType]
expected: FAIL

[WebSocket interface: operation send(DOMString)]
expected: FAIL

Expand All @@ -42,9 +39,6 @@
[WebSocket interface: new WebSocket("ws://foo") must inherit property "protocol" with the proper type (11)]
expected: FAIL

[WebSocket interface: new WebSocket("ws://foo") must inherit property "binaryType" with the proper type (14)]
expected: FAIL

[WebSocket interface: calling send(DOMString) on new WebSocket("ws://foo") with too few arguments must throw TypeError]
expected: FAIL

Expand All @@ -62,4 +56,3 @@

[CloseEvent interface: existence and properties of interface prototype object]
expected: FAIL

0 comments on commit 7c63c7d

Please sign in to comment.