summaryrefslogtreecommitdiffstats
path: root/docshell/test/navigation/test_contentpolicy_block_window.html
blob: 7ce337c131568ddbc6d33c52b055eae6a79e7180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1329288
-->
<head>
  <title>Test for Bug 1329288</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>        
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1329288">Mozilla Bug 1329288</a>


<!-- have a testlink which we can use for the test to open a new window -->
<a href="http://test1.example.org/tests/docshell/test/navigation/file_contentpolicy_block_window.html"
   target="_blank"
   id="testlink">This is a link</a>

<script class="testbody" type="text/javascript">
/*
 * Description of the test:
 * The test tries to open a new window and makes sure that a registered contentPolicy
 * gets called with the right (a non null) 'context' for the TYPE_DOCUMENT load.
 */

const Ci = SpecialPowers.Ci;

var categoryManager = SpecialPowers.Services.catMan;
var componentManager = SpecialPowers.Components.manager
                       .QueryInterface(Ci.nsIComponentRegistrar);

// Content policy / factory implementation for the test
var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID("{b80e19d0-878f-d41b-2654-194714a4115c}");
var policyName = "@mozilla.org/testpolicy;1";
var policy = {
  // nsISupports implementation
  // eslint-disable-next-line mozilla/use-chromeutils-generateqi
  QueryInterface(iid) {
    iid = SpecialPowers.wrap(iid);
    if (iid.equals(Ci.nsISupports) ||
        iid.equals(Ci.nsIFactory) ||
        iid.equals(Ci.nsIContentPolicy))
      return this;
    throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
  },

  // nsIFactory implementation
  createInstance(iid) {
    return this.QueryInterface(iid);
  },

  // nsIContentPolicy implementation
  shouldLoad(contentLocation, loadInfo, mimeTypeGuess) {
    let contentType = loadInfo.externalContentPolicyType;
    let context = loadInfo.loadingContext;

    if (SpecialPowers.wrap(contentLocation).spec !== document.getElementById("testlink").href) {
      // not the URI we are looking for, allow the load
      return Ci.nsIContentPolicy.ACCEPT;
    }

    is(contentType, Ci.nsIContentPolicy.TYPE_DOCUMENT,
       "needs to be type document load");
    ok(context, "context is not allowed to be null");
    ok(context.name.endsWith("test_contentpolicy_block_window.html"),
       "context should be the current window");

    // remove the policy and finish test.
    categoryManager.deleteCategoryEntry("content-policy", policyName, false);

    setTimeout(function() {
      // Component must be unregistered delayed, otherwise other content
      // policy will not be removed from the category correctly
      componentManager.unregisterFactory(policyID, policy);
    }, 0);

    SimpleTest.finish();
    return Ci.nsIContentPolicy.REJECT_REQUEST;
  },

  shouldProcess(contentLocation, loadInfo, mimeTypeGuess) {
    return Ci.nsIContentPolicy.ACCEPT;
  },
};

policy = SpecialPowers.wrapCallbackObject(policy);
componentManager.registerFactory(policyID, "Test content policy", policyName, policy);
categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true);

SimpleTest.waitForExplicitFinish();

// now everything is set up, let's start the test
document.getElementById("testlink").click();

</script>
</body>
</html>