Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
implement related sw interface and register method
- Loading branch information
1 parent
0594d58
commit 15a2064
Showing
33 changed files
with
1,285 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* 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::refcounted::Trusted; | ||
use dom::bindings::reflector::Reflectable; | ||
use dom::bindings::str::DOMString; | ||
use dom::bindings::structuredclone::StructuredCloneData; | ||
use js::jsapi::{JSRuntime, JS_RequestInterruptCallback}; | ||
use js::rust::Runtime; | ||
use msg::constellation_msg::{PipelineId, ReferrerPolicy}; | ||
use net_traits::{LoadOrigin, RequestSource}; | ||
use script_runtime::CommonScriptMsg; | ||
use url::Url; | ||
|
||
/// Messages used to control the worker event loops | ||
pub enum WorkerScriptMsg { | ||
/// Common variants associated with the script messages | ||
Common(CommonScriptMsg), | ||
/// Message sent through Worker.postMessage | ||
DOMMessage(StructuredCloneData), | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct WorkerScriptLoadOrigin { | ||
pub referrer_url: Option<Url>, | ||
pub referrer_policy: Option<ReferrerPolicy>, | ||
pub request_source: RequestSource, | ||
pub pipeline_id: Option<PipelineId> | ||
} | ||
|
||
impl LoadOrigin for WorkerScriptLoadOrigin { | ||
fn referrer_url(&self) -> Option<Url> { | ||
self.referrer_url.clone() | ||
} | ||
fn referrer_policy(&self) -> Option<ReferrerPolicy> { | ||
self.referrer_policy.clone() | ||
} | ||
fn request_source(&self) -> RequestSource { | ||
self.request_source.clone() | ||
} | ||
fn pipeline_id(&self) -> Option<PipelineId> { | ||
self.pipeline_id.clone() | ||
} | ||
} | ||
|
||
pub struct SimpleWorkerErrorHandler<T: Reflectable> { | ||
pub addr: Trusted<T>, | ||
} | ||
|
||
impl<T: Reflectable> SimpleWorkerErrorHandler<T> { | ||
pub fn new(addr: Trusted<T>) -> SimpleWorkerErrorHandler<T> { | ||
SimpleWorkerErrorHandler { | ||
addr: addr | ||
} | ||
} | ||
} | ||
|
||
pub struct WorkerErrorHandler<T: Reflectable> { | ||
pub addr: Trusted<T>, | ||
pub msg: DOMString, | ||
pub file_name: DOMString, | ||
pub line_num: u32, | ||
pub col_num: u32, | ||
} | ||
|
||
impl<T: Reflectable> WorkerErrorHandler<T> { | ||
pub fn new(addr: Trusted<T>, msg: DOMString, file_name: DOMString, line_num: u32, col_num: u32) | ||
-> WorkerErrorHandler<T> { | ||
WorkerErrorHandler { | ||
addr: addr, | ||
msg: msg, | ||
file_name: file_name, | ||
line_num: line_num, | ||
col_num: col_num, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct SharedRt { | ||
pub rt: *mut JSRuntime | ||
} | ||
|
||
impl SharedRt { | ||
pub fn new(rt: &Runtime) -> SharedRt { | ||
SharedRt { | ||
rt: rt.rt() | ||
} | ||
} | ||
|
||
#[allow(unsafe_code)] | ||
pub fn request_interrupt(&self) { | ||
unsafe { | ||
JS_RequestInterruptCallback(self.rt); | ||
} | ||
} | ||
|
||
pub fn rt(&self) -> *mut JSRuntime { | ||
self.rt | ||
} | ||
} | ||
#[allow(unsafe_code)] | ||
unsafe impl Send for SharedRt {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* 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::abstractworker::WorkerScriptMsg; | ||
use dom::bindings::refcounted::Trusted; | ||
use dom::bindings::reflector::Reflectable; | ||
use script_runtime::{ScriptChan, CommonScriptMsg, ScriptPort}; | ||
use std::sync::mpsc::{Receiver, Sender}; | ||
|
||
/// A ScriptChan that can be cloned freely and will silently send a TrustedWorkerAddress with | ||
/// common event loop messages. While this SendableWorkerScriptChan is alive, the associated | ||
/// Worker object will remain alive. | ||
#[derive(JSTraceable, Clone)] | ||
pub struct SendableWorkerScriptChan<T: Reflectable> { | ||
pub sender: Sender<(Trusted<T>, CommonScriptMsg)>, | ||
pub worker: Trusted<T>, | ||
} | ||
|
||
impl<T: Reflectable + 'static> ScriptChan for SendableWorkerScriptChan<T> { | ||
fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> { | ||
self.sender.send((self.worker.clone(), msg)).map_err(|_| ()) | ||
} | ||
|
||
fn clone(&self) -> Box<ScriptChan + Send> { | ||
box SendableWorkerScriptChan { | ||
sender: self.sender.clone(), | ||
worker: self.worker.clone(), | ||
} | ||
} | ||
} | ||
|
||
/// A ScriptChan that can be cloned freely and will silently send a TrustedWorkerAddress with | ||
/// worker event loop messages. While this SendableWorkerScriptChan is alive, the associated | ||
/// Worker object will remain alive. | ||
#[derive(JSTraceable, Clone)] | ||
pub struct WorkerThreadWorkerChan<T: Reflectable> { | ||
pub sender: Sender<(Trusted<T>, WorkerScriptMsg)>, | ||
pub worker: Trusted<T>, | ||
} | ||
|
||
impl<T: Reflectable + 'static> ScriptChan for WorkerThreadWorkerChan<T> { | ||
fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> { | ||
self.sender | ||
.send((self.worker.clone(), WorkerScriptMsg::Common(msg))) | ||
.map_err(|_| ()) | ||
} | ||
|
||
fn clone(&self) -> Box<ScriptChan + Send> { | ||
box WorkerThreadWorkerChan { | ||
sender: self.sender.clone(), | ||
worker: self.worker.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl<T: Reflectable> ScriptPort for Receiver<(Trusted<T>, WorkerScriptMsg)> { | ||
fn recv(&self) -> Result<CommonScriptMsg, ()> { | ||
match self.recv().map(|(_, msg)| msg) { | ||
Ok(WorkerScriptMsg::Common(script_msg)) => Ok(script_msg), | ||
Ok(WorkerScriptMsg::DOMMessage(_)) => panic!("unexpected worker event message!"), | ||
Err(_) => Err(()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* 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::ClientBinding::FrameType; | ||
use dom::bindings::codegen::Bindings::ClientBinding::{ClientMethods, Wrap}; | ||
use dom::bindings::global::GlobalRef; | ||
use dom::bindings::js::JS; | ||
use dom::bindings::js::Root; | ||
use dom::bindings::reflector::{Reflector, reflect_dom_object}; | ||
use dom::bindings::str::{DOMString, USVString}; | ||
use dom::serviceworker::ServiceWorker; | ||
use dom::window::Window; | ||
use url::Url; | ||
use uuid::Uuid; | ||
|
||
#[dom_struct] | ||
pub struct Client { | ||
reflector_: Reflector, | ||
active_worker: Option<JS<ServiceWorker>>, | ||
url: USVString, | ||
frame_type: FrameType, | ||
#[ignore_heap_size_of = "Defined in uuid"] | ||
id: Uuid | ||
} | ||
|
||
impl Client { | ||
fn new_inherited(url: Url) -> Client { | ||
Client { | ||
reflector_: Reflector::new(), | ||
active_worker: None, | ||
url: USVString(url.as_str().to_owned()), | ||
frame_type: FrameType::None, | ||
id: Uuid::new_v4() | ||
} | ||
} | ||
|
||
pub fn new(window: &Window) -> Root<Client> { | ||
reflect_dom_object(box Client::new_inherited(window.get_url()), GlobalRef::Window(window), Wrap) | ||
} | ||
} | ||
|
||
impl ClientMethods for Client { | ||
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#client-url-attribute | ||
fn Url(&self) -> USVString { | ||
self.url.clone() | ||
} | ||
|
||
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#client-frametype | ||
fn FrameType(&self) -> FrameType { | ||
self.frame_type | ||
} | ||
|
||
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#client-id | ||
fn Id(&self) -> DOMString { | ||
let uid_str = format!("{}", self.id); | ||
DOMString::from_string(uid_str) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.