Bug 774003: let social workers request their cookies via the Social API, r=gavin

--HG--
extra : rebase_source : ae4aad6caa156c83fa08c0813c79452ca68203c7
This commit is contained in:
Shane Caraveo 2012-07-25 15:26:39 -07:00
parent 447632fc11
commit 863c9841a5
4 changed files with 59 additions and 5 deletions

View File

@ -253,6 +253,10 @@ function WorkerHandle(port, worker) {
this._worker = worker;
}
WorkerHandle.prototype = {
get document() {
return this._worker.frame.contentDocument;
},
// XXX - workers have no .close() method, but *do* have a .terminate()
// method which we should implement. However, the worker spec doesn't define
// a callback to be made in the worker when this happens - it all just dies.

View File

@ -8,6 +8,9 @@
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "getFrameWorkerHandle", "resource://gre/modules/FrameWorker.jsm");
const EXPORTED_SYMBOLS = ["WorkerAPI"];
@ -25,6 +28,9 @@ function WorkerAPI(provider, port) {
// used for the api.
// later we might even include an API version - version 0 for now!
this._port.postMessage({topic: "social.initialize"});
// backwards compat, remove after Aug 1.
this._port.postMessage({topic: "social.cookie-changed"});
}
WorkerAPI.prototype = {
@ -52,6 +58,18 @@ WorkerAPI.prototype = {
"social.ambient-notification": function (data) {
this._provider.setAmbientNotification(data);
},
"social.cookies-get": function(data) {
let document = getFrameWorkerHandle(this._provider.workerURL, null).document;
let cookies = document.cookie.split(";");
let results = [];
cookies.forEach(function(aCookie) {
let [name, value] = aCookie.split("=");
results.push({name: unescape(name.trim()),
value: unescape(value.trim())});
});
this._port.postMessage({topic: "social.cookies-get-response",
data: results});
},
// XXX backwards compat for existing providers, remove these eventually
"social.ambient-notification-area": function (data) {

View File

@ -60,7 +60,7 @@ let tests = {
next();
}
Services.obs.addObserver(ob, "social:profile-changed", false);
provider.workerAPI._port.postMessage({topic: "test-profile", data: expect});
provider.port.postMessage({topic: "test-profile", data: expect});
},
testAmbientNotification: function(next) {
@ -76,7 +76,7 @@ let tests = {
next();
}
Services.obs.addObserver(ob, "social:ambient-notification-changed", false);
provider.workerAPI._port.postMessage({topic: "test-ambient", data: expect});
provider.port.postMessage({topic: "test-ambient", data: expect});
},
testProfileCleared: function(next) {
@ -93,5 +93,22 @@ let tests = {
}
Services.obs.addObserver(ob, "social:profile-changed", false);
provider.workerAPI._port.postMessage({topic: "test-profile", data: sent});
},
testCookies: function(next) {
provider.port.onmessage = function onMessage(event) {
let {topic, data} = event.data;
if (topic == "test.cookies-get-response") {
is(data.length, 1, "got one cookie");
is(data[0].name, "cheez", "cookie has the correct name");
is(data[0].value, "burger", "cookie has the correct value");
Services.cookies.remove('.example.com', '/', 'cheez', false);
next();
}
}
var MAX_EXPIRY = Math.pow(2, 62);
Services.cookies.add('.example.com', '/', 'cheez', 'burger', false, false, true, MAX_EXPIRY);
provider.port.postMessage({topic: "test.cookies-get"});
}
};

View File

@ -2,23 +2,38 @@
* 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/. */
// apiPort is our port to WorkerAPI
let apiPort;
// testerPort is whatever port a test calls us on
let testerPort;
onconnect = function(e) {
// assume this is a test connecting, but if we get
// social.initialize, we know it is our WorkerAPI
// instance connecting and we'll set apiPort
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"});
apiPort = port;
apiPort.postMessage({topic: "social.initialize-response"});
break;
case "test-initialization":
testerPort = port;
port.postMessage({topic: "test-initialization-complete"});
break;
case "test-profile":
port.postMessage({topic: "social.user-profile", data: data});
apiPort.postMessage({topic: "social.user-profile", data: data});
break;
case "test-ambient":
port.postMessage({topic: "social.ambient-notification", data: data});
apiPort.postMessage({topic: "social.ambient-notification", data: data});
break;
case "test.cookies-get":
apiPort.postMessage({topic: "social.cookies-get"});
break;
case "social.cookies-get-response":
testerPort.postMessage({topic: "test.cookies-get-response", data: data});
}
}
}