Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upServiceWorkerContainer::Promise #13419
Conversation
highfive
commented
Sep 26, 2016
|
Heads up! This PR modifies the following files:
|
highfive
commented
Sep 26, 2016
|
|
||
| // New Step 1 | ||
|
|
||
| let p = dom::promise::Promise |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
jdm
Sep 26, 2016
Member
I recommend looking for uses of Promise::new in the codebase to base your changes off of.
| @@ -6,21 +6,21 @@ | |||
| [Pref="dom.serviceworker.enabled", Exposed=(Window,Worker)] | |||
| interface ServiceWorkerContainer : EventTarget { | |||
| [Unforgeable] readonly attribute ServiceWorker? controller; | |||
| //[SameObject] readonly attribute Promise<ServiceWorkerRegistration> ready; | |||
| [SameObject] readonly attribute Promise<ServiceWorkerRegistration> ready; | |||
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
It's the first time I do one of these, I am new to the process and I am having trouble understanding how to translate the pseudo code from Service Worker Nightly Container:Register into Rust for Servo |
|
I think this PR is for @jdm |
|
You will definitely need to be able to build Servo locally to be able to make these changes effectively. |
| @@ -6,21 +6,21 @@ | |||
| [Pref="dom.serviceworker.enabled", Exposed=(Window,Worker)] | |||
| interface ServiceWorkerContainer : EventTarget { | |||
| [Unforgeable] readonly attribute ServiceWorker? controller; | |||
| //[SameObject] readonly attribute Promise<ServiceWorkerRegistration> ready; | |||
| [SameObject] readonly attribute Promise<ServiceWorkerRegistration> ready; | |||
This comment has been minimized.
This comment has been minimized.
| Let scopeURL be null. | ||
| If options.scope is present, set scopeURL to the result of parsing options.scope with the context object’s relevant settings object’s API base URL. | ||
| Invoke Start Register with scopeURL, scriptURL, p, client, client’s creation URL and options.type. | ||
| Return p. */ |
This comment has been minimized.
This comment has been minimized.
jdm
Sep 26, 2016
Member
As mentioned in #13409, I don't think there's any need to focus on the specification. It's enough to know that it says to create a promise and return it, and either resolve it with a successful service worker or else reject it with an error (instead of returning error values directly).
| //[NewObject] /*Promise<any>*/ any getRegistration(optional USVString clientURL = ""); | ||
| //[NewObject] /* Promise */<sequence<ServiceWorkerRegistration>> getRegistrations(); | ||
| [NewObject] Promise<any> any getRegistration(optional USVString clientURL = ""); | ||
| [NewObject] Promise <sequence<ServiceWorkerRegistration>> getRegistrations(); |
This comment has been minimized.
This comment has been minimized.
jdm
Sep 26, 2016
Member
We shouldn't be modifying any other methods or properties in this file for the purposes of these changes.
| //[SameObject] readonly attribute Promise<ServiceWorkerRegistration> ready; | ||
|
|
||
| [NewObject, Throws] ServiceWorkerRegistration register(USVString scriptURL, optional RegistrationOptions options); | ||
| [SameObject] readonly attribute Promise<ServiceWorkerRegistration> ready; |
This comment has been minimized.
This comment has been minimized.
Coder206
Sep 26, 2016
•
Author
Contributor
@jdm When running ./mach build -d, I am seeing this error:
WebIDL.WebIDLError: error: Unresolved type '<unresolved scope>::Promise'., /home/user/servo/components/script/dom/webidls/ServiceWorkerContainer.webidl line 9:34
[SameObject] readonly attribute Promise<ServiceWorkerRegistration> ready;
I am not sure how to solve it. Do you have any ideas?
This comment has been minimized.
This comment has been minimized.
jdm
Sep 26, 2016
Member
Your branch is based off of a master commit from Sept 6. You need to be using 2b1a39c or more recent instead.
This comment has been minimized.
This comment has been minimized.
creativcoder
Sep 26, 2016
•
Contributor
@Coder206 As an added note, We shouldn't be needing to make changes to the ready method for the relevant issue. You just have to make changes to the register api declaration from:
[NewObject, Throws] ServiceWorkerRegistration register(USVString scriptURL, optional RegistrationOptions options);
to
[NewObject] Promise<ServiceWorkerRegistration> register(USVString scriptURL, optional RegistrationOptions options);
This will result in a compilation error notifying you that, register method should return a Rc<Promise> type. So after that you need to use a combination of resolve_native and reject_error method to make the register method return a resolved/reject promise. Correct me if am wrong @jdm
Rebase your current branch to the master branch before making these changes as mentioned by jdm
This comment has been minimized.
This comment has been minimized.
Coder206
Sep 27, 2016
Author
Contributor
@creativcoder I am not sure how to get started with resolve_native and reject_error. How do I implement that in my code? (does it go in the return)
This comment has been minimized.
This comment has been minimized.
creativcoder
Sep 27, 2016
Contributor
We don't need to implement those methods, they are already implemented. In the Register method we just need to call reject_error on places where we return an Err value and resolve_native at places where we return an Ok value on the promise object.
You can have a look at my branch here, for how that is being done.
|
@jdm So I need to rebase my code to the newest commit? |
|
@Coder206 That would be easiest, yes. |
|
Looks like an extra commit was added here. I am going to remove it as soon as possible. |
|
The test file mentioned in the original issue still needs modifying, too. |
|
@jdm Thanks for the reminder |
| } | ||
| // 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())); | ||
| return { |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
|
|
This looks good with a few point addressed. |
| console.log("Registered script source: "+ navigator.serviceWorker.controller.scriptURL); | ||
| document.getElementById('iframe_sw').src = 'demo_iframe.html'; | ||
| post_message('/demo_iframe'); | ||
| /* global navigator */ |
This comment has been minimized.
This comment has been minimized.
| console.log("From client worker registered: ", navigator.serviceWorker.controller); | ||
| console.log("Registered script source: "+ navigator.serviceWorker.controller.scriptURL); | ||
| }).catch(function(error) { | ||
| console.log("Error in loading: " + navigator.serviceWorker.controller.scriptURL); |
This comment has been minimized.
This comment has been minimized.
| console.log("Registered script source: "+ navigator.serviceWorker.controller.scriptURL); | ||
| post_message('/dashboard'); | ||
| }).catch(function(error) { | ||
| console.log("Error in loading: " + navigator.serviceWorker.controller.scriptURL); |
This comment has been minimized.
This comment has been minimized.
| console.log("Registered script source: "+ navigator.serviceWorker.controller.scriptURL); | ||
| post_message('/profile'); | ||
| }).catch(function(error) { | ||
| console.log("Error in loading: " + navigator.serviceWorker.controller.scriptURL); |
This comment has been minimized.
This comment has been minimized.
|
@Coder206 Did you run the test after making the changes? My assumption when people make changes to test code is that the tests pass before they push their changes. |
|
@jdm Yes, that's when I received the error from above. I haven't rebased though with the new changes. The next push will have the tests verified with the new master. |
|
Sorry for the inconvenience. |
|
I am unable to get the error messages that @KiChjang has shared with me. Here is the output of my attempt to test my changes. Please note, this is the result despite I only corrected the first error mentioned above: Errors I am unsure what they mean:
Any ideas or suggestions? |
|
@Coder206 The main problem you're seeing is due to using the |
| function() { var sw_reg = register_sw('./in%5Csome%5fdir/sw.js'); }, | ||
| "Invalid URL Path"); | ||
| promise_test(function (p) { | ||
| promise_rejects(p, new TypeError(), register_sw('./in%5Csome%5fdir/sw.js')); |
This comment has been minimized.
This comment has been minimized.
Coder206
Oct 30, 2016
Author
Contributor
@jdm I don't fancy an inline code like this but the code below would fail:
promise_test(function (p) {
promise_rejects(p, new TypeError(), function () {
register_sw('./in%5Csome%5fdir/sw.js');
});
}, "Test: Throws Error when Invalid Url Path");
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Coder206
Oct 31, 2016
Author
Contributor
@jdm I am still getting promise.then() is not a function when I return, I am trying to figure out why and how to correct it from the testharness.js from the web platfor test folder
| function() { var sw_reg = register_sw('sw.js', './mal%5fformed/sco%5Cpe/'); }, | ||
| "Invalid URL Path"); | ||
| promise_test(function (p) { | ||
| return promise_rejects(p, new TypeError(), register_sw('sw.js', './mal%5fformed/sco%5Cpe/')); |
This comment has been minimized.
This comment has been minimized.
|
One teeny tiny change and then we can merge this! |
| }, "Test: Active Service Worker ScriptURL property"); | ||
|
|
||
| function bar() { |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Coder206
Oct 31, 2016
Author
Contributor
@jdm Yes, sorry it was test code that escaped... Will remove it asap.
The purpose of the code changes is to enable the use of promises in the Service Worker container. I also modified the Service Worker test in order to support the promises.
|
@jdm So I made the last changes requested and tested |
|
@bors-servo: r+ |
|
|
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 -->
|
|
highfive
commented
Oct 31, 2016
|
|
@bors-servo: retry |
|
|
|
|

Coder206 commentedSep 26, 2016
•
edited
The purpose of the code changes is to enable the use of promises in the Service Worker container. I also modified the Service Worker test in order to support the promises.
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThis change is