Index: safari/ext/background.js |
=================================================================== |
--- a/safari/ext/background.js |
+++ b/safari/ext/background.js |
@@ -182,44 +182,59 @@ |
var pageCounter = 0; |
var Page = function(id, tab, url) |
{ |
this.id = id; |
this._tab = tab; |
this._frames = [{url: new URL(url), parent: null}]; |
- if (tab.page) |
- this._messageProxy = new ext._MessageProxy(tab.page); |
- else |
- // while the new tab page is shown on Safari 7, the 'page' property |
- // of the tab is undefined, and we can't send messages to that page |
- this._messageProxy = { |
- handleRequest: function() {}, |
- handleResponse: function() {}, |
- sendMessage: function() {} |
- }; |
- |
this.browserAction = new BrowserAction(this); |
this.contextMenus = new ContextMenus(this); |
kzar
2017/05/19 12:33:05
I guess we should assign this._messageProxy to und
Manish Jethani
2017/05/19 23:58:48
Yeah, it'll always return undefined or a non-null
|
}; |
Page.prototype = { |
get url() |
{ |
return this._frames[0].url; |
}, |
sendMessage: function(message, responseCallback) |
{ |
var documentIds = []; |
for (var documentId in this._tab._documentLookup) |
if (this._tab._documentLookup[documentId].pageId == this.id) |
documentIds.push(documentId); |
- this._messageProxy.sendMessage(message, responseCallback, |
- {targetDocuments: documentIds}); |
+ var messageProxy = this._getMessageProxy(); |
+ if (messageProxy) |
+ { |
+ messageProxy.sendMessage(message, responseCallback, |
+ {targetDocuments: documentIds}); |
+ } |
+ }, |
+ _getMessageProxy: function() |
+ { |
+ // Instantiate the message proxy only if the page object is available. |
+ // For prerendered documents, the page object becomes available only once |
+ // the document is made visible. |
+ if (!this._messageProxy && this._tab.page) |
+ this._messageProxy = new ext._MessageProxy(this._tab.page); |
+ |
+ return this._messageProxy; |
+ }, |
+ _handleRequest: function(request, sender) |
+ { |
+ var messageProxy = this._getMessageProxy(); |
+ if (messageProxy) |
+ messageProxy.handleRequest(request, sender); |
+ }, |
+ _handleResponse: function(response) |
+ { |
+ var messageProxy = this._getMessageProxy(); |
+ if (messageProxy) |
+ messageProxy.handleResponse(response); |
} |
}; |
ext.getPage = function(id) |
{ |
return pages[id]; |
}; |
@@ -419,23 +434,23 @@ |
ext.onMessage._dispatch(message.payload, sender, sendResponse); |
event.message = response; |
break; |
} |
break; |
case "request": |
- sender.page._messageProxy.handleRequest(message, sender); |
+ sender.page._handleRequest(message, sender); |
break; |
case "response": |
// All documents within a page have the same pageId and that's all we |
// care about here. |
var pageId = tab._documentLookup[message.targetDocuments[0]].pageId; |
- pages[pageId]._messageProxy.handleResponse(message); |
+ pages[pageId]._handleResponse(message); |
break; |
case "replaced": |
// when a prerendered page is shown, forget the previous page |
// associated with its tab, and reset the toolbar item if necessary. |
// Note that it wouldn't be sufficient to do that when the old |
// page is unloading, because Safari dispatches window.onunload |
// only when reloading the page or following links, but not when |
// the current page is replaced with a prerendered page. |