Skip to content

Commit dbe5fff

Browse files
Matt Gauntjshcrowthe
Matt Gaunt
authored andcommitted
Messaging - IDB Cleanup (#523)
* Goes through and deletes old IDB entries * [AUTOMATED]: Prettier Code Styling * Fixing tests with db name differences * Altering some logic / logs
1 parent e784110 commit dbe5fff

11 files changed

+119
-33
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* There seems to have been a bug in the messaging SDK versions <= 4.9.x
19+
* where the IndexedDB model was using a database name of 'undefined'.
20+
*
21+
* In 4.10.x we changed the model implementation, but kept the database
22+
* name as it should have been. This however introduced an issue where
23+
* two tokens were pointing to the same underlying PushSubscription.
24+
*
25+
* This code will look for the undefined database and delete any of the
26+
* underlying tokens.
27+
*/
28+
29+
import IIDModel from '../models/iid-model';
30+
31+
const OLD_DB_NAME = 'undefined';
32+
const OLD_OBJECT_STORE_NAME = 'fcm_token_object_Store';
33+
34+
function handleDb(db: IDBDatabase) {
35+
if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {
36+
// We found a database with the name 'undefined', but our expected object
37+
// store isn't defined.
38+
return;
39+
}
40+
41+
const transaction = db.transaction(OLD_OBJECT_STORE_NAME);
42+
const objectStore = transaction.objectStore(OLD_OBJECT_STORE_NAME);
43+
44+
const iidModel = new IIDModel();
45+
46+
const openCursorRequest: IDBRequest = objectStore.openCursor();
47+
openCursorRequest.onerror = event => {
48+
// NOOP - Nothing we can do.
49+
console.warn('Unable to cleanup old IDB.', event);
50+
};
51+
52+
openCursorRequest.onsuccess = () => {
53+
const cursor = openCursorRequest.result;
54+
if (cursor) {
55+
// cursor.value contains the current record being iterated through
56+
// this is where you'd do something with the result
57+
const tokenDetails = cursor.value;
58+
59+
iidModel.deleteToken(
60+
tokenDetails.fcmSenderId,
61+
tokenDetails.fcmToken,
62+
tokenDetails.fcmPushSet
63+
);
64+
65+
cursor.continue();
66+
} else {
67+
db.close();
68+
indexedDB.deleteDatabase(OLD_DB_NAME);
69+
}
70+
};
71+
}
72+
73+
function cleanV1() {
74+
const request: IDBOpenDBRequest = indexedDB.open(OLD_DB_NAME);
75+
request.onerror = function(event) {
76+
// NOOP - Nothing we can do.
77+
};
78+
request.onsuccess = function(event) {
79+
const db = request.result;
80+
handleDb(db);
81+
};
82+
}
83+
84+
export { cleanV1 };

packages/messaging/src/models/db-interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default class DBInterface {
5959
request.onupgradeneeded = event => {
6060
try {
6161
var db = (<IDBRequest>event.target).result;
62-
this.onDBUpgrade(db);
62+
this.onDBUpgrade(db, event);
6363
} catch (err) {
6464
// close the database as it can't be used.
6565
db.close();
@@ -90,7 +90,7 @@ export default class DBInterface {
9090
* @protected
9191
* @param {!IDBDatabase} db
9292
*/
93-
onDBUpgrade(db) {
93+
onDBUpgrade(db: IDBDatabase, event: IDBVersionChangeEvent) {
9494
throw this.errorFactory_.create(Errors.codes.SHOULD_BE_INHERITED);
9595
}
9696
}

packages/messaging/src/models/token-details-model.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import DBInterface from './db-interface';
1919
import Errors from './errors';
2020
import arrayBufferToBase64 from '../helpers/array-buffer-to-base64';
21+
import { cleanV1 } from './clean-v1-undefined';
2122

2223
const FCM_TOKEN_OBJ_STORE = 'fcm_token_object_Store';
23-
const DB_VERSION = 1;
24+
const DB_NAME = 'fcm_token_details_db';
25+
const DB_VERSION = 2;
2426

2527
/** @record */
2628
function ValidateInput() {}
@@ -39,29 +41,32 @@ ValidateInput.prototype.fcmPushSet;
3941

4042
export default class TokenDetailsModel extends DBInterface {
4143
constructor() {
42-
super(TokenDetailsModel.DB_NAME, DB_VERSION);
44+
super(DB_NAME, DB_VERSION);
4345
}
4446

45-
static get DB_NAME() {
46-
return 'fcm_token_details_db';
47-
}
47+
onDBUpgrade(db: IDBDatabase, evt: IDBVersionChangeEvent) {
48+
if (evt.oldVersion < 1) {
49+
// New IDB instance
50+
var objectStore = db.createObjectStore(FCM_TOKEN_OBJ_STORE, {
51+
keyPath: 'swScope'
52+
});
4853

49-
/**
50-
* @override
51-
*/
52-
onDBUpgrade(db) {
53-
var objectStore = db.createObjectStore(FCM_TOKEN_OBJ_STORE, {
54-
keyPath: 'swScope'
55-
});
54+
// Make sure the sender ID can be searched
55+
objectStore.createIndex('fcmSenderId', 'fcmSenderId', {
56+
unique: false
57+
});
5658

57-
// Make sure the sender ID can be searched
58-
objectStore.createIndex('fcmSenderId', 'fcmSenderId', {
59-
unique: false
60-
});
59+
objectStore.createIndex('fcmToken', 'fcmToken', {
60+
unique: true
61+
});
62+
}
6163

62-
objectStore.createIndex('fcmToken', 'fcmToken', {
63-
unique: true
64-
});
64+
if (evt.oldVersion < 2) {
65+
// Prior to version 2, we were using either 'fcm_token_details_db'
66+
// or 'undefined' as the database name due to bug in the SDK
67+
// So remove the old tokens and databases.
68+
cleanV1();
69+
}
6570
}
6671

6772
/**

packages/messaging/src/models/vapid-details-model.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ import DBInterface from './db-interface';
1919
import Errors from './errors';
2020

2121
const FCM_VAPID_OBJ_STORE = 'fcm_vapid_object_Store';
22+
const DB_NAME = 'fcm_vapid_details_db';
2223
const DB_VERSION = 1;
2324
const UNCOMPRESSED_PUBLIC_KEY_SIZE = 65;
2425

2526
export default class VapidDetailsModel extends DBInterface {
2627
constructor() {
27-
super(VapidDetailsModel.DB_NAME, DB_VERSION);
28-
}
29-
30-
static get DB_NAME() {
31-
return 'fcm_vapid_details_db';
28+
super(DB_NAME, DB_VERSION);
3229
}
3330

3431
/**

packages/messaging/test/controller-delete-token.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('Firebase Messaging > *Controller.deleteToken()', function() {
7171
deletePromises.push(globalMessagingService.delete());
7272
}
7373
return Promise.all(deletePromises)
74-
.then(() => deleteDatabase(TokenDetailsModel.DB_NAME))
74+
.then(() => deleteDatabase('fcm_token_details_db'))
7575
.then(() => (globalMessagingService = null));
7676
};
7777

packages/messaging/test/tokenDetailsModel-deleteToken.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('Firebase Messaging > TokenDetailsModel.deleteToken()', function() {
4646
}
4747

4848
return Promise.all(promises)
49-
.then(() => deleteDatabase(TokenDetailsModel.DB_NAME))
49+
.then(() => deleteDatabase('fcm_token_details_db'))
5050
.then(() => (globalTokenModel = null));
5151
};
5252

packages/messaging/test/tokenDetailsModel-getToken.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('Firebase Messaging > TokenDetailsModel.getTokenDetailsFromToken()', fu
4545
}
4646

4747
return Promise.all(promises)
48-
.then(() => deleteDatabase(TokenDetailsModel.DB_NAME))
48+
.then(() => deleteDatabase('fcm_token_details_db'))
4949
.then(() => (globalTokenModel = null));
5050
};
5151

packages/messaging/test/tokenDetailsModel-saveToken.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('Firebase Messaging > TokenDetailsModel.saveToken()', function() {
4242
}
4343

4444
return Promise.all(promises)
45-
.then(() => deleteDatabase(TokenDetailsModel.DB_NAME))
45+
.then(() => deleteDatabase('fcm_token_details_db'))
4646
.then(() => (globalTokenModel = null));
4747
};
4848

packages/messaging/test/vapid-details-model-delete.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('Firebase Messaging > VapidDetailsModel.deleteToken()', function() {
3535
}
3636

3737
return promiseChain
38-
.then(() => deleteDatabase(VapidDetailsModel.DB_NAME))
38+
.then(() => deleteDatabase('fcm_vapid_details_db'))
3939
.then(() => (vapidModel = null));
4040
};
4141

packages/messaging/test/vapid-details-model-get.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('Firebase Messaging > VapidDetailsModel.getVapidFromSWScope()', functio
3535
}
3636

3737
return promiseChain
38-
.then(() => deleteDatabase(VapidDetailsModel.DB_NAME))
38+
.then(() => deleteDatabase('fcm_vapid_details_db'))
3939
.then(() => (vapidModel = null));
4040
};
4141

packages/messaging/test/vapid-details-model-save.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('Firebase Messaging > VapidDetailsModel.saveVapidDetails()', function()
3535
}
3636

3737
return promiseChain
38-
.then(() => deleteDatabase(VapidDetailsModel.DB_NAME))
38+
.then(() => deleteDatabase('fcm_vapid_details_db'))
3939
.then(() => (vapidModel = null));
4040
};
4141

0 commit comments

Comments
 (0)