Bug 771877: add WorkerAPI, r=jaws/mixedpuppy

--HG--
extra : rebase_source : e91a5c2586c725928d87e1bb379157561145f36d
This commit is contained in:
Gavin Sharp 2012-07-09 00:22:50 -07:00
parent 1d005f77d2
commit 90b89babe4
7 changed files with 122 additions and 13 deletions

View File

@ -13,6 +13,7 @@ EXTRA_JS_MODULES = \
FrameWorker.jsm \
SocialService.jsm \
SocialProvider.jsm \
WorkerAPI.jsm \
$(NULL)
TEST_DIRS += \

View File

@ -8,6 +8,7 @@ const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/FrameWorker.jsm");
Cu.import("resource://gre/modules/WorkerAPI.jsm");
const EXPORTED_SYMBOLS = ["SocialProvider"];
@ -27,13 +28,17 @@ function SocialProvider(input) {
this.name = input.name;
this.workerURL = input.workerURL;
this.origin = input.origin;
let workerAPIPort = this.getWorkerPort();
if (workerAPIPort)
this.workerAPI = new WorkerAPI(workerAPIPort);
}
SocialProvider.prototype = {
/**
* Terminates the provider's FrameWorker, if it has one.
*/
terminate: function shutdown() {
terminate: function terminate() {
if (this.workerURL) {
try {
getFrameWorkerHandle(this.workerURL, null).terminate();
@ -54,6 +59,11 @@ SocialProvider.prototype = {
getWorkerPort: function getWorkerPort(window) {
if (!this.workerURL)
return null;
return getFrameWorkerHandle(this.workerURL, window).port;
try {
return getFrameWorkerHandle(this.workerURL, window).port;
} catch (ex) {
Cu.reportError("SocialProvider: retrieving worker port failed:" + ex);
return null;
}
}
}

View File

@ -0,0 +1,48 @@
/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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 {classes: Cc, interfaces: Ci, utils: Cu} = Components;
const EXPORTED_SYMBOLS = ["WorkerAPI"];
function WorkerAPI(port) {
if (!port)
throw new Error("Can't initialize WorkerAPI with a null port");
this._port = port;
this._port.onmessage = this._handleMessage.bind(this);
this.initialized = false;
// Send an "intro" message so the worker knows this is the port
// used for the api.
// later we might even include an API version - version 0 for now!
this._port.postMessage({topic: "social.initialize"});
}
WorkerAPI.prototype = {
_handleMessage: function _handleMessage(event) {
let {topic, data} = event.data;
let handler = this.handlers[topic];
if (!handler) {
Cu.reportError("WorkerAPI: topic doesn't have a handler: '" + topic + "'");
return;
}
try {
handler.call(this, data);
} catch (ex) {
Cu.reportError("WorkerAPI: failed to handle message '" + topic + "': " + ex);
}
},
handlers: {
"social.initialize-response": function (data) {
this.initialized = true;
}
}
}

View File

@ -1,4 +1,3 @@
#
# 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/.
@ -14,12 +13,14 @@ MODULE = test_socialapi
include $(DEPTH)/config/autoconf.mk
MOCHITEST_BROWSER_FILES = \
head.js \
data.json \
worker_xhr.js \
browser_frameworker.js \
worker_relative.js \
relative_import.js \
$(NULL)
head.js \
data.json \
worker_xhr.js \
browser_frameworker.js \
worker_relative.js \
relative_import.js \
browser_workerAPI.js \
worker_social.js \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,34 @@
/* 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/. */
let SocialProvider = Components.utils.import("resource://gre/modules/SocialProvider.jsm", {}).SocialProvider;
function test() {
waitForExplicitFinish();
// This test creates a SocialProvider object directly - it would be nicer to
// go through the SocialService, but adding a test provider before the service
// has been initialized can be tricky.
let provider = new SocialProvider({
origin: 'http://example.com',
name: "Example Provider",
workerURL: "http://example.com/browser/toolkit/components/social/test/browser/worker_social.js"
});
ok(provider.workerAPI, "provider has a workerAPI");
is(provider.workerAPI.initialized, false, "workerAPI is not yet initialized");
let port = provider.getWorkerPort();
ok(port, "should be able to get a port from the provider");
port.onmessage = function onMessage(event) {
let {topic, data} = event.data;
if (topic == "test-initialization-complete") {
is(provider.workerAPI.initialized, true, "workerAPI is now initialized");
provider.terminate();
finish();
}
}
port.postMessage({topic: "test-initialization"});
}

View File

@ -1,5 +1,3 @@
Cu.import("resource://gre/modules/Services.jsm");
// A helper to run a suite of tests.
// The "test object" should be an object with function names as keys and a
// function as the value. The functions will be called with a "cbnext" param
@ -48,4 +46,3 @@ function runTests(tests, cbPreTest, cbPostTest) {
}
runNextTest();
}

View File

@ -0,0 +1,18 @@
/* 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/. */
onconnect = function(e) {
let port = e.ports[0];
port.onmessage = function onMessage(event) {
let {topic, data} = event.data;
switch (topic) {
case "social.initialize":
port.postMessage({topic: "social.initialize-response"});
break;
case "test-initialization":
port.postMessage({topic: "test-initialization-complete"});
break;
}
}
}