summaryrefslogtreecommitdiffstats
path: root/dom/base/test/file_simplecontentpolicy.js
blob: 1f9606c49d57850d7c315789c26b5a7d1a0957da (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
var Ci = Components.interfaces;
var Cc = Components.classes;
var Cr = Components.results;

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function executeSoon(f)
{
  Services.tm.mainThread.dispatch(f, Ci.nsIThread.DISPATCH_NORMAL);
}

var urlSuffix = "/this/is/the/test/url";

// Content policy / factory implementation for the test
var policyID = Components.ID("{6aadacff-f1f2-46f4-a6db-6d429f884a30}");
var policyName = "@mozilla.org/simpletestpolicy;1";
var policy = {
  // nsISupports implementation
  QueryInterface:
    XPCOMUtils.generateQI([
      Ci.nsISupports,
      Ci.nsIFactory,
      Ci.nsISimpleContentPolicy]),

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

  // nsIContentPolicy implementation
  shouldLoad: function(contentType,
                       contentLocation,
                       requestOrigin,
                       frame,
                       isTopLevel,
                       mimeTypeGuess,
                       extra)
  {
    // Remember last content type seen for the test url
    if (contentLocation.spec.endsWith(urlSuffix)) {
      assert.ok(frame === browserElement, "correct <browser> element");
      sendAsyncMessage("shouldLoad", {contentType: contentType, isTopLevel: isTopLevel});
      return Ci.nsIContentPolicy.REJECT_REQUEST;
    }

    return Ci.nsIContentPolicy.ACCEPT;
  },

  shouldProcess: function() {
    return Ci.nsIContentPolicy.ACCEPT;
  }
}

// Register content policy
var componentManager = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
componentManager.registerFactory(policyID, "Test simple content policy", policyName, policy);

var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
categoryManager.addCategoryEntry("simple-content-policy", policyName, policyName, false, true);

addMessageListener("finished", () => {
  // Unregister content policy
  categoryManager.deleteCategoryEntry("simple-content-policy", policyName, false);

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

sendAsyncMessage("ready");