|
264 | 264 | var client = this; |
265 | 265 |
|
266 | 266 | this._listener = function(message) { |
267 | | - var i, error, response; |
| 267 | + var i, origin, error, response; |
268 | 268 |
|
269 | | - // Ignore invalid messages, those not from the correct hub, or when |
270 | | - // the client has closed |
271 | | - if (client._closed || !message.data || typeof message.data !== 'string' || |
272 | | - message.origin !== client._origin) { |
| 269 | + // Ignore invalid messages or those after the client has closed |
| 270 | + if (client._closed || !message.data || typeof message.data !== 'string') { |
273 | 271 | return; |
274 | 272 | } |
275 | 273 |
|
| 274 | + // postMessage returns the string "null" as the origin for "file://" |
| 275 | + origin = (message.origin === 'null') ? 'file://' : message.origin; |
| 276 | + |
| 277 | + // Ignore messages not from the correct origin |
| 278 | + if (origin !== client._origin) return; |
| 279 | + |
276 | 280 | // LocalStorage isn't available in the hub |
277 | 281 | if (message.data === 'cross-storage:unavailable') { |
278 | 282 | if (!client._closed) client.close(); |
|
327 | 331 | * establish a connected state. |
328 | 332 | */ |
329 | 333 | CrossStorageClient.prototype._poll = function() { |
330 | | - var client, interval; |
| 334 | + var client, interval, targetOrigin; |
331 | 335 |
|
332 | 336 | client = this; |
| 337 | + |
| 338 | + // postMessage requires that the target origin be set to "*" for "file://" |
| 339 | + targetOrigin = (client._origin === 'file://') ? '*' : client._origin; |
| 340 | + |
333 | 341 | interval = setInterval(function() { |
334 | 342 | if (client._connected) return clearInterval(interval); |
335 | 343 | if (!client._hub) return; |
336 | 344 |
|
337 | | - client._hub.postMessage('cross-storage:poll', client._origin); |
| 345 | + client._hub.postMessage('cross-storage:poll', targetOrigin); |
338 | 346 | }, 1000); |
339 | 347 | }; |
340 | 348 |
|
|
395 | 403 | }; |
396 | 404 |
|
397 | 405 | return new this._promise(function(resolve, reject) { |
398 | | - var timeout, originalToJSON; |
| 406 | + var timeout, originalToJSON, targetOrigin; |
399 | 407 |
|
400 | 408 | // Timeout if a response isn't received after 4s |
401 | 409 | timeout = setTimeout(function() { |
|
419 | 427 | Array.prototype.toJSON = null; |
420 | 428 | } |
421 | 429 |
|
| 430 | + // postMessage requires that the target origin be set to "*" for "file://" |
| 431 | + targetOrigin = (client._origin === 'file://') ? '*' : client._origin; |
| 432 | + |
422 | 433 | // Send serialized message |
423 | | - client._hub.postMessage(JSON.stringify(req), client._origin); |
| 434 | + client._hub.postMessage(JSON.stringify(req), targetOrigin); |
424 | 435 |
|
425 | 436 | // Restore original toJSON |
426 | 437 | if (originalToJSON) { |
|
0 commit comments