|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +// This module handles storage of uploaded profile information in IndexedDB |
| 6 | +// for the about:logging page. |
| 7 | + |
| 8 | +const DB_NAME = "aboutLoggingProfiles"; |
| 9 | +const DB_VERSION = 1; |
| 10 | +const STORE_NAME = "uploadedProfiles"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Initialize the IndexedDB database for storing uploaded profile information. |
| 14 | + * @returns {Promise<IDBDatabase>} |
| 15 | + */ |
| 16 | +function initDB() { |
| 17 | + return new Promise((resolve, reject) => { |
| 18 | + const request = indexedDB.open(DB_NAME, DB_VERSION); |
| 19 | + |
| 20 | + request.onerror = () => reject(request.error); |
| 21 | + request.onsuccess = () => resolve(request.result); |
| 22 | + |
| 23 | + request.onupgradeneeded = e => { |
| 24 | + const db = request.result; |
| 25 | + db.onerror = reject; |
| 26 | + |
| 27 | + if (e.oldVersion < 1) { |
| 28 | + // Version 1: this is the first version of the DB. |
| 29 | + const store = db.createObjectStore(STORE_NAME, { |
| 30 | + keyPath: "id", |
| 31 | + autoIncrement: true, |
| 32 | + }); |
| 33 | + store.createIndex("uploadDate", "uploadDate", { unique: false }); |
| 34 | + store.createIndex("profileToken", "profileToken", { unique: true }); |
| 35 | + } |
| 36 | + }; |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Save uploaded profile information to IndexedDB. |
| 42 | + * @param {Object} profileInfo |
| 43 | + * @param {string} profileInfo.jwtToken - The JWT token returned by the server |
| 44 | + * @param {string} profileInfo.profileToken - The profile token extracted from JWT |
| 45 | + * @param {string} profileInfo.profileUrl - The full profile URL |
| 46 | + * @param {Date} profileInfo.uploadDate - When the profile was uploaded |
| 47 | + * @param {string} profileInfo.profileName - A descriptive name for the profile |
| 48 | + * @returns {Promise<number>} The ID of the stored profile |
| 49 | + */ |
| 50 | +export async function saveUploadedProfile(profileInfo) { |
| 51 | + const db = await initDB(); |
| 52 | + |
| 53 | + return new Promise((resolve, reject) => { |
| 54 | + const transaction = db.transaction([STORE_NAME], "readwrite"); |
| 55 | + const store = transaction.objectStore(STORE_NAME); |
| 56 | + |
| 57 | + const request = store.add({ |
| 58 | + jwtToken: profileInfo.jwtToken, |
| 59 | + profileToken: profileInfo.profileToken, |
| 60 | + profileUrl: profileInfo.profileUrl, |
| 61 | + uploadDate: profileInfo.uploadDate, |
| 62 | + profileName: profileInfo.profileName, |
| 63 | + }); |
| 64 | + |
| 65 | + request.onerror = () => reject(request.error); |
| 66 | + request.onsuccess = () => resolve(request.result); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Get all uploaded profiles from IndexedDB. |
| 72 | + * @returns {Promise<Array>} Array of uploaded profile information |
| 73 | + */ |
| 74 | +export async function getAllUploadedProfiles() { |
| 75 | + const db = await initDB(); |
| 76 | + |
| 77 | + return new Promise((resolve, reject) => { |
| 78 | + const transaction = db.transaction([STORE_NAME], "readonly"); |
| 79 | + const store = transaction.objectStore(STORE_NAME); |
| 80 | + const index = store.index("uploadDate"); |
| 81 | + |
| 82 | + const request = index.getAll(); |
| 83 | + |
| 84 | + request.onerror = () => reject(request.error); |
| 85 | + request.onsuccess = () => { |
| 86 | + // Sort the list with the most recent uploaded profile first. |
| 87 | + resolve(request.result.reverse()); |
| 88 | + }; |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Delete an uploaded profile from IndexedDB. |
| 94 | + * @param {number} profileId - The ID of the profile to delete |
| 95 | + * @returns {Promise<void>} |
| 96 | + */ |
| 97 | +export async function deleteUploadedProfile(profileId) { |
| 98 | + const db = await initDB(); |
| 99 | + |
| 100 | + return new Promise((resolve, reject) => { |
| 101 | + const transaction = db.transaction([STORE_NAME], "readwrite"); |
| 102 | + const store = transaction.objectStore(STORE_NAME); |
| 103 | + |
| 104 | + const request = store.delete(profileId); |
| 105 | + |
| 106 | + request.onerror = () => reject(request.error); |
| 107 | + request.onsuccess = () => resolve(); |
| 108 | + }); |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Get a specific uploaded profile by ID. |
| 113 | + * @param {number} profileId - The ID of the profile to retrieve |
| 114 | + * @returns {Promise<Object|null>} The profile information or null if not found |
| 115 | + */ |
| 116 | +export async function getUploadedProfile(profileId) { |
| 117 | + const db = await initDB(); |
| 118 | + |
| 119 | + return new Promise((resolve, reject) => { |
| 120 | + const transaction = db.transaction([STORE_NAME], "readonly"); |
| 121 | + const store = transaction.objectStore(STORE_NAME); |
| 122 | + |
| 123 | + const request = store.get(profileId); |
| 124 | + |
| 125 | + request.onerror = () => reject(request.error); |
| 126 | + request.onsuccess = () => resolve(request.result || null); |
| 127 | + }); |
| 128 | +} |
0 commit comments