diff --git a/platform/safari/vapi-background.js b/platform/safari/vapi-background.js index 5ccbf755520af..e3caca10fd5c5 100644 --- a/platform/safari/vapi-background.js +++ b/platform/safari/vapi-background.js @@ -828,7 +828,7 @@ CallbackWrapper.prototype.init = function(port, request, timeout) { this.port = port; - this.request = request; + this.request = request || port; this.timerId = timeout !== undefined ? vAPI.setTimeout(this.callback, timeout) : null; @@ -841,7 +841,7 @@ delete toAuxPending[this.timerId]; this.timerId = null; } - this.port.dispatchMessage(this.request.name, { + this.port.target.page.dispatchMessage(this.request.name, { auxProcessId: this.request.message.auxProcessId, channelName: this.request.message.channelName, msg: response !== undefined ? response : null @@ -931,7 +931,7 @@ // Auxiliary process to auxiliary process if ( message.toTabId !== undefined ) { // TODO: this doesn't work. - toAux(message, request.target.page); + toAux(message, request); return; } @@ -944,7 +944,7 @@ // Auxiliary process to main process: prepare response var callback = messaging.NOOPFUNC; if ( message.auxProcessId !== undefined ) { - callback = callbackWrapperFactory(request.target.page, request).callback; + callback = callbackWrapperFactory(request).callback; } var sender = { diff --git a/platform/safari/vapi-client.js b/platform/safari/vapi-client.js index c12daaf7fcd36..5b96ba2f37bec 100644 --- a/platform/safari/vapi-client.js +++ b/platform/safari/vapi-client.js @@ -319,8 +319,11 @@ vAPI.messaging = { channels: Object.create(null), channelCount: 0, + // Waiting for response pending: Object.create(null), pendingCount: 0, + // Waiting to send + queued: [], auxProcessId: 1, shuttingDown: false, @@ -414,6 +417,16 @@ Function(msg.details.code).call(self); } }); + // Handle queued messages when page becomes visible + document.addEventListener('visibilitychange', function() { + if (document.hidden === true) return; + var message; + for (var i = 0; i < this.queued.length; i++) { + message = this.queued[i]; + this.postMessage(message.auxProcessId, message); + } + this.queued.length = 0; + }.bind(this)); }, send: function(channelName, message, callback) { @@ -437,14 +450,19 @@ if (!this.connector) { this.connect(); } - this.postMessage(auxProcessId, { + message = { channelName: channelName, auxProcessId: auxProcessId, toTabId: toTabId, toChannel: toChannel, msg: message - }); - + }; + if (document.hidden === true) { + // Wait for visibility change + this.queued.push(message); + } else { + this.postMessage(auxProcessId, message); + } }, postMessage: function(auxProcessId, message) {