diff --git a/toolkit/components/social/FrameWorker.jsm b/toolkit/components/social/FrameWorker.jsm index fc9fff8350d..3a21a6a20d0 100644 --- a/toolkit/components/social/FrameWorker.jsm +++ b/toolkit/components/social/FrameWorker.jsm @@ -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. diff --git a/toolkit/components/social/WorkerAPI.jsm b/toolkit/components/social/WorkerAPI.jsm index f9fc4626fb4..564c96ae608 100644 --- a/toolkit/components/social/WorkerAPI.jsm +++ b/toolkit/components/social/WorkerAPI.jsm @@ -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) { diff --git a/toolkit/components/social/test/browser/browser_workerAPI.js b/toolkit/components/social/test/browser/browser_workerAPI.js index 1cf3c7c1c04..06dd0ba100e 100644 --- a/toolkit/components/social/test/browser/browser_workerAPI.js +++ b/toolkit/components/social/test/browser/browser_workerAPI.js @@ -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"}); } + }; diff --git a/toolkit/components/social/test/browser/worker_social.js b/toolkit/components/social/test/browser/worker_social.js index f0c3169fc64..8890dd5391a 100644 --- a/toolkit/components/social/test/browser/worker_social.js +++ b/toolkit/components/social/test/browser/worker_social.js @@ -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}); } } }