This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
background.js
408 lines (353 loc) · 13.8 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
'use strict';
const Qs = require('qs');
const Chrometools = require('./chrometools.js');
const Gm = require('./googlemusic.js');
const License = require('./license.js');
const Storage = require('./storage.js');
const Trackcache = require('./trackcache.js');
const Context = require('./context.js');
const Reporting = require('./reporting.js');
// {userId: {userIndex: int, tabId: int, xt: string}}
const users = {};
// {userId: <lovefield db>}
const dbs = {};
// {userId: <bool>}
const dbIsInit = {};
// {playlistId: <bool>}, locks playlists during some updates
const playlistIsUpdating = {};
function userIdForTabId(tabId) {
for (const userId in users) {
if (users[userId].tabId === tabId) {
return userId;
}
}
}
function timestampKey(userId) {
return `lastPoll-${userId}`;
}
function setPollTimestamp(userId, timestamp) {
const storageItems = {};
storageItems[timestampKey(userId)] = timestamp;
chrome.storage.local.set(storageItems, Chrometools.unlessError(() => {
console.log('set poll for', userId, timestamp);
}));
}
function getPollTimestamp(userId, callback) {
const key = timestampKey(userId);
chrome.storage.local.get(key, Chrometools.unlessError(items => {
const timestamp = items[key];
console.log('get poll for', userId, timestamp);
callback(timestamp);
}));
}
function diffUpdateLibrary(userId, timestamp, callback) {
// Update our cache with any changes since our last poll.
// Callback an object with success = true if we were able to retrieve changes.
const user = users[userId];
Gm.getTrackChanges(user, timestamp, changes => {
if (!changes.success && changes.reloadXsrf) {
console.info('request xsrf reload');
chrome.tabs.sendMessage(user.tabId, {action: 'getXsrf'});
return callback({success: false});
}
Trackcache.upsertTracks(dbs[userId], userId, changes.upsertedTracks, () => {
console.log('done with diff upsert of', changes.upsertedTracks.length);
Trackcache.deleteTracks(dbs[userId], userId, changes.deletedIds, () => {
console.log('done with diff delete of', changes.deletedIds.length);
dbIsInit[userId] = true;
if (changes.newTimestamp) {
setPollTimestamp(userId, changes.newTimestamp);
}
callback({success: true});
});
});
});
}
function syncPlaylist(playlist, attempt) {
// Make Google's playlist match the given one.
const user = users[playlist.userId];
const _attempt = attempt || 0;
console.log('syncPlaylist, attempt', _attempt);
if (!('remoteId' in playlist)) {
// Create a remote playlist.
Gm.createRemotePlaylist(user, playlist.title, remoteId => {
console.log('created remote playlist', remoteId);
const playlistToSave = JSON.parse(JSON.stringify(playlist));
playlistToSave.remoteId = remoteId;
Storage.savePlaylist(playlistToSave, () => {
// nothing else to do. listener will see the change and recall.
console.log('wrote remote id');
});
});
} else {
// refresh tracks and write out playlist
const db = dbs[playlist.userId];
Trackcache.queryTracks(db, playlist, tracks => {
if (tracks === null) {
Reporting.reportSync('failure', 'failed-query');
return;
}
console.log('lock', playlist.title);
playlistIsUpdating[playlist.remoteId] = true;
console.log(playlist.title, 'found', tracks.length);
if (tracks.length > 0) {
console.log('first is', tracks[0]);
}
if (tracks.length > 1000) {
console.warn('attempting to sync over 1000 tracks; only first 1k will sync');
}
const desiredTracks = tracks.slice(0, 1000);
Gm.setPlaylistContents(db, user, playlist.remoteId, desiredTracks, response => {
if (response !== null) {
// large updates seem to only apply partway sometimes.
// retrying like this seems to make even 1k playlists eventually consistent.
if (_attempt < 2) {
Reporting.reportSync('retry', `retry-${_attempt}`);
console.log('not a 0-track add; retrying syncPlaylist', response);
setTimeout(syncPlaylist, 10000 * (_attempt + 1), playlist, _attempt + 1);
} else {
Reporting.reportSync('failure', 'gave-up');
console.warn('giving up on syncPlaylist!', response);
/*
// doesn't give much information beyond what GA tracks.
Reporting.Raven.captureMessage('gave up on syncing', {
level: 'warning',
tags: {playlistId: playlist.remoteId},
extra: {playlist},
});
*/
// Never has the need for promises been so clear.
Gm.setPlaylistOrder(db, user, playlist, orderResponse => {
console.log('reorder response', orderResponse);
console.log('unlock', playlist.title);
playlistIsUpdating[playlist.remoteId] = false;
}, err => {
Reporting.reportSync('failure', 'failed-reorder');
console.error('failed to reorder playlist', playlist.title, err);
Reporting.Raven.captureException(err, {
tags: {playlistId: playlist.remoteId},
extra: {playlist},
});
console.log('unlock', playlist.title);
playlistIsUpdating[playlist.remoteId] = false;
});
}
} else {
Gm.setPlaylistOrder(db, user, playlist, orderResponse => {
Reporting.reportSync('success', `success-${_attempt}`);
console.log('reorder response', orderResponse);
console.log('unlock', playlist.title);
playlistIsUpdating[playlist.remoteId] = false;
}, err => {
Reporting.reportSync('failure', 'failed-reorder');
console.error('failed to reorder playlist', playlist.title, err);
Reporting.Raven.captureException(err, {
tags: {playlistId: playlist.remoteId},
extra: {playlist},
});
console.log('unlock', playlist.title);
playlistIsUpdating[playlist.remoteId] = false;
});
}
}, err => {
Reporting.reportSync('failure', 'failed-set');
console.error('failed to sync playlist', playlist.title, err);
Reporting.Raven.captureException(err, {
tags: {playlistId: playlist.remoteId},
extra: {playlist},
});
console.log('unlock', playlist.title);
playlistIsUpdating[playlist.remoteId] = false;
});
});
}
}
function renameAndSync(playlist) {
console.log('renaming to', playlist.title);
Gm.updatePlaylist(users[playlist.userId], playlist.remoteId, playlist.title, playlist, () => {
syncPlaylist(playlist);
});
}
function forceUpdate(userId) {
getPollTimestamp(userId, timestamp => {
if (!(dbIsInit[userId])) {
console.warn('refusing forceUpdate because db is not init');
// If we've just started up, this error isn't a big deal.
// If we've been running for a while, it is: means we've never been able to pull the library.
Reporting.Raven.captureMessage('refusing forceUpdate because db is not init', {
level: 'warning',
extra: {timestamp, users, dbIsInit},
});
return;
} else if (!timestamp) {
console.warn('db was init, but no timestamp found');
Reporting.Raven.captureMessage('db was init, but no timestamp found', {
level: 'warning',
});
return;
}
diffUpdateLibrary(userId, timestamp, response => {
if (response.success) {
License.hasFullVersion(false, hasFullVersion => {
Storage.getPlaylistsForUser(userId, playlists => {
for (let i = 0; i < playlists.length; i++) {
if (i > 0 && !hasFullVersion) {
console.log('skipping sync of locked playlist', playlists[i].title);
continue;
}
// This locking prevents two things:
// * slow periodic syncs from stepping on later periodic syncs
// * periodic syncs from stepping on manual syncs
// which is why it's done at this level (and not around eg syncPlaylist).
if (playlistIsUpdating[playlists[i].remoteId]) {
console.warn('skipping forceUpdate since playlist is being updated:', playlists[i].title);
} else {
renameAndSync(playlists[i]);
}
}
});
});
}
});
});
}
function periodicUpdate() {
for (const userId in users) {
console.log('periodic update for', userId);
forceUpdate(userId);
}
}
function initLibrary(userId) {
// Initialize our cache from Google's indexeddb, or fall back to a differential update from time 0.
const message = {action: 'getLocalTracks', userId};
chrome.tabs.sendMessage(users[userId].tabId, message, Chrometools.unlessError(response => {
if (response.tracks === null || response.tracks.length === 0) {
console.warn('local idb not available; falling back to diffUpdate(0)');
diffUpdateLibrary(userId, 0, diffResponse => {
if (diffResponse.success) {
forceUpdate(userId);
}
});
} else {
console.log('got tracks', response.tracks.length);
Trackcache.upsertTracks(dbs[userId], userId, response.tracks, () => {
dbIsInit[userId] = true;
// we don't want to set the poll timestamp here since
// the indexeddb is only written on page load.
console.log('found', response.tracks.length, 'tracks locally. syncing now.');
forceUpdate(userId);
});
}
}));
}
function main() {
Storage.addPlaylistChangeListener(change => {
const hasOld = 'oldValue' in change;
const hasNew = 'newValue' in change;
if (hasOld && !hasNew) {
// deletion
Gm.deleteRemotePlaylist(users[change.oldValue.userId], change.oldValue.remoteId, () => null);
} else if (hasOld && hasNew) {
// update
if (change.oldValue.title !== change.newValue.title) {
renameAndSync(change.newValue);
} else {
syncPlaylist(change.newValue);
}
} else {
// creation
syncPlaylist(change.newValue);
}
});
// Update periodically.
Storage.getSyncMs(initSyncMs => {
console.info('sync interval initially', initSyncMs);
let syncIntervalId = null;
if (initSyncMs >= 60 * 1000) {
syncIntervalId = setInterval(periodicUpdate, initSyncMs);
}
Storage.addSyncMsChangeListener(change => {
const hasNew = 'newValue' in change;
if (!hasNew) {
return;
}
const syncMs = change.newValue;
console.info('sync interval changing to', syncMs);
if (syncIntervalId !== null) {
clearInterval(syncIntervalId);
}
syncIntervalId = null;
if (syncMs >= 60 * 1000) {
syncIntervalId = setInterval(periodicUpdate, syncMs);
}
});
});
chrome.pageAction.onClicked.addListener(tab => {
const managerUrl = chrome.extension.getURL('html/playlists.html');
const qstring = Qs.stringify({userId: userIdForTabId(tab.id)});
Chrometools.focusOrCreateExtensionTab(`${managerUrl}?${qstring}`);
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// respond to manager / content script requests.
if (request.action === 'forceUpdate') {
forceUpdate(request.userId);
} else if (request.action === 'setXsrf') {
console.info('updating xt:', request);
users[request.userId].xt = request.xt;
forceUpdate(request.userId);
} else if (request.action === 'showPageAction') {
if (!(request.userId)) {
console.warn('received falsey user id from page action');
Reporting.Raven.captureMessage('received falsey user id from page action', {
level: 'warning',
extra: {user_id: request.userId},
});
return false;
}
if (!(request.userId in dbs)) {
// init the db.
Trackcache.openDb(request.userId, db => {
// TODO there's a race condition here between poll timestamp reads and
// writes if users manage to forceUpdate before this write finishes.
// that seems super unlikely so i haven't addressed it yet.
setPollTimestamp(request.userId, new Date().getTime() * 1000);
console.log('opened');
dbs[request.userId] = db;
// indexeddb is super slow.
// much faster to use memory store and refresh on load.
initLibrary(request.userId);
});
}
// In the case that an existing tab/index was changed to a new user,
// remove the old entry.
for (const userId in users) {
if (users[userId].tabId === sender.tab.id ||
users[userId].userIndex === request.userIndex) {
delete users[userId];
}
}
users[request.userId] = {userIndex: request.userIndex, tabId: sender.tab.id, xt: request.xt};
console.log('see user', request.userId, users);
License.hasFullVersion(false, hasFullVersion => {console.log('precached license status:', hasFullVersion);});
// FIXME store this in sync storage and include it in context?
// That'd mean we wouldn't get it immediately, though, so maybe this is better.
Reporting.Raven.setTagsContext({tier: request.tier});
chrome.pageAction.show(sender.tab.id);
} else if (request.action === 'query') {
Trackcache.queryTracks(dbs[request.playlist.userId], request.playlist, tracks => {
sendResponse({tracks});
});
return true; // wait for async response
} else if (request.action === 'getContext') {
Context.get(sendResponse);
return true;
} else {
console.warn('received unknown request', request);
Reporting.Raven.captureMessage('received unknown request', {
level: 'warning',
extra: {request},
});
}
});
}
Storage.handleMigrations(main);