This repository was archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 969
/
Copy pathsync.js
409 lines (385 loc) · 14 KB
/
sync.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
409
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'
const Immutable = require('immutable')
const electron = require('electron')
const qr = require('qr-image')
const ipcMain = electron.ipcMain
const locale = require('./locale')
const messages = require('../js/constants/sync/messages')
const categories = require('../js/constants/sync/proto').categories
const writeActions = require('../js/constants/sync/proto').actions
const config = require('../js/constants/appConfig').sync
const appActions = require('../js/actions/appActions')
const syncConstants = require('../js/constants/syncConstants')
const appDispatcher = require('../js/dispatcher/appDispatcher')
const AppStore = require('../js/stores/appStore')
const siteUtil = require('../js/state/siteUtil')
const syncUtil = require('../js/state/syncUtil')
const getSetting = require('../js/settings').getSetting
const settings = require('../js/constants/settings')
const CATEGORY_MAP = syncUtil.CATEGORY_MAP
const CATEGORY_NAMES = Object.keys(categories)
const SYNC_ACTIONS = Object.values(syncConstants)
let dispatcherCallback = null
const log = (message) => {
if (!config.debug) { return }
console.log(`sync ${new Date().getTime()}:`, message)
}
const syncEnabled = () => {
return getSetting(settings.SYNC_ENABLED) === true
}
let deviceId = null /** @type {Array|null} */
let pollIntervalId = null
let deviceIdSent = false
let bookmarksToolbarShown = false
/**
* Sends sync records of the same category to the sync server.
* @param {event.sender} sender
* @param {number} action
* @param {Array.<{name: string, value: Object}>} data
*/
const sendSyncRecords = (sender, action, data) => {
if (!deviceId) {
throw new Error('Cannot build a sync record because deviceId is not set')
}
if (!data || !data.length || !data[0]) {
return
}
const category = CATEGORY_MAP[data[0].name]
if (!category ||
(category.settingName && !getSetting(settings[category.settingName]))) {
return
}
sender.send(messages.SEND_SYNC_RECORDS, category.categoryName, data.map((item) => {
if (!item || !item.name || !item.value) {
return
}
return {
action,
deviceId,
objectId: item.objectId,
[item.name]: item.value
}
}))
}
/**
* @param {Object} action
* @returns {boolean}
*/
const validateAction = (action) => {
const SYNC_ACTIONS_WITHOUT_ITEMS = [
syncConstants.SYNC_CLEAR_HISTORY,
syncConstants.SYNC_CLEAR_SITE_SETTINGS,
syncConstants.SYNC_DELETE_USER
]
if (SYNC_ACTIONS.includes(action.actionType) !== true) {
return false
}
// If the action requires an item, validate the item.
if (SYNC_ACTIONS_WITHOUT_ITEMS.includes(action.actionType) !== true) {
if (!action.item || !action.item.toJS) {
log('Missing item!')
return false
}
// Only accept items who have an objectId set already
if (!action.item.get('objectId')) {
log(`Missing object ID! ${action.item.toJS()}`)
return false
}
}
return true
}
const doAction = (sender, action) => {
if (action.key === settings.SYNC_ENABLED) {
if (action.value === false) {
module.exports.stop()
}
}
// If sync is not enabled, the following actions should be ignored.
if (!syncEnabled() || validateAction(action) !== true || sender.isDestroyed()) {
return
}
switch (action.actionType) {
case syncConstants.SYNC_ADD_SITE:
sendSyncRecords(sender, writeActions.CREATE,
[syncUtil.createSiteData(action.item.toJS())])
break
case syncConstants.SYNC_UPDATE_SITE:
sendSyncRecords(sender, writeActions.UPDATE,
[syncUtil.createSiteData(action.item.toJS())])
break
case syncConstants.SYNC_REMOVE_SITE:
sendSyncRecords(sender, writeActions.DELETE,
[syncUtil.createSiteData(action.item.toJS())])
break
case syncConstants.SYNC_CLEAR_HISTORY:
sender.send(messages.DELETE_SYNC_CATEGORY, CATEGORY_MAP.historySite.categoryName)
break
case syncConstants.SYNC_ADD_SITE_SETTING:
if (syncUtil.isSyncable('siteSetting', action.item)) {
sendSyncRecords(sender, writeActions.CREATE,
[syncUtil.createSiteSettingsData(action.hostPattern, action.item.toJS())])
}
break
case syncConstants.SYNC_UPDATE_SITE_SETTING:
if (syncUtil.isSyncable('siteSetting', action.item)) {
sendSyncRecords(sender, writeActions.UPDATE,
[syncUtil.createSiteSettingsData(action.hostPattern, action.item.toJS())])
}
break
case syncConstants.SYNC_REMOVE_SITE_SETTING:
sendSyncRecords(sender, writeActions.DELETE,
[syncUtil.createSiteSettingsData(action.hostPattern, action.item.toJS())])
break
case syncConstants.SYNC_CLEAR_SITE_SETTINGS:
sender.send(messages.DELETE_SYNC_SITE_SETTINGS)
break
case syncConstants.SYNC_DELETE_USER:
sender.send(messages.DELETE_SYNC_USER)
break
default:
}
}
/**
* Called when sync client is done initializing.
* @param {boolean} isFirstRun - whether this is the first time sync is running
* @param {Event} e
*/
module.exports.onSyncReady = (isFirstRun, e) => {
appActions.setSyncSetupError(null)
if (!syncEnabled()) {
return
}
if (!deviceIdSent && isFirstRun) {
// Sync the device id for this device
sendSyncRecords(e.sender, writeActions.CREATE, [{
name: 'device',
objectId: syncUtil.newObjectId(['sync']),
value: {
name: getSetting(settings.SYNC_DEVICE_NAME)
}
}])
deviceIdSent = true
}
const appState = AppStore.getState()
const sites = appState.get('sites') || new Immutable.List()
const seed = appState.get('seed') || new Immutable.List()
/**
* Sync a bookmark that has not been synced yet, first syncing the parent
* folder if needed. For folders, set and memoize folderId to ensure
* consistent parentFolderObjectIds.
* Otherwise siteUtil.createSiteData() will generate new objectIds every
* call; there's not enough time to dispatch id updates to appStore.sites.
* @param {Immutable.Map} site
*/
const folderToObjectId = {}
const syncBookmark = (site) => {
if (!site || (site.get('objectId') && seed.equals(site.get('originalSeed'))) ||
folderToObjectId[site.get('folderId')] || !syncUtil.isSyncable('bookmark', site)) {
return
}
const siteJS = site.toJS()
const parentFolderId = site.get('parentFolderId')
if (typeof parentFolderId === 'number') {
if (!folderToObjectId[parentFolderId]) {
const folderResult = siteUtil.getFolder(sites, parentFolderId)
if (folderResult) {
syncBookmark(folderResult[1])
}
}
siteJS.parentFolderObjectId = folderToObjectId[parentFolderId]
}
const record = syncUtil.createSiteData(siteJS)
const folderId = site.get('folderId')
if (typeof folderId === 'number') {
folderToObjectId[folderId] = record.objectId
}
sendSyncRecords(e.sender, writeActions.CREATE, [record])
}
// Sync bookmarks that have not been synced yet.
siteUtil.getBookmarks(sites).sortBy(site => site.get('order'))
.forEach(syncBookmark)
// Sync site settings that have not been synced yet
// FIXME: If Sync was disabled and settings were changed, those changes
// might not be synced.
const siteSettings =
appState.get('siteSettings').filter((value, key) => {
return !value.get('objectId') && syncUtil.isSyncable('siteSetting', value)
}).toJS()
if (siteSettings) {
sendSyncRecords(e.sender, writeActions.UPDATE,
Object.keys(siteSettings).map((item) => {
return syncUtil.createSiteSettingsData(item, siteSettings[item])
}))
}
appActions.createSyncCache()
e.sender.send(messages.FETCH_SYNC_DEVICES)
// Periodically poll for new records
let startAt = appState.getIn(['sync', 'lastFetchTimestamp']) || 0
const poll = () => {
let categoryNames = []
for (let type in CATEGORY_MAP) {
let item = CATEGORY_MAP[type]
if (item.settingName && getSetting(settings[item.settingName]) === true) {
categoryNames.push(item.categoryName)
}
}
e.sender.send(messages.FETCH_SYNC_RECORDS, categoryNames, startAt)
startAt = syncUtil.now()
appActions.saveSyncInitData(null, null, startAt)
}
poll()
pollIntervalId = setInterval(poll, config.fetchInterval)
}
/**
* Called to initialize sync, regardless of whether it is enabled.
* @param {Object} initialState - initial appState.sync
*/
module.exports.init = function (appState) {
const initialState = appState.get('sync') || new Immutable.Map()
const RELOAD_MESSAGE = 'reload-sync-extension'
const RESET_SYNC = 'reset-sync'
const reset = () => {
log('Resetting browser local sync state.')
appActions.changeSetting(settings.SYNC_ENABLED, false)
appActions.changeSetting(settings.SYNC_DEVICE_NAME, undefined)
appActions.resetSyncData()
}
// sent by about:preferences when sync should be reloaded
ipcMain.on(RELOAD_MESSAGE, () => {
process.emit(RELOAD_MESSAGE)
})
// sent by about:preferences when resetting sync
ipcMain.on(RESET_SYNC, (e) => {
if (dispatcherCallback) {
// send DELETE_SYNC_USER to sync client. it replies with DELETED_SYNC_USER
dispatcherCallback({actionType: syncConstants.SYNC_DELETE_USER})
} else {
reset()
}
})
ipcMain.on(messages.DELETED_SYNC_USER, (e) => {
reset()
})
// GET_INIT_DATA is the first message sent by the sync-client when it starts
ipcMain.on(messages.GET_INIT_DATA, (e) => {
// Clear any old errors
appActions.setSyncSetupError(null)
// Unregister the previous dispatcher cb
if (dispatcherCallback) {
appDispatcher.unregister(dispatcherCallback)
}
// Register the dispatcher callback now that we have a valid sender
dispatcherCallback = doAction.bind(null, e.sender)
appDispatcher.register(dispatcherCallback)
// Send the initial data
if (syncEnabled()) {
const appState = AppStore.getState().get('sync')
const seed = appState.get('seed') ? Array.from(appState.get('seed')) : null
deviceId = appState.get('deviceId') ? Array.from(appState.get('deviceId')) : null
const syncConfig = {
apiVersion: config.apiVersion,
debug: config.debug,
serverUrl: getSetting(settings.SYNC_NETWORK_DISABLED)
? 'http://localhost' // set during tests to simulate network failure
: config.serverUrl
}
e.sender.send(messages.GOT_INIT_DATA, seed, deviceId, syncConfig)
}
})
// SAVE_INIT_DATA is sent by about:preferences before sync is enabled
// when restoring from an existing seed
ipcMain.on(messages.SAVE_INIT_DATA, (e, seed, newDeviceId) => {
const isRestoring = seed && !newDeviceId
if (!deviceId && newDeviceId) {
deviceId = Array.from(newDeviceId)
}
if (!seed && newDeviceId) {
appActions.saveSyncInitData(null, new Immutable.List(newDeviceId), null)
return
}
try {
let chunks = []
qr.image(Buffer.from(seed).toString('hex')).on('data', (chunk) => {
chunks.push(chunk)
}).on('end', () => {
let seedQr = 'data:image/png;base64,' + Buffer.concat(chunks).toString('base64')
appActions.saveSyncInitData(new Immutable.List(seed),
newDeviceId ? new Immutable.List(newDeviceId) : null, null, seedQr)
})
} catch (ex) {
console.log('qr image error: ' + ex.toString())
appActions.saveSyncInitData(new Immutable.List(seed),
newDeviceId ? new Immutable.List(newDeviceId) : null)
}
if (isRestoring) {
// we are restoring from a previous seed. wait for the seed to be saved
// before reloading, or sync-client will override the seed.
process.emit(RELOAD_MESSAGE)
}
})
const isFirstRun = !initialState.get('seed') && !initialState.get('deviceId')
ipcMain.on(messages.SYNC_READY, module.exports.onSyncReady.bind(null,
isFirstRun))
ipcMain.on(messages.SYNC_DEBUG, (e, msg) => {
log(msg)
})
ipcMain.on(messages.SYNC_SETUP_ERROR, (e, error) => {
if (error === 'Failed to fetch') {
// This is probably the most common error, so give it a more useful message.
error = locale.translation('connectionError')
}
appActions.setSyncSetupError(error || locale.translation('unknownError'))
})
ipcMain.on(messages.GET_EXISTING_OBJECTS, (event, categoryName, records, lastRecordTimestamp) => {
if (!syncEnabled()) {
return
}
let devices = {}
log(`getting existing objects for ${records.length} ${categoryName}`)
if (!CATEGORY_NAMES.includes(categoryName) || !records || !records.length) {
return
}
const recordsAndExistingObjects = records.map((record) => {
const safeRecord = syncUtil.ipcSafeObject(record)
const deviceId = syncUtil.deviceIdString(safeRecord.deviceId)
devices[deviceId] = {lastRecordTimestamp: record.syncTimestamp}
const existingObject = syncUtil.getExistingObject(categoryName, record)
return [safeRecord, existingObject]
})
event.sender.send(messages.RESOLVE_SYNC_RECORDS, categoryName, recordsAndExistingObjects)
// For each device we saw, update its last record timestamp.
appActions.saveSyncDevices(devices)
})
ipcMain.on(messages.RESOLVED_SYNC_RECORDS, (event, categoryName, records) => {
if (!records || !records.length) {
return
}
if (!bookmarksToolbarShown && isFirstRun) {
// syncing for the first time
const bookmarks = siteUtil.getBookmarks(AppStore.getState().get('sites'))
if (!bookmarks.size) {
for (const record of records) {
if (record && record.objectData === 'bookmark') {
appActions.changeSetting(settings.SHOW_BOOKMARKS_TOOLBAR, true)
bookmarksToolbarShown = true
break
}
}
}
}
syncUtil.applySyncRecords(records)
})
return appState
}
/**
* Called when sync is disabled.
*/
module.exports.stop = function () {
if (pollIntervalId !== null) {
clearInterval(pollIntervalId)
}
appActions.setSyncSetupError(null)
}