Bug 1236933 - Return null from FetchEvent.clientId for non-subresource network requests; r=bkelly
authorEhsan Akhgari <ehsan@mozilla.com>
Fri, 08 Jan 2016 15:53:38 -0500
changeset 279176 d47e55ac4177399b941289fe9042b100439bc1a5
parent 279175 bbbed2d8a80e2465654dad432c595a87dd7a3126
child 279177 b9f5b9c3d5a14d799c9cb4b9161c149e3b0e1011
push id70009
push usereakhgari@mozilla.com
push dateSat, 09 Jan 2016 03:38:52 +0000
treeherdermozilla-inbound@d47e55ac4177 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1236933
milestone46.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 1236933 - Return null from FetchEvent.clientId for non-subresource network requests; r=bkelly
dom/workers/ServiceWorkerManager.cpp
dom/workers/ServiceWorkerPrivate.cpp
testing/web-platform/mozilla/tests/service-workers/service-worker/clients-get.https.html
testing/web-platform/mozilla/tests/service-workers/service-worker/fetch-event.https.html
testing/web-platform/mozilla/tests/service-workers/service-worker/resources/clients-get-frame.html
testing/web-platform/mozilla/tests/service-workers/service-worker/resources/clients-get-worker.js
testing/web-platform/mozilla/tests/service-workers/service-worker/resources/fetch-event-test-worker.js
--- a/dom/workers/ServiceWorkerManager.cpp
+++ b/dom/workers/ServiceWorkerManager.cpp
@@ -3596,17 +3596,18 @@ ServiceWorkerManager::DispatchFetchEvent
     nsCOMPtr<nsIChannel> internalChannel;
     aRv = aChannel->GetChannel(getter_AddRefs(internalChannel));
     if (NS_WARN_IF(aRv.Failed())) {
       return;
     }
 
     internalChannel->GetLoadGroup(getter_AddRefs(loadGroup));
 
-    documentId = aDocumentIdForTopLevelNavigation;
+    // TODO: Use aDocumentIdForTopLevelNavigation for potentialClientId, pending
+    // the spec change.
 
     nsCOMPtr<nsIURI> uri;
     aRv = aChannel->GetSecureUpgradedChannelURI(getter_AddRefs(uri));
     if (NS_WARN_IF(aRv.Failed())) {
       return;
     }
 
     RefPtr<ServiceWorkerRegistrationInfo> registration =
--- a/dom/workers/ServiceWorkerPrivate.cpp
+++ b/dom/workers/ServiceWorkerPrivate.cpp
@@ -1238,17 +1238,19 @@ private:
     MOZ_ASSERT_IF(mIsHttpChannel && internalReq->IsNavigationRequest(),
                   request->Redirect() == RequestRedirect::Manual);
 
     RootedDictionary<FetchEventInit> init(aCx);
     init.mRequest.Construct();
     init.mRequest.Value() = request;
     init.mBubbles = false;
     init.mCancelable = true;
-    init.mClientId = mClientId;
+    if (!mClientId.IsEmpty()) {
+      init.mClientId = mClientId;
+    }
     init.mIsReload = mIsReload;
     RefPtr<FetchEvent> event =
       FetchEvent::Constructor(globalObj, NS_LITERAL_STRING("fetch"), init, result);
     if (NS_WARN_IF(result.Failed())) {
       result.SuppressException();
       return false;
     }
 
--- a/testing/web-platform/mozilla/tests/service-workers/service-worker/clients-get.https.html
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/clients-get.https.html
@@ -2,44 +2,62 @@
 <title>Service Worker: Clients.get</title>
 <script src="/resources/testharness.js"></script>
 <script src="/resources/testharnessreport.js"></script>
 <script src="resources/get-host-info.sub.js"></script>
 <script src="resources/test-helpers.sub.js"></script>
 <script>
 var host_info = get_host_info();
 
-var scope = 'resources/blank.html?clients-get';
+var scope = 'resources/clients-get-frame.html';
 var t = async_test('Test Clients.get()');
 var clientIds = [];
