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

Commit

Permalink
Merge pull request #19873 from crdlc/bug-1018852
Browse files Browse the repository at this point in the history
Bug 1018852 - Mechanism to migrate collections and bookmarks to datastor...
  • Loading branch information
Cristian Rodriguez committed Jun 3, 2014
2 parents 070f1a8 + 0bdce7e commit 19e0879
Show file tree
Hide file tree
Showing 5 changed files with 550 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apps/homescreen/js/grid_components.js
Expand Up @@ -117,5 +117,11 @@ Collection.prototype = {
window.dispatchEvent(new CustomEvent('collectionlaunch', {
'detail': features
}));
},

getDescriptor: function sc_getDescriptor() {
var descriptor = GridItem.prototype.getDescriptor.call(this);
descriptor.providerId = this.providerId;
return descriptor;
}
};
108 changes: 108 additions & 0 deletions apps/homescreen/js/migrator.js
@@ -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));
13 changes: 13 additions & 0 deletions apps/homescreen/manifest.webapp
Expand Up @@ -48,6 +48,19 @@
"bookmarks_store": {
"readonly": false,
"description": "Read and migrate bookmarks"
},
"collections_store": {
"readonly": false,
"description": "Read and migrate collections"
}
},
"connections": {
"migrate": {
"description": "It migrates bookmarks & collections",
"rules": {
"manifestURLs": ["app://ftu.gaiamobile.org/manifest.webapp"]
},
"handler_path": "/migrate.html"
}
}
}
18 changes: 18 additions & 0 deletions apps/homescreen/migrate.html
@@ -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>

0 comments on commit 19e0879

Please sign in to comment.