Bug 1188822 - Make service-workers/service-worker/fetch-request-resources.https.html pass. r=bkelly
authordimi <dlee@mozilla.com>
Mon, 25 Jan 2016 14:18:29 +0800
changeset 281803 74ab992b69520f1ad123e001ebad4440a893e408
parent 281802 3593d33dd15b3a1bd1723bf26ea45efa2fcb428a
child 281804 48b51827b74aaca6417cdf111400dff4f426109f
push id70935
push usercbook@mozilla.com
push dateWed, 27 Jan 2016 10:04:51 +0000
treeherdermozilla-inbound@74ab992b6952 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1188822
milestone47.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 1188822 - Make service-workers/service-worker/fetch-request-resources.https.html pass. r=bkelly
dom/base/nsScriptLoader.cpp
dom/fetch/InternalRequest.cpp
dom/fetch/InternalRequest.h
dom/workers/ServiceWorkerPrivate.cpp
testing/web-platform/mozilla/meta/service-workers/service-worker/fetch-request-css-images.https.html.ini
testing/web-platform/mozilla/meta/service-workers/service-worker/fetch-request-resources.https.html.ini
testing/web-platform/mozilla/tests/service-workers/service-worker/fetch-request-css-images.https.html
testing/web-platform/mozilla/tests/service-workers/service-worker/fetch-request-resources.https.html
testing/web-platform/mozilla/tests/service-workers/service-worker/resources/fetch-request-resources-iframe.https.html
testing/web-platform/mozilla/tests/service-workers/service-worker/resources/fetch-request-xhr-iframe.https.html
--- a/dom/base/nsScriptLoader.cpp
+++ b/dom/base/nsScriptLoader.cpp
@@ -289,17 +289,19 @@ nsScriptLoader::StartLoad(nsScriptLoadRe
   NS_ENSURE_TRUE(window, NS_ERROR_NULL_POINTER);
   nsIDocShell *docshell = window->GetDocShell();
   nsCOMPtr<nsIInterfaceRequestor> prompter(do_QueryInterface(docshell));
 
   nsSecurityFlags securityFlags =
     aRequest->mCORSMode == CORS_NONE
     ? nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL
     : nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS;
-  if (aRequest->mCORSMode == CORS_USE_CREDENTIALS) {
+  if (aRequest->mCORSMode == CORS_ANONYMOUS) {
+    securityFlags |= nsILoadInfo::SEC_COOKIES_SAME_ORIGIN;
+  } else if (aRequest->mCORSMode == CORS_USE_CREDENTIALS) {
     securityFlags |= nsILoadInfo::SEC_COOKIES_INCLUDE;
   }
   securityFlags |= nsILoadInfo::SEC_ALLOW_CHROME;
 
   nsCOMPtr<nsIChannel> channel;
   nsresult rv = NS_NewChannel(getter_AddRefs(channel),
                               aRequest->mURI,
                               context,
--- a/dom/fetch/InternalRequest.cpp
+++ b/dom/fetch/InternalRequest.cpp
@@ -319,10 +319,55 @@ InternalRequest::MapChannelToRequestMode
   uint32_t corsMode;
   MOZ_ALWAYS_TRUE(NS_SUCCEEDED(httpChannel->GetCorsMode(&corsMode)));
   MOZ_ASSERT(corsMode != nsIHttpChannelInternal::CORS_MODE_NAVIGATE);
 
   // This cast is valid due to static asserts in ServiceWorkerManager.cpp.
   return static_cast<RequestMode>(corsMode);
 }
 
+// static
+RequestCredentials
+InternalRequest::MapChannelToRequestCredentials(nsIChannel* aChannel)
+{
+  MOZ_ASSERT(aChannel);
+
+  nsCOMPtr<nsILoadInfo> loadInfo;
+  MOZ_ALWAYS_TRUE(NS_SUCCEEDED(aChannel->GetLoadInfo(getter_AddRefs(loadInfo))));
+
+  uint32_t securityMode;
+  MOZ_ALWAYS_TRUE(NS_SUCCEEDED(loadInfo->GetSecurityMode(&securityMode)));
+
+  // TODO: Remove following code after stylesheet and image support cookie policy
+  if (securityMode == nsILoadInfo::SEC_NORMAL) {
+    uint32_t loadFlags;
+    aChannel->GetLoadFlags(&loadFlags);
+
+    if (loadFlags & nsIRequest::LOAD_ANONYMOUS) {
+      return RequestCredentials::Omit;
+    } else {
+      bool includeCrossOrigin;
+      nsCOMPtr<nsIHttpChannelInternal> internalChannel = do_QueryInterface(aChannel);
+
+      internalChannel->GetCorsIncludeCredentials(&includeCrossOrigin);
+      if (includeCrossOrigin) {
+        return RequestCredentials::Include;
+      }
+    }
+    return RequestCredentials::Same_origin;
+  }
+
+  uint32_t cookiePolicy = loadInfo->GetCookiePolicy();
+
+  if (cookiePolicy == nsILoadInfo::SEC_COOKIES_INCLUDE) {
+    return RequestCredentials::Include;
+  } else if (cookiePolicy == nsILoadInfo::SEC_COOKIES_OMIT) {
+    return RequestCredentials::Omit;
+  } else if (cookiePolicy == nsILoadInfo::SEC_COOKIES_SAME_ORIGIN) {
+    return RequestCredentials::Same_origin;
+  }
+
+  MOZ_ASSERT_UNREACHABLE("Unexpected cookie policy!");
+  return RequestCredentials::Same_origin;
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/fetch/InternalRequest.h
+++ b/dom/fetch/InternalRequest.h
@@ -401,16 +401,19 @@ public:
   IsWorkerRequest() const;
 
   bool
   IsClientRequest() const;
 
   static RequestMode
   MapChannelToRequestMode(nsIChannel* aChannel);
 
+  static RequestCredentials
+  MapChannelToRequestCredentials(nsIChannel* aChannel);
+
 private:
   // Does not copy mBodyStream.  Use fallible Clone() for complete copy.
   explicit InternalRequest(const InternalRequest& aOther);
 
   ~InternalRequest();
 
   static RequestContext
   MapContentPolicyTypeToRequestContext(nsContentPolicyType aContentPolicyType);
--- a/dom/workers/ServiceWorkerPrivate.cpp
+++ b/dom/workers/ServiceWorkerPrivate.cpp
@@ -1089,25 +1089,17 @@ public:
 
       mRequestMode = InternalRequest::MapChannelToRequestMode(channel);
 
       // This is safe due to static_asserts at top of file.
       uint32_t redirectMode;
       internalChannel->GetRedirectMode(&redirectMode);
       mRequestRedirect = static_cast<RequestRedirect>(redirectMode);
 
-      if (loadFlags & nsIRequest::LOAD_ANONYMOUS) {
-        mRequestCredentials = RequestCredentials::Omit;
-      } else {
-        bool includeCrossOrigin;
-        internalChannel->GetCorsIncludeCredentials(&includeCrossOrigin);
-        if (includeCrossOrigin) {
-          mRequestCredentials = RequestCredentials::Include;
-        }
-      }
+      mRequestCredentials = InternalRequest::MapChannelToRequestCredentials(channel);
 
       rv = httpChannel->VisitNonDefaultRequestHeaders(this);
       NS_ENSURE_SUCCESS(rv, rv);
 
       nsCOMPtr<nsIUploadChannel2> uploadChannel = do_QueryInterface(httpChannel);
       if (uploadChannel) {
         MOZ_ASSERT(!mUploadStream);
         bool bodyHasHeaders = false;
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/mozilla/meta/service-workers/service-worker/fetch-request-css-images.https.html.ini
@@ -0,0 +1,5 @@
+[fetch-request-css-images.https.html]
+  type: testharness
+  [Verify FetchEvent for css images.]
+    expected: FAIL
+
deleted file mode 100644
--- a/testing/web-platform/mozilla/meta/service-workers/service-worker/fetch-request-resources.https.html.ini
+++ /dev/null
@@ -1,5 +0,0 @@
-[fetch-request-resources.https.html]
-  type: testharness
-  [Verify FetchEvent for resources.]
-    expected: FAIL
-
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/fetch-request-css-images.https.html
@@ -0,0 +1,99 @@
+<!DOCTYPE html>
+<title>Service Worker: FetchEvent for css image</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?pipe=sub"></script>
+<script>
+var url_count = 0;
+var expected_results = {};
+
+function css_image_test(frame, url, type, expexted_mode,
+                        expected_credentials) {
+  var actual_url = url + (++url_count);
+  expected_results[actual_url] = {
+      url: actual_url,
+      mode: expexted_mode,
+      credentials: expected_credentials,
+      message: 'CSSImage load (url:' + actual_url + ' type:' + type + ')'
+    };
+  return frame.contentWindow.load_css_image(actual_url, type);
+}
+
+function css_image_set_test(frame, url, type, expexted_mode,
+                            expected_credentials) {
+  var actual_url = url + (++url_count);
+  expected_results[actual_url] = {
+      url: actual_url,
+      mode: expexted_mode,
+      credentials: expected_credentials,
+      message: 'CSSImageSet load (url:' + actual_url + ' type:' + type + ')'
+    };
+  return frame.contentWindow.load_css_image_set(actual_url, type);
+}
+
+async_test(function(t) {
+    var SCOPE = 'resources/fetch-request-resources-iframe.https.html';
+    var SCRIPT = 'resources/fetch-request-resources-worker.js';
+    var host_info = get_host_info();
+    var LOCAL_URL =
+      host_info['HTTPS_ORIGIN'] + base_path() + 'resources/dummy?test';
+    var REMOTE_URL =
+      host_info['HTTPS_REMOTE_ORIGIN'] + base_path() + 'resources/dummy?test';
+    var worker;
+    var frame;
+    service_worker_unregister_and_register(t, SCRIPT, SCOPE)
+      .then(function(registration) {
+          worker = registration.installing;
+          return wait_for_state(t, worker, 'activated');
+        })
+      .then(function() {
+          return new Promise(function(resolve) {
+              var channel = new MessageChannel();
+              channel.port1.onmessage = t.step_func(function(msg) {
+                if (msg.data.ready) {
+                  resolve();
+                  return;
+                }
+                var result = msg.data;
+                var expected = expected_results[result.url];
+                if (!expected) {
+                  return;
+                }
+                assert_equals(
+                    result.mode, expected.mode,
+                    'mode of ' + expected.message +  ' must be ' +
+                    expected.mode + '.');
+                assert_equals(
+                    result.credentials, expected.credentials,
+                    'credentials of ' + expected.message +  ' must be ' +
+                    expected.credentials + '.');
+                --url_count;
+                delete expected_results[result.url];
+                if (url_count == 0) {
+                  frame.remove();
+                  service_worker_unregister_and_done(t, SCOPE);
+                }
+              });
+              worker.postMessage(
+                {port: channel.port2}, [channel.port2]);
+            });
+        })
+      .then(function() { return with_iframe(SCOPE); })
+      .then(function(f) {
+        frame = f;
+
+        css_image_test(f, LOCAL_URL, 'backgroundImage', 'no-cors', 'include');
+        css_image_test(f, REMOTE_URL, 'backgroundImage', 'no-cors', 'include');
+
+        css_image_test(f, LOCAL_URL, 'shapeOutside', 'cors', 'same-origin');
+        css_image_test(f, REMOTE_URL, 'shapeOutside', 'cors', 'same-origin');
+
+        css_image_set_test(f, LOCAL_URL, 'backgroundImage', 'no-cors', 'include');
+        css_image_set_test(f, REMOTE_URL, 'backgroundImage', 'no-cors', 'include');
+        css_image_set_test(f, LOCAL_URL, 'shapeOutside', 'cors', 'same-origin');
+        css_image_set_test(f, REMOTE_URL, 'shapeOutside', 'cors', 'same-origin');
+      })
+      .catch(unreached_rejection(t));
+  }, 'Verify FetchEvent for resources.');
+</script>
--- a/testing/web-platform/mozilla/tests/service-workers/service-worker/fetch-request-resources.https.html
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/fetch-request-resources.https.html
@@ -53,40 +53,16 @@ function font_face_test(frame, url, expe
       url: actual_url,
       mode: expexted_mode,
       credentials: expected_credentials,
       message: 'FontFace load (url:' + actual_url + ')'
     };
   return frame.contentWindow.load_font(actual_url);
 }
 
-function css_image_test(frame, url, type, expexted_mode,
-                        expected_credentials) {
-  var actual_url = url + (++url_count);
-  expected_results[actual_url] = {
-      url: actual_url,
-      mode: expexted_mode,
-      credentials: expected_credentials,
-      message: 'CSSImage load (url:' + actual_url + ' type:' + type + ')'
-    };
-  return frame.contentWindow.load_css_image(actual_url, type);
-}
-
-function css_image_set_test(frame, url, type, expexted_mode,
-                            expected_credentials) {
-  var actual_url = url + (++url_count);
-  expected_results[actual_url] = {
-      url: actual_url,
-      mode: expexted_mode,
-      credentials: expected_credentials,
-      message: 'CSSImageSet load (url:' + actual_url + ' type:' + type + ')'
-    };
-  return frame.contentWindow.load_css_image_set(actual_url, type);
-}
-
 async_test(function(t) {
     var SCOPE = 'resources/fetch-request-resources-iframe.https.html';
     var SCRIPT = 'resources/fetch-request-resources-worker.js';
     var host_info = get_host_info();
     var LOCAL_URL =
       host_info['HTTPS_ORIGIN'] + base_path() + 'resources/dummy?test';
     var REMOTE_URL =
       host_info['HTTPS_REMOTE_ORIGIN'] + base_path() + 'resources/dummy?test';
@@ -127,46 +103,40 @@ async_test(function(t) {
               });
               worker.postMessage(
                 {port: channel.port2}, [channel.port2]);
             });
         })
       .then(function() { return with_iframe(SCOPE); })
       .then(function(f) {
         frame = f;
-        image_test(f, LOCAL_URL, '', 'no-cors', 'same-origin');
-        image_test(f, LOCAL_URL, 'anonymous', 'cors', 'omit');
+
+        // TODO: Disable 'no-cors' tests for image and stylesheet until
+        //       AsyncOpen2 and cookie policy is supported.
+        // image_test(f, LOCAL_URL, '', 'no-cors', 'include');
+        // image_test(f, REMOTE_URL, '', 'no-cors', 'include');
+        // css_test(f, LOCAL_URL, '', 'no-cors', 'include');
+        // css_test(f, REMOTE_URL, '', 'no-cors', 'include');
+
+        image_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
         image_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
-        image_test(f, REMOTE_URL, '', 'no-cors', 'same-origin');
         image_test(f, REMOTE_URL, 'anonymous', 'cors', 'omit');
         image_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');
 
-        script_test(f, LOCAL_URL, '', 'no-cors', 'same-origin');
-        script_test(f, LOCAL_URL, 'anonymous', 'cors', 'omit');
+        script_test(f, LOCAL_URL, '', 'no-cors', 'include');
+        script_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
         script_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
-        script_test(f, REMOTE_URL, '', 'no-cors', 'same-origin');
-        script_test(f, REMOTE_URL, 'anonymous', 'cors', 'omit');
+        script_test(f, REMOTE_URL, '', 'no-cors', 'include');
+        script_test(f, REMOTE_URL, 'anonymous', 'cors', 'same-origin');
         script_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');
 
-        css_test(f, LOCAL_URL, '', 'no-cors', 'same-origin');
-        css_test(f, LOCAL_URL, 'anonymous', 'cors', 'omit');
+        css_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
         css_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
-        css_test(f, REMOTE_URL, '', 'no-cors', 'same-origin');
         css_test(f, REMOTE_URL, 'anonymous', 'cors', 'omit');
         css_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');
 
-        font_face_test(f, LOCAL_URL, 'no-cors', 'same-origin');
+        font_face_test(f, LOCAL_URL, 'cors', 'same-origin');
         font_face_test(f, REMOTE_URL, 'cors', 'omit');
 
-        css_image_test(f, LOCAL_URL, 'backgroundImage', 'no-cors', 'same-origin');
-        css_image_test(f, REMOTE_URL, 'backgroundImage', 'no-cors', 'same-origin');
-        css_image_test(f, LOCAL_URL, 'shapeOutside', 'cors', 'omit');
-        css_image_test(f, REMOTE_URL, 'shapeOutside', 'cors', 'omit');
-
-        css_image_set_test(f, LOCAL_URL, 'backgroundImage', 'no-cors', 'same-origin');
-        css_image_set_test(f, REMOTE_URL, 'backgroundImage', 'no-cors', 'same-origin');
-        css_image_set_test(f, LOCAL_URL, 'shapeOutside', 'cors', 'omit');
-        css_image_set_test(f, REMOTE_URL, 'shapeOutside', 'cors', 'omit');
-
       })
       .catch(unreached_rejection(t));
   }, 'Verify FetchEvent for resources.');
 </script>
