2012-07-20 08:41:30 -07:00
|
|
|
/* 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"
|
|
|
|
|
2012-09-05 20:47:47 -07:00
|
|
|
const Cu = Components.utils;
|
2012-07-20 08:41:30 -07:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/IndexedDBHelper.jsm");
|
|
|
|
|
2012-08-27 07:13:02 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageBroadcaster");
|
2012-07-20 08:41:30 -07:00
|
|
|
|
2012-10-31 01:35:11 -07:00
|
|
|
const EXPORTED_SYMBOLS = [];
|
2012-07-20 08:41:30 -07:00
|
|
|
|
|
|
|
let idbGlobal = this;
|
|
|
|
|
2012-09-05 20:47:47 -07:00
|
|
|
function debug(aMsg) {
|
2012-07-20 08:41:30 -07:00
|
|
|
//dump("-- ActivitiesService.jsm " + Date.now() + " " + aMsg + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
const DB_NAME = "activities";
|
|
|
|
const DB_VERSION = 1;
|
|
|
|
const STORE_NAME = "activities";
|
|
|
|
|
|
|
|
function ActivitiesDb() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivitiesDb.prototype = {
|
|
|
|
__proto__: IndexedDBHelper.prototype,
|
|
|
|
|
|
|
|
init: function actdb_init() {
|
|
|
|
let idbManager = Cc["@mozilla.org/dom/indexeddb/manager;1"]
|
|
|
|
.getService(Ci.nsIIndexedDatabaseManager);
|
|
|
|
idbManager.initWindowless(idbGlobal);
|
|
|
|
this.initDBHelper(DB_NAME, DB_VERSION, STORE_NAME, idbGlobal);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the initial database schema.
|
|
|
|
*
|
|
|
|
* The schema of records stored is as follows:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* id: String
|
|
|
|
* manifest: String
|
|
|
|
* name: String
|
|
|
|
* title: String
|
|
|
|
* icon: String
|
|
|
|
* description: jsval
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
upgradeSchema: function actdb_upgradeSchema(aTransaction, aDb, aOldVersion, aNewVersion) {
|
|
|
|
debug("Upgrade schema " + aOldVersion + " -> " + aNewVersion);
|
|
|
|
let objectStore = aDb.createObjectStore(STORE_NAME, { keyPath: "id" });
|
|
|
|
|
|
|
|
// indexes
|
|
|
|
objectStore.createIndex("name", "name", { unique: false });
|
|
|
|
objectStore.createIndex("manifest", "manifest", { unique: false });
|
|
|
|
|
|
|
|
debug("Created object stores and indexes");
|
|
|
|
},
|
|
|
|
|
|
|
|
// unique ids made of (uri, action)
|
|
|
|
createId: function actdb_createId(aObject) {
|
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
|
|
|
|
.createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
|
|
|
|
|
|
|
let hasher = Cc["@mozilla.org/security/hash;1"]
|
|
|
|
.createInstance(Ci.nsICryptoHash);
|
|
|
|
hasher.init(hasher.SHA1);
|
|
|
|
|
|
|
|
// add uri and action to the hash
|
|
|
|
["manifest", "name"].forEach(function(aProp) {
|
|
|
|
let data = converter.convertToByteArray(aObject[aProp], {});
|
|
|
|
hasher.update(data, data.length);
|
|
|
|
});
|
2012-09-05 20:47:47 -07:00
|
|
|
|
2012-07-20 08:41:30 -07:00
|
|
|
return hasher.finish(true);
|
|
|
|
},
|
|
|
|
|
2012-10-18 00:32:15 -07:00
|
|
|
// Add all the activities carried in the |aObjects| array.
|
|
|
|
add: function actdb_add(aObjects, aSuccess, aError) {
|
2012-07-20 08:41:30 -07:00
|
|
|
this.newTxn("readwrite", function (txn, store) {
|
2012-10-18 00:32:15 -07:00
|
|
|
aObjects.forEach(function (aObject) {
|
|
|
|
let object = {
|
|
|
|
manifest: aObject.manifest,
|
|
|
|
name: aObject.name,
|
|
|
|
title: aObject.title || "",
|
|
|
|
icon: aObject.icon || "",
|
|
|
|
description: aObject.description
|
|
|
|
};
|
|
|
|
object.id = this.createId(object);
|
|
|
|
debug("Going to add " + JSON.stringify(object));
|
|
|
|
store.put(object);
|
|
|
|
}, this);
|
2012-07-20 08:41:30 -07:00
|
|
|
}.bind(this), aSuccess, aError);
|
|
|
|
},
|
|
|
|
|
2012-10-18 00:32:15 -07:00
|
|
|
// Remove all the activities carried in the |aObjects| array.
|
|
|
|
remove: function actdb_remove(aObjects) {
|
2012-07-20 08:41:30 -07:00
|
|
|
this.newTxn("readwrite", function (txn, store) {
|
2012-10-18 00:32:15 -07:00
|
|
|
aObjects.forEach(function (aObject) {
|
|
|
|
let object = {
|
|
|
|
manifest: aObject.manifest,
|
|
|
|
name: aObject.name
|
|
|
|
};
|
|
|
|
debug("Going to remove " + JSON.stringify(object));
|
|
|
|
store.delete(this.createId(object));
|
|
|
|
}, this);
|
2012-07-20 08:41:30 -07:00
|
|
|
}.bind(this), function() {}, function() {});
|
|
|
|
},
|
|
|
|
|
|
|
|
find: function actdb_find(aObject, aSuccess, aError, aMatch) {
|
|
|
|
debug("Looking for " + aObject.options.name);
|
|
|
|
|
|
|
|
this.newTxn("readonly", function (txn, store) {
|
2012-09-05 20:47:47 -07:00
|
|
|
let index = store.index("name");
|
2012-07-20 08:41:30 -07:00
|
|
|
let request = index.mozGetAll(aObject.options.name);
|
|
|
|
request.onsuccess = function findSuccess(aEvent) {
|
|
|
|
debug("Request successful. Record count: " + aEvent.target.result.length);
|
|
|
|
if (!txn.result) {
|
|
|
|
txn.result = {
|
|
|
|
name: aObject.options.name,
|
|
|
|
options: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
aEvent.target.result.forEach(function(result) {
|
|
|
|
if (!aMatch(result))
|
|
|
|
return;
|
|
|
|
|
|
|
|
txn.result.options.push({
|
|
|
|
manifest: result.manifest,
|
|
|
|
title: result.title,
|
|
|
|
icon: result.icon,
|
|
|
|
description: result.description
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}.bind(this), aSuccess, aError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let Activities = {
|
|
|
|
messages: [
|
|
|
|
// ActivityProxy.js
|
|
|
|
"Activity:Start",
|
|
|
|
|
|
|
|
// ActivityRequestHandler.js
|
|
|
|
"Activity:PostResult",
|
|
|
|
"Activity:PostError",
|
|
|
|
|
|
|
|
"Activities:Register",
|
|
|
|
"Activities:Unregister",
|
|
|
|
],
|
|
|
|
|
|
|
|
init: function activities_init() {
|
|
|
|
this.messages.forEach(function(msgName) {
|
|
|
|
ppmm.addMessageListener(msgName, this);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
|
|
|
|
|
|
|
this.db = new ActivitiesDb();
|
|
|
|
this.db.init();
|
2012-10-15 12:26:49 -07:00
|
|
|
this.callers = {};
|
2012-07-20 08:41:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function activities_observe(aSubject, aTopic, aData) {
|
|
|
|
this.messages.forEach(function(msgName) {
|
|
|
|
ppmm.removeMessageListener(msgName, this);
|
|
|
|
}, this);
|
|
|
|
ppmm = null;
|
|
|
|
|
|
|
|
Services.obs.removeObserver(this, "xpcom-shutdown");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts an activity by doing:
|
|
|
|
* - finds a list of matching activities.
|
|
|
|
* - calls the UI glue to get the user choice.
|
2012-09-05 20:47:47 -07:00
|
|
|
* - fire an system message of type "activity" to this app, sending the
|
2012-07-20 08:41:30 -07:00
|
|
|
* activity data as a payload.
|
|
|
|
*/
|
|
|
|
startActivity: function activities_startActivity(aMsg) {
|
|
|
|
debug("StartActivity: " + JSON.stringify(aMsg));
|
|
|
|
|
|
|
|
let successCb = function successCb(aResults) {
|
|
|
|
debug(JSON.stringify(aResults));
|
|
|
|
|
|
|
|
// We have no matching activity registered, let's fire an error.
|
2012-08-04 08:28:36 -07:00
|
|
|
if (aResults.options.length === 0) {
|
2012-10-15 12:26:49 -07:00
|
|
|
Activities.callers[aMsg.id].mm.sendAsyncMessage("Activity:FireError", {
|
2012-08-04 08:28:36 -07:00
|
|
|
"id": aMsg.id,
|
2012-07-20 08:41:30 -07:00
|
|
|
"error": "NO_PROVIDER"
|
|
|
|
});
|
2012-10-15 12:26:49 -07:00
|
|
|
delete Activities.callers[aMsg.id];
|
2012-07-20 08:41:30 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getActivityChoice(aChoice) {
|
|
|
|
debug("Activity choice: " + aChoice);
|
|
|
|
|
|
|
|
// The user has cancelled the choice, fire an error.
|
|
|
|
if (aChoice === -1) {
|
2012-10-15 12:26:49 -07:00
|
|
|
Activities.callers[aMsg.id].mm.sendAsyncMessage("Activity:FireError", {
|
2012-08-04 08:28:36 -07:00
|
|
|
"id": aMsg.id,
|
2012-07-20 08:41:30 -07:00
|
|
|
"error": "USER_ABORT"
|
|
|
|
});
|
2012-10-15 12:26:49 -07:00
|
|
|
delete Activities.callers[aMsg.id];
|
2012-07-20 08:41:30 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let sysmm = Cc["@mozilla.org/system-message-internal;1"]
|
|
|
|
.getService(Ci.nsISystemMessagesInternal);
|
|
|
|
if (!sysmm) {
|
|
|
|
// System message is not present, what should we do?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Sending system message...");
|
|
|
|
let result = aResults.options[aChoice];
|
|
|
|
sysmm.sendMessage("activity", {
|
2012-08-14 19:40:12 -07:00
|
|
|
"id": aMsg.id,
|
|
|
|
"payload": aMsg.options,
|
|
|
|
"target": result.description
|
|
|
|
},
|
|
|
|
Services.io.newURI(result.description.href, null, null),
|
|
|
|
Services.io.newURI(result.manifest, null, null));
|
2012-07-20 08:41:30 -07:00
|
|
|
|
|
|
|
if (!result.description.returnValue) {
|
2012-10-15 12:26:49 -07:00
|
|
|
Activities.callers[aMsg.id].mm.sendAsyncMessage("Activity:FireSuccess", {
|
2012-07-20 08:41:30 -07:00
|
|
|
"id": aMsg.id,
|
|
|
|
"result": null
|
|
|
|
});
|
2012-10-15 12:26:49 -07:00
|
|
|
// No need to notify observers, since we don't want the caller
|
|
|
|
// to be raised on the foreground that quick.
|
|
|
|
delete Activities.callers[aMsg.id];
|
2012-07-20 08:41:30 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let glue = Cc["@mozilla.org/dom/activities/ui-glue;1"]
|
|
|
|
.createInstance(Ci.nsIActivityUIGlue);
|
|
|
|
glue.chooseActivity(aResults.name, aResults.options, getActivityChoice);
|
|
|
|
};
|
|
|
|
|
|
|
|
let errorCb = function errorCb(aError) {
|
|
|
|
// Something unexpected happened. Should we send an error back?
|
|
|
|
debug("Error in startActivity: " + aError + "\n");
|
|
|
|
};
|
|
|
|
|
|
|
|
let matchFunc = function matchFunc(aResult) {
|
|
|
|
// Bug 773383: arrays of strings / regexp.
|
|
|
|
for (let prop in aResult.description.filters) {
|
2012-08-15 15:06:23 -07:00
|
|
|
if (Array.isArray(aResult.description.filters[prop])) {
|
|
|
|
if (aResult.description.filters[prop].indexOf(aMsg.options.data[prop]) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (aResult.description.filters[prop] !== aMsg.options.data[prop] ) {
|
2012-07-20 08:41:30 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.db.find(aMsg, successCb, errorCb, matchFunc);
|
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function activities_receiveMessage(aMessage) {
|
2012-08-27 07:13:02 -07:00
|
|
|
let mm = aMessage.target;
|
2012-07-20 08:41:30 -07:00
|
|
|
let msg = aMessage.json;
|
2012-10-15 12:26:49 -07:00
|
|
|
|
|
|
|
let caller;
|
|
|
|
let obsData;
|
|
|
|
|
2012-10-19 13:53:51 -07:00
|
|
|
if (aMessage.name == "Activity:PostResult" ||
|
|
|
|
aMessage.name == "Activity:PostError") {
|
2012-10-15 12:26:49 -07:00
|
|
|
caller = this.callers[msg.id];
|
2012-10-30 16:31:59 -07:00
|
|
|
if (!caller) {
|
2012-10-19 13:53:51 -07:00
|
|
|
debug("!! caller is null for msg.id=" + msg.id);
|
2012-10-30 16:31:59 -07:00
|
|
|
return;
|
2012-10-15 12:26:49 -07:00
|
|
|
}
|
2012-10-30 16:31:59 -07:00
|
|
|
obsData = JSON.stringify({ manifestURL: caller.manifestURL,
|
|
|
|
pageURL: caller.pageURL,
|
|
|
|
success: aMessage.name == "Activity:PostResult" });
|
2012-10-15 12:26:49 -07:00
|
|
|
}
|
|
|
|
|
2012-07-20 08:41:30 -07:00
|
|
|
switch(aMessage.name) {
|
|
|
|
case "Activity:Start":
|
2012-10-15 12:26:49 -07:00
|
|
|
this.callers[msg.id] = { mm: aMessage.target,
|
|
|
|
manifestURL: msg.manifestURL,
|
|
|
|
pageURL: msg.pageURL };
|
2012-07-20 08:41:30 -07:00
|
|
|
this.startActivity(msg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "Activity:PostResult":
|
2012-10-15 12:26:49 -07:00
|
|
|
caller.mm.sendAsyncMessage("Activity:FireSuccess", msg);
|
|
|
|
Services.obs.notifyObservers(null, "activity-done", obsData);
|
|
|
|
delete this.callers[msg.id];
|
2012-07-20 08:41:30 -07:00
|
|
|
break;
|
|
|
|
case "Activity:PostError":
|
2012-10-15 12:26:49 -07:00
|
|
|
caller.mm.sendAsyncMessage("Activity:FireError", msg);
|
|
|
|
Services.obs.notifyObservers(null, "activity-done", obsData);
|
|
|
|
delete this.callers[msg.id];
|
2012-07-20 08:41:30 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "Activities:Register":
|
2012-10-18 00:32:15 -07:00
|
|
|
this.db.add(msg,
|
|
|
|
function onSuccess(aEvent) {
|
|
|
|
mm.sendAsyncMessage("Activities:Register:OK", null);
|
|
|
|
},
|
|
|
|
function onError(aEvent) {
|
|
|
|
msg.error = "REGISTER_ERROR";
|
|
|
|
mm.sendAsyncMessage("Activities:Register:KO", msg);
|
|
|
|
});
|
2012-07-20 08:41:30 -07:00
|
|
|
break;
|
|
|
|
case "Activities:Unregister":
|
|
|
|
this.db.remove(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Activities.init();
|