Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <!-- |
michael@0 | 4 | https://bugzilla.mozilla.org/show_bug.cgi?id=936340 |
michael@0 | 5 | --> |
michael@0 | 6 | <head> |
michael@0 | 7 | <title>Test that sendBeacon obeys content policy directives</title> |
michael@0 | 8 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 9 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 10 | </head> |
michael@0 | 11 | <body> |
michael@0 | 12 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=936340">Mozilla Bug 936340</a> |
michael@0 | 13 | <p id="display"></p> |
michael@0 | 14 | <div id="content" style="display: none"> |
michael@0 | 15 | |
michael@0 | 16 | </div> |
michael@0 | 17 | <pre id="test"> |
michael@0 | 18 | <script class="testbody" type="text/javascript"> |
michael@0 | 19 | |
michael@0 | 20 | var beaconUrl = "http://mochi.test:8888/tests/dom/tests/mochitest/beacon/beacon-handler.sjs"; |
michael@0 | 21 | |
michael@0 | 22 | const Cc = SpecialPowers.Cc; |
michael@0 | 23 | const Ci = SpecialPowers.Ci; |
michael@0 | 24 | |
michael@0 | 25 | // not enabled by default yet. |
michael@0 | 26 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 27 | |
michael@0 | 28 | var policy; |
michael@0 | 29 | |
michael@0 | 30 | SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, beginTest); |
michael@0 | 31 | |
michael@0 | 32 | function setupPolicy() { |
michael@0 | 33 | var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID("{b80e19d0-878f-d41b-2654-194714a4115c}"); |
michael@0 | 34 | var policyName = "@mozilla.org/testpolicy;1"; |
michael@0 | 35 | var policy = { |
michael@0 | 36 | // nsISupports implementation |
michael@0 | 37 | QueryInterface: function(iid) { |
michael@0 | 38 | iid = SpecialPowers.wrap(iid); |
michael@0 | 39 | if (iid.equals(Ci.nsISupports) || |
michael@0 | 40 | iid.equals(Ci.nsIFactory) || |
michael@0 | 41 | iid.equals(Ci.nsIContentPolicy)) |
michael@0 | 42 | return this; |
michael@0 | 43 | throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE; |
michael@0 | 44 | }, |
michael@0 | 45 | |
michael@0 | 46 | // nsIFactory implementation |
michael@0 | 47 | createInstance: function(outer, iid) { |
michael@0 | 48 | return this.QueryInterface(iid); |
michael@0 | 49 | }, |
michael@0 | 50 | |
michael@0 | 51 | // nsIContentPolicy implementation |
michael@0 | 52 | shouldLoad: function(contentType, contentLocation, requestOrigin, context, mimeTypeGuess, extra) { |
michael@0 | 53 | // Remember last content type seen for the test url |
michael@0 | 54 | |
michael@0 | 55 | if (SpecialPowers.wrap(contentLocation).spec == beaconUrl) { |
michael@0 | 56 | is(contentType, Ci.nsIContentPolicy.TYPE_BEACON, "Beacon content type should match expected. is: " + contentType + " should be: " + Ci.nsIContentPolicy.TYPE_BEACON); |
michael@0 | 57 | teardownPolicy(); |
michael@0 | 58 | SimpleTest.finish(); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | return Ci.nsIContentPolicy.ACCEPT; |
michael@0 | 62 | }, |
michael@0 | 63 | |
michael@0 | 64 | shouldProcess: function(contentType, contentLocation, requestOrigin, context, mimeTypeGuess, extra) { |
michael@0 | 65 | return Ci.nsIContentPolicy.ACCEPT; |
michael@0 | 66 | } |
michael@0 | 67 | } |
michael@0 | 68 | policy = SpecialPowers.wrapCallbackObject(policy); |
michael@0 | 69 | |
michael@0 | 70 | // Register content policy |
michael@0 | 71 | var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager.QueryInterface(Ci.nsIComponentRegistrar); |
michael@0 | 72 | componentManager.registerFactory(policyID, "Test content policy", policyName, policy); |
michael@0 | 73 | |
michael@0 | 74 | var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); |
michael@0 | 75 | categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true); |
michael@0 | 76 | |
michael@0 | 77 | return { 'policy': policy, 'policyID': policyID, 'policyName': policyName }; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | function teardownPolicy() { |
michael@0 | 81 | setTimeout(function() { |
michael@0 | 82 | // policy will not be removed from the category correctly |
michael@0 | 83 | var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager.QueryInterface(Ci.nsIComponentRegistrar); |
michael@0 | 84 | componentManager.unregisterFactory(policy.policyID, policy.policy); |
michael@0 | 85 | var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); |
michael@0 | 86 | categoryManager.deleteCategoryEntry("content-policy", policy.policyName, false); |
michael@0 | 87 | }, 0); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | function beginTest() { |
michael@0 | 91 | policy = setupPolicy(); |
michael@0 | 92 | // Make sure to hit the event loop here in order to ensure that nsContentPolicy |
michael@0 | 93 | // has been notified of the newly registered policy. |
michael@0 | 94 | SimpleTest.executeSoon(function() { |
michael@0 | 95 | navigator.sendBeacon(beaconUrl, "bacon would have been a better name than beacon"); |
michael@0 | 96 | }); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | </script> |
michael@0 | 100 | </pre> |
michael@0 | 101 | </body> |
michael@0 | 102 | </html> |