summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/plugins/browser_clearplugindata.js
blob: 69d474fed3375dbd9faa56297c927823c9a51283 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var gTestRoot = getRootDirectory(gTestPath).replace("chrome://mochitests/content/", "http://127.0.0.1:8888/");
var gPluginHost = Components.classes["@mozilla.org/plugin/host;1"].getService(Components.interfaces.nsIPluginHost);
var gTestBrowser = null;

// Test clearing plugin data using sanitize.js.
const testURL1 = gTestRoot + "browser_clearplugindata.html";
const testURL2 = gTestRoot + "browser_clearplugindata_noage.html";

var tempScope = {};
Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader)
                                           .loadSubScript("chrome://browser/content/sanitize.js", tempScope);
var Sanitizer = tempScope.Sanitizer;

const pluginHostIface = Ci.nsIPluginHost;
var pluginHost = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
pluginHost.QueryInterface(pluginHostIface);

var pluginTag = getTestPlugin();
var sanitizer = null;

function stored(needles) {
  let something = pluginHost.siteHasData(this.pluginTag, null);
  if (!needles)
    return something;

  if (!something)
    return false;

  for (let i = 0; i < needles.length; ++i) {
    if (!pluginHost.siteHasData(this.pluginTag, needles[i]))
      return false;
  }
  return true;
}

add_task(function* () {
  registerCleanupFunction(function () {
    clearAllPluginPermissions();
    Services.prefs.clearUserPref("plugins.click_to_play");
    Services.prefs.clearUserPref("extensions.blocklist.suppressUI");
    setTestPluginEnabledState(Ci.nsIPluginTag.STATE_ENABLED, "Test Plug-in");
    setTestPluginEnabledState(Ci.nsIPluginTag.STATE_ENABLED, "Second Test Plug-in");
    if (gTestBrowser) {
      gBrowser.removeCurrentTab();
    }
    window.focus();
    gTestBrowser = null;
  });
});

add_task(function* () {
  Services.prefs.setBoolPref("plugins.click_to_play", true);

  setTestPluginEnabledState(Ci.nsIPluginTag.STATE_ENABLED, "Test Plug-in");
  setTestPluginEnabledState(Ci.nsIPluginTag.STATE_ENABLED, "Second Test Plug-in");

  sanitizer = new Sanitizer();
  sanitizer.ignoreTimespan = false;
  sanitizer.prefDomain = "privacy.cpd.";
  let itemPrefs = gPrefService.getBranch(sanitizer.prefDomain);
  itemPrefs.setBoolPref("history", false);
  itemPrefs.setBoolPref("downloads", false);
  itemPrefs.setBoolPref("cache", false);
  itemPrefs.setBoolPref("cookies", true); // plugin data
  itemPrefs.setBoolPref("formdata", false);
  itemPrefs.setBoolPref("offlineApps", false);
  itemPrefs.setBoolPref("passwords", false);
  itemPrefs.setBoolPref("sessions", false);
  itemPrefs.setBoolPref("siteSettings", false);
});

add_task(function* () {
  // Load page to set data for the plugin.
  gBrowser.selectedTab = gBrowser.addTab();
  gTestBrowser = gBrowser.selectedBrowser;

  yield promiseTabLoadEvent(gBrowser.selectedTab, testURL1);

  yield promiseUpdatePluginBindings(gTestBrowser);

  ok(stored(["foo.com", "bar.com", "baz.com", "qux.com"]),
    "Data stored for sites");

  // Clear 20 seconds ago
  let now_uSec = Date.now() * 1000;
  sanitizer.range = [now_uSec - 20*1000000, now_uSec];
  yield sanitizer.sanitize();

  ok(stored(["bar.com", "qux.com"]), "Data stored for sites");
  ok(!stored(["foo.com"]), "Data cleared for foo.com");
  ok(!stored(["baz.com"]), "Data cleared for baz.com");

  // Clear everything
  sanitizer.range = null;
  yield sanitizer.sanitize();

  ok(!stored(null), "All data cleared");

  gBrowser.removeCurrentTab();
  gTestBrowser = null;
});

add_task(function* () {
  // Load page to set data for the plugin.
  gBrowser.selectedTab = gBrowser.addTab();
  gTestBrowser = gBrowser.selectedBrowser;

  yield promiseTabLoadEvent(gBrowser.selectedTab, testURL2);

  yield promiseUpdatePluginBindings(gTestBrowser);

  ok(stored(["foo.com", "bar.com", "baz.com", "qux.com"]),
    "Data stored for sites");

  // Attempt to clear 20 seconds ago. The plugin will throw
  // NS_ERROR_PLUGIN_TIME_RANGE_NOT_SUPPORTED, which should result in us
  // clearing all data regardless of age.
  let now_uSec = Date.now() * 1000;
  sanitizer.range = [now_uSec - 20*1000000, now_uSec];
  yield sanitizer.sanitize();

  ok(!stored(null), "All data cleared");

  gBrowser.removeCurrentTab();
  gTestBrowser = null;
});