Commit 388ba57c authored by Eugene Ostroukhov's avatar Eugene Ostroukhov Committed by Commit Bot

[DevTools] Fix WebSQL UI

Replace callbacks with promises.

Bug: 
Change-Id: I677ec63941ec827f559921dc4205fff9eae6a841
Reviewed-on: https://chromium-review.googlesource.com/564086
Commit-Queue: Eugene Ostroukhov <eostroukhov@chromium.org>
Reviewed-by: default avatarAlexei Filippov <alph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#486155}
parent 30c7efa7
Tests Application Panel WebSQL support.
Initial state:
Application
Manifest (selected)
Service Workers
Clear storage
Storage
Local Storage
http://127.0.0.1:8000
Session Storage
http://127.0.0.1:8000
IndexedDB
Web SQL
Cookies
http://127.0.0.1:8000
Cache
Cache Storage
Application Cache
Frames
top
Scripts
console-test.js
indexeddb-test.js
inspector-test.js
json-value.js
resources-test.js
resources-panel-websql.html
Found: true
Database created state:
Application
Manifest
Service Workers
Clear storage
Storage
Local Storage
http://127.0.0.1:8000
Session Storage
http://127.0.0.1:8000
IndexedDB
Web SQL
inspector-test-db (selected)
Cookies
http://127.0.0.1:8000
Cache
Cache Storage
Application Cache
Frames
top
Scripts
console-test.js
indexeddb-test.js
inspector-test.js
json-value.js
resources-test.js
resources-panel-websql.html
Table added:
Application
Manifest
Service Workers
Clear storage
Storage
Local Storage
http://127.0.0.1:8000
Session Storage
http://127.0.0.1:8000
IndexedDB
Web SQL
inspector-test-db (selected)
table1
Cookies
http://127.0.0.1:8000
Cache
Cache Storage
Application Cache
Frames
top
Scripts
console-test.js
indexeddb-test.js
inspector-test.js
json-value.js
resources-test.js
resources-panel-websql.html
<html>
<head>
<script>
function parse(val) {
// This is here for the JSON file imported via the script tag below
}
</script>
<script src="../inspector-test.js"></script>
<script src="../resources-test.js"></script>
<script src="../console-test.js"></script>
<script src="../resources/json-value.js"></script>
<script src="../indexeddb/indexeddb-test.js"></script>
<script>
async function test()
{
function dump(node, prefix)
{
for (var child of node.children()) {
InspectorTest.addResult(prefix + child.listItemElement.textContent + (child.selected ? " (selected)" : ""));
dump(child, prefix + " ");
}
}
function dumpCurrentState(label) {
InspectorTest.addResult(label);
dump(UI.panels.resources._sidebar._sidebarTree.rootElement(), "");
}
function findTreeElement(parent, path) {
if (path.length === 0)
return parent;
var child = parent.children().find(child => child.title === path[0]);
if (!child)
return null;
child.expand();
return findTreeElement(child, path.slice(1));
}
async function createTable(queryView) {
queryView._prompt.setText('CREATE TABLE table1 (id INTEGER PRIMARY KEY ASC, text_field TEXT)');
queryView._enterKeyPressed(new KeyboardEvent('keydown'));
await queryView.once(Resources.DatabaseQueryView.Events.SchemaUpdated);
return new Promise(resolve => setTimeout(resolve));
}
UI.viewManager.showView('resources');
dumpCurrentState('Initial state:');
await InspectorTest.evaluateInPagePromise('openDatabase("inspector-test-db", "1.0", "Database for inspector test", 1024*1024)');
var parent = UI.panels.resources._sidebar._sidebarTree.rootElement();
var databaseElement = findTreeElement(parent, ['Storage', 'Web SQL', 'inspector-test-db']);
InspectorTest.addResult("Found: " + !!databaseElement);
if (!databaseElement)
return;
databaseElement.select();
dumpCurrentState("Database created state:");
var queryView = UI.panels.resources.visibleView;
if (!queryView instanceof Resources.DatabaseQueryView) {
InspectorTest.addResult("Not a Resources.DatabaseQueryView");
return;
}
await createTable(queryView);
while (!findTreeElement(databaseElement, ["table1"])) {
databaseElement.expand();
await new Promise(resolve => setTimeout(resolve));
}
dumpCurrentState("Table added:");
InspectorTest.completeTest();
}
</script>
</head>
<body onload="runTest()">
<p>Tests Application Panel WebSQL support.</p>
</body>
</html>
...@@ -700,19 +700,10 @@ Resources.DatabaseTreeElement = class extends Resources.BaseStorageTreeElement { ...@@ -700,19 +700,10 @@ Resources.DatabaseTreeElement = class extends Resources.BaseStorageTreeElement {
this._updateChildren(); this._updateChildren();
} }
_updateChildren() { async _updateChildren() {
this.removeChildren(); var tableNames = await this._database.tableNames();
for (var tableName of tableNames)
/** this.appendChild(new Resources.DatabaseTableTreeElement(this._sidebar, this._database, tableName));
* @param {!Array.<string>} tableNames
* @this {Resources.DatabaseTreeElement}
*/
function tableNamesCallback(tableNames) {
var tableNamesLength = tableNames.length;
for (var i = 0; i < tableNamesLength; ++i)
this.appendChild(new Resources.DatabaseTableTreeElement(this._sidebar, this._database, tableNames[i]));
}
this._database.getTableNames(tableNamesCallback.bind(this));
} }
}; };
......
...@@ -81,14 +81,11 @@ Resources.Database = class { ...@@ -81,14 +81,11 @@ Resources.Database = class {
} }
/** /**
* @param {function(!Array.<string>)} callback * @return {!Promise<!Array<string>>}
*/ */
getTableNames(callback) { async tableNames() {
function sortingCallback(error, names) { var names = await this._model._agent.getDatabaseTableNames(this._id) || [];
if (!error) return names.sort();
callback(names.sort());
}
this._model._agent.getDatabaseTableNames(this._id, sortingCallback);
} }
/** /**
...@@ -96,32 +93,26 @@ Resources.Database = class { ...@@ -96,32 +93,26 @@ Resources.Database = class {
* @param {function(!Array.<string>=, !Array.<*>=)} onSuccess * @param {function(!Array.<string>=, !Array.<*>=)} onSuccess
* @param {function(string)} onError * @param {function(string)} onError
*/ */
executeSql(query, onSuccess, onError) { async executeSql(query, onSuccess, onError) {
/** var response = await this._model._agent.invoke_executeSQL({'databaseId': this._id, 'query': query});
* @param {?Protocol.Error} error var error = response[Protocol.Error];
* @param {!Array.<string>=} columnNames if (error) {
* @param {!Array.<*>=} values onError(error);
* @param {!Protocol.Database.Error=} errorObj return;
*/ }
function callback(error, columnNames, values, errorObj) { var sqlError = response.sqlError;
if (error) { if (!sqlError) {
onError(error); onSuccess(response.columnNames, response.values);
return; return;
}
if (errorObj) {
var message;
if (errorObj.message)
message = errorObj.message;
else if (errorObj.code === 2)
message = Common.UIString('Database no longer has expected version.');
else
message = Common.UIString('An unexpected error %s occurred.', errorObj.code);
onError(message);
return;
}
onSuccess(columnNames, values);
} }
this._model._agent.executeSQL(this._id, query, callback); var message;
if (sqlError.message)
message = sqlError.message;
else if (sqlError.code === 2)
message = Common.UIString('Database no longer has expected version.');
else
message = Common.UIString('An unexpected error %s occurred.', sqlError.code);
onError(message);
} }
}; };
......
...@@ -59,38 +59,16 @@ Resources.DatabaseQueryView = class extends UI.VBox { ...@@ -59,38 +59,16 @@ Resources.DatabaseQueryView = class extends UI.VBox {
* @param {boolean=} force * @param {boolean=} force
* @return {!Promise<!UI.SuggestBox.Suggestions>} * @return {!Promise<!UI.SuggestBox.Suggestions>}
*/ */
completions(expression, prefix, force) { async completions(expression, prefix, force) {
if (!prefix) if (!prefix)
return Promise.resolve([]); return [];
var fulfill;
var promise = new Promise(x => fulfill = x);
var results = [];
prefix = prefix.toLowerCase(); prefix = prefix.toLowerCase();
var tableNames = await this.database.tableNames();
function accumulateMatches(textArray) { return tableNames.map(name => name + ' ')
for (var i = 0; i < textArray.length; ++i) { .concat(Resources.DatabaseQueryView._SQL_BUILT_INS)
var text = textArray[i].toLowerCase(); .filter(proposal => proposal.toLowerCase().startsWith(prefix))
if (text.length < prefix.length) .map(completion => ({text: completion}));
continue;
if (!text.startsWith(prefix))
continue;
results.push(textArray[i]);
}
}
function tableNamesCallback(tableNames) {
accumulateMatches(tableNames.map(function(name) {
return name + ' ';
}));
accumulateMatches([
'SELECT ', 'FROM ', 'WHERE ', 'LIMIT ', 'DELETE FROM ', 'CREATE ', 'DROP ', 'TABLE ', 'INDEX ', 'UPDATE ',
'INSERT INTO ', 'VALUES ('
]);
fulfill(results.map(completion => ({text: completion})));
}
this.database.getTableNames(tableNamesCallback);
return promise;
} }
_selectStart(event) { _selectStart(event) {
...@@ -200,3 +178,8 @@ Resources.DatabaseQueryView = class extends UI.VBox { ...@@ -200,3 +178,8 @@ Resources.DatabaseQueryView = class extends UI.VBox {
Resources.DatabaseQueryView.Events = { Resources.DatabaseQueryView.Events = {
SchemaUpdated: Symbol('SchemaUpdated') SchemaUpdated: Symbol('SchemaUpdated')
}; };
Resources.DatabaseQueryView._SQL_BUILT_INS = [
'SELECT ', 'FROM ', 'WHERE ', 'LIMIT ', 'DELETE FROM ', 'CREATE ', 'DROP ', 'TABLE ', 'INDEX ', 'UPDATE ',
'INSERT INTO ', 'VALUES ('
];
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment