test_bug375314-2.html (4191B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=375314 5 --> 6 <head> 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" /> 10 </head> 11 <body> 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=375314">Mozilla Bug 375314</a> 13 <p id="display"></p> 14 <div id="content" style="display: none"> 15 16 </div> 17 <pre id="test"> 18 <script class="testbody" type="text/javascript"> 19 20 /** Test for Bug 375314 */ 21 22 var lastContentType = -1; 23 const testURL = window.location.href + "/this/is/the/test/url"; 24 25 function createChromeScript() { 26 /* eslint-env mozilla/chrome-script */ 27 var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService( 28 Ci.nsICategoryManager 29 ); 30 31 const POLICYNAME = "@mozilla.org/testpolicy;1"; 32 const POLICYID = Components.ID("{6cc95ef3-40e1-4d59-87f0-86f100373227}"); 33 34 var policy = { 35 // nsISupports implementation 36 QueryInterface: ChromeUtils.generateQI([ 37 "nsIFactory", 38 "nsIContentPolicy", 39 ]), 40 41 // nsIFactory implementation 42 createInstance(iid) { 43 return this.QueryInterface(iid); 44 }, 45 46 // nsIContentPolicy implementation 47 shouldLoad(contentLocation, loadInfo) { 48 if (contentLocation.asciiSpec === "http://mochi.test:8888/tests/dom/base/test/test_bug375314-2.html/this/is/the/test/url") { 49 sendAsyncMessage("loadBlocked", { policyType: loadInfo.externalContentPolicyType}); 50 return Ci.nsIContentPolicy.REJECT_REQUEST; 51 } 52 return Ci.nsIContentPolicy.ACCEPT; 53 }, 54 55 shouldProcess(contentLocation, loadInfo) { 56 return Ci.nsIContentPolicy.ACCEPT; 57 } 58 }; 59 60 // Register content policy 61 var componentManager = Components.manager.QueryInterface( 62 Ci.nsIComponentRegistrar 63 ); 64 65 componentManager.registerFactory( 66 POLICYID, 67 "Test content policy", 68 POLICYNAME, 69 policy 70 ); 71 categoryManager.addCategoryEntry( 72 "content-policy", 73 POLICYNAME, 74 POLICYNAME, 75 false, 76 true 77 ); 78 79 addMessageListener("shutdown", _ => { 80 categoryManager.deleteCategoryEntry( 81 "content-policy", 82 POLICYNAME, 83 false 84 ); 85 componentManager.unregisterFactory(POLICYID, policy); 86 }); 87 88 // Adding a new category dispatches an event to update 89 // caches, so we need to also dispatch an event to make 90 // sure we don't start the load until after that happens. 91 Services.tm.dispatchToMainThread(() => { 92 sendAsyncMessage("setupComplete"); 93 }); 94 } 95 96 // Request creating functions 97 98 function requestDocument() { 99 // GeckoView shows an error page for CSP errors, which breaks this test, so just skip in that case. 100 try { 101 if (!SpecialPowers.Cc["@mozilla.org/android/bridge;1"].getService(SpecialPowers.Ci.nsIGeckoViewBridge).isFennec) { 102 return false; 103 } 104 } catch (e){} 105 106 top.location.href = testURL; 107 return true; 108 } 109 110 function requestSubdocument() { 111 var content = $("content"); 112 113 var frame = document.createElement("iframe"); 114 frame.setAttribute("src", testURL); 115 content.appendChild(frame); 116 } 117 118 function requestObject() { 119 var content = $("content"); 120 121 var object = document.createElement("embed"); 122 object.setAttribute("src", testURL); 123 content.appendChild(object); 124 } 125 126 add_task(async function() { 127 let chromeScript = SpecialPowers.loadChromeScript(createChromeScript); 128 await chromeScript.promiseOneMessage("setupComplete"); 129 130 if (requestDocument()) { 131 let result = await chromeScript.promiseOneMessage("loadBlocked"); 132 is(result.policyType, SpecialPowers.Ci.nsIContentPolicy.TYPE_DOCUMENT, "Content policies triggered for TYPE_DOCUMENT"); 133 } 134 135 requestSubdocument(); 136 result = await chromeScript.promiseOneMessage("loadBlocked"); 137 is(result.policyType, SpecialPowers.Ci.nsIContentPolicy.TYPE_SUBDOCUMENT, "Content policies triggered for TYPE_SUBDOCUMENT"); 138 139 requestObject(); 140 result = await chromeScript.promiseOneMessage("loadBlocked"); 141 is(result.policyType, SpecialPowers.Ci.nsIContentPolicy.TYPE_OBJECT, "Content policies triggered for TYPE_OBJECT"); 142 143 chromeScript.sendAsyncMessage("shutdown"); 144 chromeScript.destroy(); 145 }); 146 147 </script> 148 </pre> 149 </body> 150 </html>