OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 | 180 |
181 var pages = Object.create(null); | 181 var pages = Object.create(null); |
182 var pageCounter = 0; | 182 var pageCounter = 0; |
183 | 183 |
184 var Page = function(id, tab, url) | 184 var Page = function(id, tab, url) |
185 { | 185 { |
186 this.id = id; | 186 this.id = id; |
187 this._tab = tab; | 187 this._tab = tab; |
188 this._frames = [{url: new URL(url), parent: null}]; | 188 this._frames = [{url: new URL(url), parent: null}]; |
189 | 189 |
190 if (tab.page) | |
191 this._messageProxy = new ext._MessageProxy(tab.page); | |
192 else | |
193 // while the new tab page is shown on Safari 7, the 'page' property | |
194 // of the tab is undefined, and we can't send messages to that page | |
195 this._messageProxy = { | |
196 handleRequest: function() {}, | |
197 handleResponse: function() {}, | |
198 sendMessage: function() {} | |
199 }; | |
200 | |
201 this.browserAction = new BrowserAction(this); | 190 this.browserAction = new BrowserAction(this); |
202 this.contextMenus = new ContextMenus(this); | 191 this.contextMenus = new ContextMenus(this); |
203 }; | 192 }; |
204 Page.prototype = { | 193 Page.prototype = { |
205 get url() | 194 get url() |
206 { | 195 { |
207 return this._frames[0].url; | 196 return this._frames[0].url; |
208 }, | 197 }, |
209 sendMessage: function(message, responseCallback) | 198 sendMessage: function(message, responseCallback) |
210 { | 199 { |
211 var documentIds = []; | 200 var documentIds = []; |
212 for (var documentId in this._tab._documentLookup) | 201 for (var documentId in this._tab._documentLookup) |
213 if (this._tab._documentLookup[documentId].pageId == this.id) | 202 if (this._tab._documentLookup[documentId].pageId == this.id) |
214 documentIds.push(documentId); | 203 documentIds.push(documentId); |
215 | 204 |
216 this._messageProxy.sendMessage(message, responseCallback, | 205 var messageProxy = this._getMessageProxy(); |
217 {targetDocuments: documentIds}); | 206 if (messageProxy) |
| 207 { |
| 208 messageProxy.sendMessage(message, responseCallback, |
| 209 {targetDocuments: documentIds}); |
| 210 } |
| 211 }, |
| 212 _getMessageProxy: function() |
| 213 { |
| 214 // Instantiate the message proxy only if the page object is available. |
| 215 // For prerendered documents, the page object becomes available only once |
| 216 // the document is made visible. |
| 217 if (!this._messageProxy && this._tab.page) |
| 218 this._messageProxy = new ext._MessageProxy(this._tab.page); |
| 219 |
| 220 return this._messageProxy; |
| 221 }, |
| 222 _handleRequest: function(request, sender) |
| 223 { |
| 224 var messageProxy = this._getMessageProxy(); |
| 225 if (messageProxy) |
| 226 messageProxy.handleRequest(request, sender); |
| 227 }, |
| 228 _handleResponse: function(response) |
| 229 { |
| 230 var messageProxy = this._getMessageProxy(); |
| 231 if (messageProxy) |
| 232 messageProxy.handleResponse(response); |
218 } | 233 } |
219 }; | 234 }; |
220 | 235 |
221 ext.getPage = function(id) | 236 ext.getPage = function(id) |
222 { | 237 { |
223 return pages[id]; | 238 return pages[id]; |
224 }; | 239 }; |
225 | 240 |
226 var isPageActive = function(page) | 241 var isPageActive = function(page) |
227 { | 242 { |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 var response = null; | 432 var response = null; |
418 var sendResponse = function(message) { response = message; }; | 433 var sendResponse = function(message) { response = message; }; |
419 | 434 |
420 ext.onMessage._dispatch(message.payload, sender, sendResponse); | 435 ext.onMessage._dispatch(message.payload, sender, sendResponse); |
421 | 436 |
422 event.message = response; | 437 event.message = response; |
423 break; | 438 break; |
424 } | 439 } |
425 break; | 440 break; |
426 case "request": | 441 case "request": |
427 sender.page._messageProxy.handleRequest(message, sender); | 442 sender.page._handleRequest(message, sender); |
428 break; | 443 break; |
429 case "response": | 444 case "response": |
430 // All documents within a page have the same pageId and that's all we | 445 // All documents within a page have the same pageId and that's all we |
431 // care about here. | 446 // care about here. |
432 var pageId = tab._documentLookup[message.targetDocuments[0]].pageId; | 447 var pageId = tab._documentLookup[message.targetDocuments[0]].pageId; |
433 pages[pageId]._messageProxy.handleResponse(message); | 448 pages[pageId]._handleResponse(message); |
434 break; | 449 break; |
435 case "replaced": | 450 case "replaced": |
436 // when a prerendered page is shown, forget the previous page | 451 // when a prerendered page is shown, forget the previous page |
437 // associated with its tab, and reset the toolbar item if necessary. | 452 // associated with its tab, and reset the toolbar item if necessary. |
438 // Note that it wouldn't be sufficient to do that when the old | 453 // Note that it wouldn't be sufficient to do that when the old |
439 // page is unloading, because Safari dispatches window.onunload | 454 // page is unloading, because Safari dispatches window.onunload |
440 // only when reloading the page or following links, but not when | 455 // only when reloading the page or following links, but not when |
441 // the current page is replaced with a prerendered page. | 456 // the current page is replaced with a prerendered page. |
442 replacePage(sender.page); | 457 replacePage(sender.page); |
443 break; | 458 break; |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 /* Windows */ | 612 /* Windows */ |
598 ext.windows = { | 613 ext.windows = { |
599 // Safari doesn't provide as rich a windows API as Chrome does, so instead | 614 // Safari doesn't provide as rich a windows API as Chrome does, so instead |
600 // of chrome.windows.create we have to fall back to just opening a new tab. | 615 // of chrome.windows.create we have to fall back to just opening a new tab. |
601 create: function(createData, callback) | 616 create: function(createData, callback) |
602 { | 617 { |
603 ext.pages.open(createData.url, callback); | 618 ext.pages.open(createData.url, callback); |
604 } | 619 } |
605 }; | 620 }; |
606 })(); | 621 })(); |
OLD | NEW |