Bug 1259164 - Set ServiceWorkerMessageEvent.origin correctly when calling ServiceWorkerClient.postMessage(); r=bkelly
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 28 Mar 2016 10:27:20 -0400
changeset 290754 110003172c5b120c6d6a0aacb63c96661666efa5
parent 290753 6378e50f68ff730702eaf321b4a02f0e437c6161
child 290755 9a135bc3752971f8a76c1cb6fa4ffefbb613d93c
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1259164
milestone48.0a1
Bug 1259164 - Set ServiceWorkerMessageEvent.origin correctly when calling ServiceWorkerClient.postMessage(); r=bkelly
dom/workers/ServiceWorkerClient.cpp
dom/workers/test/serviceworkers/test_match_all_client_id.html
testing/web-platform/mozilla/tests/service-workers/service-worker/postmessage-to-client.https.html
--- a/dom/workers/ServiceWorkerClient.cpp
+++ b/dom/workers/ServiceWorkerClient.cpp
@@ -121,28 +121,47 @@ public:
   }
 
 private:
   NS_IMETHOD
   DispatchDOMEvent(JSContext* aCx, ServiceWorkerContainer* aTargetContainer)
   {
     AssertIsOnMainThread();
 
+    MOZ_ASSERT(aTargetContainer->GetParentObject(),
+               "How come we don't have a window here?!");
+
     JS::Rooted<JS::Value> messageData(aCx);
     ErrorResult rv;
     Read(aTargetContainer->GetParentObject(), aCx, &messageData, rv);
     if (NS_WARN_IF(rv.Failed())) {
       xpc::Throw(aCx, rv.StealNSResult());
       return NS_ERROR_FAILURE;
     }
 
     RootedDictionary<ServiceWorkerMessageEventInit> init(aCx);
 
+    nsCOMPtr<nsIPrincipal> principal = aTargetContainer->GetParentObject()->PrincipalOrNull();
+    NS_WARN_IF_FALSE(principal, "Why is the principal null here?");
+
+    bool isNullPrincipal = false;
+    bool isSystemPrincipal = false;
+    if (principal) {
+      principal->GetIsNullPrincipal(&isNullPrincipal);
+      MOZ_ASSERT(!isNullPrincipal);
+      principal->GetIsSystemPrincipal(&isSystemPrincipal);
+      MOZ_ASSERT(!isSystemPrincipal);
+    }
+
     init.mData = messageData;
-    init.mOrigin.Construct(EmptyString());
+    nsAutoCString origin;
+    if (principal && !isNullPrincipal && !isSystemPrincipal) {
+      principal->GetOrigin(origin);
+    }
+    init.mOrigin.Construct(NS_ConvertUTF8toUTF16(origin));
     init.mLastEventId.Construct(EmptyString());
     init.mPorts.Construct();
     init.mPorts.Value().SetNull();
 
     RefPtr<ServiceWorker> serviceWorker = aTargetContainer->GetController();
     init.mSource.Construct();
     if (serviceWorker) {
       init.mSource.Value().SetValue().SetAsServiceWorker() = serviceWorker;
--- a/dom/workers/test/serviceworkers/test_match_all_client_id.html
+++ b/dom/workers/test/serviceworkers/test_match_all_client_id.html
@@ -29,16 +29,17 @@
       dump("Unregistering the SW failed with " + e + "\n");
     });
   }
 
   function getMessageListener() {
     return new Promise(function(res, rej) {
       window.onmessage = function(e) {
         ok(e.data, "Same client id for multiple calls.");
+        is(e.origin, "http://mochi.test:8888", "Event should have the correct origin");
 
         if (!e.data) {
           rej();
           return;
         }
 
         info("DONE from: " + e.source);
         res();
--- a/testing/web-platform/mozilla/tests/service-workers/service-worker/postmessage-to-client.https.html
+++ b/testing/web-platform/mozilla/tests/service-workers/service-worker/postmessage-to-client.https.html
@@ -1,18 +1,20 @@
 <!DOCTYPE html>
 <title>Service Worker: postMessage to Client</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 frame;
 var t = async_test('postMessage from ServiceWorker to Client');
 t.step(function() {
     var scope = 'resources/blank.html';
+    var host_info = get_host_info();
     var sw;
     service_worker_unregister_and_register(
         t, 'resources/postmessage-to-client-worker.js', scope)
       .then(function(registration) {
           return wait_for_state(t, registration.installing, 'activated');
         })
       .then(function() { return with_iframe(scope); })
       .then(function(f) {
@@ -24,16 +26,17 @@ t.step(function() {
       .catch(unreached_rejection(t));
 
     var result = [];
     var expected = ['Sending message via clients'];
 
     function onMessage(e) {
       assert_equals(e.bubbles, false, 'message events should not bubble.');
       assert_equals(e.cancelable, false, 'message events should not be cancelable.');
+      assert_equals(e.origin, host_info['HTTPS_ORIGIN'], 'message event\'s origin should be set correctly.');
 // XXXkhuey fixme!
 //      assert_equals(e.source, sw.controller, 'source should be ServiceWorker.');
 
       var message = e.data;
       if (message === 'quit') {
         assert_array_equals(result, expected,
                             'Worker should post back expected messages.');
         frame.remove();