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 var indexedDB = window.indexeddb || window.webkitIndexedDB;
104 window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
106 function dispatchCallback(callbackId)
108 console.log(callbackId);
111 function onIndexedDBError(e)
113 console.error("IndexedDB error: " + e);
116 function onIndexedDBBlocked(e)
118 console.error("IndexedDB blocked: " + e);
121 function doWithDatabase(databaseName, callback)
123 function innerCallback()
125 var db = request.result;
129 var request = indexedDB.open(databaseName);
130 request.onblocked = onIndexedDBBlocked;
131 request.onerror = onIndexedDBError;
132 request.onsuccess = innerCallback;
135 function doWithVersionTransaction(databaseName, callback, commitCallback)
137 doWithDatabase(databaseName, step2);
141 var version = db.version;
143 request = indexedDB.open(databaseName, version + 1);
144 request.onerror = onIndexedDBError;
145 request.onupgradeneeded = onUpgradeNeeded;
146 request.onsuccess = onOpened;
148 function onUpgradeNeeded(e)
150 var db = e.target.result;
151 var trans = e.target.transaction;
157 var db = e.target.result;
164 function doWithReadWriteTransaction(databaseName, objectStoreName, callback, commitCallback)
166 doWithDatabase(databaseName, step2);
170 var transaction = db.transaction([objectStoreName], 'readwrite');
171 var objectStore = transaction.objectStore(objectStoreName);
172 callback(objectStore, innerCommitCallback);
174 function innerCommitCallback()
182 function createDatabase(callback, databaseName)
184 var request = indexedDB.open(databaseName);
185 request.onerror = onIndexedDBError;
186 request.onsuccess = closeDatabase;
188 function closeDatabase()
190 request.result.close();
195 function deleteDatabase(callback, databaseName)
197 var request = indexedDB.deleteDatabase(databaseName);
198 request.onerror = onIndexedDBError;
199 request.onsuccess = callback;
202 function createObjectStore(callback, databaseName, objectStoreName, keyPath, autoIncrement)
204 doWithVersionTransaction(databaseName, withTransactionCallback, callback);
206 function withTransactionCallback(db, transaction)
208 var store = db.createObjectStore(objectStoreName, { keyPath: keyPath, autoIncrement: autoIncrement });
212 function deleteObjectStore(callback, databaseName, objectStoreName)
214 doWithVersionTransaction(databaseName, withTransactionCallback, callback);
216 function withTransactionCallback(db, transaction)
218 var store = db.deleteObjectStore(objectStoreName);
222 function createObjectStoreIndex(callback, databaseName, objectStoreName, objectStoreIndexName, keyPath, unique, multiEntry)
224 doWithVersionTransaction(databaseName, withTransactionCallback, callback);
226 function withTransactionCallback(db, transaction)
228 var objectStore = transaction.objectStore(objectStoreName);
229 objectStore.createIndex(objectStoreIndexName, keyPath, { unique: unique, multiEntry: multiEntry });
233 function deleteObjectStoreIndex(callback, databaseName, objectStoreName, objectStoreIndexName)
235 doWithVersionTransaction(databaseName, withTransactionCallback, callback);
237 function withTransactionCallback(db, transaction)
239 var objectStore = transaction.objectStore(objectStoreName);
240 objectStore.deleteIndex(objectStoreIndexName);
244 function addIDBValue(callback, databaseName, objectStoreName, value, key)
246 doWithReadWriteTransaction(databaseName, objectStoreName, withTransactionCallback, callback)
248 function withTransactionCallback(objectStore, commitCallback)
252 request = objectStore.add(value, key);
254 request = objectStore.add(value);
255 request.onerror = onIndexedDBError;
256 request.onsuccess = commitCallback;