Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /** | |
| 2 * Copyright 2016 The Chromium Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can be | |
| 4 * found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 /** @private */ | |
| 8 var kDatabaseName = 'WebRTC-Database'; | |
| 9 | |
| 10 /** | |
| 11 * The one and only |IDBDatabase| in this page. | |
| 12 * @private | |
| 13 */ | |
| 14 var gDatabase = null; | |
| 15 | |
| 16 /** | |
| 17 * Set by |generateAndCloneCertificate|. | |
| 18 */ | |
| 19 var gCertificate = null; | |
| 20 var gCertificateClone = null; | |
| 21 | |
| 22 // Public interface to tests. These are expected to be called with | |
| 23 // ExecuteJavascript invocations from the browser tests and will return answers | |
| 24 // through the DOM automation controller. | |
| 25 | |
| 26 function openDatabase() { | |
| 27 if (gDatabase !== null) | |
| 28 throw failTest('The database is already open.'); | |
| 29 var reqOpen = indexedDB.open(kDatabaseName); | |
| 30 reqOpen.onupgradeneeded = function() { | |
| 31 // This happens before |onsuccess| if the database is new or its version is | |
| 32 // updated. Create object stores. | |
| 33 var db = reqOpen.result; | |
| 34 var certStore = db.createObjectStore('certificates', { keyPath: 'id' }); | |
| 35 certStore.createIndex('by_id', 'id'); | |
|
jsbell
2016/05/09 16:42:39
The store is already keyed by the id property - wh
hbos_chromium
2016/05/09 18:23:32
Ah, no need. Done.
| |
| 36 }; | |
| 37 reqOpen.onsuccess = function() { | |
| 38 if (gDatabase !== null) | |
| 39 throw failTest('The database is already open.'); | |
| 40 gDatabase = reqOpen.result; | |
| 41 returnToTest('ok-database-opened'); | |
| 42 } | |
| 43 reqOpen.onerror = function() { | |
| 44 throw failTest('The database could not be opened. Error: ' + reqOpen.error); | |
|
jsbell
2016/05/09 16:42:38
`throw` won't do anything here (exception thrown i
hbos_chromium
2016/05/09 18:23:32
Done.
| |
| 45 } | |
| 46 } | |
| 47 | |
| 48 function closeDatabase() { | |
| 49 if (gDatabase === null) | |
| 50 throw failTest('The database is already closed.'); | |
| 51 gDatabase.close(); | |
| 52 gDatabase = null; | |
| 53 returnToTest('ok-database-closed'); | |
| 54 } | |
| 55 | |
| 56 function deleteDatabase() { | |
| 57 if (gDatabase !== null) | |
| 58 throw failTest('The database should be closed before deleting.'); | |
| 59 var reqDelete = indexedDB.deleteDatabase(kDatabaseName); | |
| 60 reqDelete.onsuccess = function () { | |
| 61 returnToTest('ok-database-deleted'); | |
| 62 }; | |
| 63 reqDelete.onerror = function () { | |
| 64 throw failTest( | |
|
jsbell
2016/05/09 16:42:39
Ditto
hbos_chromium
2016/05/09 18:23:32
Done.
| |
| 65 'The database could not be deleted. Error: ' + reqDelete.error); | |
| 66 }; | |
| 67 } | |
| 68 | |
| 69 /** | |
| 70 * Generates a certificate and clones it by saving and loading it to the | |
| 71 * database (requires database to be open, see |openDatabase|). After returning | |
| 72 * successfully to the test, the global variables |gCertificate| and | |
| 73 * |gCertificateClone| have been set. | |
| 74 * @param {!Object} keygenAlgorithm An |AlgorithmIdentifier| to be used as | |
| 75 * parameter to |RTCPeerConnection.generateCertificate|. The resulting | |
| 76 * certificate will be used by the peer connection. | |
| 77 */ | |
| 78 function generateAndCloneCertificate(keygenAlgorithm) { | |
| 79 RTCPeerConnection.generateCertificate(keygenAlgorithm).then( | |
| 80 function(certificate) { | |
| 81 gCertificate = certificate; | |
| 82 cloneCertificate(gCertificate, | |
| 83 function(clone) { | |
| 84 gCertificateClone = clone; | |
| 85 // TODO(hbos): Verify that |gCertificate| is value-equal to | |
| 86 // |gCertificateClone|. crbug.com/609108 | |
| 87 returnToTest('ok-generated-and-cloned'); | |
| 88 }, | |
| 89 function() { | |
| 90 throw failTest('Error cloning certificate.'); | |
| 91 }); | |
| 92 }, | |
| 93 function() { | |
| 94 failTest('Certificate generation failed. keygenAlgorithm: ' + | |
| 95 JSON.stringify(keygenAlgorithm)); | |
| 96 }); | |
| 97 } | |
| 98 | |
| 99 // Public helper functions to test functions. Expected to indirectly be called | |
|
phoglund_chromium
2016/05/09 14:59:57
You mean these are only called from the functions
hbos_chromium
2016/05/09 18:23:32
Done.
| |
| 100 // with ExecuteJavascript from browser tests, but does not do |returnToTest|. | |
| 101 | |
| 102 function saveCertificate(certificate, onsuccess, onerror) { | |
|
jsbell
2016/05/09 16:42:39
Code might be more readable returning a Promise ra
hbos_chromium
2016/05/09 18:23:32
Makes sense, changed to promises.
| |
| 103 if (gDatabase === null) | |
| 104 throw failTest('The database is not open.'); | |
| 105 | |
| 106 var certTrans = gDatabase.transaction('certificates', 'readwrite'); | |
| 107 var certStore = certTrans.objectStore('certificates'); | |
| 108 var certPut = certStore.put({ | |
| 109 id:0, | |
| 110 cert:certificate | |
| 111 }); | |
| 112 | |
| 113 certPut.onsuccess = function() { | |
| 114 onsuccess(); | |
| 115 }; | |
| 116 certPut.onerror = function() { | |
| 117 onerror(certPut.error); | |
| 118 }; | |
| 119 } | |
| 120 | |
| 121 function loadCertificate(onsuccess, onerror) { | |
| 122 if (gDatabase === null) | |
| 123 throw failTest('The database is not open.'); | |
| 124 | |
| 125 var certTrans = gDatabase.transaction('certificates', 'readonly'); | |
| 126 var certStore = certTrans.objectStore('certificates'); | |
| 127 var certById = certStore.index('by_id'); | |
| 128 | |
| 129 var reqGet = certById.get(0); | |
|
jsbell
2016/05/09 16:42:38
Could just use certStore.get(0) - no need for the
hbos_chromium
2016/05/09 18:23:32
Done.
| |
| 130 reqGet.onsuccess = function() { | |
| 131 var match = reqGet.result; | |
| 132 if (match !== undefined) { | |
| 133 onsuccess(match.cert); | |
| 134 } else { | |
| 135 onsuccess(null); | |
| 136 } | |
| 137 }; | |
| 138 reqGet.onerror = function() { | |
| 139 onerror(reqGet.error); | |
| 140 }; | |
| 141 } | |
| 142 | |
| 143 function cloneCertificate(certificate, onsuccess, onerror) { | |
| 144 saveCertificate(certificate, | |
| 145 function() { | |
| 146 // Certificate saved successfully, now load it. | |
| 147 loadCertificate( | |
| 148 function(clone) { | |
| 149 // Load successful. | |
| 150 if (clone === null) | |
| 151 throw failTest('loadCertificate returned a null certificate.'); | |
| 152 onsuccess(clone); | |
| 153 }, | |
| 154 onerror); | |
| 155 }, | |
| 156 onerror); | |
| 157 } | |
| OLD | NEW |