Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Bug 1215086 - Resolve the two records have the same URL. #32578

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
105 changes: 58 additions & 47 deletions apps/sync/js/adapters/bookmarks.js
Expand Up @@ -34,14 +34,14 @@ const BOOKMARKS_COLLECTION_MTIME = 'collections::bookmarks::mtime';
const BOOKMARKS_SYNCTOID_PREFIX = 'synctoid::bookmarks::';

var BookmarksHelper = (() => {
var store;
var _store;
function _ensureStore() {
if (store) {
return Promise.resolve(store);
if (_store) {
return Promise.resolve(_store);
}
return navigator.getDataStores('sync_bookmarks_store').then(stores => {
store = stores[0];
return store;
return navigator.getDataStores('bookmarks_store').then(stores => {
_store = stores[0];
return _store;
});
}

Expand Down Expand Up @@ -86,22 +86,13 @@ var BookmarksHelper = (() => {
console.error('Inconsistent records', localRecord, remoteRecord);
throw new Error('Inconsistent records');
}
if (!localRecord.fxsyncId && typeof remoteRecord.fxsyncId === 'string') {
/* When a localRecord has no fxsyncId, assign fxsyncId to it from a
remoteRecord. This case always happens at first synchronization or
merging two records with the same URL. */
localRecord.fxsyncId = remoteRecord.fxsyncId;
} else if (localRecord.fxsyncId !== remoteRecord.fxsyncId) {
// Two records have different fxsyncId but have the same url(id).
console.log('Records should have the same Firefox Sync ID',
localRecord, remoteRecord);
throw new Error('Records should have the same Firefox Sync ID',
localRecord, remoteRecord);
}

localRecord.name = remoteRecord.name;
localRecord.fxsyncPayload = remoteRecord.fxsyncPayload;

if (!localRecord.fxsyncRecords) {
localRecord.fxsyncRecords = {};
}
localRecord.fxsyncRecords[remoteRecord.fxsyncId] =
remoteRecord.fxsyncRecords[remoteRecord.fxsyncId];
return localRecord;
}

Expand All @@ -116,14 +107,16 @@ var BookmarksHelper = (() => {
var revisionId;
return _ensureStore().then(store => {
revisionId = store.revisionId;
return store.get(id);
}).then(localRecord => {
if (localRecord) {
var newBookmark = mergeRecordsToDataStore(localRecord, remoteRecord);
return store.put(newBookmark, id, revisionId);
}
return store.add(remoteRecord, id, revisionId).then(() => {
return setDataStoreId(remoteRecord.fxsyncId, id);
return store.get(id).then(localRecord => {
if (localRecord) {
var newBookmark = mergeRecordsToDataStore(localRecord, remoteRecord);
return store.put(newBookmark, id, revisionId).then(() => {
return setDataStoreId(remoteRecord.fxsyncId, id);
});
}
return store.add(remoteRecord, id, revisionId).then(() => {
return setDataStoreId(remoteRecord.fxsyncId, id);
});
});
}).catch(e => {
console.error(e);
Expand All @@ -135,7 +128,7 @@ var BookmarksHelper = (() => {
records.reduce((reduced, current) => {
return reduced.then(() => {
if (current.deleted) {
return deleteBookmark(current.fxsyncId);
return deleteBookmark(current.id);
}
return addBookmark(current);
});
Expand All @@ -148,11 +141,26 @@ var BookmarksHelper = (() => {
return getDataStoreId(fxsyncId).then(id => {
if (!id) {
console.warn('No DataStore ID corresponded to FxSyncID', fxsyncId);
return Promise.resolve();
}
url = id;
return _ensureStore();
}).then(store => {
return store.remove(url);
return _ensureStore().then(store => {
var revisionId = store.revisionId;
return store.get(url).then(localRecord => {
localRecord.fxsyncRecords[fxsyncId] = {
deleted: true,
id: fxsyncId
};
var isEmpty = Object.keys(localRecord.fxsyncRecords).every(value => {
return localRecord.fxsyncRecords[value].deleted;
});
if (isEmpty && localRecord.syncNeeded) {
return store.remove(url, revisionId);
} else {
return store.put(localRecord, url, revisionId);
}
});
});
});
}

Expand Down Expand Up @@ -188,7 +196,11 @@ DataAdapters.bookmarks = {
"type": "url",
"iconable": false,
"icon": "http://www.lego.com/favicon.ico",
"fxsyncPayload": Object, // payload from BC
"fxsyncRecords": {
'fxsyncID_A': fxsync_payload_A,
'fxsyncID_B': fxsync_payload_B,
'fxsyncID_C': fxsync_payload_C
}, // payload from BC
"fxsyncId": "REMOTE_ID" // [1.1]
}

Expand Down Expand Up @@ -263,27 +275,23 @@ DataAdapters.bookmarks = {
var bookmarks = [];
for (var i = 0; i < remoteRecords.length; i++) {
var payload = remoteRecords[i].payload;
if (remoteRecords[i].last_modified <= lastModifiedTime) {
break;
}
if (payload.type === 'microsummary') {
console.warn('microsummary is OBSOLETED ', payload);
continue;
}
if (['query', 'bookmark', 'folder', 'livemark', 'separator']
.every(value => value !== payload.type)) {
console.error('Unknown type? ', payload);
continue;
}
if (!Number.isInteger(remoteRecords[i].last_modified)) {
console.warn('Incorrect payload::last_modified? ', payload);
continue;
}
if (remoteRecords[i].last_modified <= lastModifiedTime) {
break;
}
if (payload.deleted) {
bookmarks.push({
deleted: true,
fxsyncId: payload.id
});
bookmarks.push(payload);
continue;
} else if (['query', 'bookmark', 'folder', 'livemark', 'separator']
.every(value => value !== payload.type)) {
console.error('Unknown type? ', payload);
continue;
}
var typeWithUri = ['query', 'bookmark']
Expand All @@ -292,6 +300,9 @@ DataAdapters.bookmarks = {
console.warn('Incorrect payload? ', payload);
continue;
}
var fxsyncRecords = {};
fxsyncRecords[payload.id] = remoteRecords[i].payload;
fxsyncRecords[payload.id].timestamp = remoteRecords[i].last_modified;
bookmarks.push({
// URL is the ID for bookmark records in bookmarks_store, but there are
// some types without a valid URL except bookmark type. URL is used as
Expand All @@ -305,8 +316,8 @@ DataAdapters.bookmarks = {
type: payload.type === 'bookmark' ? 'url' : 'others',
iconable: false,
icon: '',
last_modified: remoteRecords[i].last_modified,
fxsyncPayload: payload,
syncNeeded: true,
fxsyncRecords: fxsyncRecords,
fxsyncId: payload.id
});
}
Expand Down
4 changes: 4 additions & 0 deletions apps/sync/manifest.webapp
Expand Up @@ -18,6 +18,10 @@
"places": {
"readonly": false,
"description": "Stores data about browsing history."
},
"bookmarks_store": {
"readonly": false,
"description": "Stores data about browsing history."
}
},
"connections": {
Expand Down