summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/storage-listings.html
blob: c3b7ef3c8cb3e5ec279ab0f70f269f0d782d98be (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
<!DOCTYPE HTML>
<html>
<!--
Bug 965872 - Storage inspector actor with cookies, local storage and session storage.
-->
<head>
  <meta charset="utf-8">
  <title>Storage inspector test for listing hosts and storages</title>
</head>
<body>
<iframe src="http://sectest1.example.org/browser/devtools/server/tests/browser/storage-unsecured-iframe.html"></iframe>
<iframe src="https://sectest1.example.org:443/browser/devtools/server/tests/browser/storage-secured-iframe.html"></iframe>
<script type="application/javascript;version=1.7">
"use strict";
const partialHostname = location.hostname.match(/^[^.]+(\..*)$/)[1];
const cookieExpiresTime1 = 2000000000000;
const cookieExpiresTime2 = 2000000001000;
// Setting up some cookies to eat.
document.cookie = "c1=foobar; expires=" +
  new Date(cookieExpiresTime1).toGMTString() + "; path=/browser";
document.cookie = "cs2=sessionCookie; path=/; domain=" + partialHostname;
document.cookie = "c3=foobar-2; secure=true; expires=" +
  new Date(cookieExpiresTime2).toGMTString() + "; path=/";
// ... and some local storage items ..
localStorage.setItem("ls1", "foobar");
localStorage.setItem("ls2", "foobar-2");
// ... and finally some session storage items too
sessionStorage.setItem("ss1", "foobar-3");
console.log("added cookies and stuff from main page");

let idbGenerator = function*() {
  let request = indexedDB.open("idb1", 1);
  request.onerror = function() {
    throw new Error("error opening db connection");
  };
  let db = yield new Promise(done => {
    request.onupgradeneeded = event => {
      let db = event.target.result;
      let store1 = db.createObjectStore("obj1", { keyPath: "id" });
      store1.createIndex("name", "name", { unique: false });
      store1.createIndex("email", "email", { unique: true });
      let store2 = db.createObjectStore("obj2", { keyPath: "id2" });
      store1.transaction.oncomplete = () => {
        done(db);
      };
    };
  });

  // Prevents AbortError
  yield new Promise(done => {
    request.onsuccess = done;
  });

  let transaction = db.transaction(["obj1", "obj2"], "readwrite");
  let store1 = transaction.objectStore("obj1");
  let store2 = transaction.objectStore("obj2");
  store1.add({id: 1, name: "foo", email: "foo@bar.com"});
  store1.add({id: 2, name: "foo2", email: "foo2@bar.com"});
  store1.add({id: 3, name: "foo2", email: "foo3@bar.com"});
  store2.add({
    id2: 1,
    name: "foo",
    email: "foo@bar.com",
    extra: "baz"
  });
  // Prevents AbortError during close()
  yield new Promise(success => {
    transaction.oncomplete = success;
  });

  db.close();

  request = indexedDB.open("idb2", 1);
  let db2 = yield new Promise(done => {
    request.onupgradeneeded = event => {
      let db2 = event.target.result;
      let store3 = db2.createObjectStore("obj3", { keyPath: "id3" });
      store3.createIndex("name2", "name2", { unique: true });
      store3.transaction.oncomplete = () => {
        done(db2);
      }
    };
  });
  // Prevents AbortError during close()
  yield new Promise(done => {
    request.onsuccess = done;
  });
  db2.close();

  dump("added cookies and stuff from main page\n");
};

function deleteDB(dbName) {
  return new Promise(resolve => {
    dump("removing database " + dbName + " from " + document.location + "\n");
    indexedDB.deleteDatabase(dbName).onsuccess = resolve;
  });
}

window.setup = function*() {
  yield idbGenerator();
};

window.clear = function*() {
  document.cookie = "c1=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/browser";
  document.cookie =
    "c3=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; secure=true";
  document.cookie =
    "cs2=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=" +
    partialHostname;

  localStorage.clear();
  sessionStorage.clear();

  yield deleteDB("idb1");
  yield deleteDB("idb2");

  dump("removed cookies, localStorage, sessionStorage and indexedDB data " +
       "from " + document.location + "\n");
};
</script>
</body>
</html>