4 https://bugzilla.mozilla.org/show_bug.cgi?id=375314
7 <title>Test for Bug
375314</title>
8 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
12 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=375314">Mozilla Bug
375314</a>
14 <div id=
"content" style=
"display: none">
18 <script class=
"testbody" type=
"text/javascript">
20 /** Test for Bug
375314 **/
22 var lastContentType = -
1;
23 const testURL = window.location.href +
"/this/is/the/test/url";
24 const Cc = SpecialPowers.Cc;
25 const Ci = SpecialPowers.Ci;
26 var {AppConstants} = SpecialPowers.Cu.import(
"resource://gre/modules/AppConstants.jsm", {});
28 // Content policy / factory implementation for the test
29 var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID(
"{b80e19d0-878f-d41b-2654-194714a4115c}");
30 var policyName =
"@mozilla.org/testpolicy;1";
32 // nsISupports implementation
35 iid = SpecialPowers.wrap(iid);
36 if (iid.equals(Ci.nsISupports) ||
37 iid.equals(Ci.nsIFactory) ||
38 iid.equals(Ci.nsIContentPolicy))
41 throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
44 // nsIFactory implementation
45 createInstance(outer, iid) {
46 return this.QueryInterface(iid);
49 // nsIContentPolicy implementation
50 shouldLoad(contentLocation, loadInfo, mimeTypeGuess) {
51 let contentType = loadInfo.externalContentPolicyType;
52 // Remember last content type seen for the test url
53 if (SpecialPowers.wrap(contentLocation).spec == testURL) {
54 lastContentType = contentType;
55 return Ci.nsIContentPolicy.REJECT_REQUEST;
58 return Ci.nsIContentPolicy.ACCEPT;
61 shouldProcess(contentLocation, loadInfo, mimeTypeGuess) {
62 return Ci.nsIContentPolicy.ACCEPT;
65 policy = SpecialPowers.wrapCallbackObject(policy);
67 // Register content policy
68 var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager
69 .QueryInterface(Ci.nsIComponentRegistrar);
71 componentManager.registerFactory(policyID,
"Test content policy", policyName, policy);
73 var categoryManager = Cc[
"@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
74 categoryManager.addCategoryEntry(
"content-policy", policyName, policyName, false, true);
76 // Try creating different request types
77 var tests = [
"SCRIPT",
"IMAGE",
"STYLESHEET",
"XMLHTTPREQUEST"];
81 SimpleTest.waitForExplicitFinish();
82 setTimeout(runNextTest,
0);
84 function runNextTest() {
87 var type =
"TYPE_" + tests[curTest];
88 is(lastContentType, Ci.nsIContentPolicy[type],
"Content policies triggered for " + type);
92 if (curTest < tests.length) {
93 var method =
"request_" + tests[curTest].toLowerCase();
97 setTimeout(runNextTest,
0);
100 // Unregister content policy
101 categoryManager.deleteCategoryEntry(
"content-policy", policyName, false);
103 setTimeout(function() {
104 // Component must be unregistered delayed, otherwise other content
105 // policy will not be removed from the category correctly
106 componentManager.unregisterFactory(policyID, policy);
113 // Request creating functions
115 function request_script() {
116 var content = $(
"content");
118 var script = document.createElement(
"script");
119 script.setAttribute(
"type",
"text/javascript")
120 script.setAttribute(
"src", testURL)
121 content.appendChild(script);
124 function request_image() {
125 var content = $(
"content");
127 var image = new Image();
131 function request_stylesheet() {
132 var content = $(
"content");
134 var stylesheet = document.createElement(
"link");
135 stylesheet.setAttribute(
"rel",
"stylesheet");
136 stylesheet.setAttribute(
"type",
"text/css");
137 stylesheet.setAttribute(
"href", testURL);
138 content.appendChild(stylesheet);
141 function request_object() {
142 var content = $(
"content");
144 var object = document.createElement(
"embed");
145 object.setAttribute(
"src", testURL);
146 content.appendChild(object);
149 function request_xmlhttprequest() {
150 var request = new XMLHttpRequest();
151 request.open(
"GET", testURL, false);