 Issue 4731979438227456:
  Issue 1663 - Emulate background page and implement proper message responder  (Closed)
    
  
    Issue 4731979438227456:
  Issue 1663 - Emulate background page and implement proper message responder  (Closed) 
  | Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 1 /* | 1 /* | 
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 | 
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. | 
| 13 * | 13 * | 
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License | 
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 
| 16 */ | 16 */ | 
| 17 | 17 | 
| 18 (function(global) | 18 (function(global) | 
| 19 { | 19 { | 
| 20 var subscriptionKeys = ["disabled", "homepage", "lastSuccess", "title", "url", "downloadStatus"]; | 20 var subscriptionKeys = ["disabled", "homepage", "lastSuccess", "title", "url", "downloadStatus"]; | 
| 21 function convertSubscription(subscription) | 21 function convertSubscription(subscription) | 
| 22 { | 22 { | 
| 23 var result = {}; | 23 var result = {}; | 
| 24 for (var i = 0; i < subscriptionKeys.length; i++) | 24 for (var i = 0; i < subscriptionKeys.length; i++) | 
| 25 result[subscriptionKeys[i]] = subscription[subscriptionKeys[i]] | 25 result[subscriptionKeys[i]] = subscription[subscriptionKeys[i]] | 
| 26 return result; | 26 return result; | 
| 27 } | 27 } | 
| 28 | 28 | 
| 29 var changeListeners = null; | 29 var changeListeners = null; | 
| 30 var messageTypes = { | |
| 31 "app": "app.listen", | |
| 32 "filter": "filters.listen", | |
| 33 "subscription": "subscriptions.listen" | |
| 34 }; | |
| 35 | |
| 30 function onFilterChange(action) | 36 function onFilterChange(action) | 
| 31 { | 37 { | 
| 32 var parts = action.split(".", 2); | 38 var parts = action.split(".", 2); | 
| 33 var type; | 39 var type; | 
| 34 if (parts.length == 1) | 40 if (parts.length == 1) | 
| 35 { | 41 { | 
| 36 type = "app"; | 42 type = "app"; | 
| 37 action = parts[0]; | 43 action = parts[0]; | 
| 38 } | 44 } | 
| 39 else | 45 else | 
| 40 { | 46 { | 
| 41 type = parts[0]; | 47 type = parts[0]; | 
| 42 action = parts[1]; | 48 action = parts[1]; | 
| 43 } | 49 } | 
| 44 | 50 | 
| 45 for (var i = 0; i < changeListeners.length; i++) | 51 if (!messageTypes.hasOwnProperty(type)) | 
| 52 return; | |
| 53 | |
| 54 var args = Array.prototype.slice.call(arguments, 1).map(function(arg) | |
| 46 { | 55 { | 
| 47 if (changeListeners[i].type == type && changeListeners[i].filter.indexOf(a ction) >= 0) | 56 if (arg instanceof Subscription) | 
| 57 return convertSubscription(arg); | |
| 58 else | |
| 59 return arg; | |
| 60 }); | |
| 61 | |
| 62 var pages = changeListeners.keys(); | |
| 63 for (var i = 0; i < pages.length; i++) | |
| 64 { | |
| 65 var filters = changeListeners.get(pages[i]); | |
| 66 if (filters[type] && filters[type].indexOf(action) >= 0) | |
| 48 { | 67 { | 
| 49 changeListeners[i].page.sendMessage({ | 68 pages[i].sendMessage({ | 
| 50 type: changeListeners[i].messageType, | 69 type: messageTypes[type], | 
| 51 action: action, | 70 action: action, | 
| 52 args: Array.prototype.slice.call(arguments, 1) | 71 args: args | 
| 53 }); | 72 }); | 
| 54 } | 73 } | 
| 55 } | 74 } | 
| 56 }; | 75 }; | 
| 57 | 76 | 
| 58 ext.onMessage.addListener(function(message, sender, callback) | 77 ext.onMessage.addListener(function(message, sender, callback) | 
| 59 { | 78 { | 
| 60 switch (message.type) | 79 switch (message.type) | 
| 61 { | 80 { | 
| 62 case "app.doclink": | 81 case "app.get": | 
| 63 callback(Utils.getDocLink(message.args[0])); | 82 if (message.what == "issues") | 
| 83 { | |
| 84 var info = require("info"); | |
| 85 callback({ | |
| 86 seenDataCorruption: "seenDataCorruption" in global ? global.seenData Corruption : false, | |
| 87 filterlistsReinitialized: "filterlistsReinitialized" in global ? glo bal.filterlistsReinitialized : false, | |
| 88 legacySafariVersion: (info.platform == "safari" && ( | |
| 89 Services.vc.compare(info.platformVersion, "6.0") < 0 || // bef oreload breaks websites in Safari 5 | |
| 90 Services.vc.compare(info.platformVersion, "6.1") == 0 || // ext ensions are broken in 6.1 and 7.0 | |
| 91 Services.vc.compare(info.platformVersion, "7.0") == 0)) | |
| 92 }); | |
| 93 } | |
| 94 else if (message.what == "doclink") | |
| 95 callback(Utils.getDocLink(message.link)); | |
| 96 else | |
| 97 callback(null); | |
| 64 break; | 98 break; | 
| 65 case "app.info": | 99 case "app.open": | 
| 66 callback(require("info")); | 100 if (message.what == "options") | 
| 67 break; | 101 { | 
| 68 case "app.issues": | 102 if (typeof UI != "undefined") | 
| 69 callback({ | 103 UI.openFiltersDialog(); | 
| 70 seenDataCorruption: "seenDataCorruption" in global ? global.seenDataCo rruption : false, | 104 else | 
| 71 filterlistsReinitialized: "filterlistsReinitialized" in global ? globa l.filterlistsReinitialized : false | 105 global.openOptions(); | 
| 
Sebastian Noack
2014/12/22 09:56:17
window.openOptions() is deprecated. Please use ext
 
Wladimir Palant
2015/01/15 13:03:22
I filed #1813 and #1814 on that.
 | |
| 72 }); | 106 } | 
| 73 break; | |
| 74 case "app.options": | |
| 75 if (typeof UI != "undefined") | |
| 76 UI.openFiltersDialog(); | |
| 77 else | |
| 78 global.openOptions(); | |
| 79 break; | 107 break; | 
| 80 case "subscriptions.get": | 108 case "subscriptions.get": | 
| 81 callback(FilterStorage.subscriptions.map(convertSubscription)); | 109 var subscriptions = FilterStorage.subscriptions.filter(function(s) | 
| 110 { | |
| 111 if (message.ignoreDisabled && s.disabled) | |
| 112 return false; | |
| 113 if (s instanceof DownloadableSubscription && message.downloadable) | |
| 114 return true; | |
| 115 if (s instanceof SpecialSubscription && message.special) | |
| 116 return true; | |
| 117 return false; | |
| 118 }); | |
| 119 callback(subscriptions.map(convertSubscription)); | |
| 82 break; | 120 break; | 
| 83 case "filters.blocked": | 121 case "filters.blocked": | 
| 84 var filter = defaultMatcher.matchesAny(message.url, message.requestType, message.docDomain, message.thirdParty); | 122 var filter = defaultMatcher.matchesAny(message.url, message.requestType, message.docDomain, message.thirdParty); | 
| 85 callback(filter instanceof BlockingFilter); | 123 callback(filter instanceof BlockingFilter); | 
| 86 break; | 124 break; | 
| 87 case "subscriptions.toggle": | 125 case "subscriptions.toggle": | 
| 88 var subscription = Subscription.fromURL(message.url); | 126 var subscription = Subscription.fromURL(message.url); | 
| 89 if (subscription.url in FilterStorage.knownSubscriptions && !subscriptio n.disabled) | 127 if (subscription.url in FilterStorage.knownSubscriptions && !subscriptio n.disabled) | 
| 90 FilterStorage.removeSubscription(subscription); | 128 FilterStorage.removeSubscription(subscription); | 
| 91 else | 129 else | 
| 92 { | 130 { | 
| 93 subscription.disabled = false; | 131 subscription.disabled = false; | 
| 94 subscription.title = message.title; | 132 subscription.title = message.title; | 
| 95 subscription.homepage = message.homepage; | 133 subscription.homepage = message.homepage; | 
| 96 FilterStorage.addSubscription(subscription); | 134 FilterStorage.addSubscription(subscription); | 
| 97 if (!subscription.lastDownload) | 135 if (!subscription.lastDownload) | 
| 98 Synchronizer.execute(subscription); | 136 Synchronizer.execute(subscription); | 
| 99 } | 137 } | 
| 100 break; | 138 break; | 
| 101 case "subscriptions.listen": | 139 case "subscriptions.listen": | 
| 102 if (!changeListeners) | 140 if (!changeListeners) | 
| 103 { | 141 { | 
| 104 changeListeners = []; | 142 changeListeners = new global.ext.PageMap(); | 
| 105 FilterNotifier.addListener(onFilterChange); | 143 FilterNotifier.addListener(onFilterChange); | 
| 106 } | 144 } | 
| 107 changeListeners.push({ | |
| 108 type: "subscription", | |
| 109 filter: message.filter, | |
| 110 messageType: message.type, | |
| 111 page: sender.page | |
| 112 }); | |
| 113 break; | |
| 114 case "removePage": | |
| 115 if (!changeListeners) | |
| 116 return; | |
| 117 | 145 | 
| 118 for (var i = 0; i < changeListeners.length; i++) | 146 var filters = changeListeners.get(sender.page); | 
| 119 if (changeListeners[i].page.equals(sender.page)) | 147 if (!filters) | 
| 120 changeListeners.splice(i--, 1); | 148 { | 
| 149 filters = Object.create(null); | |
| 150 changeListeners.set(sender.page, filters); | |
| 151 } | |
| 121 | 152 | 
| 122 if (!changeListeners.length) | 153 if (message.filter) | 
| 123 { | 154 filters.subscription = message.filter; | 
| 124 changeListeners = null; | 155 else | 
| 125 FilterNotifier.removeListener(onFilterChange); | 156 delete filters.subscription; | 
| 126 } | |
| 127 break; | 157 break; | 
| 128 } | 158 } | 
| 129 }); | 159 }); | 
| 130 })(this); | 160 })(this); | 
| LEFT | RIGHT |