Auto merge of #13419 - Coder206:swPromise, r=jdm

ServiceWorkerContainer::Promise

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

<!-- 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 #13409  (github issue number if applicable).

<!-- Either: -->
- [ ] 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/13419)

<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-10-31 11:19:01 -05:00 committed by GitHub
commit 0d46c7c708
4 changed files with 83 additions and 51 deletions

View file

@ -4,18 +4,20 @@
use dom::bindings::codegen::Bindings::ServiceWorkerContainerBinding::{ServiceWorkerContainerMethods, Wrap};
use dom::bindings::codegen::Bindings::ServiceWorkerContainerBinding::RegistrationOptions;
use dom::bindings::error::{Error, Fallible};
use dom::bindings::error::Error;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::reflector::{Reflectable, reflect_dom_object};
use dom::bindings::str::USVString;
use dom::eventtarget::EventTarget;
use dom::globalscope::GlobalScope;
use dom::promise::Promise;
use dom::serviceworker::ServiceWorker;
use dom::serviceworkerregistration::ServiceWorkerRegistration;
use script_thread::ScriptThread;
use std::ascii::AsciiExt;
use std::default::Default;
use std::rc::Rc;
#[dom_struct]
pub struct ServiceWorkerContainer {
@ -53,26 +55,36 @@ impl ServiceWorkerContainerMethods for ServiceWorkerContainer {
return self.controller.get()
}
#[allow(unrooted_must_root)]
// https://w3c.github.io/ServiceWorker/#service-worker-container-register-method
fn Register(&self,
script_url: USVString,
options: &RegistrationOptions) -> Fallible<Root<ServiceWorkerRegistration>> {
options: &RegistrationOptions) -> Rc<Promise> {
let promise = Promise::new(&*self.global());
let ctx = self.global().get_cx();
let USVString(ref script_url) = script_url;
let api_base_url = self.global().api_base_url();
// Step 3-4
let script_url = match api_base_url.join(script_url) {
Ok(url) => url,
Err(_) => return Err(Error::Type("Invalid script URL".to_owned()))
Err(_) => {
promise.reject_error(ctx, Error::Type("Invalid script URL".to_owned()));
return promise;
}
};
// Step 5
match script_url.scheme() {
"https" | "http" => {},
_ => return Err(Error::Type("Only secure origins are allowed".to_owned()))
_ => {
promise.reject_error(ctx, Error::Type("Only secure origins are allowed".to_owned()));
return promise;
}
}
// Step 6
if script_url.path().to_ascii_lowercase().contains("%2f") ||
script_url.path().to_ascii_lowercase().contains("%5c") {
return Err(Error::Type("Script URL contains forbidden characters".to_owned()));
promise.reject_error(ctx, Error::Type("Script URL contains forbidden characters".to_owned()));
return promise;
}
// Step 8-9
let scope = match options.scope {
@ -80,7 +92,10 @@ impl ServiceWorkerContainerMethods for ServiceWorkerContainer {
let &USVString(ref inner_scope) = scope;
match api_base_url.join(inner_scope) {
Ok(url) => url,
Err(_) => return Err(Error::Type("Invalid scope URL".to_owned()))
Err(_) => {
promise.reject_error(ctx, Error::Type("Invalid scope URL".to_owned()));
return promise;
}
}
},
None => script_url.join("./").unwrap()
@ -88,12 +103,16 @@ impl ServiceWorkerContainerMethods for ServiceWorkerContainer {
// Step 11
match scope.scheme() {
"https" | "http" => {},
_ => return Err(Error::Type("Only secure origins are allowed".to_owned()))
_ => {
promise.reject_error(ctx, Error::Type("Only secure origins are allowed".to_owned()));
return promise;
}
}
// Step 12
if scope.path().to_ascii_lowercase().contains("%2f") ||
scope.path().to_ascii_lowercase().contains("%5c") {
return Err(Error::Type("Scope URL contains forbidden characters".to_owned()));
promise.reject_error(ctx, Error::Type("Scope URL contains forbidden characters".to_owned()));
return promise;
}
let global = self.global();
@ -101,7 +120,9 @@ impl ServiceWorkerContainerMethods for ServiceWorkerContainer {
script_url,
scope.clone(),
self);
ScriptThread::set_registration(scope, &*worker_registration, global.pipeline_id());
Ok(worker_registration)
ScriptThread::set_registration(scope, &*worker_registration, self.global().pipeline_id());
promise.resolve_native(ctx, &*worker_registration);
promise
}
}

View file

@ -8,7 +8,7 @@ interface ServiceWorkerContainer : EventTarget {
[Unforgeable] readonly attribute ServiceWorker? controller;
//[SameObject] readonly attribute Promise<ServiceWorkerRegistration> ready;
[NewObject, Throws] ServiceWorkerRegistration register(USVString scriptURL, optional RegistrationOptions options);
[NewObject] Promise<ServiceWorkerRegistration> register(USVString scriptURL, optional RegistrationOptions options);
//[NewObject] /*Promise<any>*/ any getRegistration(optional USVString clientURL = "");
//[NewObject] /* Promise */<sequence<ServiceWorkerRegistration>> getRegistrations();