+var frame;
 t.step(function() {
     service_worker_unregister_and_register(
         t, 'resources/clients-get-worker.js', scope)
       .then(function(registration) {
           return wait_for_state(t, registration.installing, 'activated');
         })
       .then(function() {
           return with_iframe(scope + '#1');
         })
       .then(function(frame1) {
-          clientIds.push(frame1.contentDocument.body.textContent);
           frame1.focus();
+          return wait_for_clientId();
+        })
+      .then(function(clientId) {
+          clientIds.push(clientId);
           return with_iframe(scope + '#2');
         })
       .then(function(frame2) {
-          clientIds.push(frame2.contentDocument.body.textContent);
+          frame = frame2;
+          return wait_for_clientId();
+        })
+      .then(function(clientId) {
+          clientIds.push(clientId);
           var channel = new MessageChannel();
           channel.port1.onmessage = t.step_func(on_message);
-          frame2.contentWindow.navigator.serviceWorker.controller.postMessage(
+          frame.contentWindow.navigator.serviceWorker.controller.postMessage(
               {port:channel.port2, clientIds:clientIds,
                message: 'get_client_ids'}, [channel.port2]);
         })
       .catch(unreached_rejection(t));
   });
 
+function wait_for_clientId() {
+  return new Promise(function(resolve, reject) {
+      function get_client_id(e) {
+        window.removeEventListener("message", get_client_id);
+        resolve(e.data.clientId);
+      }
+      window.addEventListener("message", get_client_id, false);
+    });
+}
+
 var expected = [
     /* visibilityState, focused, url, frameType */
     ['visible', true, new URL(scope + '#1', location).toString(), 'nested'],
     ['visible', false, new URL(scope + '#2', location).toString(), 'nested'],
     undefined
 ];
 
 function on_message(e) {
--- a/testing/web-platform/mozilla/tests/service-workers/service-worker/fetch-event.https.html
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/fetch-event.https.html
@@ -66,42 +66,36 @@ async_test(function(t) {
           return service_worker_unregister_and_done(t, scope);
         })
       .catch(unreached_rejection(t));
   }, 'Service Worker responds to fetch event with the referrer URL');
 
 async_test(function(t) {
     var scope = 'resources/simple.html?clientId';
     var frame;
-    var initial_client_id;
     service_worker_unregister_and_register(t, worker, scope)
       .then(function(reg) {
           return wait_for_state(t, reg.installing, 'activated');
         })
       .then(function() { return with_iframe(scope); })
       .then(function(f) {
           frame = f;
           assert_equals(
-            frame.contentDocument.body.textContent.substr(0, 15),
-            'Client ID Found',
+            frame.contentDocument.body.textContent,
+            'Client ID Not Found',
             'Service Worker should respond to fetch with a client id');
-          initial_client_id = frame.contentDocument.body.textContent.substr(17);
           return frame.contentWindow.fetch('resources/other.html?clientId');
         })
       .then(function(response) { return response.text(); })
       .then(function(response_text) {
           var new_client_id = response_text.substr(17);
           assert_equals(
             response_text.substr(0, 15),
             'Client ID Found',
             'Service Worker should respond to fetch with an existing client id');
-          assert_equals(
-            initial_client_id,
-            new_client_id,
-            'Service Worker should observe the correct client ID for a controlled document');
           frame.remove();
           return service_worker_unregister_and_done(t, scope);
         })
       .catch(unreached_rejection(t));
   }, 'Service Worker responds to fetch event with an existing client id');
 
 async_test(function(t) {
     var scope = 'resources/simple.html?ignore';
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/clients-get-frame.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<script>
+
+  fetch("clientId")
+    .then(function(response) {
+        return response.text();
+      })
+    .then(function(text) {
+        parent.postMessage({clientId: text}, "*");
+      });
+
+</script>
--- a/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/clients-get-worker.js
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/clients-get-worker.js
@@ -1,9 +1,17 @@
 self.onfetch = function(e) {
+  if (e.request.url.indexOf("clients-get-frame.html") >= 0) {
+    if (e.clientId === null) {
+      e.respondWith(fetch(e.request));
+    } else {
+      e.respondWith(Response.error());
+    }
+    return;
+  }
   e.respondWith(new Response(e.clientId));
 };
 
 self.onmessage = function(e) {
   var port = e.data.port;
   if (e.data.message == 'get_client_ids') {
     var clientIds = e.data.clientIds;
     var message = [];
--- a/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/fetch-event-test-worker.js
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/fetch-event-test-worker.js
@@ -8,17 +8,17 @@ function handleBlob(event) {
 
 function handleReferrer(event) {
   event.respondWith(new Response(new Blob(
     ['Referrer: ' + event.request.referrer])));
 }
 
 function handleClientId(event) {
   var body;
-  if (event.clientId !== '') {
+  if (event.clientId !== null) {
     body = 'Client ID Found: ' + event.clientId;
   } else {
     body = 'Client ID Not Found';
   }
   event.respondWith(new Response(body));
 }
 
 function handleNullBody(event) {