This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19873 from crdlc/bug-1018852
Bug 1018852 - Mechanism to migrate collections and bookmarks to datastor...
- Loading branch information
Showing
5 changed files
with
550 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
'use strict'; | ||
|
||
/* global HomeState, GridItemsFactory, CollectionsDatabase, BookmarksDatabase */ | ||
|
||
(function(exports) { | ||
|
||
function Migrator() { | ||
navigator.mozSetMessageHandler('connection', this.onConnection.bind(this)); | ||
} | ||
|
||
Migrator.prototype = { | ||
/** | ||
* It performs the migration task. | ||
*/ | ||
start: function(event) { | ||
if (this.migrating) { | ||
// Migration in progress | ||
return; | ||
} | ||
|
||
this.migrating = this.iterating = true; | ||
this.pendingItems = 0; | ||
HomeState.getGrid(this.iteratePage.bind(this), | ||
this.onHomeStateSuccess.bind(this), | ||
this.onHomeStateError.bind(this)); | ||
}, | ||
|
||
/** | ||
* This method iterates icons within a page. If an icon is a bookmark or | ||
* collection, it will be added to their own datastore | ||
*/ | ||
iteratePage: function(page) { | ||
console.debug('Migrating the page number', page.index); | ||
|
||
var types = GridItemsFactory.TYPE; | ||
var onItemMigrated = this.onItemMigrated.bind(this); | ||
page.icons.forEach(function(icon) { | ||
var type = icon.type; | ||
|
||
if (!type && icon.bookmarkURL) { | ||
// pre-1.3 bookmarks | ||
type = icon.type = types.BOOKMARK; | ||
} | ||
|
||
var database = null; | ||
|
||
if (type === types.COLLECTION) { | ||
database = CollectionsDatabase; | ||
} else if (type === types.BOOKMARK) { | ||
database = BookmarksDatabase; | ||
} | ||
|
||
if (!database) { | ||
return; | ||
} | ||
|
||
++this.pendingItems; | ||
// We are going to propagate the bookmark/collection to datastore | ||
var descriptor = GridItemsFactory.create(icon).getDescriptor(); | ||
database.add(descriptor).then(onItemMigrated, onItemMigrated); | ||
}.bind(this)); | ||
}, | ||
|
||
onConnection: function(connectionRequest) { | ||
if (connectionRequest.keyword !== 'migrate') { | ||
return; | ||
} | ||
|
||
var port = this.port = connectionRequest.port; | ||
port.onmessage = this.start.bind(this); | ||
port.start(); | ||
}, | ||
|
||
/** | ||
* This method is performed when the migration finishes. | ||
*/ | ||
onFinish: function() { | ||
this.migrating = this.iterating = false; | ||
this.port.postMessage('Done'); | ||
}, | ||
|
||
/** | ||
* This method is performed when a item has been migrated. | ||
*/ | ||
onItemMigrated: function(event) { | ||
--this.pendingItems === 0 && !this.iterating && this.onFinish(); | ||
}, | ||
|
||
/** | ||
* This method is performed when indexedDB has been read and iterated. | ||
*/ | ||
onHomeStateSuccess: function() { | ||
this.iterating = false; | ||
this.pendingItems === 0 && this.onFinish(); | ||
}, | ||
|
||
/** | ||
* This method is performed when an error happens loading indexedDB. | ||
*/ | ||
onHomeStateError: function(error) { | ||
this.migrating = this.iterating = false; | ||
this.port.postMessage('Failed'); | ||
console.error('Bookmarks & collections migration failed', error); | ||
} | ||
}; | ||
|
||
exports.migrator = new Migrator(); | ||
}(window)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1"> | ||
|
||
<script type="text/javascript" defer src="shared/js/bookmarks_database.js"></script> | ||
<script type="text/javascript" defer src="shared/js/collections_database.js"></script> | ||
|
||
<script type="text/javascript" defer src="js/bookmark.js"></script> | ||
<script type="text/javascript" defer src="js/grid_components.js"></script> | ||
<script type="text/javascript" defer src="js/state.js"></script> | ||
<script type="text/javascript" defer src="js/migrator.js"></script> | ||
</head> | ||
|
||
<body> | ||
</body> | ||
</html> |
Oops, something went wrong.