Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: chrome/test/data/webrtc/indexeddb.js

Issue 1962673002: WebRtcBrowserTest prep-CL for IndexedDB cloning of RTCCertificate. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments (promises, internals, ok-derp str equality) Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 };
36 reqOpen.onsuccess = function() {
37 if (gDatabase !== null)
38 failTest('The database is already open.');
39 gDatabase = reqOpen.result;
40 returnToTest('ok-database-opened');
41 }
42 reqOpen.onerror = function() {
43 failTest('The database could not be opened. Error: ' + reqOpen.error);
44 }
45 }
46
47 function closeDatabase() {
48 if (gDatabase === null)
49 throw failTest('The database is already closed.');
50 gDatabase.close();
51 gDatabase = null;
52 returnToTest('ok-database-closed');
53 }
54
55 function deleteDatabase() {
56 if (gDatabase !== null)
57 throw failTest('The database should be closed before deleting.');
58 var reqDelete = indexedDB.deleteDatabase(kDatabaseName);
59 reqDelete.onsuccess = function () {
60 returnToTest('ok-database-deleted');
61 };
62 reqDelete.onerror = function () {
63 failTest('The database could not be deleted. Error: ' + reqDelete.error);
64 };
65 }
66
67 /**
68 * Generates a certificate and clones it by saving and loading it to the
69 * database (requires database to be open, see |openDatabase|). After returning
70 * successfully to the test, the global variables |gCertificate| and
71 * |gCertificateClone| have been set.
72 * @param {!Object} keygenAlgorithm An |AlgorithmIdentifier| to be used as
73 * parameter to |RTCPeerConnection.generateCertificate|. The resulting
74 * certificate will be used by the peer connection.
75 */
76 function generateAndCloneCertificate(keygenAlgorithm) {
77 RTCPeerConnection.generateCertificate(keygenAlgorithm).then(
78 function(certificate) {
79 gCertificate = certificate;
80 cloneCertificate_(gCertificate).then(
81 function(clone) {
82 gCertificateClone = clone;
83 // TODO(hbos): Verify that |gCertificate| is value-equal to
84 // |gCertificateClone|. crbug.com/609108
85 returnToTest('ok-generated-and-cloned');
86 },
87 function() {
88 failTest('Error cloning certificate.');
89 });
90 },
91 function() {
92 failTest('Certificate generation failed. keygenAlgorithm: ' +
93 JSON.stringify(keygenAlgorithm));
94 });
95 }
96
97 // Internals.
98
99 /** @private */
100 function saveCertificate_(certificate) {
101 return new Promise(function(resolve, reject) {
102 if (gDatabase === null)
phoglund_chromium 2016/05/10 08:10:16 Nit: indent 2?
hbos_chromium 2016/05/10 12:17:27 Done.
103 throw failTest('The database is not open.');
104
105 var certTrans = gDatabase.transaction('certificates', 'readwrite');
106 var certStore = certTrans.objectStore('certificates');
107 var certPut = certStore.put({
108 id:0,
109 cert:certificate
110 });
111
112 certPut.onsuccess = function() {
113 resolve();
114 };
115 certPut.onerror = function() {
116 reject(certPut.error);
117 };
118 });
119 }
120
121 /** @private */
122 function loadCertificate_() {
123 return new Promise(function(resolve, reject) {
124 if (gDatabase === null)
125 throw failTest('The database is not open.');
126
127 var certTrans = gDatabase.transaction('certificates', 'readonly');
128 var certStore = certTrans.objectStore('certificates');
129
130 var reqGet = certStore.get(0);
131 reqGet.onsuccess = function() {
132 var match = reqGet.result;
133 if (match !== undefined) {
134 resolve(match.cert);
135 } else {
136 resolve(null);
137 }
138 };
139 reqGet.onerror = function() {
140 reject(reqGet.error);
141 };
142 });
143 }
144
145 /** @private */
146 function cloneCertificate_(certificate) {
jsbell 2016/05/09 18:35:33 Since all of the functions this calls return promi
hbos_chromium 2016/05/10 06:37:59 Nice. I'll do that before landing.
147 return new Promise(function(resolve, reject) {
148 saveCertificate_(certificate).then(
149 function() {
150 // Certificate saved successfully, now load it.
151 loadCertificate_().then(
152 function(clone) {
153 // Load successful.
154 if (clone === null)
155 failTest('loadCertificate returned a null certificate.');
156 resolve(clone);
157 },
158 reject);
159 },
160 reject);
161 });
162 }
OLDNEW
« no previous file with comments | « chrome/browser/media/webrtc_browsertest_base.cc ('k') | chrome/test/data/webrtc/peerconnection.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698