Auto merge of #12910 - creativcoder:swsender, r=jdm

Implement postMessage for ServiceWorkers

<!-- Please describe your changes on the following line: -->

Fixes #12773
r? @jdm

Changes:
* Implements `postMessage` on `ServiceWorker` object.
* Removes unused channels from sw and their scopes.
* Fixes a crash when calling `scope.script_chan()` in sw-scopes event handling.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #12773

<!-- Either: -->
- [X] There are tests for these changes at `tests/html/service-worker`

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12910)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-09-15 08:34:22 -05:00 committed by GitHub
commit 0ec4ea4ee1
19 changed files with 256 additions and 118 deletions

View file

@ -12,11 +12,14 @@ use js::jsapi::{JSContext, JS_ReadStructuredClone, JS_STRUCTURED_CLONE_VERSION};
use js::jsapi::{JS_ClearPendingException, JS_WriteStructuredClone};
use libc::size_t;
use std::ptr;
use std::slice;
/// A buffer for a structured clone.
pub struct StructuredCloneData {
data: *mut u64,
nbytes: size_t,
pub enum StructuredCloneData {
/// A non-serializable (default) variant
Struct(*mut u64, size_t),
/// A variant that can be serialized
Vector(Vec<u8>)
}
impl StructuredCloneData {
@ -39,26 +42,47 @@ impl StructuredCloneData {
}
return Err(Error::DataClone);
}
Ok(StructuredCloneData {
data: data,
nbytes: nbytes,
})
Ok(StructuredCloneData::Struct(data, nbytes))
}
/// Converts a StructuredCloneData to Vec<u8> for inter-thread sharing
pub fn move_to_arraybuffer(self) -> Vec<u8> {
match self {
StructuredCloneData::Struct(data, nbytes) => {
unsafe {
slice::from_raw_parts(data as *mut u8, nbytes).to_vec()
}
}
StructuredCloneData::Vector(msg) => msg
}
}
/// Reads a structured clone.
///
/// Panics if `JS_ReadStructuredClone` fails.
pub fn read(self, global: GlobalRef, rval: MutableHandleValue) {
fn read_clone(global: GlobalRef, data: *mut u64, nbytes: size_t, rval: MutableHandleValue) {
unsafe {
assert!(JS_ReadStructuredClone(global.get_cx(),
self.data,
self.nbytes,
data,
nbytes,
JS_STRUCTURED_CLONE_VERSION,
rval,
ptr::null(),
ptr::null_mut()));
}
}
/// Thunk for the actual `read_clone` method. Resolves proper variant for read_clone.
pub fn read(self, global: GlobalRef, rval: MutableHandleValue) {
match self {
StructuredCloneData::Vector(mut vec_msg) => {
let nbytes = vec_msg.len();
let data = vec_msg.as_mut_ptr() as *mut u64;
StructuredCloneData::read_clone(global, data, nbytes, rval);
}
StructuredCloneData::Struct(data, nbytes) => StructuredCloneData::read_clone(global, data, nbytes, rval)
}
}
}
unsafe impl Send for StructuredCloneData {}