--- a/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/fetch-request-resources-iframe.https.html
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/fetch-request-resources-iframe.https.html
@@ -26,17 +26,17 @@ function load_css(url, cross_origin) {
   link.type = 'text/css';
   if (cross_origin != '') {
     link.crossOrigin = cross_origin;
   }
   document.body.appendChild(link);
 }
 
 function load_font(url) {
-  var fontFace = new FontFace(url, 'url(' + url + ')');
+  var fontFace = new FontFace('test', 'url(' + url + ')');
   fontFace.load();
 }
 
 function load_css_image(url, type) {
   var div = document.createElement('div');
   document.body.appendChild(div);
   div.style[type] = 'url(' + url + ')';
 }
--- a/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/fetch-request-xhr-iframe.https.html
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/resources/fetch-request-xhr-iframe.https.html
@@ -132,17 +132,17 @@ function mode_credentials_test() {
       })
     .then(function(response){
         assert_equals(response.mode, 'cors');
         assert_equals(response.credentials, 'include');
         return xhr_send(host_info['HTTPS_REMOTE_ORIGIN'], 'GET', '', false);
       })
     .then(function(response){
         assert_equals(response.mode, 'cors');
-        assert_equals(response.credentials, 'omit');
+        assert_equals(response.credentials, 'same-origin');
         return xhr_send(host_info['HTTPS_REMOTE_ORIGIN'], 'GET', '', true);
       })
     .then(function(response){
         assert_equals(response.mode, 'cors');
         assert_equals(response.credentials, 'include');
       });
 }