| 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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 /* Pages */ | 179 /* Pages */ |
| 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 this._messageProxy = null; |
| 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 | 190 |
| 201 this.browserAction = new BrowserAction(this); | 191 this.browserAction = new BrowserAction(this); |
| 202 this.contextMenus = new ContextMenus(this); | 192 this.contextMenus = new ContextMenus(this); |
| 203 }; | 193 }; |
| 204 Page.prototype = { | 194 Page.prototype = { |
| 205 get url() | 195 get url() |
| 206 { | 196 { |
| 207 return this._frames[0].url; | 197 return this._frames[0].url; |
| 208 }, | 198 }, |
| 209 sendMessage: function(message, responseCallback) | 199 sendMessage: function(message, responseCallback) |
| 210 { | 200 { |
| 211 var documentIds = []; | 201 var documentIds = []; |
| 212 for (var documentId in this._tab._documentLookup) | 202 for (var documentId in this._tab._documentLookup) |
| 213 if (this._tab._documentLookup[documentId].pageId == this.id) | 203 if (this._tab._documentLookup[documentId].pageId == this.id) |
| 214 documentIds.push(documentId); | 204 documentIds.push(documentId); |
| 215 | 205 |
| 216 this._messageProxy.sendMessage(message, responseCallback, | 206 var messageProxy = this._getMessageProxy(); |
| 217 {targetDocuments: documentIds}); | 207 if (messageProxy) |
| 208 { |
| 209 messageProxy.sendMessage(message, responseCallback, |
| 210 {targetDocuments: documentIds}); |
| 211 } |
| 212 }, |
| 213 _getMessageProxy: function() |
| 214 { |
| 215 // Instantiate the message proxy only if the page object is available. |
| 216 // For prerendered documents, the page object becomes available only once |
| 217 // the document is made visible. |
| 218 if (!this._messageProxy && this._tab.page) |
| 219 this._messageProxy = new ext._MessageProxy(this._tab.page); |
| 220 |
| 221 return this._messageProxy; |
| 222 }, |
| 223 _handleRequest: function(request, sender) |
| 224 { |
| 225 var messageProxy = this._getMessageProxy(); |
| 226 if (messageProxy) |
| 227 messageProxy.handleRequest(request, sender); |
| 228 }, |
| 229 _handleResponse: function(response) |
| 230 { |
| 231 var messageProxy = this._getMessageProxy(); |
| 232 if (messageProxy) |
| 233 messageProxy.handleResponse(response); |
| 218 } | 234 } |
| 219 }; | 235 }; |
| 220 | 236 |
| 221 ext.getPage = function(id) | 237 ext.getPage = function(id) |
| 222 { | 238 { |
| 223 return pages[id]; | 239 return pages[id]; |
| 224 }; | 240 }; |
| 225 | 241 |
| 226 var isPageActive = function(page) | 242 var isPageActive = function(page) |
| 227 { | 243 { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 var response = null; | 433 var response = null; |
| 418 var sendResponse = function(message) { response = message; }; | 434 var sendResponse = function(message) { response = message; }; |
| 419 | 435 |
| 420 ext.onMessage._dispatch(message.payload, sender, sendResponse); | 436 ext.onMessage._dispatch(message.payload, sender, sendResponse); |
| 421 | 437 |
| 422 event.message = response; | 438 event.message = response; |
| 423 break; | 439 break; |
| 424 } | 440 } |
| 425 break; | 441 break; |
| 426 case "request": | 442 case "request": |
| 427 sender.page._messageProxy.handleRequest(message, sender); | 443 sender.page._handleRequest(message, sender); |
| 428 break; | 444 break; |
| 429 case "response": | 445 case "response": |
| 430 // All documents within a page have the same pageId and that's all we | 446 // All documents within a page have the same pageId and that's all we |
| 431 // care about here. | 447 // care about here. |
| 432 var pageId = tab._documentLookup[message.targetDocuments[0]].pageId; | 448 var pageId = tab._documentLookup[message.targetDocuments[0]].pageId; |
| 433 pages[pageId]._messageProxy.handleResponse(message); | 449 pages[pageId]._handleResponse(message); |
| 434 break; | 450 break; |
| 435 case "replaced": | 451 case "replaced": |
| 436 // when a prerendered page is shown, forget the previous page | 452 // when a prerendered page is shown, forget the previous page |
| 437 // associated with its tab, and reset the toolbar item if necessary. | 453 // 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 | 454 // Note that it wouldn't be sufficient to do that when the old |
| 439 // page is unloading, because Safari dispatches window.onunload | 455 // page is unloading, because Safari dispatches window.onunload |
| 440 // only when reloading the page or following links, but not when | 456 // only when reloading the page or following links, but not when |
| 441 // the current page is replaced with a prerendered page. | 457 // the current page is replaced with a prerendered page. |
| 442 replacePage(sender.page); | 458 replacePage(sender.page); |
| 443 break; | 459 break; |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 597 /* Windows */ | 613 /* Windows */ |
| 598 ext.windows = { | 614 ext.windows = { |
| 599 // Safari doesn't provide as rich a windows API as Chrome does, so instead | 615 // 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. | 616 // of chrome.windows.create we have to fall back to just opening a new tab. |
| 601 create: function(createData, callback) | 617 create: function(createData, callback) |
| 602 { | 618 { |
| 603 ext.pages.open(createData.url, callback); | 619 ext.pages.open(createData.url, callback); |
| 604 } | 620 } |
| 605 }; | 621 }; |
| 606 })(); | 622 })(); |
| OLD | NEW |