1 var initialize_IndexedDBTest = function() {
3 InspectorTest.dumpIndexedDBTree = function()
5 InspectorTest.addResult("Dumping IndexedDB tree:");
6 var indexedDBTreeElement = WebInspector.panels.resources.indexedDBListTreeElement;
7 if (!indexedDBTreeElement.children.length) {
8 InspectorTest.addResult(" (empty)");
11 for (var i = 0; i < indexedDBTreeElement.children.length; ++i) {
12 var databaseTreeElement = indexedDBTreeElement.children[i];
13 InspectorTest.addResult(" database: " + databaseTreeElement.titleText);
14 if (!databaseTreeElement.children.length) {
15 InspectorTest.addResult(" (no object stores)");
18 for (var j = 0; j < databaseTreeElement.children.length; ++j) {
19 var objectStoreTreeElement = databaseTreeElement.children[j];
20 InspectorTest.addResult(" Object store: " + objectStoreTreeElement.titleText);
21 if (!objectStoreTreeElement.children.length) {
22 InspectorTest.addResult(" (no indexes)");
25 for (var j = 0; j < objectStoreTreeElement.children.length; ++j) {
26 var indexTreeElement = objectStoreTreeElement.children[j];
27 InspectorTest.addResult(" Index: " + indexTreeElement.titleText);
33 var lastCallbackId = 0;
35 var callbackIdPrefix = "InspectorTest.IndexedDB_callback";
36 InspectorTest.evaluateWithCallback = function(frameId, methodName, parameters, callback)
38 InspectorTest._installIndexedDBSniffer();
39 var callbackId = ++lastCallbackId;
40 callbacks[callbackId] = callback;
41 var parametersString = "dispatchCallback.bind(this, \"" + callbackIdPrefix + callbackId + "\")";
42 for (var i = 0; i < parameters.length; ++i)
43 parametersString += ", " + JSON.stringify(parameters[i]);
45 var requestString = methodName + "(" + parametersString + ")";
46 InspectorTest.evaluateInPage(requestString);
49 InspectorTest._installIndexedDBSniffer = function()
51 InspectorTest.addConsoleSniffer(consoleMessageOverride, false);
53 function consoleMessageOverride(msg)
55 var text = msg.consoleMessage().messageText;
56 if (!text.startsWith(callbackIdPrefix)) {
57 InspectorTest.addConsoleSniffer(consoleMessageOverride, false);
60 var callbackId = text.substring(callbackIdPrefix.length);
61 callbacks[callbackId].call();
62 delete callbacks[callbackId];
66 InspectorTest.createDatabase = function(frameId, databaseName, callback)
68 InspectorTest.evaluateWithCallback(frameId, "createDatabase", [databaseName], callback)
71 InspectorTest.deleteDatabase = function(frameId, databaseName, callback)
73 InspectorTest.evaluateWithCallback(frameId, "deleteDatabase", [databaseName], callback)
76 InspectorTest.createObjectStore = function(frameId, databaseName, objectStoreName, keyPath, autoIncrement, callback)
78 InspectorTest.evaluateWithCallback(frameId, "createObjectStore", [databaseName, objectStoreName, keyPath, autoIncrement], callback)
81 InspectorTest.deleteObjectStore = function(frameId, databaseName, objectStoreName, callback)
83 InspectorTest.evaluateWithCallback(frameId, "deleteObjectStore", [databaseName, objectStoreName], callback)
86 InspectorTest.createObjectStoreIndex = function(frameId, databaseName, objectStoreName, objectStoreIndexName, keyPath, unique, multiEntry, callback)
88 InspectorTest.evaluateWithCallback(frameId, "createObjectStoreIndex", [databaseName, objectStoreName, objectStoreIndexName, keyPath, unique, multiEntry], callback)
91 InspectorTest.deleteObjectStoreIndex = function(frameId, databaseName, objectStoreName, objectStoreIndexName, callback)
93 InspectorTest.evaluateWithCallback(frameId, "deleteObjectStoreIndex", [databaseName, objectStoreName, objectStoreIndexName], callback)
96 InspectorTest.addIDBValue = function(frameId, databaseName, objectStoreName, value, key, callback)
98 InspectorTest.evaluateWithCallback(frameId, "addIDBValue", [databaseName, objectStoreName, value, key], callback)
103 function dispatchCallback(callbackId)
105 console.log(callbackId);
108 function onIndexedDBError(e)
110 console.error("IndexedDB error: " + e);
113 function onIndexedDBBlocked(e)
115 console.error("IndexedDB blocked: " + e);
118 function doWithDatabase(databaseName, callback)
120 function innerCallback()
122 var db = request.result;
126 var request = indexedDB.open(databaseName);
127 request.onblocked = onIndexedDBBlocked;
128 request.onerror = onIndexedDBError;
129 request.onsuccess = innerCallback;
132 function doWithVersionTransaction(databaseName, callback, commitCallback)
134 doWithDatabase(databaseName, step2);
138 var version = db.version;
140 request = indexedDB.open(databaseName, version + 1);
141 request.onerror = onIndexedDBError;
142 request.onupgradeneeded = onUpgradeNeeded;
143 request.onsuccess = onOpened;
145 function onUpgradeNeeded(e)
147 var db = e.target.result;
148 var trans = e.target.transaction;
154 var db = e.target.result;
161 function doWithReadWriteTransaction(databaseName, objectStoreName, callback, commitCallback)
163 doWithDatabase(databaseName, step2);
167 var transaction = db.transaction([objectStoreName], 'readwrite');
168 var objectStore = transaction.objectStore(objectStoreName);
169 callback(objectStore, innerCommitCallback);
171 function innerCommitCallback()
179 function createDatabase(callback, databaseName)
181 var request = indexedDB.open(databaseName);
182 request.onerror = onIndexedDBError;
183 request.onsuccess = closeDatabase;
185 function closeDatabase()
187 request.result.close();
192 function deleteDatabase(callback, databaseName)
194 var request = indexedDB.deleteDatabase(databaseName);
195 request.onerror = onIndexedDBError;
196 request.onsuccess = callback;
199 function createObjectStore(callback, databaseName, objectStoreName, keyPath, autoIncrement)
201 doWithVersionTransaction(databaseName, withTransactionCallback, callback);
203 function withTransactionCallback(db, transaction)
205 var store = db.createObjectStore(objectStoreName, { keyPath: keyPath, autoIncrement: autoIncrement });
209 function deleteObjectStore(callback, databaseName, objectStoreName)
211 doWithVersionTransaction(databaseName, withTransactionCallback, callback);
213 function withTransactionCallback(db, transaction)
215 var store = db.deleteObjectStore(objectStoreName);
219 function createObjectStoreIndex(callback, databaseName, objectStoreName, objectStoreIndexName, keyPath, unique, multiEntry)
221 doWithVersionTransaction(databaseName, withTransactionCallback, callback);
223 function withTransactionCallback(db, transaction)
225 var objectStore = transaction.objectStore(objectStoreName);
226 objectStore.createIndex(objectStoreIndexName, keyPath, { unique: unique, multiEntry: multiEntry });
230 function deleteObjectStoreIndex(callback, databaseName, objectStoreName, objectStoreIndexName)
232 doWithVersionTransaction(databaseName, withTransactionCallback, callback);
234 function withTransactionCallback(db, transaction)
236 var objectStore = transaction.objectStore(objectStoreName);
237 objectStore.deleteIndex(objectStoreIndexName);
241 function addIDBValue(callback, databaseName, objectStoreName, value, key)
243 doWithReadWriteTransaction(databaseName, objectStoreName, withTransactionCallback, callback)
245 function withTransactionCallback(objectStore, commitCallback)
249 request = objectStore.add(value, key);
251 request = objectStore.add(value);
252 request.onerror = onIndexedDBError;
253 request.onsuccess = commitCallback;