Bug 903441 (ServiceWorkers)

[meta] Implement Service Workers

NEW
Unassigned

Status

()

Core
DOM
4 years ago
2 months ago

People

(Reporter: Ehsan, Unassigned)

Tracking

(Depends on: 10 bugs, Blocks: 2 bugs, {dev-doc-needed})

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: [games:p1][platform-rel-Games], URL)

Attachments

(26 obsolete attachments)

Comment hidden (empty)

Comment 1

4 years ago
Is there a spec somewhere? Or is it an internal thing?
Its an experimental API right now. Updated URL to relevant discussion, but everything else is hand-wavy.
OS: Mac OS X → All
Hardware: x86 → All
Created attachment 789277 [details] [diff] [review]
Patch 1: rebased-sharedworkers

A cleaned up patch bent sent me that is required before applying the event page/service patch.
Created attachment 789292 [details] [diff] [review]
Patch 2: Service

This is a first pass of Services that doesn't do a lot, but might be useful to Ehsan to figure out Navigation Controller and also a good time to layout my thoughts about further implementation.

What this code does do right now:
Inherits SharedWorker, to expose a Service() constructor to DOM (we could call it EventPage I guess, depending on how it gets used). This starts a (shared )worker, with the addition of a `mIsService` flag. A service is *not* immediately killed when all referring windows are closed. In fact, right now its lifetime is completely isolated from referring windows (which is not how it should be!). When it's event loop Dispatch() receives no events for some time (right now 13 seconds for no real reason), it cancels the service.

The rest of the discussion is in dev.platform (will paste link here).
Attachment #789292 - Flags: feedback?(mounir)
Attachment #789292 - Flags: feedback?(jonas)
Attachment #789292 - Flags: feedback?(ehsan)
Comment on attachment 789292 [details] [diff] [review]
Patch 2: Service

Jonas and Ehsan's feedbacks should be enough.
Attachment #789292 - Flags: feedback?(mounir)
Comment on attachment 789292 [details] [diff] [review]
Patch 2: Service

Review of attachment 789292 [details] [diff] [review]:
-----------------------------------------------------------------

Feedback on the concept level issues given on the dev.webapi thread.

::: dom/workers/RuntimeService.cpp
@@ +2091,5 @@
> +    }
> +
> +    if (!windowArray->Contains(workerPrivate)) {
> +      windowArray->AppendElement(workerPrivate);
> +    }

Can this be refactored into a helper method?

::: dom/workers/RuntimeService.h
@@ +155,5 @@
>  
> +  nsresult
> +  CreateBackgroundService(JSContext* aCx, nsPIDOMWindow* aWindow,
> +                          const nsAString& aScriptURL, const nsAString& aName,
> +                          Service** aSharedWorker);

Nit: please prefer returning an already_AddRefed<Service> if possible.

::: dom/workers/Service.cpp
@@ +20,5 @@
> +} // anonymous namespace
> +
> +Service::Service(nsPIDOMWindow* aWindow,
> +                           WorkerPrivate* aWorkerPrivate)
> +: SharedWorker(aWindow, aWorkerPrivate)

Not sure if you're looking for whitespace nits quite yet.  ;-)

