mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
make structured clone an enum
This commit is contained in:
parent
9dcb7348a2
commit
314dedb96f
6 changed files with 51 additions and 56 deletions
|
@ -11,14 +11,15 @@ use js::jsapi::{HandleValue, MutableHandleValue};
|
||||||
use js::jsapi::{JSContext, JS_ReadStructuredClone, JS_STRUCTURED_CLONE_VERSION};
|
use js::jsapi::{JSContext, JS_ReadStructuredClone, JS_STRUCTURED_CLONE_VERSION};
|
||||||
use js::jsapi::{JS_ClearPendingException, JS_WriteStructuredClone};
|
use js::jsapi::{JS_ClearPendingException, JS_WriteStructuredClone};
|
||||||
use libc::size_t;
|
use libc::size_t;
|
||||||
use script_traits::DOMMessage;
|
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
|
||||||
/// A buffer for a structured clone.
|
/// A buffer for a structured clone.
|
||||||
pub struct StructuredCloneData {
|
pub enum StructuredCloneData {
|
||||||
data: *mut u64,
|
/// A non-serializable (default) variant
|
||||||
nbytes: size_t,
|
Struct(*mut u64, size_t),
|
||||||
|
/// A variant that can be serialized
|
||||||
|
Vector(Vec<u8>)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StructuredCloneData {
|
impl StructuredCloneData {
|
||||||
|
@ -41,44 +42,47 @@ impl StructuredCloneData {
|
||||||
}
|
}
|
||||||
return Err(Error::DataClone);
|
return Err(Error::DataClone);
|
||||||
}
|
}
|
||||||
Ok(StructuredCloneData {
|
Ok(StructuredCloneData::Struct(data, nbytes))
|
||||||
data: data,
|
|
||||||
nbytes: nbytes,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a StructuredCloneData to Vec<u8> for inter-thread sharing
|
/// Converts a StructuredCloneData to Vec<u8> for inter-thread sharing
|
||||||
pub fn move_to_arraybuffer(self) -> DOMMessage {
|
pub fn move_to_arraybuffer(self) -> Vec<u8> {
|
||||||
unsafe {
|
match self {
|
||||||
DOMMessage(slice::from_raw_parts(self.data as *mut u8, self.nbytes).to_vec())
|
StructuredCloneData::Struct(data, nbytes) => {
|
||||||
}
|
unsafe {
|
||||||
}
|
slice::from_raw_parts(data as *mut u8, nbytes).to_vec()
|
||||||
|
}
|
||||||
/// Converts back to StructuredCloneData
|
}
|
||||||
pub fn make_structured_clone(data: DOMMessage) -> StructuredCloneData {
|
StructuredCloneData::Vector(msg) => msg
|
||||||
let DOMMessage(mut data) = data;
|
|
||||||
let nbytes = data.len();
|
|
||||||
let data = data.as_mut_ptr() as *mut u64;
|
|
||||||
StructuredCloneData {
|
|
||||||
data: data,
|
|
||||||
nbytes: nbytes
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a structured clone.
|
/// Reads a structured clone.
|
||||||
///
|
///
|
||||||
/// Panics if `JS_ReadStructuredClone` fails.
|
/// 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 {
|
unsafe {
|
||||||
assert!(JS_ReadStructuredClone(global.get_cx(),
|
assert!(JS_ReadStructuredClone(global.get_cx(),
|
||||||
self.data,
|
data,
|
||||||
self.nbytes,
|
nbytes,
|
||||||
JS_STRUCTURED_CLONE_VERSION,
|
JS_STRUCTURED_CLONE_VERSION,
|
||||||
rval,
|
rval,
|
||||||
ptr::null(),
|
ptr::null(),
|
||||||
ptr::null_mut()));
|
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 {}
|
unsafe impl Send for StructuredCloneData {}
|
||||||
|
|
|
@ -11,13 +11,13 @@ use dom::bindings::global::GlobalRef;
|
||||||
use dom::bindings::inheritance::Castable;
|
use dom::bindings::inheritance::Castable;
|
||||||
use dom::bindings::js::Root;
|
use dom::bindings::js::Root;
|
||||||
use dom::bindings::refcounted::Trusted;
|
use dom::bindings::refcounted::Trusted;
|
||||||
use dom::bindings::reflector::reflect_dom_object;
|
use dom::bindings::reflector::{Reflectable, reflect_dom_object};
|
||||||
use dom::bindings::str::USVString;
|
use dom::bindings::str::USVString;
|
||||||
use dom::bindings::structuredclone::StructuredCloneData;
|
use dom::bindings::structuredclone::StructuredCloneData;
|
||||||
use dom::eventtarget::EventTarget;
|
use dom::eventtarget::EventTarget;
|
||||||
use js::jsapi::{HandleValue, JSContext};
|
use js::jsapi::{HandleValue, JSContext};
|
||||||
use script_thread::Runnable;
|
use script_thread::Runnable;
|
||||||
use script_traits::ScriptMsg;
|
use script_traits::{ScriptMsg, DOMMessage};
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ pub type TrustedServiceWorkerAddress = Trusted<ServiceWorker>;
|
||||||
pub struct ServiceWorker {
|
pub struct ServiceWorker {
|
||||||
eventtarget: EventTarget,
|
eventtarget: EventTarget,
|
||||||
script_url: DOMRefCell<String>,
|
script_url: DOMRefCell<String>,
|
||||||
scope_url: DOMRefCell<String>,
|
scope_url: Url,
|
||||||
state: Cell<ServiceWorkerState>,
|
state: Cell<ServiceWorkerState>,
|
||||||
skip_waiting: Cell<bool>
|
skip_waiting: Cell<bool>
|
||||||
}
|
}
|
||||||
|
@ -35,21 +35,23 @@ pub struct ServiceWorker {
|
||||||
impl ServiceWorker {
|
impl ServiceWorker {
|
||||||
fn new_inherited(script_url: &str,
|
fn new_inherited(script_url: &str,
|
||||||
skip_waiting: bool,
|
skip_waiting: bool,
|
||||||
scope_url: &str) -> ServiceWorker {
|
scope_url: Url) -> ServiceWorker {
|
||||||
ServiceWorker {
|
ServiceWorker {
|
||||||
eventtarget: EventTarget::new_inherited(),
|
eventtarget: EventTarget::new_inherited(),
|
||||||
script_url: DOMRefCell::new(String::from(script_url)),
|
script_url: DOMRefCell::new(String::from(script_url)),
|
||||||
state: Cell::new(ServiceWorkerState::Installing),
|
state: Cell::new(ServiceWorkerState::Installing),
|
||||||
scope_url: DOMRefCell::new(String::from(scope_url)),
|
scope_url: scope_url,
|
||||||
skip_waiting: Cell::new(skip_waiting)
|
skip_waiting: Cell::new(skip_waiting)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(global: GlobalRef,
|
pub fn install_serviceworker(global: GlobalRef,
|
||||||
script_url: &str,
|
script_url: Url,
|
||||||
scope_url: &str,
|
scope_url: Url,
|
||||||
skip_waiting: bool) -> Root<ServiceWorker> {
|
skip_waiting: bool) -> Root<ServiceWorker> {
|
||||||
reflect_dom_object(box ServiceWorker::new_inherited(script_url, skip_waiting, scope_url), global, Wrap)
|
reflect_dom_object(box ServiceWorker::new_inherited(script_url.as_str(),
|
||||||
|
skip_waiting,
|
||||||
|
scope_url), global, Wrap)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dispatch_simple_error(address: TrustedServiceWorkerAddress) {
|
pub fn dispatch_simple_error(address: TrustedServiceWorkerAddress) {
|
||||||
|
@ -65,16 +67,6 @@ impl ServiceWorker {
|
||||||
pub fn get_script_url(&self) -> Url {
|
pub fn get_script_url(&self) -> Url {
|
||||||
Url::parse(&self.script_url.borrow().clone()).unwrap()
|
Url::parse(&self.script_url.borrow().clone()).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn install_serviceworker(global: GlobalRef,
|
|
||||||
script_url: Url,
|
|
||||||
scope_url: &str,
|
|
||||||
skip_waiting: bool) -> Root<ServiceWorker> {
|
|
||||||
ServiceWorker::new(global,
|
|
||||||
script_url.as_str(),
|
|
||||||
scope_url,
|
|
||||||
skip_waiting)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServiceWorkerMethods for ServiceWorker {
|
impl ServiceWorkerMethods for ServiceWorker {
|
||||||
|
@ -96,9 +88,9 @@ impl ServiceWorkerMethods for ServiceWorker {
|
||||||
}
|
}
|
||||||
// Step 7
|
// Step 7
|
||||||
let data = try!(StructuredCloneData::write(cx, message));
|
let data = try!(StructuredCloneData::write(cx, message));
|
||||||
let msg_vec = data.move_to_arraybuffer();
|
let msg_vec = DOMMessage(data.move_to_arraybuffer());
|
||||||
let scope_url = Url::parse(&*self.scope_url.borrow()).unwrap();
|
let _ = self.global().r().constellation_chan().send(ScriptMsg::ForwardDOMMessage(msg_vec,
|
||||||
let _ = self.global().r().constellation_chan().send(ScriptMsg::ForwardDOMMessage(msg_vec, scope_url));
|
self.scope_url.clone()));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -95,10 +95,9 @@ impl ServiceWorkerContainerMethods for ServiceWorkerContainer {
|
||||||
return Err(Error::Type("Scope URL contains forbidden characters".to_owned()));
|
return Err(Error::Type("Scope URL contains forbidden characters".to_owned()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let scope_str = scope.as_str().to_owned();
|
|
||||||
let worker_registration = ServiceWorkerRegistration::new(self.global().r(),
|
let worker_registration = ServiceWorkerRegistration::new(self.global().r(),
|
||||||
script_url,
|
script_url,
|
||||||
scope_str.clone(),
|
scope.clone(),
|
||||||
self);
|
self);
|
||||||
ScriptThread::set_registration(scope, &*worker_registration, self.global().r().pipeline());
|
ScriptThread::set_registration(scope, &*worker_registration, self.global().r().pipeline());
|
||||||
Ok(worker_registration)
|
Ok(worker_registration)
|
||||||
|
|
|
@ -25,21 +25,21 @@ pub struct ServiceWorkerRegistration {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServiceWorkerRegistration {
|
impl ServiceWorkerRegistration {
|
||||||
fn new_inherited(active_sw: &ServiceWorker, scope: String) -> ServiceWorkerRegistration {
|
fn new_inherited(active_sw: &ServiceWorker, scope: Url) -> ServiceWorkerRegistration {
|
||||||
ServiceWorkerRegistration {
|
ServiceWorkerRegistration {
|
||||||
eventtarget: EventTarget::new_inherited(),
|
eventtarget: EventTarget::new_inherited(),
|
||||||
active: Some(JS::from_ref(active_sw)),
|
active: Some(JS::from_ref(active_sw)),
|
||||||
installing: None,
|
installing: None,
|
||||||
waiting: None,
|
waiting: None,
|
||||||
scope: scope,
|
scope: scope.as_str().to_owned(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[allow(unrooted_must_root)]
|
#[allow(unrooted_must_root)]
|
||||||
pub fn new(global: GlobalRef,
|
pub fn new(global: GlobalRef,
|
||||||
script_url: Url,
|
script_url: Url,
|
||||||
scope: String,
|
scope: Url,
|
||||||
container: &Controllable) -> Root<ServiceWorkerRegistration> {
|
container: &Controllable) -> Root<ServiceWorkerRegistration> {
|
||||||
let active_worker = ServiceWorker::install_serviceworker(global, script_url.clone(), &scope, true);
|
let active_worker = ServiceWorker::install_serviceworker(global, script_url.clone(), scope.clone(), true);
|
||||||
active_worker.set_transition_state(ServiceWorkerState::Installed);
|
active_worker.set_transition_state(ServiceWorkerState::Installed);
|
||||||
container.set_controller(&*active_worker.clone());
|
container.set_controller(&*active_worker.clone());
|
||||||
reflect_dom_object(box ServiceWorkerRegistration::new_inherited(&*active_worker, scope), global, Wrap)
|
reflect_dom_object(box ServiceWorkerRegistration::new_inherited(&*active_worker, scope), global, Wrap)
|
||||||
|
|
|
@ -123,9 +123,9 @@ impl ServiceWorkerManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn forward_message(&self, msg: DOMMessage, sender: &Sender<ServiceWorkerScriptMsg>) {
|
fn forward_message(&self, msg: DOMMessage, sender: &Sender<ServiceWorkerScriptMsg>) {
|
||||||
let data = StructuredCloneData::make_structured_clone(msg);
|
let DOMMessage(data) = msg;
|
||||||
|
let data = StructuredCloneData::Vector(data);
|
||||||
let _ = sender.send(ServiceWorkerScriptMsg::CommonWorker(WorkerScriptMsg::DOMMessage(data)));
|
let _ = sender.send(ServiceWorkerScriptMsg::CommonWorker(WorkerScriptMsg::DOMMessage(data)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -180,7 +180,7 @@ pub enum ServiceWorkerMsg {
|
||||||
RegisterServiceWorker(ScopeThings, Url),
|
RegisterServiceWorker(ScopeThings, Url),
|
||||||
/// Timeout message sent by active service workers
|
/// Timeout message sent by active service workers
|
||||||
Timeout(Url),
|
Timeout(Url),
|
||||||
/// Backup message
|
/// Message sent by constellation to forward to a running service worker
|
||||||
ForwardDOMMessage(DOMMessage, Url),
|
ForwardDOMMessage(DOMMessage, Url),
|
||||||
/// Exit the service worker manager
|
/// Exit the service worker manager
|
||||||
Exit,
|
Exit,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue