Bug 1218135 - Remove FetchEvent.client; r=bzbarsky
authorEhsan Akhgari <ehsan@mozilla.com>
Sat, 24 Oct 2015 21:42:05 -0400
changeset 269596 2d428d8ed0986e8ace0762d591669a308cb39a54
parent 269595 d599cee8d32a52505a0afbfff3acb41ead9df829
child 269597 d67a59a199664e8430d7553e7ba1c9afca76e5bd
push id67141
push usereakhgari@mozilla.com
push dateTue, 27 Oct 2015 03:00:28 +0000
treeherdermozilla-inbound@253416fea643 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1218135
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1218135 - Remove FetchEvent.client; r=bzbarsky This has been removed from the spec. See: https://github.com/slightlyoff/ServiceWorker/issues/723#issuecomment-123516555
dom/push/test/lifetime_worker.js
dom/webidl/FetchEvent.webidl
dom/workers/ServiceWorkerEvents.cpp
dom/workers/ServiceWorkerEvents.h
dom/workers/ServiceWorkerPrivate.cpp
dom/workers/test/serviceworkers/fetch/plugin/worker.js
dom/workers/test/serviceworkers/fetch_event_client.js
dom/workers/test/serviceworkers/mochitest.ini
dom/workers/test/serviceworkers/sw_clients/dummy.html
dom/workers/test/serviceworkers/test_fetch_event_client_postmessage.html
dom/workers/test/serviceworkers/unresolved_fetch_worker.js
--- a/dom/push/test/lifetime_worker.js
+++ b/dom/push/test/lifetime_worker.js
@@ -2,23 +2,25 @@ var state = "from_scope";
 var resolvePromiseCallback;
 
 onfetch = function(event) {
   if (event.request.url.indexOf("lifetime_frame.html") >= 0) {
     event.respondWith(new Response("iframe_lifetime"));
     return;
   }
 
-  if (!event.client) {
-    dump("ERROR: no client to post the message to!\n");
-    dump("request.url=" + event.request.url + "\n");
-    return;
-  }
-
-  event.client.postMessage({type: "fetch", state: state});
+  var currentState = state;
+  event.waitUntil(
+    clients.matchAll()
+           .then(clients => {
+             clients.forEach(client => {
+               client.postMessage({type: "fetch", state: currentState});
+             });
+           })
+  );
 
   if (event.request.url.indexOf("update") >= 0) {
     state = "update";
   } else if (event.request.url.indexOf("wait") >= 0) {
     event.respondWith(new Promise(function(res, rej) {
       if (resolvePromiseCallback) {
         dump("ERROR: service worker was already waiting on a promise.\n");
       }
--- a/dom/webidl/FetchEvent.webidl
+++ b/dom/webidl/FetchEvent.webidl
@@ -7,22 +7,18 @@
  * http://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html
  */
 
 [Constructor(DOMString type, optional FetchEventInit eventInitDict),
  Func="mozilla::dom::workers::ServiceWorkerVisible",
  Exposed=(ServiceWorker)]
 interface FetchEvent : ExtendableEvent {
   [SameObject] readonly attribute Request request;
-
-  // https://github.com/slightlyoff/ServiceWorker/issues/631
-  readonly attribute Client? client; // The window issuing the request.
   readonly attribute boolean isReload;
 
   [Throws]
   void respondWith(Promise<Response> r);
 };
 
 dictionary FetchEventInit : EventInit {
   Request request;
-  Client client;
   boolean isReload;
 };
--- a/dom/workers/ServiceWorkerEvents.cpp
+++ b/dom/workers/ServiceWorkerEvents.cpp
@@ -1,16 +1,15 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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/. */
 
 #include "ServiceWorkerEvents.h"
-#include "ServiceWorkerClient.h"
 
 #include "nsIHttpChannelInternal.h"
 #include "nsINetworkInterceptController.h"
 #include "nsIOutputStream.h"
 #include "nsContentPolicyUtils.h"
 #include "nsContentUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsServiceManagerUtils.h"
@@ -67,22 +66,20 @@ FetchEvent::FetchEvent(EventTarget* aOwn
 }
 
 FetchEvent::~FetchEvent()
 {
 }
 
 void
 FetchEvent::PostInit(nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
-                     const nsACString& aScriptSpec,
-                     UniquePtr<ServiceWorkerClientInfo>&& aClientInfo)
+                     const nsACString& aScriptSpec)
 {
   mChannel = aChannel;
   mScriptSpec.Assign(aScriptSpec);
-  mClientInfo = Move(aClientInfo);
 }
 
 /*static*/ already_AddRefed<FetchEvent>
 FetchEvent::Constructor(const GlobalObject& aGlobal,
                         const nsAString& aType,
                         const FetchEventInit& aOptions,
                         ErrorResult& aRv)
 {
@@ -91,18 +88,16 @@ FetchEvent::Constructor(const GlobalObje
   RefPtr<FetchEvent> e = new FetchEvent(owner);
   bool trusted = e->Init(owner);
   e->InitEvent(aType, aOptions.mBubbles, aOptions.mCancelable);
   e->SetTrusted(trusted);
   e->mRequest = aOptions.mRequest.WasPassed() ?
       &aOptions.mRequest.Value() : nullptr;
   e->mIsReload = aOptions.mIsReload.WasPassed() ?
       aOptions.mIsReload.Value() : false;
-  e->mClient = aOptions.mClient.WasPassed() ?
-      &aOptions.mClient.Value() : nullptr;
   return e.forget();
 }
 
 namespace {
 
 class FinishResponse final : public nsRunnable
 {
   nsMainThreadPtrHandle<nsIInterceptedChannel> mChannel;
@@ -455,42 +450,24 @@ FetchEvent::RespondWith(Promise& aArg, E
   StopImmediatePropagation();
   mWaitToRespond = true;
   RefPtr<RespondWithHandler> handler =
     new RespondWithHandler(mChannel, mRequest->Mode(), ir->IsClientRequest(),
                            ir->IsNavigationRequest(), mScriptSpec);
   aArg.AppendNativeHandler(handler);
 }
 
-already_AddRefed<ServiceWorkerClient>
-FetchEvent::GetClient()
-{
-  if (!mClient) {
-    if (!mClientInfo) {
-      return nullptr;
-    }
-
-    WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
-    MOZ_ASSERT(worker);
-    RefPtr<nsIGlobalObject> global = worker->GlobalScope();
-
-    mClient = new ServiceWorkerClient(global, *mClientInfo);
-  }
-  RefPtr<ServiceWorkerClient> client = mClient;
-  return client.forget();
-}
-
 NS_IMPL_ADDREF_INHERITED(FetchEvent, ExtendableEvent)
 NS_IMPL_RELEASE_INHERITED(FetchEvent, ExtendableEvent)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(FetchEvent)
 NS_INTERFACE_MAP_END_INHERITING(ExtendableEvent)
 
 NS_IMPL_CYCLE_COLLECTION_INHERITED(FetchEvent, ExtendableEvent,
-                                   mRequest, mClient, mPromise)
+                                   mRequest, mPromise)
 
 ExtendableEvent::ExtendableEvent(EventTarget* aOwner)
   : Event(aOwner, nullptr, nullptr)
 {
 }
 
 void
 ExtendableEvent::WaitUntil(Promise& aPromise, ErrorResult& aRv)
--- a/dom/workers/ServiceWorkerEvents.h
+++ b/dom/workers/ServiceWorkerEvents.h
@@ -30,18 +30,16 @@ namespace dom {
 class Blob;
 class Request;
 class ResponseOrPromise;
 } // namespace dom
 } // namespace mozilla
 
 BEGIN_WORKERS_NAMESPACE
 
-class ServiceWorkerClient;
-
 class CancelChannelRunnable final : public nsRunnable
 {
   nsMainThreadPtrHandle<nsIInterceptedChannel> mChannel;
   const nsresult mStatus;
 public:
   CancelChannelRunnable(nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
                         nsresult aStatus);
 
@@ -98,20 +96,18 @@ public:
   {
     return this;
   }
 };
 
 class FetchEvent final : public ExtendableEvent
 {
   nsMainThreadPtrHandle<nsIInterceptedChannel> mChannel;
-  RefPtr<ServiceWorkerClient> mClient;
   RefPtr<Request> mRequest;
   nsCString mScriptSpec;
-  UniquePtr<ServiceWorkerClientInfo> mClientInfo;
   RefPtr<Promise> mPromise;
   bool mIsReload;
   bool mWaitToRespond;
 protected:
   explicit FetchEvent(EventTarget* aOwner);
   ~FetchEvent();
 
 public:
@@ -120,18 +116,17 @@ public:
   NS_FORWARD_TO_EVENT
 
   virtual JSObject* WrapObjectInternal(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override
   {
     return FetchEventBinding::Wrap(aCx, this, aGivenProto);
   }
 
   void PostInit(nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
-                const nsACString& aScriptSpec,
-                UniquePtr<ServiceWorkerClientInfo>&& aClientInfo);
+                const nsACString& aScriptSpec);
 
   static already_AddRefed<FetchEvent>
   Constructor(const GlobalObject& aGlobal,
               const nsAString& aType,
               const FetchEventInit& aOptions,
               ErrorResult& aRv);
 
   bool
@@ -141,19 +136,16 @@ public:
   }
 
   Request*
   Request_() const
   {
     return mRequest;
   }
 
-  already_AddRefed<ServiceWorkerClient>
-  GetClient();
-
   bool
   IsReload() const
   {
     return mIsReload;
   }
 
   void
   RespondWith(Promise& aArg, ErrorResult& aRv);
--- a/dom/workers/ServiceWorkerPrivate.cpp
+++ b/dom/workers/ServiceWorkerPrivate.cpp
@@ -910,17 +910,16 @@ namespace {
 // Inheriting ExtendableEventWorkerRunnable so that the worker is not terminated
 // while handling the fetch event, though that's very unlikely.
 class FetchEventRunnable : public ExtendableFunctionalEventWorkerRunnable
                          , public nsIHttpHeaderVisitor {
   nsMainThreadPtrHandle<nsIInterceptedChannel> mInterceptedChannel;
   const nsCString mScriptSpec;
   nsTArray<nsCString> mHeaderNames;
   nsTArray<nsCString> mHeaderValues;
-  UniquePtr<ServiceWorkerClientInfo> mClientInfo;
   nsCString mSpec;
   nsCString mMethod;
   bool mIsReload;
   DebugOnly<bool> mIsHttpChannel;
   RequestMode mRequestMode;
   RequestRedirect mRequestRedirect;
   RequestCredentials mRequestCredentials;
   nsContentPolicyType mContentPolicyType;
@@ -935,17 +934,16 @@ public:
                      const nsACString& aScriptSpec,
                      nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
                      UniquePtr<ServiceWorkerClientInfo>&& aClientInfo,
                      bool aIsReload)
     : ExtendableFunctionalEventWorkerRunnable(
         aWorkerPrivate, aKeepAliveToken, aRegistration)
     , mInterceptedChannel(aChannel)
     , mScriptSpec(aScriptSpec)
-    , mClientInfo(Move(aClientInfo))
     , mIsReload(aIsReload)
     , mIsHttpChannel(false)
     , mRequestMode(RequestMode::No_cors)
     , mRequestRedirect(RequestRedirect::Follow)
     // By default we set it to same-origin since normal HTTP fetches always
     // send credentials to same-origin websites unless explicitly forbidden.
     , mRequestCredentials(RequestCredentials::Same_origin)
     , mContentPolicyType(nsIContentPolicy::TYPE_INVALID)
@@ -1156,17 +1154,17 @@ private:
     init.mIsReload.Construct(mIsReload);
     RefPtr<FetchEvent> event =
       FetchEvent::Constructor(globalObj, NS_LITERAL_STRING("fetch"), init, result);
     if (NS_WARN_IF(result.Failed())) {
       result.SuppressException();
       return false;
     }
 
-    event->PostInit(mInterceptedChannel, mScriptSpec, Move(mClientInfo));
+    event->PostInit(mInterceptedChannel, mScriptSpec);
     event->SetTrusted(true);
 
     RefPtr<EventTarget> target = do_QueryObject(aWorkerPrivate->GlobalScope());
     nsresult rv2 = target->DispatchDOMEvent(nullptr, event, nullptr, nullptr);
     if (NS_WARN_IF(NS_FAILED(rv2)) || !event->WaitToRespond()) {
       nsCOMPtr<nsIRunnable> runnable;
       if (event->DefaultPrevented(aCx)) {
         runnable = new CancelChannelRunnable(mInterceptedChannel, NS_ERROR_INTERCEPTION_CANCELED);
--- a/dom/workers/test/serviceworkers/fetch/plugin/worker.js
+++ b/dom/workers/test/serviceworkers/fetch/plugin/worker.js
@@ -1,7 +1,14 @@
 self.addEventListener("fetch", function(event) {
   var resource = event.request.url.split('/').pop();
-  if (event.client) {
-    event.client.postMessage({context: event.request.context,
-                              resource: resource});
-  }
+  event.waitUntil(
+    clients.matchAll()
+           .then(clients => {
+             clients.forEach(client => {
+               if (client.url.includes("plugins.html")) {
+                 client.postMessage({context: event.request.context,
+                                     resource: resource});
+               }
+             });
+           })
+  );
 });
deleted file mode 100644
--- a/dom/workers/test/serviceworkers/fetch_event_client.js
+++ /dev/null
@@ -1,6 +0,0 @@
-var CLIENT_URL =
-  "http://mochi.test:8888/tests/dom/workers/test/serviceworkers/sw_clients/dummy.html"
-
-self.addEventListener("fetch", function(event) {
-  event.client.postMessage({status: event.client.url === CLIENT_URL});
-});
--- a/dom/workers/test/serviceworkers/mochitest.ini
+++ b/dom/workers/test/serviceworkers/mochitest.ini
@@ -168,18 +168,16 @@ support-files =
   register_https.html
   gzip_redirect_worker.js
   sw_clients/navigator.html
   eval_worker.js
   test_eval_not_allowed.html^headers^
   opaque_intercept_worker.js
   notify_loaded.js
   test_request_context.js
-  fetch_event_client.js
-  sw_clients/dummy.html
   fetch/plugin/worker.js
   fetch/plugin/plugins.html
   eventsource/*
   sw_clients/file_blob_upload_frame.html
   redirect_post.sjs
   xslt_worker.js
   xslt/*
   unresolved_fetch_worker.js
@@ -266,17 +264,16 @@ skip-if = toolkit == "android" || toolki
 [test_skip_waiting.html]
 [test_strict_mode_warning.html]
 [test_third_party_iframes.html]
 [test_unregister.html]
 [test_workerUnregister.html]
 [test_workerUpdate.html]
 [test_workerupdatefoundevent.html]
 [test_opaque_intercept.html]
-[test_fetch_event_client_postmessage.html]
 [test_xslt.html]
 [test_escapedSlashes.html]
 [test_eventsource_intercept.html]
 [test_not_intercept_plugin.html]
 [test_file_blob_upload.html]
 [test_unresolved_fetch_interception.html]
 [test_hsts_upgrade_intercept.html]
 skip-if = e10s # Bug 1214305
deleted file mode 100644
--- a/dom/workers/test/serviceworkers/sw_clients/dummy.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<!--
-  Any copyright is dedicated to the Public Domain.
-  http://creativecommons.org/publicdomain/zero/1.0/
--->
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Bug 1158735 - Dummy page</title>
-</head>
-<body>
-  <script type="text/javascript" >
-    window.onload = function() {
-      navigator.serviceWorker.ready.then(function(swr) {
-        fetch("foo.txt");
-      });
-    }
-  </script>
-</body>
-</html>
deleted file mode 100644
--- a/dom/workers/test/serviceworkers/test_fetch_event_client_postmessage.html
+++ /dev/null
@@ -1,73 +0,0 @@
-<!--
-  Any copyright is dedicated to the Public Domain.
-  http://creativecommons.org/publicdomain/zero/1.0/
--->
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Bug 1158735 - FetchEvent.client asserting in onFetch when there's no document.</title>
-  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
-  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
-</head>
-<body>
-  <p id="display"></p>
-  <div id="content" style="display: none"></div>
-  <pre id="test"></pre>
-  <script class="testbody" type="text/javascript">
-    SimpleTest.requestCompleteLog();
-
-    var registration;
-    function start() {
-      return navigator.serviceWorker.register("fetch_event_client.js", { scope: "./"})
-        .then((swr) => registration = swr);
-    }
-
-    function unregister() {
-      return registration.unregister().then(function(result) {
-        ok(result, "Unregister should return true.");
-      }, function(e) {
-        dump("Unregistering the SW failed with " + e + "\n");
-      });
-    }
-
-    function testFetchEvent() {
-      var p = new Promise(function(resolve, reject) {
-        var content = document.getElementById("content");
-        ok(content, "parent exists.");
-
-        var iframe = document.createElement('iframe');
-        iframe.setAttribute('src', "sw_clients/dummy.html");
-        content.appendChild(iframe);
-
-        var w = iframe.contentWindow;
-        w.navigator.serviceWorker.onmessage = function(msg) {
-          ok(msg.data.status, "Receive message posted by client successfully");
-
-          resolve();
-        }
-      });
-
-      return p;
-    }
-
-    function runTest() {
-      start()
-        .then(testFetchEvent)
-        .then(unregister)
-        .then(function() {
-        }).catch(function(e) {
-          ok(false, "Some test failed with error " + e);
-        }).then(SimpleTest.finish);
-    }
-
-    SimpleTest.waitForExplicitFinish();
-    SpecialPowers.pushPrefEnv({"set": [
-      ["dom.serviceWorkers.exemptFromPerDomainMax", true],
-      ["dom.serviceWorkers.enabled", true],
-      ["dom.serviceWorkers.testing.enabled", true],
-      ["dom.serviceWorkers.testing.enabled", true],
-      ['dom.serviceWorkers.interception.enabled', true],
-    ]}, runTest);
-  </script>
-</body>
-</html>
--- a/dom/workers/test/serviceworkers/unresolved_fetch_worker.js
+++ b/dom/workers/test/serviceworkers/unresolved_fetch_worker.js
@@ -1,14 +1,17 @@
 onfetch = function(event) {
-  if (!event.client) {
-    dump("ERROR: event doesnt have a client");
-  }
-
-  event.client.postMessage("continue");
+  event.waitUntil(
+    clients.matchAll()
+           .then(clients => {
+             clients.forEach(client => {
+               client.postMessage("continue");
+             });
+           })
+  );
 
   // never resolve
   event.respondWith(new Promise(function(res, rej) {}));
 }
 
 onactivate = function(event) {
   event.waitUntil(clients.claim());
 }