Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | const events = require("sdk/system/events"); |
michael@0 | 6 | const self = require("sdk/self"); |
michael@0 | 7 | const { Cc, Ci, Cu } = require("chrome"); |
michael@0 | 8 | const { setTimeout } = require("sdk/timers"); |
michael@0 | 9 | const { Loader, LoaderWithHookedConsole2 } = require("sdk/test/loader"); |
michael@0 | 10 | const nsIObserverService = Cc["@mozilla.org/observer-service;1"]. |
michael@0 | 11 | getService(Ci.nsIObserverService); |
michael@0 | 12 | |
michael@0 | 13 | let isConsoleEvent = (topic) => |
michael@0 | 14 | !!~["console-api-log-event", "console-storage-cache-event"].indexOf(topic) |
michael@0 | 15 | |
michael@0 | 16 | exports["test basic"] = function(assert) { |
michael@0 | 17 | let type = Date.now().toString(32); |
michael@0 | 18 | |
michael@0 | 19 | let timesCalled = 0; |
michael@0 | 20 | function handler({subject, data}) { timesCalled++; }; |
michael@0 | 21 | |
michael@0 | 22 | events.on(type, handler); |
michael@0 | 23 | events.emit(type, { data: "yo yo" }); |
michael@0 | 24 | |
michael@0 | 25 | assert.equal(timesCalled, 1, "event handler was called"); |
michael@0 | 26 | |
michael@0 | 27 | events.off(type, handler); |
michael@0 | 28 | events.emit(type, { data: "no way" }); |
michael@0 | 29 | |
michael@0 | 30 | assert.equal(timesCalled, 1, "removed handler is no longer called"); |
michael@0 | 31 | |
michael@0 | 32 | events.once(type, handler); |
michael@0 | 33 | events.emit(type, { data: "and we meet again" }); |
michael@0 | 34 | events.emit(type, { data: "it's always hard to say bye" }); |
michael@0 | 35 | |
michael@0 | 36 | assert.equal(timesCalled, 2, "handlers added via once are triggered once"); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | exports["test simple argument passing"] = function (assert) { |
michael@0 | 40 | let type = Date.now().toString(32); |
michael@0 | 41 | |
michael@0 | 42 | let lastArg; |
michael@0 | 43 | function handler({data}) { lastArg = data; } |
michael@0 | 44 | events.on(type, handler); |
michael@0 | 45 | |
michael@0 | 46 | [true, false, 100, 0, 'a string', ''].forEach(arg => { |
michael@0 | 47 | events.emit(type, arg); |
michael@0 | 48 | assert.strictEqual(lastArg, arg + '', |
michael@0 | 49 | 'event emitted for ' + arg + ' has correct data value'); |
michael@0 | 50 | |
michael@0 | 51 | events.emit(type, { data: arg }); |
michael@0 | 52 | assert.strictEqual(lastArg, arg + '', |
michael@0 | 53 | 'event emitted for ' + arg + ' has correct data value when a property on an object'); |
michael@0 | 54 | }); |
michael@0 | 55 | |
michael@0 | 56 | [null, undefined, {}].forEach(arg => { |
michael@0 | 57 | events.emit(type, arg); |
michael@0 | 58 | assert.strictEqual(lastArg, null, |
michael@0 | 59 | 'emitting ' + arg + ' gets null data'); |
michael@0 | 60 | }); |
michael@0 | 61 | |
michael@0 | 62 | events.off(type, handler); |
michael@0 | 63 | }; |
michael@0 | 64 | |
michael@0 | 65 | exports["test error reporting"] = function(assert) { |
michael@0 | 66 | let { loader, messages } = LoaderWithHookedConsole2(module); |
michael@0 | 67 | |
michael@0 | 68 | let events = loader.require("sdk/system/events"); |
michael@0 | 69 | function brokenHandler(subject, data) { throw new Error("foo"); }; |
michael@0 | 70 | |
michael@0 | 71 | let lineNumber; |
michael@0 | 72 | try { brokenHandler() } catch (error) { lineNumber = error.lineNumber } |
michael@0 | 73 | |
michael@0 | 74 | let errorType = Date.now().toString(32); |
michael@0 | 75 | |
michael@0 | 76 | events.on(errorType, brokenHandler); |
michael@0 | 77 | events.emit(errorType, { data: "yo yo" }); |
michael@0 | 78 | |
michael@0 | 79 | assert.equal(messages.length, 2, "Got an exception"); |
michael@0 | 80 | assert.equal(messages[0], "console.error: " + self.name + ": \n", |
michael@0 | 81 | "error is logged"); |
michael@0 | 82 | let text = messages[1]; |
michael@0 | 83 | assert.ok(text.indexOf("Error: foo") >= 0, "error message is logged"); |
michael@0 | 84 | assert.ok(text.indexOf(module.uri) >= 0, "module uri is logged"); |
michael@0 | 85 | assert.ok(text.indexOf(lineNumber) >= 0, "error line is logged"); |
michael@0 | 86 | |
michael@0 | 87 | events.off(errorType, brokenHandler); |
michael@0 | 88 | |
michael@0 | 89 | loader.unload(); |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | exports["test listeners are GC-ed"] = function(assert, done) { |
michael@0 | 93 | let receivedFromWeak = []; |
michael@0 | 94 | let receivedFromStrong = []; |
michael@0 | 95 | let loader = Loader(module); |
michael@0 | 96 | let events = loader.require('sdk/system/events'); |
michael@0 | 97 | |
michael@0 | 98 | let type = 'test-listeners-are-garbage-collected'; |
michael@0 | 99 | function handler(event) { receivedFromStrong.push(event); } |
michael@0 | 100 | function weakHandler(event) { receivedFromWeak.push(event); } |
michael@0 | 101 | |
michael@0 | 102 | events.on(type, handler, true); |
michael@0 | 103 | events.on(type, weakHandler); |
michael@0 | 104 | |
michael@0 | 105 | events.emit(type, { data: 1 }); |
michael@0 | 106 | assert.equal(receivedFromStrong.length, 1, "strong listener invoked"); |
michael@0 | 107 | assert.equal(receivedFromWeak.length, 1, "weak listener invoked"); |
michael@0 | 108 | |
michael@0 | 109 | handler = weakHandler = null; |
michael@0 | 110 | |
michael@0 | 111 | Cu.schedulePreciseGC(function() { |
michael@0 | 112 | events.emit(type, { data: 2 }); |
michael@0 | 113 | |
michael@0 | 114 | assert.equal(receivedFromWeak.length, 1, "weak listener was GC-ed"); |
michael@0 | 115 | assert.equal(receivedFromStrong.length, 2, "strong listener was invoked"); |
michael@0 | 116 | |
michael@0 | 117 | loader.unload(); |
michael@0 | 118 | done(); |
michael@0 | 119 | }); |
michael@0 | 120 | }; |
michael@0 | 121 | |
michael@0 | 122 | exports["test alive listeners are removed on unload"] = function(assert) { |
michael@0 | 123 | let receivedFromWeak = []; |
michael@0 | 124 | let receivedFromStrong = []; |
michael@0 | 125 | let loader = Loader(module); |
michael@0 | 126 | let events = loader.require('sdk/system/events'); |
michael@0 | 127 | |
michael@0 | 128 | let type = 'test-alive-listeners-are-removed'; |
michael@0 | 129 | const handler = (event) => receivedFromStrong.push(event); |
michael@0 | 130 | const weakHandler = (event) => receivedFromWeak.push(event); |
michael@0 | 131 | |
michael@0 | 132 | events.on(type, handler, true); |
michael@0 | 133 | events.on(type, weakHandler); |
michael@0 | 134 | |
michael@0 | 135 | events.emit(type, { data: 1 }); |
michael@0 | 136 | assert.equal(receivedFromStrong.length, 1, "strong listener invoked"); |
michael@0 | 137 | assert.equal(receivedFromWeak.length, 1, "weak listener invoked"); |
michael@0 | 138 | |
michael@0 | 139 | loader.unload(); |
michael@0 | 140 | events.emit(type, { data: 2 }); |
michael@0 | 141 | |
michael@0 | 142 | assert.equal(receivedFromWeak.length, 1, "weak listener was removed"); |
michael@0 | 143 | assert.equal(receivedFromStrong.length, 1, "strong listener was removed"); |
michael@0 | 144 | }; |
michael@0 | 145 | |
michael@0 | 146 | exports["test handle nsIObserverService notifications"] = function(assert) { |
michael@0 | 147 | let ios = Cc['@mozilla.org/network/io-service;1'] |
michael@0 | 148 | .getService(Ci.nsIIOService); |
michael@0 | 149 | |
michael@0 | 150 | let uri = ios.newURI("http://www.foo.com", null, null); |
michael@0 | 151 | |
michael@0 | 152 | let type = Date.now().toString(32); |
michael@0 | 153 | let timesCalled = 0; |
michael@0 | 154 | let lastSubject = null; |
michael@0 | 155 | let lastData = null; |
michael@0 | 156 | let lastType = null; |
michael@0 | 157 | |
michael@0 | 158 | function handler({ subject, data, type }) { |
michael@0 | 159 | // Ignores internal console events |
michael@0 | 160 | if (isConsoleEvent(type)) |
michael@0 | 161 | return; |
michael@0 | 162 | timesCalled++; |
michael@0 | 163 | lastSubject = subject; |
michael@0 | 164 | lastData = data; |
michael@0 | 165 | lastType = type; |
michael@0 | 166 | }; |
michael@0 | 167 | |
michael@0 | 168 | events.on(type, handler); |
michael@0 | 169 | nsIObserverService.notifyObservers(uri, type, "some data"); |
michael@0 | 170 | |
michael@0 | 171 | assert.equal(timesCalled, 1, "notification invokes handler"); |
michael@0 | 172 | assert.equal(lastType, type, "event.type is notification topic"); |
michael@0 | 173 | assert.equal(lastSubject, uri, "event.subject is notification subject"); |
michael@0 | 174 | assert.equal(lastData, "some data", "event.data is notification data"); |
michael@0 | 175 | |
michael@0 | 176 | function customSubject() {} |
michael@0 | 177 | function customData() {} |
michael@0 | 178 | |
michael@0 | 179 | events.emit(type, { data: customData, subject: customSubject }); |
michael@0 | 180 | |
michael@0 | 181 | assert.equal(timesCalled, 2, "notification invokes handler"); |
michael@0 | 182 | assert.equal(lastType, type, "event.type is notification topic"); |
michael@0 | 183 | assert.equal(lastSubject, customSubject, |
michael@0 | 184 | "event.subject is wrapped & unwrapped"); |
michael@0 | 185 | assert.equal(lastData, customData, "event.data is wrapped & unwrapped"); |
michael@0 | 186 | |
michael@0 | 187 | events.off(type, handler); |
michael@0 | 188 | |
michael@0 | 189 | nsIObserverService.notifyObservers(null, type, "some data"); |
michael@0 | 190 | |
michael@0 | 191 | assert.equal(timesCalled, 2, "event handler is removed"); |
michael@0 | 192 | |
michael@0 | 193 | events.on("*", handler); |
michael@0 | 194 | |
michael@0 | 195 | nsIObserverService.notifyObservers(null, type, "more data"); |
michael@0 | 196 | |
michael@0 | 197 | assert.equal(timesCalled, 3, "notification invokes * handler"); |
michael@0 | 198 | assert.equal(lastType, type, "event.type is notification topic"); |
michael@0 | 199 | assert.equal(lastSubject, null, |
michael@0 | 200 | "event.subject is notification subject"); |
michael@0 | 201 | assert.equal(lastData, "more data", "event.data is notification data"); |
michael@0 | 202 | |
michael@0 | 203 | events.off("*", handler); |
michael@0 | 204 | |
michael@0 | 205 | nsIObserverService.notifyObservers(null, type, "last data"); |
michael@0 | 206 | |
michael@0 | 207 | assert.equal(timesCalled, 3, "* event handler is removed"); |
michael@0 | 208 | }; |
michael@0 | 209 | |
michael@0 | 210 | exports["test emit to nsIObserverService observers"] = function(assert) { |
michael@0 | 211 | let ios = Cc['@mozilla.org/network/io-service;1'] |
michael@0 | 212 | .getService(Ci.nsIIOService); |
michael@0 | 213 | |
michael@0 | 214 | let uri = ios.newURI("http://www.foo.com", null, null); |
michael@0 | 215 | let timesCalled = 0; |
michael@0 | 216 | let lastSubject = null; |
michael@0 | 217 | let lastData = null; |
michael@0 | 218 | let lastTopic = null; |
michael@0 | 219 | |
michael@0 | 220 | var topic = Date.now().toString(32) |
michael@0 | 221 | let nsIObserver = { |
michael@0 | 222 | QueryInterface: function() { |
michael@0 | 223 | return nsIObserver; |
michael@0 | 224 | }, |
michael@0 | 225 | observe: function(subject, topic, data) { |
michael@0 | 226 | // Ignores internal console events |
michael@0 | 227 | if (isConsoleEvent(topic)) |
michael@0 | 228 | return; |
michael@0 | 229 | timesCalled = timesCalled + 1; |
michael@0 | 230 | lastSubject = subject; |
michael@0 | 231 | lastData = data; |
michael@0 | 232 | lastTopic = topic; |
michael@0 | 233 | } |
michael@0 | 234 | }; |
michael@0 | 235 | |
michael@0 | 236 | nsIObserverService.addObserver(nsIObserver, topic, false); |
michael@0 | 237 | |
michael@0 | 238 | events.emit(topic, { subject: uri, data: "some data" }); |
michael@0 | 239 | |
michael@0 | 240 | assert.equal(timesCalled, 1, "emit notifies observers"); |
michael@0 | 241 | assert.equal(lastTopic, topic, "event type is notification topic"); |
michael@0 | 242 | assert.equal(lastSubject.wrappedJSObject.object, uri, |
michael@0 | 243 | "event.subject is notification subject"); |
michael@0 | 244 | assert.equal(lastData, "some data", "event.data is notification data"); |
michael@0 | 245 | function customSubject() {} |
michael@0 | 246 | function customData() {} |
michael@0 | 247 | events.emit(topic, { subject: customSubject, data: customData }); |
michael@0 | 248 | |
michael@0 | 249 | assert.equal(timesCalled, 2, "emit notifies observers"); |
michael@0 | 250 | assert.equal(lastTopic, topic, "event.type is notification"); |
michael@0 | 251 | assert.equal(lastSubject.wrappedJSObject.object, customSubject, |
michael@0 | 252 | "event.subject is notification subject"); |
michael@0 | 253 | assert.equal(lastData, customData, "event.data is notification data"); |
michael@0 | 254 | |
michael@0 | 255 | nsIObserverService.removeObserver(nsIObserver, topic); |
michael@0 | 256 | |
michael@0 | 257 | events.emit(topic, { data: "more data" }); |
michael@0 | 258 | |
michael@0 | 259 | assert.equal(timesCalled, 2, "removed observers no longer invoked"); |
michael@0 | 260 | |
michael@0 | 261 | nsIObserverService.addObserver(nsIObserver, "*", false); |
michael@0 | 262 | |
michael@0 | 263 | events.emit(topic, { data: "data again" }); |
michael@0 | 264 | |
michael@0 | 265 | assert.equal(timesCalled, 3, "emit notifies * observers"); |
michael@0 | 266 | |
michael@0 | 267 | assert.equal(lastTopic, topic, "event.type is notification"); |
michael@0 | 268 | assert.equal(lastSubject, null, |
michael@0 | 269 | "event.subject is notification subject"); |
michael@0 | 270 | assert.equal(lastData, "data again", "event.data is notification data"); |
michael@0 | 271 | |
michael@0 | 272 | nsIObserverService.removeObserver(nsIObserver, "*"); |
michael@0 | 273 | |
michael@0 | 274 | events.emit(topic, { data: "last data" }); |
michael@0 | 275 | assert.equal(timesCalled, 3, "removed observers no longer invoked"); |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | require("test").run(exports); |