Auto merge of #12501 - mephisto41:worker-close-impl, r=KiChjang

Implement DedicatedWorkerGlobalScope.close

<!-- Please describe your changes on the following line: -->
Implement DedicatedWorkerGlobalScope.close().
---
<!-- 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 #12466 (github issue number if applicable).

<!-- Either: -->
- [X] There are tests for these changes

<!-- 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/12501)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-07-25 19:34:36 -05:00 committed by GitHub
commit 4e18c230d0
7 changed files with 29 additions and 19 deletions

View file

@ -370,6 +370,12 @@ impl DedicatedWorkerGlobalScopeMethods for DedicatedWorkerGlobalScope {
Ok(())
}
// https://html.spec.whatwg.org/multipage/#dom-dedicatedworkerglobalscope-close
fn Close(&self) {
// Step 2
self.upcast::<WorkerGlobalScope>().close();
}
// https://html.spec.whatwg.org/multipage/#handler-dedicatedworkerglobalscope-onmessage
event_handler!(message, GetOnmessage, SetOnmessage);
}

View file

@ -8,4 +8,6 @@
[Throws]
void postMessage(any message/*, optional sequence<Transferable> transfer*/);
attribute EventHandler onmessage;
void close();
};

View file

@ -27,6 +27,7 @@ use js::jsapi::{HandleValue, JSContext, JSAutoCompartment};
use js::jsval::UndefinedValue;
use script_thread::Runnable;
use script_traits::WorkerScriptLoadOrigin;
use std::cell::Cell;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{Sender, channel};
use std::sync::{Arc, Mutex};
@ -43,7 +44,8 @@ pub struct Worker {
sender: Sender<(TrustedWorkerAddress, WorkerScriptMsg)>,
closing: Arc<AtomicBool>,
#[ignore_heap_size_of = "Defined in rust-mozjs"]
runtime: Arc<Mutex<Option<SharedRt>>>
runtime: Arc<Mutex<Option<SharedRt>>>,
terminated: Cell<bool>,
}
impl Worker {
@ -53,7 +55,8 @@ impl Worker {
eventtarget: EventTarget::new_inherited(),
sender: sender,
closing: closing,
runtime: Arc::new(Mutex::new(None))
runtime: Arc::new(Mutex::new(None)),
terminated: Cell::new(false),
}
}
@ -112,11 +115,15 @@ impl Worker {
self.closing.load(Ordering::SeqCst)
}
pub fn is_terminated(&self) -> bool {
self.terminated.get()
}
pub fn handle_message(address: TrustedWorkerAddress,
data: StructuredCloneData) {
let worker = address.root();
if worker.is_closing() {
if worker.is_terminated() {
return;
}
@ -137,7 +144,7 @@ impl Worker {
filename: DOMString, lineno: u32, colno: u32) {
let worker = address.root();
if worker.is_closing() {
if worker.is_terminated() {
return;
}
@ -169,7 +176,10 @@ impl WorkerMethods for Worker {
return;
}
// Step 4
// Step 2
self.terminated.set(true);
// Step 3
if let Some(runtime) = *self.runtime.lock().unwrap() {
runtime.request_interrupt();
}

View file

@ -444,4 +444,10 @@ impl WorkerGlobalScope {
pub fn set_devtools_wants_updates(&self, value: bool) {
self.devtools_wants_updates.set(value);
}
pub fn close(&self) {
if let Some(ref closing) = self.closing {
closing.store(true, Ordering::SeqCst);
}
}
}

View file

@ -1,5 +0,0 @@
[WorkerGlobalScope_close.htm]
type: testharness
[ WorkerGlobalScope close(): clear events queue ]
expected: FAIL

View file

@ -21,9 +21,6 @@
[DedicatedWorkerGlobalScope interface: attribute onmessage]
expected: FAIL
[WorkerGlobalScope interface: self must inherit property "close" with the proper type (2)]
expected: FAIL
[WorkerGlobalScope interface: self must inherit property "onerror" with the proper type (3)]
expected: FAIL

View file

@ -1,6 +0,0 @@
[sending-messages.html]
type: testharness
expected: TIMEOUT
[close() and sending messages]
expected: TIMEOUT