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 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | MARIONETTE_CONTEXT = "chrome"; |
michael@0 | 5 | |
michael@0 | 6 | let Promise = Cu.import("resource://gre/modules/Promise.jsm").Promise; |
michael@0 | 7 | |
michael@0 | 8 | /** |
michael@0 | 9 | * Name space for MobileMessageDB.jsm. Only initialized after first call to |
michael@0 | 10 | * newMobileMessageDB. |
michael@0 | 11 | */ |
michael@0 | 12 | let MMDB; |
michael@0 | 13 | |
michael@0 | 14 | /** |
michael@0 | 15 | * Create a new MobileMessageDB instance. |
michael@0 | 16 | * |
michael@0 | 17 | * @return A MobileMessageDB instance. |
michael@0 | 18 | */ |
michael@0 | 19 | function newMobileMessageDB() { |
michael@0 | 20 | if (!MMDB) { |
michael@0 | 21 | MMDB = Cu.import("resource://gre/modules/MobileMessageDB.jsm", {}); |
michael@0 | 22 | is(typeof MMDB.MobileMessageDB, "function", "MMDB.MobileMessageDB"); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | let mmdb = new MMDB.MobileMessageDB(); |
michael@0 | 26 | ok(mmdb, "MobileMessageDB instance"); |
michael@0 | 27 | return mmdb; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | /** |
michael@0 | 31 | * Initialize a MobileMessageDB. Resolve if initialized with success, reject |
michael@0 | 32 | * otherwise. |
michael@0 | 33 | * |
michael@0 | 34 | * Fulfill params: a MobileMessageDB instance. |
michael@0 | 35 | * Reject params: a MobileMessageDB instance. |
michael@0 | 36 | * |
michael@0 | 37 | * @param aMmdb |
michael@0 | 38 | * A MobileMessageDB instance. |
michael@0 | 39 | * @param aDbName |
michael@0 | 40 | * A string name for that database. |
michael@0 | 41 | * @param aDbVersion |
michael@0 | 42 | * The version that MobileMessageDB should upgrade to. 0 for the lastest |
michael@0 | 43 | * version. |
michael@0 | 44 | * |
michael@0 | 45 | * @return A deferred promise. |
michael@0 | 46 | */ |
michael@0 | 47 | function initMobileMessageDB(aMmdb, aDbName, aDbVersion) { |
michael@0 | 48 | let deferred = Promise.defer(); |
michael@0 | 49 | |
michael@0 | 50 | aMmdb.init(aDbName, aDbVersion, function(aError) { |
michael@0 | 51 | if (aError) { |
michael@0 | 52 | deferred.reject(aMmdb); |
michael@0 | 53 | } else { |
michael@0 | 54 | deferred.resolve(aMmdb); |
michael@0 | 55 | } |
michael@0 | 56 | }); |
michael@0 | 57 | |
michael@0 | 58 | return deferred.promise; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | /** |
michael@0 | 62 | * Close a MobileMessageDB. |
michael@0 | 63 | * |
michael@0 | 64 | * @param aMmdb |
michael@0 | 65 | * A MobileMessageDB instance. |
michael@0 | 66 | * |
michael@0 | 67 | * @return The passed MobileMessageDB instance. |
michael@0 | 68 | */ |
michael@0 | 69 | function closeMobileMessageDB(aMmdb) { |
michael@0 | 70 | aMmdb.close(); |
michael@0 | 71 | return aMmdb; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Utility function for calling MMDB methods that takes either a |
michael@0 | 76 | * nsIRilMobileMessageDatabaseCallback or a |
michael@0 | 77 | * nsIRilMobileMessageDatabaseRecordCallback. |
michael@0 | 78 | * |
michael@0 | 79 | * Resolve when the target method notifies us with a successful result code; |
michael@0 | 80 | * reject otherwise. In either case, the arguments passed are packed into an |
michael@0 | 81 | * array and propagated to next action. |
michael@0 | 82 | * |
michael@0 | 83 | * Fulfill params: an array whose elements are the arguments of the original |
michael@0 | 84 | * callback. |
michael@0 | 85 | * Reject params: same as fulfill params. |
michael@0 | 86 | * |
michael@0 | 87 | * @param aMmdb |
michael@0 | 88 | * A MobileMessageDB instance. |
michael@0 | 89 | * @param aMethodName |
michael@0 | 90 | * A string name for that target method. |
michael@0 | 91 | * @param ... |
michael@0 | 92 | * Extra arguments to pass to that target method. The last callback |
michael@0 | 93 | * argument should always be excluded. |
michael@0 | 94 | * |
michael@0 | 95 | * @return A deferred promise. |
michael@0 | 96 | */ |
michael@0 | 97 | function callMmdbMethod(aMmdb, aMethodName) { |
michael@0 | 98 | let deferred = Promise.defer(); |
michael@0 | 99 | |
michael@0 | 100 | let args = Array.slice(arguments, 2); |
michael@0 | 101 | args.push({ |
michael@0 | 102 | notify: function(aRv) { |
michael@0 | 103 | if (!Components.isSuccessCode(aRv)) { |
michael@0 | 104 | ok(true, aMethodName + " returns a unsuccessful code: " + aRv); |
michael@0 | 105 | deferred.reject(Array.slice(arguments)); |
michael@0 | 106 | } else { |
michael@0 | 107 | ok(true, aMethodName + " returns a successful code: " + aRv); |
michael@0 | 108 | deferred.resolve(Array.slice(arguments)); |
michael@0 | 109 | } |
michael@0 | 110 | } |
michael@0 | 111 | }); |
michael@0 | 112 | aMmdb[aMethodName].apply(aMmdb, args); |
michael@0 | 113 | |
michael@0 | 114 | return deferred.promise; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | /** |
michael@0 | 118 | * A convenient function for calling |mmdb.saveSendingMessage(...)|. |
michael@0 | 119 | * |
michael@0 | 120 | * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. |
michael@0 | 121 | * Reject params: same as fulfill params. |
michael@0 | 122 | * |
michael@0 | 123 | * @return A deferred promise. |
michael@0 | 124 | */ |
michael@0 | 125 | function saveSendingMessage(aMmdb, aMessage) { |
michael@0 | 126 | return callMmdbMethod(aMmdb, "saveSendingMessage", aMessage); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | /** |
michael@0 | 130 | * A convenient function for calling |mmdb.saveReceivedMessage(...)|. |
michael@0 | 131 | * |
michael@0 | 132 | * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. |
michael@0 | 133 | * Reject params: same as fulfill params. |
michael@0 | 134 | * |
michael@0 | 135 | * @return A deferred promise. |
michael@0 | 136 | */ |
michael@0 | 137 | function saveReceivedMessage(aMmdb, aMessage) { |
michael@0 | 138 | return callMmdbMethod(aMmdb, "saveReceivedMessage", aMessage); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | /** |
michael@0 | 142 | * A convenient function for calling |mmdb.setMessageDeliveryByMessageId(...)|. |
michael@0 | 143 | * |
michael@0 | 144 | * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. |
michael@0 | 145 | * Reject params: same as fulfill params. |
michael@0 | 146 | * |
michael@0 | 147 | * @return A deferred promise. |
michael@0 | 148 | */ |
michael@0 | 149 | function setMessageDeliveryByMessageId(aMmdb, aMessageId, aReceiver, aDelivery, |
michael@0 | 150 | aDeliveryStatus, aEnvelopeId) { |
michael@0 | 151 | return callMmdbMethod(aMmdb, "setMessageDeliveryByMessageId", aMessageId, |
michael@0 | 152 | aReceiver, aDelivery, aDeliveryStatus, aEnvelopeId); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | /** |
michael@0 | 156 | * A convenient function for calling |
michael@0 | 157 | * |mmdb.setMessageDeliveryStatusByEnvelopeId(...)|. |
michael@0 | 158 | * |
michael@0 | 159 | * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. |
michael@0 | 160 | * Reject params: same as fulfill params. |
michael@0 | 161 | * |
michael@0 | 162 | * @return A deferred promise. |
michael@0 | 163 | */ |
michael@0 | 164 | function setMessageDeliveryStatusByEnvelopeId(aMmdb, aEnvelopeId, aReceiver, |
michael@0 | 165 | aDeliveryStatus) { |
michael@0 | 166 | return callMmdbMethod(aMmdb, "setMessageDeliveryStatusByEnvelopeId", |
michael@0 | 167 | aMmdb, aEnvelopeId, aReceiver, aDeliveryStatus); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | /** |
michael@0 | 171 | * A convenient function for calling |
michael@0 | 172 | * |mmdb.setMessageReadStatusByEnvelopeId(...)|. |
michael@0 | 173 | * |
michael@0 | 174 | * Fulfill params: [<Cr.NS_ERROR_FOO>, <DOM message>]. |
michael@0 | 175 | * Reject params: same as fulfill params. |
michael@0 | 176 | * |
michael@0 | 177 | * @return A deferred promise. |
michael@0 | 178 | */ |
michael@0 | 179 | function setMessageReadStatusByEnvelopeId(aMmdb, aEnvelopeId, aReceiver, |
michael@0 | 180 | aReadStatus) { |
michael@0 | 181 | return callMmdbMethod(aMmdb, "setMessageReadStatusByEnvelopeId", |
michael@0 | 182 | aEnvelopeId, aReceiver, aReadStatus); |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | /** |
michael@0 | 186 | * A convenient function for calling |
michael@0 | 187 | * |mmdb.getMessageRecordByTransactionId(...)|. |
michael@0 | 188 | * |
michael@0 | 189 | * Fulfill params: [<Cr.NS_ERROR_FOO>, <DB Record>, <DOM message>]. |
michael@0 | 190 | * Reject params: same as fulfill params. |
michael@0 | 191 | * |
michael@0 | 192 | * @return A deferred promise. |
michael@0 | 193 | */ |
michael@0 | 194 | function getMessageRecordByTransactionId(aMmdb, aTransactionId) { |
michael@0 | 195 | return callMmdbMethod(aMmdb, "getMessageRecordByTransactionId", |
michael@0 | 196 | aTransactionId); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | /** |
michael@0 | 200 | * A convenient function for calling |mmdb.getMessageRecordById(...)|. |
michael@0 | 201 | * |
michael@0 | 202 | * Fulfill params: [<Cr.NS_ERROR_FOO>, <DB Record>, <DOM message>]. |
michael@0 | 203 | * Reject params: same as fulfill params. |
michael@0 | 204 | * |
michael@0 | 205 | * @return A deferred promise. |
michael@0 | 206 | */ |
michael@0 | 207 | function getMessageRecordById(aMmdb, aMessageId) { |
michael@0 | 208 | return callMmdbMethod(aMmdb, "getMessageRecordById", aMessageId); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | /** |
michael@0 | 212 | * A convenient function for calling |mmdb.markMessageRead(...)|. |
michael@0 | 213 | * |
michael@0 | 214 | * Fulfill params: Ci.nsIMobileMessageCallback.FOO. |
michael@0 | 215 | * Reject params: same as fulfill params. |
michael@0 | 216 | * |
michael@0 | 217 | * @return A deferred promise. |
michael@0 | 218 | */ |
michael@0 | 219 | function markMessageRead(aMmdb, aMessageId, aRead) { |
michael@0 | 220 | let deferred = Promise.defer(); |
michael@0 | 221 | |
michael@0 | 222 | aMmdb.markMessageRead(aMessageId, aRead, false, { |
michael@0 | 223 | notifyMarkMessageReadFailed: function(aRv) { |
michael@0 | 224 | ok(true, "markMessageRead returns a unsuccessful code: " + aRv); |
michael@0 | 225 | deferred.reject(aRv); |
michael@0 | 226 | }, |
michael@0 | 227 | |
michael@0 | 228 | notifyMessageMarkedRead: function(aRead) { |
michael@0 | 229 | ok(true, "markMessageRead returns a successful code: " + Cr.NS_OK); |
michael@0 | 230 | deferred.resolve(Ci.nsIMobileMessageCallback.SUCCESS_NO_ERROR); |
michael@0 | 231 | } |
michael@0 | 232 | }); |
michael@0 | 233 | |
michael@0 | 234 | return deferred.promise; |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | /** |
michael@0 | 238 | * Utility function for calling cursor-based MMDB methods. |
michael@0 | 239 | * |
michael@0 | 240 | * Resolve when the target method notifies us with |notifyCursorDone|, |
michael@0 | 241 | * reject otherwise. |
michael@0 | 242 | * |
michael@0 | 243 | * Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM message/thread>]] |
michael@0 | 244 | * Reject params: same as fulfill params. |
michael@0 | 245 | * |
michael@0 | 246 | * @param aMmdb |
michael@0 | 247 | * A MobileMessageDB instance. |
michael@0 | 248 | * @param aMethodName |
michael@0 | 249 | * A string name for that target method. |
michael@0 | 250 | * @param ... |
michael@0 | 251 | * Extra arguments to pass to that target method. The last callback |
michael@0 | 252 | * argument should always be excluded. |
michael@0 | 253 | * |
michael@0 | 254 | * @return A deferred promise. |
michael@0 | 255 | */ |
michael@0 | 256 | function createMmdbCursor(aMmdb, aMethodName) { |
michael@0 | 257 | let deferred = Promise.defer(); |
michael@0 | 258 | |
michael@0 | 259 | let cursor; |
michael@0 | 260 | let results = []; |
michael@0 | 261 | let args = Array.slice(arguments, 2); |
michael@0 | 262 | args.push({ |
michael@0 | 263 | notifyCursorError: function(aRv) { |
michael@0 | 264 | ok(true, "notifyCursorError: " + aRv); |
michael@0 | 265 | deferred.reject([aRv, results]); |
michael@0 | 266 | }, |
michael@0 | 267 | |
michael@0 | 268 | notifyCursorResult: function(aResult) { |
michael@0 | 269 | ok(true, "notifyCursorResult: " + aResult.id); |
michael@0 | 270 | results.push(aResult); |
michael@0 | 271 | cursor.handleContinue(); |
michael@0 | 272 | }, |
michael@0 | 273 | |
michael@0 | 274 | notifyCursorDone: function() { |
michael@0 | 275 | ok(true, "notifyCursorDone"); |
michael@0 | 276 | deferred.resolve([Ci.nsIMobileMessageCallback.SUCCESS_NO_ERROR, results]); |
michael@0 | 277 | } |
michael@0 | 278 | }); |
michael@0 | 279 | |
michael@0 | 280 | cursor = aMmdb[aMethodName].apply(aMmdb, args); |
michael@0 | 281 | |
michael@0 | 282 | return deferred.promise; |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | /** |
michael@0 | 286 | * A convenient function for calling |mmdb.createMessageCursor(...)|. |
michael@0 | 287 | * |
michael@0 | 288 | * Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM message>]]. |
michael@0 | 289 | * Reject params: same as fulfill params. |
michael@0 | 290 | * |
michael@0 | 291 | * @return A deferred promise. |
michael@0 | 292 | */ |
michael@0 | 293 | function createMessageCursor(aMmdb, aFilter, aReverse) { |
michael@0 | 294 | return createMmdbCursor(aMmdb, "createMessageCursor", aFilter, aReverse); |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | /** |
michael@0 | 298 | * A convenient function for calling |mmdb.createThreadCursor(...)|. |
michael@0 | 299 | * |
michael@0 | 300 | * Fulfill params: [<Ci.nsIMobileMessageCallback.FOO>, [<DOM thread>]]. |
michael@0 | 301 | * Reject params: same as fulfill params. |
michael@0 | 302 | * |
michael@0 | 303 | * @return A deferred promise. |
michael@0 | 304 | */ |
michael@0 | 305 | function createThreadCursor(aMmdb) { |
michael@0 | 306 | return createMmdbCursor(aMmdb, "createThreadCursor"); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | // A reference to a nsIUUIDGenerator service. |
michael@0 | 310 | let _uuidGenerator; |
michael@0 | 311 | |
michael@0 | 312 | /** |
michael@0 | 313 | * Generate a new UUID. |
michael@0 | 314 | * |
michael@0 | 315 | * @return A UUID string. |
michael@0 | 316 | */ |
michael@0 | 317 | function newUUID() { |
michael@0 | 318 | if (!_uuidGenerator) { |
michael@0 | 319 | _uuidGenerator = Cc["@mozilla.org/uuid-generator;1"] |
michael@0 | 320 | .getService(Ci.nsIUUIDGenerator); |
michael@0 | 321 | ok(_uuidGenerator, "uuidGenerator"); |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | return _uuidGenerator.generateUUID().toString(); |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | /** |
michael@0 | 328 | * Flush permission settings and call |finish()|. |
michael@0 | 329 | */ |
michael@0 | 330 | function cleanUp() { |
michael@0 | 331 | // Use ok here so that we have at least one test run. |
michael@0 | 332 | ok(true, "permissions flushed"); |
michael@0 | 333 | |
michael@0 | 334 | finish(); |
michael@0 | 335 | } |
michael@0 | 336 | |
michael@0 | 337 | /** |
michael@0 | 338 | * Basic test routine helper for mobile message tests. |
michael@0 | 339 | * |
michael@0 | 340 | * This helper does nothing but clean-ups. |
michael@0 | 341 | * |
michael@0 | 342 | * @param aTestCaseMain |
michael@0 | 343 | * A function that takes no parameter. |
michael@0 | 344 | */ |
michael@0 | 345 | function startTestBase(aTestCaseMain) { |
michael@0 | 346 | Promise.resolve() |
michael@0 | 347 | .then(aTestCaseMain) |
michael@0 | 348 | .then(null, function() { |
michael@0 | 349 | ok(false, 'promise rejects during test.'); |
michael@0 | 350 | }) |
michael@0 | 351 | .then(cleanUp); |
michael@0 | 352 | } |