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
128
129
130
131
132
133
134
135
(function() {
// Restore the IndexedDB databases captured by `idb_save.js`.
//
// Kickoff-and-poll for the same reason as the save side: the host
// calls this until `state` leaves `pending`. The payload is spliced in
// by the caller as `var payload = {...};` ahead of this body.
//
// Each database is opened at the version it was captured at, missing
// stores are created in the upgrade transaction (the only place a
// store can be created), and records are written with `put` so a
// restore over an existing database overwrites rather than throws.
var S = window.__vsIdbLoad;
if (S && S.token === payload.token) {
return JSON.stringify({
state: S.state,
error: S.error || S.waiting,
trace: S.trace.join(' > '),
});
}
S = window.__vsIdbLoad = {
token: payload.token,
state: 'pending',
error: '',
// Failures, collected so one database's success cannot erase
// another's failure. `waiting` is not a failure: it says a
// connection is still holding a database open.
failures: [],
waiting: '',
// Breadcrumbs for the host's error message. A restore that
// reports success while the database stays empty is the one
// failure this path must never hide, so it says where it went.
trace: [],
};
var dbs = payload.dbs || [];
var left = dbs.length;
if (!left || !window.indexedDB) {
S.state = 'done';
return JSON.stringify({ state: S.state, error: S.error });
}
dbs.forEach(function(spec) { restore(spec, done); });
return JSON.stringify({ state: S.state, error: S.error });
// A database that could not be written must not land as success.
// Reporting `done` on a blocked or failed open is how a restore
// claims a session it never wrote, which is the failure this whole
// path exists to remove. `onblocked` deliberately does not report
// at all: another connection is holding the database open and may
// yet close, so the request stays live and the host's deadline
// decides, with `S.error` naming what it was waiting on.
function done(why) {
if (why) { S.failures.push(why); }
if (--left === 0) {
S.error = S.failures.join('; ');
S.state = S.failures.length ? 'error' : 'done';
}
}
function restore(spec, done) {
S.trace.push('open ' + spec.name + '@v' + (spec.version || 1));
open(spec, spec.version || 1, done, true);
}
// `version` decides everything here: object stores can only be
// created inside an upgrade transaction, and that only runs when
// the requested version is higher than the one on disk. A database
// that already exists at the captured version — an empty one the
// page created on open, or a partial restore — would otherwise
// report success with none of its stores. When stores are missing
// after a successful open, `retry` reopens one version up, which
// is the only way to add them.
function open(spec, version, done, retry) {
var req;
try {
req = indexedDB.open(spec.name, version);
} catch (e) {
done('open threw: ' + spec.name);
return;
}
req.onerror = function() { done('open failed: ' + spec.name); };
req.onblocked = function() { S.waiting = 'blocked by another connection: ' + spec.name; };
req.onupgradeneeded = function() {
var db = req.result;
(spec.stores || []).forEach(function(store) {
if (db.objectStoreNames.contains(store.name)) return;
try {
S.trace.push('create ' + store.name);
db.createObjectStore(store.name, {
keyPath: store.keyPath === null ? undefined : store.keyPath,
autoIncrement: !!store.autoIncrement,
});
} catch (e) {}
});
};
req.onsuccess = function() {
var db = req.result;
var wanted = (spec.stores || []).map(function(s) { return s.name; });
var names = wanted.filter(function(n) { return db.objectStoreNames.contains(n); });
S.trace.push('opened v' + db.version + ' stores=' + names.join(','));
if (names.length < wanted.length && retry) {
var next = db.version + 1;
db.close();
S.trace.push('reopen@v' + next);
open(spec, next, done, false);
return;
}
if (!names.length) { db.close(); done('no stores restored: ' + spec.name); return; }
var tx;
try {
tx = db.transaction(names, 'readwrite');
} catch (e) {
db.close();
done('transaction failed: ' + spec.name);
return;
}
(spec.stores || []).forEach(function(store) {
if (names.indexOf(store.name) < 0) return;
var os = tx.objectStore(store.name);
(store.records || []).forEach(function(rec) {
try {
// An in-line-key store derives the key from the
// value and rejects an explicit one.
if (os.keyPath === null || os.keyPath === undefined) {
os.put(rec.value, rec.key);
} else {
os.put(rec.value);
}
} catch (e) {}
});
});
tx.oncomplete = function() { S.trace.push('wrote ' + names.join(',')); db.close(); done(); };
tx.onerror = function() { db.close(); done('write failed: ' + spec.name); };
tx.onabort = function() { db.close(); done('write aborted: ' + spec.name); };
};
}
})()