blob: 61114e51329ccc507c28ca31409efa37409eadf0 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * @unrestricted
33 */
34Resources.IDBDatabaseView = class extends UI.VBox {
35 /**
36 * @param {!Resources.IndexedDBModel} model
37 * @param {?Resources.IndexedDBModel.Database} database
38 */
39 constructor(model, database) {
40 super();
41
42 this._model = model;
Harley Li6d66b9c2019-03-23 00:39:3443 const databaseName = database ? database.databaseId.name : ls`Loading\u2026`;
Blink Reformat4c46d092018-04-07 15:32:3744
45 this._reportView = new UI.ReportView(databaseName);
46 this._reportView.show(this.contentElement);
47
48 const bodySection = this._reportView.appendSection('');
Harley Li6d66b9c2019-03-23 00:39:3449 this._securityOriginElement = bodySection.appendField(ls`Security origin`);
50 this._versionElement = bodySection.appendField(ls`Version`);
51 this._objectStoreCountElement = bodySection.appendField(ls`Object stores`);
Blink Reformat4c46d092018-04-07 15:32:3752
53 const footer = this._reportView.appendSection('').appendRow();
Harley Li6d66b9c2019-03-23 00:39:3454 this._clearButton = UI.createTextButton(ls`Delete database`, () => this._deleteDatabase(), ls`Delete database`);
Blink Reformat4c46d092018-04-07 15:32:3755 footer.appendChild(this._clearButton);
56
Harley Li6d66b9c2019-03-23 00:39:3457 this._refreshButton =
58 UI.createTextButton(ls`Refresh database`, () => this._refreshDatabaseButtonClicked(), ls`Refresh database`);
Blink Reformat4c46d092018-04-07 15:32:3759 footer.appendChild(this._refreshButton);
60
Tim van der Lippe1d6e57a2019-09-30 11:55:3461 if (database) {
Blink Reformat4c46d092018-04-07 15:32:3762 this.update(database);
Tim van der Lippe1d6e57a2019-09-30 11:55:3463 }
Blink Reformat4c46d092018-04-07 15:32:3764 }
65
66 _refreshDatabase() {
67 this._securityOriginElement.textContent = this._database.databaseId.securityOrigin;
68 this._versionElement.textContent = this._database.version;
Harley Li6d66b9c2019-03-23 00:39:3469 this._objectStoreCountElement.textContent = Object.keys(this._database.objectStores).length;
Blink Reformat4c46d092018-04-07 15:32:3770 }
71
72 _refreshDatabaseButtonClicked() {
73 this._model.refreshDatabase(this._database.databaseId);
74 }
75
76 /**
77 * @param {!Resources.IndexedDBModel.Database} database
78 */
79 update(database) {
80 this._database = database;
81 this._reportView.setTitle(this._database.databaseId.name);
82 this._refreshDatabase();
83 this._updatedForTests();
84 }
85
86 _updatedForTests() {
87 // Sniffed in tests.
88 }
89
90 async _deleteDatabase() {
91 const ok = await UI.ConfirmDialog.show(
92 Common.UIString('Please confirm delete of "%s" database.', this._database.databaseId.name), this.element);
Tim van der Lippe1d6e57a2019-09-30 11:55:3493 if (ok) {
Blink Reformat4c46d092018-04-07 15:32:3794 this._model.deleteDatabase(this._database.databaseId);
Tim van der Lippe1d6e57a2019-09-30 11:55:3495 }
Blink Reformat4c46d092018-04-07 15:32:3796 }
97};
98
99/**
100 * @unrestricted
101 */
102Resources.IDBDataView = class extends UI.SimpleView {
103 /**
104 * @param {!Resources.IndexedDBModel} model
105 * @param {!Resources.IndexedDBModel.DatabaseId} databaseId
106 * @param {!Resources.IndexedDBModel.ObjectStore} objectStore
107 * @param {?Resources.IndexedDBModel.Index} index
108 * @param {function()} refreshObjectStoreCallback
109 */
110 constructor(model, databaseId, objectStore, index, refreshObjectStoreCallback) {
111 super(Common.UIString('IDB'));
112 this.registerRequiredCSS('resources/indexedDBViews.css');
113
114 this._model = model;
115 this._databaseId = databaseId;
116 this._isIndex = !!index;
117 this._refreshObjectStoreCallback = refreshObjectStoreCallback;
118
119 this.element.classList.add('indexed-db-data-view', 'storage-view');
120
121 this._refreshButton = new UI.ToolbarButton(Common.UIString('Refresh'), 'largeicon-refresh');
122 this._refreshButton.addEventListener(UI.ToolbarButton.Events.Click, this._refreshButtonClicked, this);
123
124 this._deleteSelectedButton = new UI.ToolbarButton(Common.UIString('Delete selected'), 'largeicon-delete');
125 this._deleteSelectedButton.addEventListener(UI.ToolbarButton.Events.Click, () => this._deleteButtonClicked(null));
126
127 this._clearButton = new UI.ToolbarButton(Common.UIString('Clear object store'), 'largeicon-clear');
128 this._clearButton.addEventListener(UI.ToolbarButton.Events.Click, this._clearButtonClicked, this);
129
Jeff Fisher7e55ff42019-03-02 01:13:53130 this._needsRefresh =
131 new UI.ToolbarItem(UI.createIconLabel(Common.UIString('Data may be stale'), 'smallicon-warning'));
Blink Reformat4c46d092018-04-07 15:32:37132 this._needsRefresh.setVisible(false);
133 this._needsRefresh.setTitle(Common.UIString('Some entries may have been modified'));
134
135 this._createEditorToolbar();
136
137 this._pageSize = 50;
138 this._skipCount = 0;
139
140 this.update(objectStore, index);
141 this._entries = [];
142 }
143
144 /**
145 * @return {!DataGrid.DataGrid}
146 */
147 _createDataGrid() {
148 const keyPath = this._isIndex ? this._index.keyPath : this._objectStore.keyPath;
149
150 const columns = /** @type {!Array<!DataGrid.DataGrid.ColumnDescriptor>} */ ([]);
151 columns.push({id: 'number', title: Common.UIString('#'), sortable: false, width: '50px'});
152 columns.push(
153 {id: 'key', titleDOMFragment: this._keyColumnHeaderFragment(Common.UIString('Key'), keyPath), sortable: false});
154 if (this._isIndex) {
155 columns.push({
156 id: 'primaryKey',
157 titleDOMFragment: this._keyColumnHeaderFragment(Common.UIString('Primary key'), this._objectStore.keyPath),
158 sortable: false
159 });
160 }
161 columns.push({id: 'value', title: Common.UIString('Value'), sortable: false});
162
163 const dataGrid = new DataGrid.DataGrid(
164 columns, undefined, this._deleteButtonClicked.bind(this), this._updateData.bind(this, true));
165 dataGrid.setStriped(true);
166 dataGrid.addEventListener(DataGrid.DataGrid.Events.SelectedNode, event => this._updateToolbarEnablement(), this);
167 return dataGrid;
168 }
169
170 /**
171 * @param {string} prefix
172 * @param {*} keyPath
173 * @return {!DocumentFragment}
174 */
175 _keyColumnHeaderFragment(prefix, keyPath) {
176 const keyColumnHeaderFragment = createDocumentFragment();
177 keyColumnHeaderFragment.createTextChild(prefix);
Tim van der Lippe1d6e57a2019-09-30 11:55:34178 if (keyPath === null) {
Blink Reformat4c46d092018-04-07 15:32:37179 return keyColumnHeaderFragment;
Tim van der Lippe1d6e57a2019-09-30 11:55:34180 }
Blink Reformat4c46d092018-04-07 15:32:37181
182 keyColumnHeaderFragment.createTextChild(' (' + Common.UIString('Key path: '));
183 if (Array.isArray(keyPath)) {
184 keyColumnHeaderFragment.createTextChild('[');
185 for (let i = 0; i < keyPath.length; ++i) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34186 if (i !== 0) {
Blink Reformat4c46d092018-04-07 15:32:37187 keyColumnHeaderFragment.createTextChild(', ');
Tim van der Lippe1d6e57a2019-09-30 11:55:34188 }
Blink Reformat4c46d092018-04-07 15:32:37189 keyColumnHeaderFragment.appendChild(this._keyPathStringFragment(keyPath[i]));
190 }
191 keyColumnHeaderFragment.createTextChild(']');
192 } else {
193 const keyPathString = /** @type {string} */ (keyPath);
194 keyColumnHeaderFragment.appendChild(this._keyPathStringFragment(keyPathString));
195 }
196 keyColumnHeaderFragment.createTextChild(')');
197 return keyColumnHeaderFragment;
198 }
199
200 /**
201 * @param {string} keyPathString
202 * @return {!DocumentFragment}
203 */
204 _keyPathStringFragment(keyPathString) {
205 const keyPathStringFragment = createDocumentFragment();
206 keyPathStringFragment.createTextChild('"');
207 const keyPathSpan = keyPathStringFragment.createChild('span', 'source-code indexed-db-key-path');
208 keyPathSpan.textContent = keyPathString;
209 keyPathStringFragment.createTextChild('"');
210 return keyPathStringFragment;
211 }
212
213 _createEditorToolbar() {
214 const editorToolbar = new UI.Toolbar('data-view-toolbar', this.element);
215
216 editorToolbar.appendToolbarItem(this._refreshButton);
Blink Reformat4c46d092018-04-07 15:32:37217
218 editorToolbar.appendToolbarItem(new UI.ToolbarSeparator());
219
220 this._pageBackButton = new UI.ToolbarButton(Common.UIString('Show previous page'), 'largeicon-play-back');
221 this._pageBackButton.addEventListener(UI.ToolbarButton.Events.Click, this._pageBackButtonClicked, this);
222 editorToolbar.appendToolbarItem(this._pageBackButton);
223
224 this._pageForwardButton = new UI.ToolbarButton(Common.UIString('Show next page'), 'largeicon-play');
225 this._pageForwardButton.setEnabled(false);
226 this._pageForwardButton.addEventListener(UI.ToolbarButton.Events.Click, this._pageForwardButtonClicked, this);
227 editorToolbar.appendToolbarItem(this._pageForwardButton);
228
Junyi Xiao6e3798d2019-09-23 19:12:27229 this._keyInput = new UI.ToolbarInput(ls`Start from key`, '', 0.5);
Erik Luoe6a74642018-10-26 22:03:54230 this._keyInput.addEventListener(UI.ToolbarInput.Event.TextChanged, this._updateData.bind(this, false));
231 editorToolbar.appendToolbarItem(this._keyInput);
Harley Lif0f328e2018-12-15 01:49:47232 editorToolbar.appendToolbarItem(new UI.ToolbarSeparator());
233 editorToolbar.appendToolbarItem(this._clearButton);
234 editorToolbar.appendToolbarItem(this._deleteSelectedButton);
Blink Reformat4c46d092018-04-07 15:32:37235
236 editorToolbar.appendToolbarItem(this._needsRefresh);
237 }
238
239 /**
240 * @param {!Common.Event} event
241 */
242 _pageBackButtonClicked(event) {
243 this._skipCount = Math.max(0, this._skipCount - this._pageSize);
244 this._updateData(false);
245 }
246
247 /**
248 * @param {!Common.Event} event
249 */
250 _pageForwardButtonClicked(event) {
251 this._skipCount = this._skipCount + this._pageSize;
252 this._updateData(false);
253 }
254
Brandon Goddard020ed5e2019-10-16 20:53:17255 /**
256 * @param {!UI.ContextMenu} contextMenu
257 * @param {!DataGrid.DataGridNode} gridNode
258 */
259 _populateContextMenu(contextMenu, gridNode) {
260 const node = /** @type {!Resources.IDBDataGridNode} */ (gridNode);
261 if (node.valueObjectPresentation) {
262 contextMenu.revealSection().appendItem(ls`Expand Recursively`, () => {
263 node.valueObjectPresentation.objectTreeElement().expandRecursively();
264 });
265 contextMenu.revealSection().appendItem(ls`Collapse`, () => {
266 node.valueObjectPresentation.objectTreeElement().collapse();
267 });
268 }
269 }
270
Blink Reformat4c46d092018-04-07 15:32:37271 refreshData() {
272 this._updateData(true);
273 }
274
275 /**
276 * @param {!Resources.IndexedDBModel.ObjectStore} objectStore
277 * @param {?Resources.IndexedDBModel.Index} index
278 */
279 update(objectStore, index) {
280 this._objectStore = objectStore;
281 this._index = index;
282
Tim van der Lippe1d6e57a2019-09-30 11:55:34283 if (this._dataGrid) {
Blink Reformat4c46d092018-04-07 15:32:37284 this._dataGrid.asWidget().detach();
Tim van der Lippe1d6e57a2019-09-30 11:55:34285 }
Blink Reformat4c46d092018-04-07 15:32:37286 this._dataGrid = this._createDataGrid();
Brandon Goddard020ed5e2019-10-16 20:53:17287 this._dataGrid.setRowContextMenuCallback(this._populateContextMenu.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37288 this._dataGrid.asWidget().show(this.element);
289
290 this._skipCount = 0;
291 this._updateData(true);
292 }
293
294 /**
295 * @param {string} keyString
296 */
297 _parseKey(keyString) {
298 let result;
299 try {
300 result = JSON.parse(keyString);
301 } catch (e) {
302 result = keyString;
303 }
304 return result;
305 }
306
307 /**
308 * @param {boolean} force
309 */
310 _updateData(force) {
Erik Luoe6a74642018-10-26 22:03:54311 const key = this._parseKey(this._keyInput.value());
Blink Reformat4c46d092018-04-07 15:32:37312 const pageSize = this._pageSize;
313 let skipCount = this._skipCount;
314 let selected = this._dataGrid.selectedNode ? this._dataGrid.selectedNode.data['number'] : 0;
315 selected = Math.max(selected, this._skipCount); // Page forward should select top entry
316 this._refreshButton.setEnabled(false);
317 this._clearButton.setEnabled(!this._isIndex);
318
Tim van der Lippe1d6e57a2019-09-30 11:55:34319 if (!force && this._lastKey === key && this._lastPageSize === pageSize && this._lastSkipCount === skipCount) {
Blink Reformat4c46d092018-04-07 15:32:37320 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34321 }
Blink Reformat4c46d092018-04-07 15:32:37322
323 if (this._lastKey !== key || this._lastPageSize !== pageSize) {
324 skipCount = 0;
325 this._skipCount = 0;
326 }
327 this._lastKey = key;
328 this._lastPageSize = pageSize;
329 this._lastSkipCount = skipCount;
330
331 /**
332 * @param {!Array.<!Resources.IndexedDBModel.Entry>} entries
333 * @param {boolean} hasMore
334 * @this {Resources.IDBDataView}
335 */
336 function callback(entries, hasMore) {
337 this._refreshButton.setEnabled(true);
338 this.clear();
339 this._entries = entries;
340 let selectedNode = null;
341 for (let i = 0; i < entries.length; ++i) {
342 const data = {};
343 data['number'] = i + skipCount;
344 data['key'] = entries[i].key;
345 data['primaryKey'] = entries[i].primaryKey;
346 data['value'] = entries[i].value;
347
348 const node = new Resources.IDBDataGridNode(data);
349 this._dataGrid.rootNode().appendChild(node);
Tim van der Lippe1d6e57a2019-09-30 11:55:34350 if (data['number'] <= selected) {
Blink Reformat4c46d092018-04-07 15:32:37351 selectedNode = node;
Tim van der Lippe1d6e57a2019-09-30 11:55:34352 }
Blink Reformat4c46d092018-04-07 15:32:37353 }
354
Tim van der Lippe1d6e57a2019-09-30 11:55:34355 if (selectedNode) {
Blink Reformat4c46d092018-04-07 15:32:37356 selectedNode.select();
Tim van der Lippe1d6e57a2019-09-30 11:55:34357 }
Blink Reformat4c46d092018-04-07 15:32:37358 this._pageBackButton.setEnabled(!!skipCount);
359 this._pageForwardButton.setEnabled(hasMore);
360 this._needsRefresh.setVisible(false);
361 this._updateToolbarEnablement();
362 this._updatedDataForTests();
363 }
364
365 const idbKeyRange = key ? window.IDBKeyRange.lowerBound(key) : null;
366 if (this._isIndex) {
367 this._model.loadIndexData(
368 this._databaseId, this._objectStore.name, this._index.name, idbKeyRange, skipCount, pageSize,
369 callback.bind(this));
370 } else {
371 this._model.loadObjectStoreData(
372 this._databaseId, this._objectStore.name, idbKeyRange, skipCount, pageSize, callback.bind(this));
373 }
Harley Lib8eb0762019-03-15 03:51:22374 this._model.getMetadata(this._databaseId, this._objectStore).then(this._updateSummaryBar.bind(this));
Harley Li2397c022019-02-15 22:54:12375 }
376
Harley Lib8eb0762019-03-15 03:51:22377 /**
378 * @param {?Resources.IndexedDBModel.ObjectStoreMetadata} metadata
379 */
380 _updateSummaryBar(metadata) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34381 if (!this._summaryBarElement) {
Harley Li2397c022019-02-15 22:54:12382 this._summaryBarElement = this.element.createChild('div', 'object-store-summary-bar');
Tim van der Lippe1d6e57a2019-09-30 11:55:34383 }
Harley Li2397c022019-02-15 22:54:12384 this._summaryBarElement.removeChildren();
Tim van der Lippe1d6e57a2019-09-30 11:55:34385 if (!metadata) {
Harley Lib8eb0762019-03-15 03:51:22386 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34387 }
Harley Lib8eb0762019-03-15 03:51:22388
389 const separator = '\u2002\u2758\u2002';
390
Harley Li2397c022019-02-15 22:54:12391 const span = this._summaryBarElement.createChild('span');
Harley Lib8eb0762019-03-15 03:51:22392 span.textContent = ls`Total entries: ${String(metadata.entriesCount)}`;
393
394 if (this._objectStore.autoIncrement) {
395 span.textContent += separator;
396 span.textContent += ls`Key generator value: ${String(metadata.keyGeneratorValue)}`;
397 }
Blink Reformat4c46d092018-04-07 15:32:37398 }
399
400 _updatedDataForTests() {
401 // Sniffed in tests.
402 }
403
404 /**
405 * @param {?Common.Event} event
406 */
407 _refreshButtonClicked(event) {
408 this._updateData(true);
409 }
410
411 /**
412 * @param {!Common.Event} event
413 */
414 async _clearButtonClicked(event) {
415 this._clearButton.setEnabled(false);
416 await this._model.clearObjectStore(this._databaseId, this._objectStore.name);
417 this._clearButton.setEnabled(true);
418 this._updateData(true);
419 }
420
421 markNeedsRefresh() {
422 this._needsRefresh.setVisible(true);
423 }
424
425 /**
426 * @param {?DataGrid.DataGridNode} node
427 */
428 async _deleteButtonClicked(node) {
429 if (!node) {
430 node = this._dataGrid.selectedNode;
Tim van der Lippe1d6e57a2019-09-30 11:55:34431 if (!node) {
Blink Reformat4c46d092018-04-07 15:32:37432 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34433 }
Blink Reformat4c46d092018-04-07 15:32:37434 }
435 const key = /** @type {!SDK.RemoteObject} */ (this._isIndex ? node.data.primaryKey : node.data.key);
436 const keyValue = /** @type {!Array<?>|!Date|number|string} */ (key.value);
437 await this._model.deleteEntries(this._databaseId, this._objectStore.name, window.IDBKeyRange.only(keyValue));
438 this._refreshObjectStoreCallback();
439 }
440
441 clear() {
442 this._dataGrid.rootNode().removeChildren();
443 this._entries = [];
444 }
445
446 _updateToolbarEnablement() {
447 const empty = !this._dataGrid || this._dataGrid.rootNode().children.length === 0;
448 this._clearButton.setEnabled(!empty);
449 this._deleteSelectedButton.setEnabled(!empty && this._dataGrid.selectedNode !== null);
450 }
451};
452
453/**
454 * @unrestricted
455 */
456Resources.IDBDataGridNode = class extends DataGrid.DataGridNode {
457 /**
458 * @param {!Object.<string, *>} data
459 */
460 constructor(data) {
461 super(data, false);
462 this.selectable = true;
Brandon Goddard020ed5e2019-10-16 20:53:17463 /** @type {?ObjectUI.ObjectPropertiesSection} */
464 this.valueObjectPresentation = null;
Blink Reformat4c46d092018-04-07 15:32:37465 }
466
467 /**
468 * @override
469 * @return {!Element}
470 */
471 createCell(columnIdentifier) {
472 const cell = super.createCell(columnIdentifier);
473 const value = /** @type {!SDK.RemoteObject} */ (this.data[columnIdentifier]);
474
475 switch (columnIdentifier) {
476 case 'value':
Brandon Goddard020ed5e2019-10-16 20:53:17477 cell.removeChildren();
478 const objectPropSection = ObjectUI.ObjectPropertiesSection.defaultObjectPropertiesSection(
479 value, undefined /* linkifier */, true /* skipProto */, true /* readOnly */);
480 cell.appendChild(objectPropSection.element);
481 this.valueObjectPresentation = objectPropSection;
482 break;
Blink Reformat4c46d092018-04-07 15:32:37483 case 'key':
484 case 'primaryKey':
485 cell.removeChildren();
Brandon Goddard020ed5e2019-10-16 20:53:17486 const objectElement = ObjectUI.ObjectPropertiesSection.defaultObjectPresentation(
487 value, undefined /* linkifier */, true /* skipProto */, true /* readOnly */);
Blink Reformat4c46d092018-04-07 15:32:37488 cell.appendChild(objectElement);
489 break;
490 default:
491 }
492
493 return cell;
494 }
495};