@@ +27,5 @@
> +}
> +
> +Service::~Service()
> +{
> +  AssertIsOnMainThread();

It's not immediately clear to me what guarantees this...

::: dom/workers/SharedWorker.h
@@ +47,5 @@
>    nsRefPtr<MessagePort> mMessagePort;
> +
> +  /**
> +   * Used to dispatch a Resume() event back to the SharedWorker itself.
> +   * XXXnsm I'm not sure why this has to be done using a Runnable.

Perhaps because we want to keep the object alive until the desired method is called on it?

@@ -80,5 @@
>  
>    virtual nsresult
>    PreHandleEvent(nsEventChainPreVisitor& aVisitor) MOZ_OVERRIDE;
>  
> -private:

Nit: please make these protected.

::: dom/workers/Worker.cpp
@@ +105,5 @@
>  
>    static JSObject*
>    Construct(JSContext* aCx, WorkerPrivate* aParentObj,
>              const nsAString& aScriptURL, bool aIsChromeWorker,
> +            const nsAString& aSharedWorkerName, bool aIsService);

Nit: an enum would probably make the call sites more readable than a bool.

::: modules/libpref/src/init/all.js
@@ +89,5 @@
>  // Whether or not Shared Web Workers are enabled.
>  pref("dom.workers.sharedWorkers.enabled", true);
>  
> +// Whether or not Background Service Workers are enabled.
> +pref("dom.workers.service.enabled", true);

This should obviously default to false when you get to land this...
Attachment #789292 - Flags: feedback?(ehsan) → feedback+
Comment on attachment 789292 [details] [diff] [review]
Patch 2: Service

Review of attachment 789292 [details] [diff] [review]:
-----------------------------------------------------------------

Nikhil, sadly I won't have time to go over this very soon. Especially as I'm heading out for vacation for a week and then it's the B2G workweek. Feel free to poke me after that if you want my feedback on the code. But I'd prefer to just go over the API instead.
Attachment #789292 - Flags: feedback?(jonas) → feedback?
Created attachment 801003 [details] [diff] [review]
Service Worker proof of concept

This is a much simplified patch over the earlier 'ugliness'. It:

* Does not present a DOM facing constructor or access to these workers.
* Does not require Shared Workers patches
* Almost entirely avoids modifying any existing worker code.
* Adds a single interface nsIServiceWorkerManager with a SendEvent() method that always creates a new Worker and sends the event to it. The plan is to make this do caching and stuff and integrate it with existing worker infrastructure.

Ehsan,
If you have the time, can you fiddle with integrating this into Necko in some tiny sense? intercepting a http request and feeding it to this as an event. SendEvent() does not currently accept data, just an event name.
Attachment #789277 - Attachment is obsolete: true
Attachment #789292 - Attachment is obsolete: true
Attachment #789292 - Flags: feedback?
Flags: needinfo?(ehsan)
Created attachment 801004 [details] [diff] [review]
Alarm API that uses Service Worker

Alarm API with certain security checks disabled that enables it on web pages on Firefox Desktop, and uses nsIServiceWorkerManager rather than system messages.
Forgot to point out that the patch hard codes the Service Worker being at http://localhost:8000. Change the paths when you compile. Patches to fix this will come later.

Small test page https://github.com/nikhilm/serviceworker-demo which served from |python -m SimpleHTTPServer| will work with the localhost:8000 thing.
Nikhil, Josh is now working on NavController.  Josh, please see comment 8.  Thanks!
Flags: needinfo?(ehsan) → needinfo?(josh)
Summary: Implement Event Pages → Implement Service Workers
Keywords: dev-doc-needed
Created attachment 812948 [details] [diff] [review]
ServiceWorkerManager

Creates a window-less Worker.

RegisterServiceWorker() will load the URL and send it an install event.

SendEvent() can be used to send events. This will spawn a new Worker for every event, which is not what we want if the previous worker is alive (which it is since this does not terminate service workers yet).
Attachment #801003 - Attachment is obsolete: true
Created attachment 812949 [details] [diff] [review]
Switch to enum over bool for worker type

Useful for doing service worker specific stuff like introducing Cache APIs.

Josh, could you write a small WebIDL wrapper around your work in the Navigation Controller bug to create a simple Cache or SameOriginResponse and introduce it into the worker scope of a Service Worker?
Depends on: 643325
SharedWorkers patch that applies against current m-c at https://hg.mozilla.org/try/rev/092e09f82da8
Created attachment 814940 [details] [diff] [review]
Promises on Workers.

Updated

4 years ago
Assignee: nsm.nikhil → josh
Status: NEW → ASSIGNED
Created attachment 814942 [details] [diff] [review]
Add Response, Request, SameOriginResponse, and FetchEvent implementations.

This is still missing some glue to create a FetchEvent, since we need to pass it a channel. In theory calling respondWith and passing a SameOriginResponse would do the right thing, however.
Marked Promises on worker bug as blocking this. That patch may be updated on bug 915233.

I have a WIP patch that adds InstallPhaseEvent and InstallEvent and some changes to existing patches.Since ServiceWorkers introduces these new events that may be dispatched on the Worker global, we need to wait for the conversion of WorkerPrivate and WorkerScope to use the main thread EventTarget infrastructure rather than implement custom events on the current code. khuey has a WIP patch for this, but he is on pseudo-vacation.
Depends on: 915233
Comment on attachment 814940 [details] [diff] [review]
Promises on Workers.

Obsoleting this patch as it does not make Promises 'usable' (runnable dispatch) on workers, while patch in referenced bug does.refer
Attachment #814940 - Attachment is obsolete: true

Updated

4 years ago
Blocks: 922363

Comment 19

4 years ago
(In reply to Nikhil Marathe [:nsm] from comment #17)
> … we need to wait for the conversion of WorkerPrivate and WorkerScope to
> use the main thread EventTarget infrastructure rather than implement 
> custom events on the current code. khuey has a WIP patch for this, but
> he is on pseudo-vacation.

Can you add the bug to the dependency list then, please?

Comment 20

4 years ago
Ah sorry, I misunderstood your comment. I just saw that Bug 915233 depends on Bug 914762 which you probably meant in Comment 18, right?
(In reply to Florian Bender from comment #20)
> Ah sorry, I misunderstood your comment. I just saw that Bug 915233 depends
> on Bug 914762 which you probably meant in Comment 18, right?

I did mean 915233 in comment 18. As for Kyle's patch, I don't know the bug number.
Created attachment 815393 [details] [diff] [review]
Make document navigation cause a ServiceWorker fetch event.

Unfinished, since the whole bit about actually creating and dispatching the FetchEvent is unclear to me. However, this should theoretically intercept document navigations for documents with active service workers.

Updated

4 years ago
Flags: needinfo?(josh)
I stole this by accident.
Assignee: josh → nsm.nikhil

Updated

4 years ago
Depends on: 898524
Alias: ServiceWorkers
Blocks: 857464
Josh, ' Add Response, Request, SameOriginResponse, and FetchEvent implementations.' is missing FetchEvent.cpp. Could you update the patch? Thanks!
Flags: needinfo?(josh)
(please use Gecko coding style in content/ and dom/* code)
Created attachment 815624 [details] [diff] [review]
Add Response, Request, SameOriginResponse, and FetchEvent implementations.

Updated

4 years ago
Attachment #814942 - Attachment is obsolete: true

Updated

4 years ago
Assignee: nsm.nikhil → josh
Created attachment 816658 [details] [diff] [review]
Intercept images, fonts, objects, stylesheets, and XHRs.

More interception, along with deducing fetch type and top level-ness.

Updated

4 years ago
Assignee: josh → nsm.nikhil
Flags: needinfo?(josh)
Depends on: 930347
Depends on: 930348
Depends on: 930349
Hanging off tasks off this as I think of them.
Summary: Implement Service Workers → [meta] Implement Service Workers
Depends on: 930351
Depends on: 930611
Depends on: 931243
Depends on: 931249
Created attachment 829002 [details] [diff] [review]
window.navigator stubs for ServiceWorker.

Adds empty (un)registerServiceWorker.
Attachment #801004 - Attachment is obsolete: true
Attachment #812948 - Attachment is obsolete: true
Attachment #812949 - Attachment is obsolete: true
Attachment #829002 - Attachment is obsolete: true
Created attachment 829005 [details] [diff] [review]
WIP: ServiceWorker, ServiceWorkerGlobalScope, InstallEvent WebIDL + basic implementation

Integrates into the SharedWorker infrastructure (needs more work) and dispatches the "install" event. Will be split into more bugs soon.
Created attachment 830584 [details] [diff] [review]
WIP: ServiceWorker, ServiceWorkerGlobalScope, InstallEvent WebIDL + basic implementation

Plug service workers into domain info.
Attachment #829005 - Attachment is obsolete: true
Created attachment 830586 [details] [diff] [review]
Add Response, Request, SameOriginResponse, and FetchEvent implementations.

Unbitrotted and rebased over ServiceWorker.
Attachment #815624 - Attachment is obsolete: true
Created attachment 830589 [details] [diff] [review]
Make document navigation cause a ServiceWorker fetch event.

Unbitrot, add support for querying domain info about service worker.
Attachment #815393 - Attachment is obsolete: true
Created attachment 830923 [details] [diff] [review]
Intercept images, fonts, objects, stylesheets, and XHRs.

Patch rebased to compile, haven't tested yet.
Attachment #816658 - Attachment is obsolete: true
Comment on attachment 830923 [details] [diff] [review]
Intercept images, fonts, objects, stylesheets, and XHRs.

Review of attachment 830923 [details] [diff] [review]:
-----------------------------------------------------------------

::: image/src/imgLoader.cpp
@@ +434,5 @@
>                       aPolicy);
>    if (NS_FAILED(rv))
>      return rv;
>  
> +  newChannel = aLoadingDocument->InterceptFetch(newChannel, nsIDocument::Fetch);

This needs a null check.
Blocks: 943220
Created attachment 8343436 [details] [diff] [review]
WIP: ServiceWorker, ServiceWorkerGlobalScope, InstallEvent WebIDL + basic implementation* * *

ServiceWorker in memory structures and installation procedure.
Created attachment 8343446 [details] [diff] [review]
Add Response, Request, SameOriginResponse, and FetchEvent implementations.

Cycle collection fixes and other additions.
Attachment #830584 - Attachment is obsolete: true
Attachment #830586 - Attachment is obsolete: true
Created attachment 8343448 [details] [diff] [review]
Make document navigation cause a ServiceWorker fetch event.

Added hashtables to keep track of ServiceWorkers.
Various Runnables to pass requests to and from workers.
Mark a document as being controlled in StartDocumentLoad.
Attachment #830589 - Attachment is obsolete: true
Attachment #8343448 - Flags: feedback?(josh)
Created attachment 8343449 [details] [diff] [review]
Intercept images, fonts, objects, stylesheets, and XHRs.

Minor changes.
Attachment #830923 - Attachment is obsolete: true
Attachment #8343448 - Flags: feedback?(josh)
Comment on attachment 8343448 [details] [diff] [review]
Make document navigation cause a ServiceWorker fetch event.

Review of attachment 8343448 [details] [diff] [review]:
-----------------------------------------------------------------

::: content/base/src/nsDocument.cpp
@@ +2474,5 @@
>    }
>  
> +  bool dummy;
> +  nsCOMPtr<nsIServiceWorkerManager> swm = do_GetService(SERVICEWORKERMANAGER_CONTRACTID);
> +  swm->StartInterception(this, &dummy);

How do we plan to make use of the success value in the future?

::: dom/interfaces/base/nsIServiceWorkerManager.idl
@@ +17,5 @@
> +  void sendFetchEvent(in nsIDocument aDocument, in nsIAlternateSourceChannel aChannel, in nsIURI aPageURI);
> +  bool isControlled(in nsIDocument aDocument);
> +  // Returns a ServiceWorker or null.
> +  nsISupports GetServiceWorker(in nsIDocument aDocument);
> +  bool startInterception(in nsIDocument aDocument);

The return value needs documentation.

::: dom/workers/ServiceWorkerManager.cpp
@@ +355,5 @@
> +    JS::Rooted<JSObject*> jsGlobal(aCx, target->GetWrapper());
> +    GlobalObject globalObject(aCx, jsGlobal);
> +
> +    mozilla::dom::RequestInit init;
> +    init.mUrl.Construct(NS_ConvertUTF8toUTF16(mResourceURL));

I'm not convinced that using this URL across threads is safe. I would be inclined to store the spec and create a new URL here.

@@ +468,5 @@
> +  return NS_OK;
> +}
> +
> +static nsresult
> +GetDocumentDomain(nsIDocument* aDoc, nsCString& domain, JSContext* cx = nullptr)

This method feels like it should reside elsewhere (like in document code), but that's just my gut feeling.

@@ +520,5 @@
> +ServiceWorkerManager::IsControlled(nsIDocument* aDoc, bool* aControlled)
> +{
> +  MOZ_ASSERT(NS_IsMainThread());
> +  nsCString domain;
> +  nsresult rv = GetDocumentDomain(aDoc, domain);

rv is ignored

@@ +560,5 @@
> +ServiceWorkerManager::StartInterception(nsIDocument* aDoc, bool* aSuccess)
> +{
> +  MOZ_ASSERT(NS_IsMainThread());
> +  nsCString domain;
> +  nsresult rv = GetDocumentDomain(aDoc, domain);

rv is ignored.

@@ +821,5 @@
> +    return NS_OK;
> +  }
> +
> +  nsCString domain;
> +  nsresult rv = aDocument->GetDocumentURI()->GetHost(domain);

rv is ignored
Depends on: 967264
Depends on: 975799
Depends on: 984050
Attachment #8343436 - Attachment is obsolete: true
Created attachment 8400324 [details] [diff] [review]
Add Response, Request, SameOriginResponse, and FetchEvent implementations.

Various rebases, spec updates and minor things to make these usable.
Attachment #8343446 - Attachment is obsolete: true
Created attachment 8400330 [details] [diff] [review]
Make document navigation cause a ServiceWorker fetch event.
Attachment #8343448 - Attachment is obsolete: true
Created attachment 8400331 [details] [diff] [review]
Patch 3 - Setup Request object
Created attachment 8400335 [details] [diff] [review]
Patch 4 - Intercept images, fonts, objects, stylesheets, and XHRs.
Attachment #8343449 - Attachment is obsolete: true
Created attachment 8400339 [details] [diff] [review]
Patch 5 - Don't intercept ServiceWorker load requests.
Blocks: 868322
Blocks: 1018320
Depends on: 1025077
Depends on: 1025082
Depends on: 1032521
Blocks: 1038811

Updated

3 years ago
Depends on: 1038973
Depends on: 1041335
Depends on: 1041340
Depends on: 1043711
Depends on: 1045257
Depends on: 1046107
Depends on: 1043004
Depends on: 1011268
Depends on: 1043701
Depends on: 1040924
Depends on: 1041339
Depends on: 1003991
Depends on: 1002571
Depends on: 982787
Depends on: 983499
Depends on: 984575
Depends on: 982728
Depends on: 982711
Depends on: 1047398
Depends on: 1048699
Depends on: 1048595
Depends on: 1049599
Depends on: 1049802
Depends on: 1052935
Depends on: 1052936
Blocks: 1053255
Depends on: 1053275
Depends on: 1053483
Depends on: 1056702
Depends on: 1058311
Depends on: 1059174
Depends on: 1059784

Updated

3 years ago
Depends on: 1065216
Depends on: 1065515
Depends on: 1065367
Depends on: 1065366
Depends on: 1078808
Depends on: 1080109
Depends on: 1080110
Whiteboard: [dependency: marketplace-partners]

Updated

2 years ago
Depends on: 1109680
Depends on: 1112469
Depends on: 1113003
Depends on: 1130684
Depends on: 1130685
Depends on: 1130686
Depends on: 1130687
Depends on: 1130688
Depends on: 1131322
jdm, are any of the attachments here relevant any longer? Otherwise I'll obsolete all of them.
Flags: needinfo?(josh)
No longer depends on: 930349
No longer depends on: 931249
No longer depends on: 982787
No longer depends on: 984050
No longer depends on: 1032521
No longer depends on: 1038973
No longer depends on: 1040924
No longer depends on: 1041339
No longer depends on: 1041340
No longer depends on: 1049802
No longer depends on: 1052935
No longer depends on: 1052936
No longer depends on: 1053275
No longer depends on: 1056702
No longer depends on: 1058311
No longer depends on: 1065216
No longer depends on: 1065515
No longer depends on: 1078808
No longer depends on: 1080109
No longer depends on: 1080110
Wow. No.
Flags: needinfo?(josh)
No longer depends on: 1112469
No longer depends on: 1113003
No longer depends on: 1130684
No longer depends on: 1130685
No longer depends on: 1130686
No longer blocks: 1038811
No longer blocks: 868322
No longer blocks: 857464
No longer depends on: 1130687
No longer depends on: 1130688
Attachment #8400324 - Attachment is obsolete: true
Attachment #8400330 - Attachment is obsolete: true
Attachment #8400331 - Attachment is obsolete: true
Attachment #8400335 - Attachment is obsolete: true
Attachment #8400339 - Attachment is obsolete: true

Updated

2 years ago
Depends on: 1173500
Assignee: nsm.nikhil → nobody
Status: ASSIGNED → NEW
Depends on: 1194562

Updated

a year ago
See Also: → bug 1227104

Updated

a year ago
Blocks: 1212648
Whiteboard: [dependency: marketplace-partners] → [dependency: marketplace-partners][games:p1]
Whiteboard: [dependency: marketplace-partners][games:p1] → [dependency: marketplace-partners][games:p1][platform-rel-Games]

Updated

10 months ago
platform-rel: --- → ?
Depends on: 1226983, 1262699, 1328391
Depends on: 1328614
No longer depends on: 1131322
No longer blocks: 943220
Whiteboard: [dependency: marketplace-partners][games:p1][platform-rel-Games] → [games:p1][platform-rel-Games]
Depends on: 1328622
Depends on: 1283191
Depends on: 1212882
I've attempted to clean up this bug to use as a meta for ongoing service worker efforts.  I've hung some child meta bugs for different efforts underneath it:

  Bug 1226983 (ServiceWorkers-compat): Spec compatibility issues
  Bug 1262699 (ServiceWorkers-devtools): Make it easier for web devs to build service workers
  Bug 1328391 (ServiceWorkers-data): Add telemetry to support data driven development
  Bug 1328622 (ServiceWorkers-perf): Performance testing and optimizations
  Bug 1328614 (ServiceWorkers-tests): Issues with service worker related tests
  Bug 1283191 (ServiceWorkers-streams): Integrate WHATWG streams API into service workers
  Bug 1212882 (ServiceWorkers-foreign): Implement foreign fetch
Depends on: 1328631
I also added:

  Bug 1328631 (ServiceWorkers-stability): To track crashes, assertions, or other stability issues
Depends on: 1231208
platform-rel: ? → ---
Depends on: 1346245
You need to log in before you can comment on or make changes to this bug.